[Practicalxml-commits] SF.net SVN: practicalxml:[94] branches/dev-1.1/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2009-07-23 22:06:37
|
Revision: 94
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=94&view=rev
Author: kdgregory
Date: 2009-07-23 22:06:24 +0000 (Thu, 23 Jul 2009)
Log Message:
-----------
add XmlUtil.formatXsdBoolean(), XmlUtil.parseXsdBoolean()
Modified Paths:
--------------
branches/dev-1.1/src/main/java/net/sf/practicalxml/XmlUtil.java
branches/dev-1.1/src/test/java/net/sf/practicalxml/TestXmlUtil.java
Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/XmlUtil.java
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/XmlUtil.java 2009-07-23 21:36:40 UTC (rev 93)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/XmlUtil.java 2009-07-23 22:06:24 UTC (rev 94)
@@ -100,6 +100,32 @@
/**
+ * Parses an XML Schema <code>dateTime</code> value, accepting any of
+ * the legal formats. Note that this method can also be used to parse
+ * a generic ISO-8601 date.
+ *
+ * @throws XmlException if unable to parse.
+ */
+ public static Date parseXsdDatetime(String value)
+ {
+ Calendar cal = GregorianCalendar.getInstance();
+ cal.setTimeInMillis(0);
+ int idx = parserHelper(value, cal, Calendar.YEAR, 0);
+ idx = parserHelper(value, cal, Calendar.MONTH, idx+1);
+ idx = parserHelper(value, cal, Calendar.DAY_OF_MONTH, idx+1);
+ idx = parserHelper(value, cal, Calendar.HOUR_OF_DAY, idx+1);
+ idx = parserHelper(value, cal, Calendar.MINUTE, idx+1);
+ idx = parserHelper(value, cal, Calendar.SECOND, idx+1);
+ if (idx < value.length() && (value.charAt(idx) == '.'))
+ {
+ idx = parserHelper(value, cal, Calendar.MILLISECOND, idx+1);
+ }
+ parseTimezone(value, cal, idx);
+ return cal.getTime();
+ }
+
+
+ /**
* 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)},
@@ -125,28 +151,39 @@
/**
- * 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.
+ * Converts a <code>boolean</code> value to the literal strings "true" or
+ * "false" (XML Schema <code>boolean</code> fields also allow "1" or "0").
+ */
+ public static String formatXsdBoolean(boolean value)
+ {
+ return value ? "true" : "false";
+ }
+
+
+ /**
+ * Parses an XML Schema <code>boolean</code> value, accepting any of
+ * the legal formats and trimming whitespace.
*
- * @throws XmlException if unable to parse.
+ * @throws XmlException the passed value, after trimming, is not one
+ * of the 4 legal representations of boolean data under XML
+ * Schema.
*/
- public static Date parseXsdDatetime(String value)
+ public static boolean parseXsdBoolean(String value)
{
- Calendar cal = GregorianCalendar.getInstance();
- cal.setTimeInMillis(0);
- int idx = parserHelper(value, cal, Calendar.YEAR, 0);
- idx = parserHelper(value, cal, Calendar.MONTH, idx+1);
- idx = parserHelper(value, cal, Calendar.DAY_OF_MONTH, idx+1);
- idx = parserHelper(value, cal, Calendar.HOUR_OF_DAY, idx+1);
- idx = parserHelper(value, cal, Calendar.MINUTE, idx+1);
- idx = parserHelper(value, cal, Calendar.SECOND, idx+1);
- if (idx < value.length() && (value.charAt(idx) == '.'))
+ try
{
- idx = parserHelper(value, cal, Calendar.MILLISECOND, idx+1);
+ value = value.trim();
+ if (value.equals("true") || value.equals("1"))
+ return true;
+ else if (value.equals("false") || value.equals("0"))
+ return false;
+ else
+ throw new XmlException("not an XSD boolean value: " + value);
}
- parseTimezone(value, cal, idx);
- return cal.getTime();
+ catch (NullPointerException e)
+ {
+ throw new XmlException("null values not allowed");
+ }
}
Modified: branches/dev-1.1/src/test/java/net/sf/practicalxml/TestXmlUtil.java
===================================================================
--- branches/dev-1.1/src/test/java/net/sf/practicalxml/TestXmlUtil.java 2009-07-23 21:36:40 UTC (rev 93)
+++ branches/dev-1.1/src/test/java/net/sf/practicalxml/TestXmlUtil.java 2009-07-23 22:06:24 UTC (rev 94)
@@ -88,6 +88,52 @@
}
+ public void testFormatXsdBoolean() throws Exception
+ {
+ assertEquals("true", XmlUtil.formatXsdBoolean(true));
+ assertEquals("false", XmlUtil.formatXsdBoolean(false));
+ }
+
+
+ public void testParseXsdBoolean() throws Exception
+ {
+ assertEquals(true, XmlUtil.parseXsdBoolean("true"));
+ assertEquals(true, XmlUtil.parseXsdBoolean("1"));
+ assertEquals(false, XmlUtil.parseXsdBoolean("false"));
+ assertEquals(false, XmlUtil.parseXsdBoolean("0"));
+
+ try
+ {
+ XmlUtil.parseXsdBoolean("TRUE");
+ fail("uppercase \"TRUE\" not legal per XML schema");
+ }
+ catch (XmlException e)
+ {
+ // success
+ }
+
+ try
+ {
+ XmlUtil.parseXsdBoolean("");
+ fail("empty string not legal per XML schema");
+ }
+ catch (XmlException e)
+ {
+ // success
+ }
+
+ try
+ {
+ XmlUtil.parseXsdBoolean(null);
+ fail("null not legal");
+ }
+ catch (XmlException e)
+ {
+ // success
+ }
+ }
+
+
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.
|