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/03/12 18:03:55 UTC

svn commit: r517284 - in /xerces/java/trunk/src/org/apache/xerces/impl/dv/xs: AbstractDateTimeDV.java DurationDV.java

Author: mrglavas
Date: Mon Mar 12 10:03:54 2007
New Revision: 517284

URL: http://svn.apache.org/viewvc?view=rev&rev=517284
Log:
Fixing JIRA Bug #1233:
http://issues.apache.org/jira/browse/XERCESJ-1233

The canonical value we produce for duration was sometimes invalid. If the 
conversion of seconds from a double to String results in a number expressed 
in scientific notation it is converted to a normal decimal value before 
appending it to the StringBuffer.

Modified:
    xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/AbstractDateTimeDV.java
    xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/DurationDV.java

Modified: xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/AbstractDateTimeDV.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/AbstractDateTimeDV.java?view=diff&rev=517284&r1=517283&r2=517284
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/AbstractDateTimeDV.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/AbstractDateTimeDV.java Mon Mar 12 10:03:54 2007
@@ -748,7 +748,7 @@
 		return message.toString();
 	}
 	
-	protected void append(StringBuffer message, int value, int nch) {
+	protected final void append(StringBuffer message, int value, int nch) {
         if (value == Integer.MIN_VALUE) {
             message.append(value);
             return;
@@ -777,7 +777,7 @@
 		}
 	}
 	
-	protected void append(StringBuffer message, double value) {
+	protected final void append(StringBuffer message, double value) {
 	    if (value < 0) {
 	        message.append('-');
 	        value = -value;
@@ -785,52 +785,85 @@
 	    if (value < 10) {
 	        message.append('0');
 	    }
-	    final int intValue = (int) value;
-	    if (value == intValue) {
-	        message.append(intValue);
-	    }
-	    else {
-	        append2(message, value);
-	    }
+        append2(message, value);
 	}
     
-    private void append2(StringBuffer message, double value) {
+    protected final void append2(StringBuffer message, double value) {
+        final int intValue = (int) value;
+        if (value == intValue) {
+            message.append(intValue);
+        }
+        else {
+            append3(message, value);
+        }
+    }
+    
+    private void append3(StringBuffer message, double value) {
         String d = String.valueOf(value);
         int eIndex = d.indexOf('E');
         if (eIndex == -1) {
             message.append(d);
             return;
         }
-        // Need to convert from scientific notation of the form 
-        // n.nnn...E-N (N >= 4) to a normal decimal value.
         int exp;
-        try {
-            exp = parseInt(d, eIndex+2, d.length());
-        }
-        // This should never happen. 
-        // It's only possible if String.valueOf(double) is broken.
-        catch (Exception e) {
-            message.append(d);
-            return;
-        }
-        message.append("0.");
-        for (int i = 1; i < exp; ++i) {
-            message.append('0');
-        }
-        // Remove trailing zeros.
-        int end = eIndex - 1;
-        while (end > 0) {
-            char c = d.charAt(end);
-            if (c != '0') {
-                break;
+        if (value < 1) {
+            // Need to convert from scientific notation of the form 
+            // n.nnn...E-N (N >= 4) to a normal decimal value.
+            try {
+                exp = parseInt(d, eIndex+2, d.length());
+            }
+            // This should never happen. 
+            // It's only possible if String.valueOf(double) is broken.
+            catch (Exception e) {
+                message.append(d);
+                return;
+            }
+            message.append("0.");
+            for (int i = 1; i < exp; ++i) {
+                message.append('0');
+            }
+            // Remove trailing zeros.
+            int end = eIndex - 1;
+            while (end > 0) {
+                char c = d.charAt(end);
+                if (c != '0') {
+                    break;
+                }
+                --end;
+            }
+            // Now append the digits to the end. Skip over the decimal point.
+            for (int i = 0; i <= end; ++i) {
+                char c = d.charAt(i);
+                if (c != '.') {
+                    message.append(c);
+                }
             }
-            --end;
         }
-        // Now append the digits to the end. Skip over the decimal point.
-        for (int i = 0; i <= end; ++i) {
-            char c = d.charAt(i);
-            if (c != '.') {
-                message.append(c);
+        else {
+            // Need to convert from scientific notation of the form 
+            // n.nnn...EN (N >= 7) to a normal decimal value.
+            try {
+                exp = parseInt(d, eIndex+1, d.length());
+            }
+            // This should never happen. 
+            // It's only possible if String.valueOf(double) is broken.
+            catch (Exception e) {
+                message.append(d);
+                return;
+            }
+            final int integerEnd = exp + 2;
+            for (int i = 0; i < eIndex; ++i) {
+                char c = d.charAt(i);
+                if (c != '.') {
+                    if (i == integerEnd) {
+                        message.append('.');
+                    }
+                    message.append(c);
+                }
+            }
+            // Append trailing zeroes if necessary.
+            for (int i = integerEnd - eIndex; i > 0; --i) {
+                message.append('0');
             }
         }
     }

Modified: xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/DurationDV.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/DurationDV.java?view=diff&rev=517284&r1=517283&r2=517284
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/DurationDV.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/dv/xs/DurationDV.java Mon Mar 12 10:03:54 2007
@@ -339,7 +339,11 @@
         if (dot+1 == end) {
             throw new NumberFormatException("'" + buffer + "' has wrong format");
         }
-        return Double.parseDouble(buffer.substring(start, end));
+        double value = Double.parseDouble(buffer.substring(start, end));
+        if (value == Double.POSITIVE_INFINITY) {
+            throw new NumberFormatException("'" + buffer + "' has wrong format");
+        }
+        return value;
     }
 
     protected String dateToString(DateTimeData date) {
@@ -360,7 +364,7 @@
         message.append('H');
         message.append((date.minute < 0?-1:1) * date.minute);
         message.append('M');
-        message.append((date.second < 0?-1:1) * date.second);
+        append2(message, (date.second < 0?-1:1) * date.second);
         message.append('S');
 
         return message.toString();



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