You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2007/07/11 05:44:09 UTC

svn commit: r555153 - /xerces/java/trunk/src/org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl.java

Author: mrglavas
Date: Tue Jul 10 20:44:08 2007
New Revision: 555153

URL: http://svn.apache.org/viewvc?view=rev&rev=555153
Log:
Fixing a bug and made a few perf improvements to isValid(). It wasn't checking if the value of days is valid 
for the specified month. This was allowing bogus dates like April 31st through.

Performance:
If abs(year) < 10^9 avoid calling getEonAndYear() when checking the number of days in February for a particular 
year. We were wasting time checking for year 0 if eon != null. By definition that could never be true (i.e.
eon != null implies abs(year) > 10^9, so I eliminated this code.

Modified:
    xerces/java/trunk/src/org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl.java?view=diff&rev=555153&r1=555152&r2=555153
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl.java Tue Jul 10 20:44:08 2007
@@ -2035,51 +2035,35 @@
         // no need to check for anything except for constraints
         // between fields. 
 
-        //check if days in month is valid. Can be dependent on leap year.
-        if (getMonth() == DatatypeConstants.FEBRUARY) {
-            // years could not be set
-            int maxDays = DatatypeConstants.FIELD_UNDEFINED;
-            BigInteger years = getEonAndYear();
-            if (years != null) {
-                maxDays = maximumDayInMonthFor(getEonAndYear(), DatatypeConstants.FEBRUARY);
-            } 
-            else {
-                // year is undefined, allow 29 days
-                maxDays = 29;
+        // check if days in month is valid. Can be dependent on leap year.
+        if (month != DatatypeConstants.FIELD_UNDEFINED && day != DatatypeConstants.FIELD_UNDEFINED) {
+            if (year != DatatypeConstants.FIELD_UNDEFINED) {
+                if (eon == null) {
+                    if (day > maximumDayInMonthFor(year, month)) {
+                        return false;
+                    }
+                }
+                else if (day > maximumDayInMonthFor(getEonAndYear(), month)) {
+                    return false;
+                }
             }
-            if (getDay() > maxDays) {
+            // Use 2000 as a default since it's a leap year.
+            else if (day > maximumDayInMonthFor(2000, month)) {
                 return false;
             }
         }
 
         // http://www.w3.org/2001/05/xmlschema-errata#e2-45
-        if (getHour() == 24) {
-            if (getMinute() != 0) {
-                return false;
-            } 
-            else if (getSecond() != 0) {
-                return false;
-            }
+        if (hour == 24 && (minute != 0 || second != 0)) {
+            return false;
         }
 
         // XML Schema 1.0 specification defines year value of zero as
         // invalid. Allow this class to set year field to zero
         // since XML Schema 1.0 errata states that lexical zero will 
         // be allowed in next version and treated as 1 B.C.E.
-        if (eon == null) {
-            // optimize check.
-            if (year == 0) {
-                return false;
-            }
-        } 
-        else {
-            BigInteger yearField = getEonAndYear();
-            if (yearField != null) {
-                int result = compareField(yearField, BigInteger.ZERO);
-                if (result == DatatypeConstants.EQUAL) {
-                    return false;
-                }
-            }
+        if (eon == null && year == 0) {
+            return false;
         }
         return true;
     }
@@ -2357,14 +2341,15 @@
     private static final BigDecimal DECIMAL_ONE = new BigDecimal("1");
     private static final BigDecimal DECIMAL_SIXTY = new BigDecimal("60");
 
-
-    private static int daysInMonth[] = { 0,  // XML Schema months start at 1.
-				       31, 28, 31, 30, 31, 30,
-                                       31, 31, 30, 31, 30, 31};
+    private static class DaysInMonth {
+        private static final int [] table = { 0,  // XML Schema months start at 1.
+            31, 28, 31, 30, 31, 30,
+            31, 31, 30, 31, 30, 31};
+    }
 
     private static int maximumDayInMonthFor(BigInteger year, int month) {
         if (month != DatatypeConstants.FEBRUARY) {
-            return daysInMonth[month];
+            return DaysInMonth.table[month];
         } 
         else {
             if (year.mod(FOUR_HUNDRED).equals(BigInteger.ZERO) || 
@@ -2374,14 +2359,14 @@
                 return 29;
             } 
             else {
-                return daysInMonth[month];
+                return DaysInMonth.table[month];
             }
         }
     }
 
     private static int maximumDayInMonthFor(int year, int month) {
         if (month != DatatypeConstants.FEBRUARY) {
-            return daysInMonth[month];
+            return DaysInMonth.table[month];
         } 
         else {
             if ( ((year %400) == 0) || 
@@ -2390,7 +2375,7 @@
                 return 29;
             } 
             else {
-                return daysInMonth[DatatypeConstants.FEBRUARY];
+                return DaysInMonth.table[DatatypeConstants.FEBRUARY];
             }
         }
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org