Re: CVS Update: xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic
Dennis Sosnoski <[email protected]> Tue, 01 Jul 2003 13:36:50 -0700
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
You're going to have some problems with these. For one thing,
Integer.parseInt() will blow up with a leading '+' sign, and
parseDouble()/parseFloat() will accept some values that WXS does not
allow. Finally, you need to trim leading and trailing whitespace on the
text values before parsing them for compatibility with WXS.
You might want to check my JiBX code for handling the WXS types:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jibx/distrib/build/src/org/jibx/runtime/Utility.java
This has date/time handling as well as the numeric types. The methods
don't trim the whitespace, but that can easily be handled before calling
them.
- Dennis
Aleksander Andrzej Slominski wrote:
>aslom 03/07/01 14:02:47
>
> Modified: addons/java/wrapper/src/org/xmlpull/v1/wrapper
> XmlPullParserWrapper.java XmlSerializerWrapper.java
> addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic
> StaticXmlPullParserWrapper.java
> StaticXmlSerializerWrapper.java
> Log:
> added set of methods to read and write XSD types
>
> Revision Changes Path
> 1.5 +8 -1 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullParserWrapper.java
>
> Index: XmlPullParserWrapper.java
> ===================================================================
> RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullParserWrapper.java,v
> retrieving revision 1.4
> retrieving revision 1.5
> diff -u -b -t -w -r1.4 -r1.5
> --- XmlPullParserWrapper.java 18 May 2003 13:21:15 -0000 1.4
> +++ XmlPullParserWrapper.java 1 Jul 2003 19:02:46 -0000 1.5
> @@ -131,6 +131,13 @@
> */
> public void skipSubTree()
> throws XmlPullParserException, IOException;
> +
> + // set of methods to read XSD types
> + public double readDouble() throws XmlPullParserException, IOException;
> + public float readFloat() throws XmlPullParserException, IOException;
> + public int readInt() throws XmlPullParserException, IOException;
> + public String readString() throws XmlPullParserException, IOException;
> +
>
> }
>
>
>
>
> 1.6 +30 -2 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlSerializerWrapper.java
>
> Index: XmlSerializerWrapper.java
> ===================================================================
> RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlSerializerWrapper.java,v
> retrieving revision 1.5
> retrieving revision 1.6
> diff -u -b -t -w -r1.5 -r1.6
> --- XmlSerializerWrapper.java 18 May 2003 13:21:15 -0000 1.5
> +++ XmlSerializerWrapper.java 1 Jul 2003 19:02:46 -0000 1.6
> @@ -69,7 +69,35 @@
> public void event(XmlPullParser pp)
> throws IOException, IllegalArgumentException, IllegalStateException, XmlPullParserException;
>
> - public String escapeText(String text);
> - public String escapeAttributeValue(String text);
> + public String escapeText(String text) throws IllegalArgumentException;
> + public String escapeAttributeValue(String text) throws IllegalArgumentException;
> +
> + // set of methods to make easy to write XSD types
> +
> + /**
> + * Write as text value of argument.
> + */
> + public void writeDouble(double d)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> + public void writeFloat(float f)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> + public void writeInt(int i)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> + public void writeString(String s)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> +
> + /**
> + * Write as element with namesoace and name and value inside
> + * (looks like <ns:name>value</ns:name> where ns is prefix
> + * for namespace autoatically declared if needed).
> + */
> + public void writeDoubleElement(String namespace, String name, double d)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> + public void writeFloatElement(String namespace, String name, float f)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> + public void writeIntElement(String namespace, String name, int i)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> + public void wiriteStringElement(String namespace, String name, String s)
> + throws XmlPullParserException, IOException, IllegalArgumentException;
> }
>
>
>
>
> 1.4 +67 -14 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlPullParserWrapper.java
>
> Index: StaticXmlPullParserWrapper.java
> ===================================================================
> RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlPullParserWrapper.java,v
> retrieving revision 1.3
> retrieving revision 1.4
> diff -u -b -t -w -r1.3 -r1.4
> --- StaticXmlPullParserWrapper.java 5 May 2003 01:36:18 -0000 1.3
> +++ StaticXmlPullParserWrapper.java 1 Jul 2003 19:02:46 -0000 1.4
> @@ -144,6 +144,59 @@
>
> public void skipSubTree() throws XmlPullParserException, IOException {
> XmlPullUtil.skipSubTree(pp);
> + }
> +
> + public double readDouble() throws XmlPullParserException, IOException {
> + String value = pp.nextText();
> + double d;
> + try {
> + d = Double.parseDouble(value);
> + } catch(NumberFormatException ex) {
> + if(value.equals("INF") || value.toLowerCase().equals("infinity")) {
> + d = Double.POSITIVE_INFINITY;
> + } else if (value.equals("-INF")
> + || value.toLowerCase().equals("-infinity")) {
> + d = Double.NEGATIVE_INFINITY;
> + } else if (value.equals("NaN")) {
> + d = Double.NaN;
> + } else {
> + throw new XmlPullParserException("can't parse double value '"+value+"'", this, ex);
> + }
> + }
> + return d;
> + }
> +
> + public float readFloat() throws XmlPullParserException, IOException {
> + String value = pp.nextText();
> + float f;
> + try {
> + f = Float.parseFloat(value);
> + } catch(NumberFormatException ex) {
> + if(value.equals("INF") || value.toLowerCase().equals("infinity")) {
> + f = Float.POSITIVE_INFINITY;
> + } else if (value.equals("-INF")
> + || value.toLowerCase().equals("-infinity")) {
> + f = Float.NEGATIVE_INFINITY;
> + } else if (value.equals("NaN")) {
> + f = Float.NaN;
> + } else {
> + throw new XmlPullParserException("can't parse float value '"+value+"'", this, ex);
> + }
> + }
> + return f;
> + }
> +
> + public int readInt() throws XmlPullParserException, IOException {
> + try {
> + int i = Integer.parseInt(pp.nextText());
> + return i;
> + } catch(NumberFormatException ex) {
> + throw new XmlPullParserException("can't parse int value", this, ex);
> + }
> + }
> +
> + public String readString() throws XmlPullParserException, IOException {
> + return pp.nextText();
> }
>
> }
>
>
>
> 1.8 +71 -0 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlSerializerWrapper.java
>
> Index: StaticXmlSerializerWrapper.java
> ===================================================================
> RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlSerializerWrapper.java,v
> retrieving revision 1.7
> retrieving revision 1.8
> diff -u -b -t -w -r1.7 -r1.8
> --- StaticXmlSerializerWrapper.java 20 May 2003 10:03:13 -0000 1.7
> +++ StaticXmlSerializerWrapper.java 1 Jul 2003 19:02:46 -0000 1.8
> @@ -348,5 +348,76 @@
> }
>
>
> + public void writeDouble(double d)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + if(d == Double.POSITIVE_INFINITY) {
> + xs.text("INF");
> + } else if(d == Double.NEGATIVE_INFINITY) {
> + xs.text("-INF");
> + } else {
> + xs.text(Double.toString(d));
> + }
> + }
> +
> + public void writeFloat(float f)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + if(f == Float.POSITIVE_INFINITY) {
> + xs.text("INF");
> + } else if(f == Float.NEGATIVE_INFINITY) {
> + xs.text("-INF");
> + } else {
> + xs.text(Float.toString(f));
> + }
> + }
> +
> + public void writeInt(int i)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + xs.text(Integer.toString(i));
> + }
> +
> + public void writeString(String s)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + if( s == null ) {
> + throws new IllegalArgumentException("null string can not be written");
> + }
> + xs.text(s);
> + }
> +
> + public void writeDoubleElement(String namespace, String name, double d)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + xs.startTag(namespace, name);
> + writeDouble(d);
> + xs.endTag(namespace, name);
> + }
> +
> + public void writeFloatElement(String namespace, String name, float f)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + xs.startTag(namespace, name);
> + writeFloat(f);
> + xs.endTag(namespace, name);
> + }
> +
> + public void writeIntElement(String namespace, String name, int i)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + xs.startTag(namespace, name);
> + writeInt(i);
> + xs.endTag(namespace, name);
> + }
> +
> + public void wiriteStringElement(String namespace, String name, String s)
> + throws XmlPullParserException, IOException, IllegalArgumentException
> + {
> + xs.startTag(namespace, name);
> + writeString(s);
> + xs.endTag(namespace, name);
> + }
> +
> }
>
>
>
>
>
>
>
>To unsubscribe from this group, send an email to:
>[email protected]
>
>
>
>Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
>
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading! Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/Lj3uPC/Me7FAA/ySSFAA/2U_rlB/TM
---------------------------------------------------------------------~->
To unsubscribe from this group, send an email to:
[email protected]
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/