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/06 21:30:11 UTC

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

Author: mrglavas
Date: Tue Mar  6 12:30:10 2007
New Revision: 515286

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

The canonical value we produce for dateTime and time were incorrect. We now
ensure that that the value contains no trailing zeros. 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

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=515286&r1=515285&r2=515286
==============================================================================
--- 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 Tue Mar  6 12:30:10 2007
@@ -17,6 +17,8 @@
 
 package org.apache.xerces.impl.dv.xs;
 
+import java.math.BigDecimal;
+
 import javax.xml.datatype.DatatypeFactory;
 import javax.xml.datatype.Duration;
 import javax.xml.datatype.XMLGregorianCalendar;
@@ -782,10 +784,58 @@
 			message.append('-');
 			value = -value;
 		}
-		if (value < 10)
+		if (value < 10) {
 			message.append('0');
-		message.append(value);
+        }
+        final int intValue = (int) value;
+        if (value == intValue) {
+            message.append(intValue);
+        }
+        else {
+            append2(message, value);
+        }
 	}
+    
+    private void append2(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;
+            }
+            --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);
+            }
+        }
+    }
 	
 	protected double parseSecond(String buffer, int start, int end)
 	throws NumberFormatException {



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