[Practicalxml-commits] SF.net SVN: practicalxml:[84] trunk
                
                Brought to you by:
                
                    kdgregory
                    
                
            
            
        
        
        
    | 
      
      
      From: Auto-Generated S. C. M. <pra...@li...> - 2009-07-14 14:06:16
      
     | 
| Revision: 84
          http://practicalxml.svn.sourceforge.net/practicalxml/?rev=84&view=rev
Author:   kdgregory
Date:     2009-07-14 14:06:13 +0000 (Tue, 14 Jul 2009)
Log Message:
-----------
add XmlUtil.formatXsdDecimal()
Modified Paths:
--------------
    trunk/pom.xml
    trunk/src/main/java/net/sf/practicalxml/XmlUtil.java
    trunk/src/test/java/net/sf/practicalxml/TestXmlUtil.java
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml	2009-07-14 12:26:58 UTC (rev 83)
+++ trunk/pom.xml	2009-07-14 14:06:13 UTC (rev 84)
@@ -5,7 +5,7 @@
   <groupId>net.sf.practicalxml</groupId>
   <artifactId>practicalxml</artifactId>
   <packaging>jar</packaging>
-  <version>1.0.2</version>
+  <version>1.0.3</version>
   <name>practicalxml</name>
   <url>http://sourceforge.net/projects/practicalxml/</url>
 
Modified: trunk/src/main/java/net/sf/practicalxml/XmlUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/XmlUtil.java	2009-07-14 12:26:58 UTC (rev 83)
+++ trunk/src/main/java/net/sf/practicalxml/XmlUtil.java	2009-07-14 14:06:13 UTC (rev 84)
@@ -15,6 +15,7 @@
 package net.sf.practicalxml;
 
 import java.text.DateFormat;
+import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
@@ -99,6 +100,31 @@
 
 
     /**
+     *  Converts a Java <code>double</code> to a string, using the format
+     *  specified by XML Schema for <code>decimal</code> elements. This
+     *  method wraps the value and calls {@link #formatXsdDecimal(Number)},
+     *  so call that method if you already have an object.
+     */
+    public static String formatXsdDecimal(double value)
+    {
+        return formatXsdDecimal(Double.valueOf(value));
+    }
+
+
+    /**
+     *  Converts a Java <code>double</code> to a string, using the format
+     *  specified by XML Schema for <code>decimal</code> elements. If
+     *  passed <code>null</code>, returns an empty string
+     */
+    public static String formatXsdDecimal(Number value)
+    {
+        if (value == null)
+            return "";
+        return getXsdDecimalFormatter().format(value);
+    }
+
+
+    /**
      *  Parses an XML Schema <code>dateTime</code> object, accepting any of
      *  the legal formats. Note that this method can also be used to parse
      *  a generic ISO-8601 date.
@@ -231,6 +257,9 @@
     private static ThreadLocal<DateFormat> _xsdDatetimeFormatter = new ThreadLocal<DateFormat>();
 
 
+    // used by getXsdDecimalFormatter()
+    private static ThreadLocal<DecimalFormat> _xsdDecimalFormatter = new ThreadLocal<DecimalFormat>();
+
     /**
      *  Does the actual work of {@link isLegal(String)}.
      */
@@ -247,7 +276,8 @@
 
 
     /**
-     *  Returns a DateFormat that will output our standard XSD dateTime format.
+     *  Returns a DateFormat that will output the standard XSD dateTime format.
+     *  This is managed as a ThreadLocal because formatters are not threadsafe.
      */
     private static DateFormat getXsdDatetimeFormatter()
     {
@@ -263,6 +293,30 @@
 
 
     /**
+     *  Returns a DecimalFormat that will output the standard XSD decimalformat.
+     *  This is managed as a ThreadLocal because formatters are not threadsafe.
+     *  <p>
+     *  Note: output is limited to 17 digits to the right of the decimal point,
+     *  because we assume a <code>double</code> input. For that reason, while
+     *  you can use this method for <code>BigDecimal</code> values, that class'
+     *  <code>toString()</code> is a better choice.
+     *  <p>
+     *  Note 2: there is no corresponding parse method; <code>Double.parseDouble()
+     *  </code> will do the job for you.
+     */
+    private static DecimalFormat getXsdDecimalFormatter()
+    {
+        DecimalFormat format = _xsdDecimalFormatter.get();
+        if (format == null)
+        {
+            format = new DecimalFormat("#0.0################;-#");
+            _xsdDecimalFormatter.set(format);
+        }
+        return format;
+    }
+
+
+    /**
      *  Used by {@link parseXsdDatetime} to process individual fields of the
      *  dateTime string and store them into a calendar object. It expects to
      *  be called with <code>index</code> pointing to the start of the field,
Modified: trunk/src/test/java/net/sf/practicalxml/TestXmlUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestXmlUtil.java	2009-07-14 12:26:58 UTC (rev 83)
+++ trunk/src/test/java/net/sf/practicalxml/TestXmlUtil.java	2009-07-14 14:06:13 UTC (rev 84)
@@ -77,6 +77,17 @@
     }
 
 
+    public void testFormatXsdDecimal() throws Exception
+    {
+        assertEquals("", XmlUtil.formatXsdDecimal(null));
+        assertEquals("0.0", XmlUtil.formatXsdDecimal(0));
+        assertEquals("1234.0", XmlUtil.formatXsdDecimal(1234));
+        assertEquals("-1234.0", XmlUtil.formatXsdDecimal(-1234));
+        assertEquals("1234.5", XmlUtil.formatXsdDecimal(1234.5));
+        assertEquals("1234567890.123456", XmlUtil.formatXsdDecimal(1234567890.123456));
+    }
+
+
     public void testEscape() throws Exception
     {
         assertEquals("", XmlUtil.escape(null));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |