CVS Update: xmlpull-api-v1/addons/java/wrapper/tests/org/xmlpull/v1/wrapper/junit

Aleksander Andrzej Slominski <[email protected]> Sun, 4 May 2003 20:36:18 -0500 (EST)
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       03/05/04 20:36:18

  Modified:    addons/java/util/src/org/xmlpull/v1/util XmlPullUtil.java
               addons/java/wrapper/src/org/xmlpull/v1/wrapper
                        XmlPullParserWrapper.java
                        XmlPullWrapperFactory.java
                        XmlSerializerWrapper.java
               addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic
                        StaticXmlPullParserWrapper.java
                        StaticXmlSerializerWrapper.java
                        XmlSerializerDelegate.java
               addons/java/wrapper/tests/org/xmlpull/v1/wrapper/junit
                        TestXmlPullWrapper.java
  Log:
  minor improvements, additional methods, improved unitt tests and most importantly various bug fixes reported by Naresh
  
  Revision  Changes    Path
  1.3       +13 -3     xmlpull-api-v1/addons/java/util/src/org/xmlpull/v1/util/XmlPullUtil.java
  
  Index: XmlPullUtil.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/util/src/org/xmlpull/v1/util/XmlPullUtil.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -t -w -r1.2 -r1.3
  --- XmlPullUtil.java	3 May 2003 05:51:29 -0000	1.2
  +++ XmlPullUtil.java	5 May 2003 01:36:17 -0000	1.3
  @@ -136,6 +136,16 @@
       }
   
       /**
  +     * combine nextTag(); pp.require(pp.START_TAG, null, name);
  +     */
  +    public static void nextStartTag(XmlPullParser pp, String name)
  +        throws XmlPullParserException, IOException
  +    {
  +        pp.nextTag();
  +        pp.require(XmlPullParser.START_TAG, null, name);
  +    }
  +
  +    /**
        * combine nextTag(); pp.require(pp.START_TAG, namespace, name);
        */
       public static void nextStartTag(XmlPullParser pp, String namespace, String name)
  @@ -210,9 +220,9 @@
       public static boolean matches(XmlPullParser pp, int type, String namespace, String name)
           throws XmlPullParserException
       {
  -        boolean matches = type != pp.getEventType()
  -            || (namespace != null && !namespace.equals (pp.getNamespace()))
  -            || (name != null && !name.equals (pp.getName ()));
  +        boolean matches = type == pp.getEventType()
  +            && (namespace == null || namespace.equals (pp.getNamespace()))
  +            && (name == null || name.equals (pp.getName ()));
   
           return matches;
       }
  
  
  
  1.3       +43 -19    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.2
  retrieving revision 1.3
  diff -u -b -t -w -r1.2 -r1.3
  --- XmlPullParserWrapper.java	3 May 2003 22:23:12 -0000	1.2
  +++ XmlPullParserWrapper.java	5 May 2003 01:36:17 -0000	1.3
  @@ -17,10 +17,15 @@
    * @author Naresh Bhatia
    */
   public interface XmlPullParserWrapper extends XmlPullParser {
  -
  +    public static final String NO_NAMESPACE = "";
       public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
   
       /**
  +     * Return value of attribute with given name and no namespace.
  +     */
  +    public String getAttributeValue(String name);
  +
  +    /**
        * Return PITarget from Processing Instruction (PI) as defined in
        * XML 1.0 Section 2.6 Processing Instructions
        *  <code>[16] PI ::= '&lt;?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'</code>
  @@ -36,6 +41,28 @@
        */
       public String getPIData() throws IllegalStateException;
   
  +
  +    /**
  +     * Read attribute value and return it or throw exception if
  +     * current element does not have such attribute.
  +     */
  +    public String getRequiredAttributeValue(String name)
  +        throws IOException, XmlPullParserException;
  +
  +    /**
  +     * Read attribute value and return it or throw exception if
  +     * current element does not have such attribute.
  +     */
  +    public String getRequiredAttributeValue(String namespace, String name)
  +        throws IOException, XmlPullParserException;
  +
  +    /**
  +     * Read the text of a required element and return it or throw exception if
  +     * required element is not found. Useful for getting the text of simple
  +     * elements such as <username>johndoe</username>. Assumes that parser is
  +     * just before the start tag and leaves the parser at the end tag. If the
  +     * text is nil (e.g. <username xsi:nil="true"/>), then a null will be returned.
  +     */
       public String getRequiredElementText(String namespace, String name)
           throws IOException, XmlPullParserException;
   
  @@ -54,14 +81,15 @@
   
   
       /**
  -     * Return value of attribute with given name and no namespace.
  +     * call parser nextTag() and check that it is START_TAG, throw exception if not.
        */
  -    public String getAttributeValue(String name);
  +    public void nextStartTag()
  +        throws XmlPullParserException, IOException;
   
       /**
  -     * call parser nextTag() and check that it is START_TAG, throw exception if not.
  +     * combine nextTag(); pp.require(pp.START_TAG, null, name);
        */
  -    public void nextStartTag()
  +    public void nextStartTag(String name)
           throws XmlPullParserException, IOException;
   
       /**
  @@ -71,6 +99,16 @@
           throws XmlPullParserException, IOException;
   
   
  +    /**
  +     * Call parser nextTag() and check that it is END_TAG, throw exception if not.
  +     */
  +    public void nextEndTag() throws XmlPullParserException, IOException;
  +
  +    /**
  +     * combine nextTag(); pp.require(pp.END_TAG, null, name);
  +     */
  +    public void nextEndTag(String name)
  +        throws XmlPullParserException, IOException;
   
   
       /**
  @@ -87,20 +125,6 @@
   
       public String nextText(String namespace, String name)
           throws IOException, XmlPullParserException;
  -
  -    /**
  -     * Read attribute value and return it or throw exception if
  -     * current element does not have such attribute.
  -     */
  -
  -    public String getRequiredAttributeValue(String namespace, String name)
  -        throws IOException, XmlPullParserException;
  -
  -    /**
  -     * Call parser nextTag() and check that it is END_TAG, throw exception if not.
  -     */
  -    public void nextEndTag() throws XmlPullParserException, IOException;
  -
   
       /**
        * Skip sub tree that is currently porser positioned on.
  
  
  
  1.4       +1 -1      xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullWrapperFactory.java
  
  Index: XmlPullWrapperFactory.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullWrapperFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -t -w -r1.3 -r1.4
  --- XmlPullWrapperFactory.java	3 May 2003 22:23:12 -0000	1.3
  +++ XmlPullWrapperFactory.java	5 May 2003 01:36:17 -0000	1.4
  @@ -19,7 +19,7 @@
   
   public class XmlPullWrapperFactory {
       private final static boolean DEBUG = false;
  -    protected ClassLoader classLoader;
  +    //protected ClassLoader classLoader;
       protected XmlPullParserFactory f;
       //protected boolean useDynamic;
   
  
  
  
  1.4       +24 -3     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.3
  retrieving revision 1.4
  diff -u -b -t -w -r1.3 -r1.4
  --- XmlSerializerWrapper.java	3 May 2003 22:48:22 -0000	1.3
  +++ XmlSerializerWrapper.java	5 May 2003 01:36:17 -0000	1.4
  @@ -14,10 +14,32 @@
    * @author Naresh Bhatia
    */
   public interface XmlSerializerWrapper extends XmlSerializer {
  +    public static final String NO_NAMESPACE = "";
       public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
   
  -    public String getCurrentNamespace();
  -    public void setCurrentNamespace(String value);
  +    /**
  +     * Get namespace that is used as default when no namespace parameter is used for
  +     * startTag(), endTag() and element()
  +     */
  +    public String getCurrentNamespaceForElements();
  +
  +    /**
  +     * Set namespace to use in startTag(), endTag() and element()
  +     * when methods called are those without namespace parameter.
  +     */
  +    public String setCurrentNamespaceForElements(String value);
  +
  +    /**
  +     * Write an attribute without namespace.
  +     * Calls to attribute() MUST follow a call to
  +     * startTag() immediately. If there is no prefix defined for the
  +     * given namespace, a prefix will be defined automatically.
  +     * NOTE: current element namespace is not used attribute and attributre has no namespace.
  +     */
  +    public XmlSerializer attribute (String name, String value)
  +        throws IOException, IllegalArgumentException, IllegalStateException;
  +
  +
   
       /** Write start tag in current namespace with name given as argument. */
       public XmlSerializer startTag (String name)
  @@ -26,7 +48,6 @@
       /** Write end tag in current namespace with name given as argument. */
       public XmlSerializer endTag (String name)
           throws IOException, IllegalArgumentException, IllegalStateException;
  -
   
       /**
        * Writes a simple element such as &lt;username>johndoe&lt;/username>. The namespace
  
  
  
  1.3       +36 -32    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.2
  retrieving revision 1.3
  diff -u -b -t -w -r1.2 -r1.3
  --- StaticXmlPullParserWrapper.java	3 May 2003 22:23:13 -0000	1.2
  +++ StaticXmlPullParserWrapper.java	5 May 2003 01:36:18 -0000	1.3
  @@ -28,6 +28,19 @@
           return XmlPullUtil.getAttributeValue(pp, name);
       }
   
  +    public String getRequiredAttributeValue(String name)
  +        throws IOException, XmlPullParserException
  +    {
  +        return XmlPullUtil.getRequiredAttributeValue(pp, null, name);
  +    }
  +
  +
  +    public String getRequiredAttributeValue(String namespace, String name)
  +        throws IOException, XmlPullParserException
  +    {
  +        return XmlPullUtil.getRequiredAttributeValue(pp, namespace, name);
  +    }
  +
   
     /**
        * Read the text of a required element and return it or throw exception if
  @@ -83,58 +96,49 @@
           return XmlPullUtil.matches(pp, type, namespace, name);
       }
   
  -    /**
  -     * call parser nextTag() and check that it is START_TAG, throw exception if not.
  -     */
       public void nextStartTag()
           throws XmlPullParserException, IOException
       {
  -        XmlPullUtil.nextStartTag(pp);
  +        if(pp.nextTag() != XmlPullParser.START_TAG) {
  +            throw new XmlPullParserException(
  +                "expected START_TAG and not "+pp.getPositionDescription());
  +        }
       }
   
  -    /**
  -     * combine nextTag(); pp.require(pp.START_TAG, namespace, name);
  -     */
  -    public void nextStartTag(String namespace, String name)
  +    public void nextStartTag(String name)
           throws XmlPullParserException, IOException
       {
  -        XmlPullUtil.nextStartTag(pp, namespace, name);
  +        pp.nextTag();
  +        pp.require(XmlPullParser.START_TAG, null, name);
       }
   
  -    /**
  -     * combine nextTag(); pp.require(pp.END_TAG, namespace, name);
  -     */
  -    public void nextEndTag(String namespace, String name)
  +    public void nextStartTag(String namespace, String name)
           throws XmlPullParserException, IOException
       {
  -        XmlPullUtil.nextEndTag(pp, namespace, name);
  +        pp.nextTag();
  +        pp.require(XmlPullParser.START_TAG, namespace, name);
       }
   
  +    public void nextEndTag() throws XmlPullParserException, IOException {
  +        XmlPullUtil.nextEndTag(pp);
  +    }
   
  -    /**
  -     * Read text content of element with given namespace and name
  -     * (use null namespace do indicate that nemspace should not be checked)
  -     */
  -
  -    public String nextText(String namespace, String name)
  -        throws IOException, XmlPullParserException
  +    public void nextEndTag(String name)
  +        throws XmlPullParserException, IOException
       {
  -        return XmlPullUtil.nextText(pp, namespace, name);
  +        XmlPullUtil.nextEndTag(pp, null, name);
       }
   
  -    /**
  -     * Read attribute value and return it or throw exception if
  -     * current element does not have such attribute.
  -     */
  -
  -    public String getRequiredAttributeValue(String namespace, String name)
  -        throws IOException, XmlPullParserException
  +    public void nextEndTag(String namespace, String name)
  +        throws XmlPullParserException, IOException
       {
  -        return XmlPullUtil.getRequiredAttributeValue(pp, namespace, name);
  +        XmlPullUtil.nextEndTag(pp, namespace, name);
       }
   
  -    public void nextEndTag() throws XmlPullParserException, IOException {
  -        XmlPullUtil.nextEndTag(pp);
  +    public String nextText(String namespace, String name)
  +        throws IOException, XmlPullParserException
  +    {
  +        return XmlPullUtil.nextText(pp, namespace, name);
       }
   
   
  
  
  
  1.4       +12 -2     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.3
  retrieving revision 1.4
  diff -u -b -t -w -r1.3 -r1.4
  --- StaticXmlSerializerWrapper.java	3 May 2003 22:48:22 -0000	1.3
  +++ StaticXmlSerializerWrapper.java	5 May 2003 01:36:18 -0000	1.4
  @@ -25,9 +25,19 @@
           super(xs);
       }
   
  -    public String getCurrentNamespace() { return currentNs; }
  -    public void setCurrentNamespace(String value) { currentNs = value; }
  +    public String getCurrentNamespaceForElements() { return currentNs; }
  +    public String setCurrentNamespaceForElements(String value)
  +    {
  +        String old = currentNs;
  +        currentNs = value;
  +        return old;
  +    }
   
  +   public XmlSerializer attribute (String name, String value)
  +        throws IOException, IllegalArgumentException, IllegalStateException
  +    {
  +        return xs.attribute(null, name, value);
  +    }
   
       public XmlSerializer startTag (String name)
           throws IOException, IllegalArgumentException, IllegalStateException
  
  
  
  1.2       +1 -1      xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/XmlSerializerDelegate.java
  
  Index: XmlSerializerDelegate.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/XmlSerializerDelegate.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -t -w -r1.1 -r1.2
  --- XmlSerializerDelegate.java	3 May 2003 05:56:46 -0000	1.1
  +++ XmlSerializerDelegate.java	5 May 2003 01:36:18 -0000	1.2
  @@ -101,7 +101,7 @@
       }
   
       public XmlSerializer attribute(String namespace, String name, String value) throws IOException, IllegalArgumentException, IllegalStateException {
  -        return attribute(namespace, name, value);
  +        return xs.attribute(namespace, name, value);
       }
   
       public void startDocument(String encoding, Boolean standalone) throws IOException, IllegalArgumentException, IllegalStateException {
  
  
  
  1.2       +35 -0     xmlpull-api-v1/addons/java/wrapper/tests/org/xmlpull/v1/wrapper/junit/TestXmlPullWrapper.java
  
  Index: TestXmlPullWrapper.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/tests/org/xmlpull/v1/wrapper/junit/TestXmlPullWrapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -t -w -r1.1 -r1.2
  --- TestXmlPullWrapper.java	3 May 2003 17:11:51 -0000	1.1
  +++ TestXmlPullWrapper.java	5 May 2003 01:36:18 -0000	1.2
  @@ -10,8 +10,11 @@
   import junit.framework.TestSuite;
   import org.xmlpull.v1.XmlPullParser;
   import org.xmlpull.v1.XmlPullParserException;
  +import org.xmlpull.v1.XmlPullParserFactory;
   import org.xmlpull.v1.wrapper.XmlPullParserWrapper;
   import org.xmlpull.v1.wrapper.XmlPullWrapperFactory;
  +import org.xmlpull.v1.wrapper.XmlSerializerWrapper;
  +import java.io.StringWriter;
   
   /**
    * Test some wrapper utility operations.
  @@ -35,6 +38,38 @@
           wrappedFactory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
           assertEquals(true, wrappedFactory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
           assertEquals(false, wrappedFactory.getFeature(XmlPullParser.FEATURE_VALIDATION));
  +    }
  +
  +    public void testSimple() throws IOException, XmlPullParserException {
  +        XmlPullWrapperFactory wrapperFactory = XmlPullWrapperFactory.newInstance(
  +            System.getProperty(XmlPullParserFactory.PROPERTY_NAME),
  +            XmlPullParserFactory.class);
  +        wrapperFactory.setNamespaceAware(true);
  +        XmlSerializerWrapper xs = wrapperFactory.newSerializerWrapper();
  +        StringWriter sw = new StringWriter();
  +        xs.setOutput(sw);
  +        xs.startDocument("UTF-8", Boolean.TRUE);
  +        xs.startTag("test");
  +        xs.attribute("fooAttr", "fooValue");
  +        final String NS = "http://tempuri.org/xmlpull-test";
  +        xs.setCurrentNamespaceForElements(NS);
  +        final String MSG = "World & Universe!";
  +        xs.element("hello", MSG);
  +        xs.setCurrentNamespaceForElements(null);
  +        xs.endTag("test");
  +        xs.endDocument();
  +
  +        String s = sw.toString();
  +        System.out.println(getClass()+" s="+s);
  +        XmlPullParserWrapper pw = wrapperFactory.newPullWrapper();
  +        pw.setInput(new StringReader(s));
  +        pw.nextStartTag("test");
  +        assertEquals(pw.getAttributeValue("fooAttr"), "fooValue");
  +        pw.nextTag();
  +        assertTrue(pw.matches(XmlPullParser.START_TAG, NS, "hello"));
  +        assertEquals(pw.nextText(NS, "hello"), MSG);
  +        assertTrue(pw.matches(XmlPullParser.END_TAG, null, "hello"));
  +        pw.nextEndTag("test");
       }
   
       public void testPI() throws IOException, XmlPullParserException {
  
  
  


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading!
Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/cjB9SD/od7FAA/uetFAA/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/