You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@vxquery.apache.org by pr...@apache.org on 2012/08/01 23:58:12 UTC

svn commit: r1368291 - in /incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery: runtime/functions/cast/CastToStringOperation.java serializer/XMLSerializer.java

Author: prestonc
Date: Wed Aug  1 21:58:11 2012
New Revision: 1368291

URL: http://svn.apache.org/viewvc?rev=1368291&view=rev
Log:
Updated the XMLSerializer to use the CastToString for float and double so we can get a XML formated value for INF. Also update CastToString to support the double and float special values.

Modified:
    incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToStringOperation.java
    incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java

Modified: incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToStringOperation.java
URL: http://svn.apache.org/viewvc/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToStringOperation.java?rev=1368291&r1=1368290&r2=1368291&view=diff
==============================================================================
--- incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToStringOperation.java (original)
+++ incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToStringOperation.java Wed Aug  1 21:58:11 2012
@@ -158,7 +158,7 @@ public class CastToStringOperation exten
                 value %= pow10;
                 pow10 /= 10;
             }
-            if (i == decimalPlace) {
+            if (i == decimalPlace && value > 0) {
                 dOutInner.writeChar('.');
             }
         }
@@ -171,42 +171,50 @@ public class CastToStringOperation exten
         abvsInner.reset();
         double value = doublep.getDouble();
 
-        if (value < 0) {
-            // Negative result, but the rest of the calculations can be based on a positive value.
-            dOutInner.writeChar('-');
-            value *= -1;
-        }
-        byte decimalPlace = 0;
-        // Move the decimal
-        while (value % 1 != 0) {
-            --decimalPlace;
-            value *= 10;
-        }
-        // Remove extra zeros
-        while (value != 0 && value % 10 == 0) {
-            value /= 10;
-            ++decimalPlace;
-        }
-        // Print out the value.
-        int nDigits = (int) Math.log10(value) + 1;
-        long pow10 = (long) Math.pow(10, nDigits - 1);
-        if (nDigits < 0) {
-            dOutInner.writeChars("0.0");
+        if (Double.isInfinite(value)) {
+            if (value == Double.NEGATIVE_INFINITY) {
+                dOutInner.writeChars("-");
+            }
+            dOutInner.writeChars("INF");
+        } else if (Double.isNaN(value)) {
+            dOutInner.writeChars("NaN");
         } else {
-            for (int i = nDigits - 1; i >= 0; --i) {
-                dOutInner.writeChar((char) ('0' + (value / pow10)));
-                value %= pow10;
-                pow10 /= 10;
-                if (i == nDigits - 1) {
-                    dOutInner.writeChar('.');
-                } else {
-                    ++decimalPlace;
+            if (value < 0) {
+                // Negative result, but the rest of the calculations can be based on a positive value.
+                dOutInner.writeChar('-');
+                value *= -1;
+            }
+            byte decimalPlace = 0;
+            // Move the decimal
+            while (value % 1 != 0) {
+                --decimalPlace;
+                value *= 10;
+            }
+            // Remove extra zeros
+            while (value != 0 && value % 10 == 0) {
+                value /= 10;
+                ++decimalPlace;
+            }
+            // Print out the value.
+            int nDigits = (int) Math.log10(value) + 1;
+            long pow10 = (long) Math.pow(10, nDigits - 1);
+            if (nDigits < 0) {
+                dOutInner.writeChars("0.0");
+            } else {
+                for (int i = nDigits - 1; i >= 0; --i) {
+                    dOutInner.writeChar((char) ('0' + (value / pow10)));
+                    value %= pow10;
+                    pow10 /= 10;
+                    if (i == nDigits - 1) {
+                        dOutInner.writeChar('.');
+                    } else {
+                        ++decimalPlace;
+                    }
                 }
             }
+            dOutInner.writeChar('E');
+            writeNumberWithPadding(decimalPlace, 1, dOutInner);
         }
-        dOutInner.writeChar('E');
-        writeNumberWithPadding(decimalPlace, 1, dOutInner);
-
         sendStringDataOutput(dOut);
     }
 
@@ -262,22 +270,25 @@ public class CastToStringOperation exten
     @Override
     public void convertDuration(XSDurationPointable durationp, DataOutput dOut) throws SystemException, IOException {
         abvsInner.reset();
+        int yearMonth = durationp.getYearMonth();
         int dayTime = durationp.getDayTime();
 
-        if (durationp.getYearMonth() < 0 || dayTime < 0) {
+        if (yearMonth < 0 || dayTime < 0) {
             dOutInner.writeChar('-');
+            yearMonth *= -1;
+            dayTime *= -1;
         }
         dOutInner.writeChar('P');
 
         // Year
-        if (durationp.getYearMonth() > 12) {
-            dOutInner.writeChars(String.format("%01d", durationp.getYearMonth() / 12));
+        if (yearMonth > 12) {
+            dOutInner.writeChars(String.format("%01d", yearMonth / 12));
             dOutInner.writeChar('Y');
         }
 
         // Month
-        if (durationp.getYearMonth() % 12 > 0) {
-            dOutInner.writeChars(String.format("%01d", durationp.getYearMonth() % 12));
+        if (yearMonth % 12 > 0) {
+            dOutInner.writeChars(String.format("%01d", yearMonth % 12));
             dOutInner.writeChar('M');
         }
 
@@ -324,42 +335,50 @@ public class CastToStringOperation exten
         abvsInner.reset();
         float value = floatp.getFloat();
 
-        if (value < 0) {
-            // Negative result, but the rest of the calculations can be based on a positive value.
-            dOutInner.writeChar('-');
-            value *= -1;
-        }
-        byte decimalPlace = 0;
-        // Move the decimal
-        while (value % 1 != 0) {
-            --decimalPlace;
-            value *= 10;
-        }
-        // Remove extra zeros
-        while (value != 0 && value % 10 == 0) {
-            value /= 10;
-            ++decimalPlace;
-        }
-        // Print out the value.
-        int nDigits = (int) Math.log10(value) + 1;
-        long pow10 = (long) Math.pow(10, nDigits - 1);
-        if (nDigits < 0) {
-            dOutInner.writeChars("0.0");
+        if (Float.isInfinite(value)) {
+            if (value == Float.NEGATIVE_INFINITY) {
+                dOutInner.writeChars("-");
+            }
+            dOutInner.writeChars("INF");
+        } else if (Float.isNaN(value)) {
+            dOutInner.writeChars("NaN");
         } else {
-            for (int i = nDigits - 1; i >= 0; --i) {
-                dOutInner.writeChar((char) ('0' + (value / pow10)));
-                value %= pow10;
-                pow10 /= 10;
-                if (i == nDigits - 1) {
-                    dOutInner.writeChar('.');
-                } else {
-                    ++decimalPlace;
+            if (value < 0) {
+                // Negative result, but the rest of the calculations can be based on a positive value.
+                dOutInner.writeChar('-');
+                value *= -1;
+            }
+            byte decimalPlace = 0;
+            // Move the decimal
+            while (value % 1 != 0) {
+                --decimalPlace;
+                value *= 10;
+            }
+            // Remove extra zeros
+            while (value != 0 && value % 10 == 0) {
+                value /= 10;
+                ++decimalPlace;
+            }
+            // Print out the value.
+            int nDigits = (int) Math.log10(value) + 1;
+            long pow10 = (long) Math.pow(10, nDigits - 1);
+            if (nDigits < 0) {
+                dOutInner.writeChars("0.0");
+            } else {
+                for (int i = nDigits - 1; i >= 0; --i) {
+                    dOutInner.writeChar((char) ('0' + (value / pow10)));
+                    value %= pow10;
+                    pow10 /= 10;
+                    if (i == nDigits - 1) {
+                        dOutInner.writeChar('.');
+                    } else {
+                        ++decimalPlace;
+                    }
                 }
             }
+            dOutInner.writeChar('E');
+            writeNumberWithPadding(decimalPlace, 1, dOutInner);
         }
-        dOutInner.writeChar('E');
-        writeNumberWithPadding(decimalPlace, 1, dOutInner);
-
         sendStringDataOutput(dOut);
     }
 

Modified: incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java
URL: http://svn.apache.org/viewvc/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java?rev=1368291&r1=1368290&r2=1368291&view=diff
==============================================================================
--- incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java (original)
+++ incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java Wed Aug  1 21:58:11 2012
@@ -412,7 +412,13 @@ public class XMLSerializer implements IP
         DoublePointable dp = pp.takeOne(DoublePointable.class);
         try {
             tvp.getValue(dp);
-            ps.print(dp.doubleValue());
+            abvs.reset();
+            castToString.convertDouble(dp, dOut);
+            printStringAbvs(ps);
+        } catch (SystemException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
         } finally {
             pp.giveBack(dp);
         }
@@ -486,7 +492,13 @@ public class XMLSerializer implements IP
         FloatPointable fp = pp.takeOne(FloatPointable.class);
         try {
             tvp.getValue(fp);
-            ps.print(fp.floatValue());
+            abvs.reset();
+            castToString.convertFloat(fp, dOut);
+            printStringAbvs(ps);
+        } catch (SystemException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
         } finally {
             pp.giveBack(fp);
         }