CVS Update: xmlpull-api-v1/addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder

Aleksander Andrzej Slominski <[email protected]> Tue, 29 Jul 2003 16:44:14 -0500 (EST)
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       03/07/29 16:44:14

  Modified:    addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder
                        DOM2XmlPullBuilder.java
  Log:
  made it completely stateless (do not require XPP factory in constructor) and removed throwing exception from constructor
  
  Revision  Changes    Path
  1.5       +102 -96   xmlpull-api-v1/addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder/DOM2XmlPullBuilder.java
  
  Index: DOM2XmlPullBuilder.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder/DOM2XmlPullBuilder.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -t -w -r1.4 -r1.5
  --- DOM2XmlPullBuilder.java	8 Jul 2003 22:23:07 -0000	1.4
  +++ DOM2XmlPullBuilder.java	29 Jul 2003 21:44:14 -0000	1.5
  @@ -22,115 +22,39 @@
   import org.xmlpull.v1.XmlPullParserFactory;
   import org.w3c.dom.DOMException;
   
  +//TOOD add parse methodthat will usenextToken() to reconstruct complete XML infoset
  +
   /**
    * <strong>Simplistic</strong> DOM2 builder that should be enough to do support most cases.
    * Requires JAXP DOMBuilder to provide DOM2 implementation.
  + * <p>NOTE:this class is stateless factory and it is safe to share between multiple threads.
  + * </p>
    *
    * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
    */
   
   public class DOM2XmlPullBuilder {
   
  -    /**
  -     * Minimal inline test
  -     */
  -    public static void main(String[] args) throws Exception {
  -        DOM2XmlPullBuilder builder = new DOM2XmlPullBuilder();
  -
  -        final String XML = "<n:foo xmlns:n='uri1'><bar n:attr='test' xmlns='uri2'>baz</bar></n:foo>";
  -        StringReader reader = new StringReader(XML);
  -
  -        // create document
  -
  -        Element el1 = builder.parse(reader);
  -        //System.out.println("doc="+doc);
  -
  -
  -        // serialize and deserialzie ...
  -        StringWriter sw = new StringWriter();
  -
  -        // requires JAXP
  -        //        TransformerFactory xformFactory
  -        //            = TransformerFactory.newInstance();
  -        //        Transformer idTransform = xformFactory.newTransformer();
  -        //        Source input = new DOMSource(doc1);
  -        //        Result output = new StreamResult(sw);
  -        //        idTransform.transform(input, output);
  -
  -        //OutputFormat fmt = new OutputFormat();
  -        //XMLSerializer serializer = new XMLSerializer(sw, null);
  -        //serializer.serialize(doc1);
  -        //sw.close();
  -        //String serialized = sw.toString();
  -        //System.out.println("serialized="+serialized);
  -
  -        reader = new StringReader(XML);
  -
  -        // reparse
  -
  -        Element el2 = builder.parse(reader);
  -
  -        // check that what was written is OK
  -
  -        Element root = el2; //doc2.getDocumentElement();
  -        //System.out.println("root="+root);
  -        System.out.println ("root ns=" + root.getNamespaceURI() + ", localName=" +root.getLocalName());
  -        assertEquals("uri1", root.getNamespaceURI());
  -        assertEquals("foo", root.getLocalName());
  -
  -        NodeList children = root.getElementsByTagNameNS("*","bar");
  -        Element bar = (Element)children.item(0);
  -        System.out.println ("bar ns=" + bar.getNamespaceURI() + ", localName=" +bar.getLocalName());
  -        assertEquals("uri2", bar.getNamespaceURI());
  -        assertEquals("bar", bar.getLocalName());
  -
  -        //
  -        String attrValue = bar.getAttributeNS("uri1", "attr");
  -        assertEquals("test", attrValue);
  -        Attr attr = bar.getAttributeNodeNS("uri1", "attr");
  -        assertNotNull(attr);
  -        assertEquals("uri1", attr.getNamespaceURI());
  -        assertEquals("attr", attr.getLocalName());
  -        assertEquals("test", attr.getValue());
  -
  -
  -        Text text = (Text)bar.getFirstChild();
  -        System.out.println("text="+text.getNodeValue());
  -        assertEquals("baz", text.getNodeValue());
  -
  -    }
  -
  -    private static void assertEquals(String expected, String s) {
  -        if((expected != null && !expected.equals(s)) || (expected == null && s == null)) {
  -            throw new RuntimeException("expected '"+expected+"' but got '"+s+"'");
  -        }
  -    }
  -    private static void assertNotNull(Object o) {
  -        if(o == null) {
  -            throw new RuntimeException("expected no null value");
  -        }
  -    }
  -
       //protected XmlPullParser pp;
  -    protected XmlPullParserFactory factory;
  +    //protected XmlPullParserFactory factory;
   
  -    public DOM2XmlPullBuilder() throws XmlPullParserException {
  -        factory = XmlPullParserFactory.newInstance();
  +    public DOM2XmlPullBuilder() { //throws XmlPullParserException {
  +        //factory = XmlPullParserFactory.newInstance();
       }
       //public DOM2XmlPullBuilder(XmlPullParser pp) throws XmlPullParserException {
  -    public DOM2XmlPullBuilder(XmlPullParserFactory factory)throws XmlPullParserException
  -    {
  -        this.factory = factory;
  -    }
  +    //public DOM2XmlPullBuilder(XmlPullParserFactory factory)throws XmlPullParserException
  +    //{
  +    //    this.factory = factory;
  +    //}
   
       protected Document newDoc() throws XmlPullParserException {
           try {
               // if you see dreaded "java.lang.NoClassDefFoundError: org/w3c/dom/ranges/DocumentRange"
               // add the newest xercesImpl and apis JAR file to JRE/lib/endorsed
               // for more deatils see: http://java.sun.com/j2se/1.4.2/docs/guide/standards/index.html
  -            DocumentBuilderFactory factory
  +            DocumentBuilderFactory domFactory
                   = DocumentBuilderFactory.newInstance();
  -            DocumentBuilder builder = factory.newDocumentBuilder();
  +            DocumentBuilder builder = domFactory.newDocumentBuilder();
               DOMImplementation impl = builder.getDOMImplementation();
               return builder.newDocument();
           } catch (FactoryConfigurationError ex) {
  @@ -143,7 +67,7 @@
       }
   
       protected XmlPullParser newParser() throws XmlPullParserException {
  -        return factory.newPullParser();
  +        return null; //factory.newPullParser();
       }
   
       public Element parse(Reader reader) throws XmlPullParserException, IOException {
  @@ -154,13 +78,17 @@
       public Element parse(Reader reader, Document docFactory)
           throws XmlPullParserException, IOException
       {
  -        Document doc = newDoc();
           XmlPullParser pp = newParser();
           pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
           pp.setInput(reader);
           pp.next();
  -        Element root = parseSubTree(pp, doc);
  -        //doc.appendChild(root);
  +        return parse(pp, docFactory);
  +    }
  +
  +    public Element parse(XmlPullParser pp, Document docFactory)
  +        throws XmlPullParserException, IOException
  +    {
  +        Element root = parseSubTree(pp, docFactory);
           return root;
       }
   
  @@ -182,7 +110,7 @@
           private Document docFactory;
           private boolean scanNamespaces = true;
   
  -        BuildProcess() {
  +        private BuildProcess() {
           }
   
           public Element parseSubTree(XmlPullParser pp, Document docFactory)
  @@ -241,8 +169,6 @@
   
           private void declareNamespaces(XmlPullParser pp, Element parent)
               throws DOMException, XmlPullParserException
  -
  -
           {
               if(scanNamespaces) {
                   scanNamespaces = false;
  @@ -280,6 +206,86 @@
               parent.setAttributeNS("http://www.w3.org/2000/xmlns/", xmlnsDecl, xmlnsUri);
           }
   
  +
  +    }
  +
  +    private static void assertEquals(String expected, String s) {
  +        if((expected != null && !expected.equals(s)) || (expected == null && s == null)) {
  +            throw new RuntimeException("expected '"+expected+"' but got '"+s+"'");
  +        }
  +    }
  +    private static void assertNotNull(Object o) {
  +        if(o == null) {
  +            throw new RuntimeException("expected no null value");
  +        }
  +    }
  +
  +    /**
  +     * Minimal inline test
  +     */
  +    public static void main(String[] args) throws Exception {
  +        DOM2XmlPullBuilder builder = new DOM2XmlPullBuilder();
  +
  +        final String XML = "<n:foo xmlns:n='uri1'><bar n:attr='test' xmlns='uri2'>baz</bar></n:foo>";
  +        StringReader reader = new StringReader(XML);
  +
  +        // create document
  +
  +        Element el1 = builder.parse(reader);
  +        //System.out.println("doc="+doc);
  +
  +
  +        // serialize and deserialzie ...
  +        StringWriter sw = new StringWriter();
  +
  +        // requires JAXP
  +        //        TransformerFactory xformFactory
  +        //            = TransformerFactory.newInstance();
  +        //        Transformer idTransform = xformFactory.newTransformer();
  +        //        Source input = new DOMSource(doc1);
  +        //        Result output = new StreamResult(sw);
  +        //        idTransform.transform(input, output);
  +
  +        //OutputFormat fmt = new OutputFormat();
  +        //XMLSerializer serializer = new XMLSerializer(sw, null);
  +        //serializer.serialize(doc1);
  +        //sw.close();
  +        //String serialized = sw.toString();
  +        //System.out.println("serialized="+serialized);
  +
  +        reader = new StringReader(XML);
  +
  +        // reparse
  +
  +        Element el2 = builder.parse(reader);
  +
  +        // check that what was written is OK
  +
  +        Element root = el2; //doc2.getDocumentElement();
  +        //System.out.println("root="+root);
  +        System.out.println ("root ns=" + root.getNamespaceURI() + ", localName=" +root.getLocalName());
  +        assertEquals("uri1", root.getNamespaceURI());
  +        assertEquals("foo", root.getLocalName());
  +
  +        NodeList children = root.getElementsByTagNameNS("*","bar");
  +        Element bar = (Element)children.item(0);
  +        System.out.println ("bar ns=" + bar.getNamespaceURI() + ", localName=" +bar.getLocalName());
  +        assertEquals("uri2", bar.getNamespaceURI());
  +        assertEquals("bar", bar.getLocalName());
  +
  +        //
  +        String attrValue = bar.getAttributeNS("uri1", "attr");
  +        assertEquals("test", attrValue);
  +        Attr attr = bar.getAttributeNodeNS("uri1", "attr");
  +        assertNotNull(attr);
  +        assertEquals("uri1", attr.getNamespaceURI());
  +        assertEquals("attr", attr.getLocalName());
  +        assertEquals("test", attr.getValue());
  +
  +
  +        Text text = (Text)bar.getFirstChild();
  +        System.out.println("text="+text.getNodeValue());
  +        assertEquals("baz", text.getNodeValue());
   
       }
   
  
  
  


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Free shipping on all inkjet cartridge & refill kit orders to US & Canada. Low prices up to 80% off. We have your brand: HP, Epson, Lexmark & more.
http://www.c1tracking.com/l.asp?cid=5510
http://us.click.yahoo.com/GHXcIA/n.WGAA/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/