Author: veithen
Date: Sat Apr 11 20:43:34 2009
New Revision: 764251

URL: http://svn.apache.org/viewvc?rev=764251&view=rev
Log:
* Improvements in SAXOMBuilder.
* Merged test files for SAXOMBuilder and OMStAXWrapper.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/test.xml
   (props changed)
      - copied unchanged from r761900, 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/builder/test.xml
Removed:
    
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/builder/test.xml
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml
    
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/SAXOMBuilderSAXParserTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMBuilder.java?rev=764251&r1=764250&r2=764251&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMBuilder.java
 Sat Apr 11 20:43:34 2009
@@ -22,6 +22,7 @@
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
@@ -38,50 +39,77 @@
 import java.util.List;
 
 public class SAXOMBuilder extends DefaultHandler implements LexicalHandler {
+    private OMDocument document;
+    
     OMElement root = null;
 
     OMNode lastNode = null;
 
     OMElement nextElem = null;
 
-    OMFactory factory = OMAbstractFactory.getOMFactory();
+    private final OMFactory factory;
 
     List prefixMappings = new ArrayList();
     
     int textNodeType = OMNode.TEXT_NODE;
 
+    public SAXOMBuilder(OMFactory factory) {
+        this.factory = factory;
+    }
+    
+    public SAXOMBuilder() {
+        this(OMAbstractFactory.getOMFactory());
+    }
+    
+    private OMContainer getContainer() {
+        if (lastNode != null) {
+            return lastNode.isComplete() ? lastNode.getParent() : 
(OMContainer)lastNode;
+        } else if (document != null) {
+            return document;
+        } else {
+            throw new OMException("Unexpected event. There is no container to 
add the node to.");
+        }
+    }
+    
+    private void addNode(OMNode node) {
+        if (lastNode != null) {
+            if (lastNode.isComplete()) {
+                ((OMNodeEx) lastNode).setNextOMSibling(node);
+                ((OMNodeEx) node).setPreviousOMSibling(lastNode);
+            } else {
+                ((OMContainerEx) lastNode).setFirstChild(node);
+            }
+        } else if (document != null) {
+            ((OMContainerEx)document).setFirstChild(node);
+        }
+        if (root == null && node instanceof OMElement) {
+            root = (OMElement)node;
+        }
+        lastNode = node;
+    }
+    
     public void setDocumentLocator(Locator arg0) {
     }
 
     public void startDocument() throws SAXException {
-
+        document = factory.createOMDocument(null);
     }
 
     public void endDocument() throws SAXException {
+        ((OMContainerEx)document).setComplete(true);
     }
 
     public void startDTD(String name, String publicId, String systemId) throws 
SAXException {
+//        addNode(factory.createOMDocType(getContainer(), ""));
     }
 
     public void endDTD() throws SAXException {
     }
 
     protected OMElement createNextElement(String localName) throws OMException 
{
-        OMElement e;
-        if (lastNode == null) {
-            root = e = factory.createOMElement(localName, null, null, null);
-        } else if (lastNode.isComplete()) {
-            e = factory.createOMElement(localName, null, lastNode.getParent(),
-                                        null);
-            ((OMNodeEx) lastNode).setNextOMSibling(e);
-            ((OMNodeEx) e).setPreviousOMSibling(lastNode);
-        } else {
-            OMContainerEx parent = (OMContainerEx) lastNode;
-            e = factory.createOMElement(localName, null, (OMElement) lastNode,
-                                        null);
-            parent.setFirstChild(e);
-        }
-        return e;
+        OMElement element = factory.createOMElement(localName, null, 
getContainer(), null);
+        addNode(element);
+        return element;
     }
 
     /*
@@ -92,8 +120,9 @@
      */
     public void startPrefixMapping(String prefix, String uri)
             throws SAXException {
-        if (nextElem == null)
+        if (nextElem == null) {
             nextElem = createNextElement(null);
+        }
         if (prefix.length() == 0) {
             nextElem.declareDefaultNamespace(uri);
         } else {
@@ -167,36 +196,27 @@
         textNodeType = OMNode.TEXT_NODE;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
-     */
-    public void characters(char[] ch, int start, int length)
+    public void characterData(char[] ch, int start, int length, int nodeType)
             throws SAXException {
         if (lastNode == null) {
             throw new SAXException("");
         }
-        OMNode node;
-        if (lastNode.isComplete()) {
-            node = factory.createOMText(lastNode.getParent(),
-                    new String(ch, start, length), textNodeType);
-            ((OMNodeEx) lastNode).setNextOMSibling(node);
-            ((OMNodeEx) node).setPreviousOMSibling(lastNode);
-        } else {
-            OMContainerEx e = (OMContainerEx) lastNode;
-            node = factory.createOMText(e, new String(ch, start, length), 
textNodeType);
-            e.setFirstChild(node);
-        }
-        lastNode = node;
+        addNode(factory.createOMText(getContainer(), new String(ch, start, 
length), nodeType));
     }
 
-    public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
+    public void characters(char[] ch, int start, int length)
             throws SAXException {
+        characterData(ch, start, length, textNodeType);
+    }
+    
+    public void ignorableWhitespace(char[] ch, int start, int length)
+            throws SAXException {
+        characterData(ch, start, length, OMNode.SPACE_NODE);
     }
 
-    public void processingInstruction(String arg0, String arg1)
+    public void processingInstruction(String target, String data)
             throws SAXException {
+        addNode(factory.createOMProcessingInstruction(getContainer(), target, 
data));
     }
 
     public void comment(char[] ch, int start, int length) throws SAXException {
@@ -204,18 +224,7 @@
             // Do nothing: the comment appears before the root element.
             return;
         } 
-        OMNode node;
-        if (lastNode.isComplete()) {
-            node = factory.createOMComment(lastNode.getParent(),
-                    new String(ch, start, length));
-            ((OMNodeEx) lastNode).setNextOMSibling(node);
-            ((OMNodeEx) node).setPreviousOMSibling(lastNode);
-        } else {
-            OMContainerEx e = (OMContainerEx) lastNode;
-            node = factory.createOMComment(e, new String(ch, start, length));
-            e.setFirstChild(node);
-        }
-        lastNode = node;
+        addNode(factory.createOMComment(getContainer(), new String(ch, start, 
length)));
     }
 
     public void skippedEntity(String arg0) throws SAXException {
@@ -227,6 +236,14 @@
     public void endEntity(String name) throws SAXException {
     }
 
+    public OMDocument getDocument() {
+        if (document != null && document.isComplete()) {
+            return document;
+        } else {
+            throw new OMException("Tree not complete");
+        }
+    }
+    
     /**
      * Get the root element of the Axiom tree built by this content handler.
      * 

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/test.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/test.xml
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml?rev=764251&r1=764250&r2=764251&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml 
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml 
Sat Apr 11 20:43:34 2009
@@ -87,6 +87,12 @@
             <artifactId>saxon-dom</artifactId>
             <version>8.9</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.ws.commons.axiom</groupId>
+            <artifactId>axiom-api</artifactId>
+            <classifier>tests</classifier>
+            <version>${version}</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/SAXOMBuilderSAXParserTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/SAXOMBuilderSAXParserTest.java?rev=764251&r1=764250&r2=764251&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/SAXOMBuilderSAXParserTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/SAXOMBuilderSAXParserTest.java
 Sat Apr 11 20:43:34 2009
@@ -19,65 +19,76 @@
 
 package org.apache.axiom.om.impl.builder;
 
-import static org.custommonkey.xmlunit.XMLAssert.assertXMLIdentical;
-import static org.custommonkey.xmlunit.XMLUnit.compareXML;
-
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.util.Arrays;
-import java.util.List;
 
+import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.SAXParserFactory;
 
-import org.apache.axiom.om.OMElement;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import junit.framework.TestSuite;
+
+import org.apache.axiom.om.AbstractTestCase;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;
 
-...@runwith(Parameterized.class)
-public class SAXOMBuilderSAXParserTest {
+public class SAXOMBuilderSAXParserTest extends AbstractTestCase {
     private final SAXParserFactory factory;
+    private final String file;
     
-    @Parameters
-    public static List<Object[]> parameters() {
-        return Arrays.asList(new Object[][] {
-                { org.apache.crimson.jaxp.SAXParserFactoryImpl.class },
-                { org.apache.xerces.jaxp.SAXParserFactoryImpl.class }
-        });
-    }
-    
-    public SAXOMBuilderSAXParserTest(Class<? extends SAXParserFactory> 
factoryClass) throws Exception {
-        this.factory = factoryClass.newInstance();
+    public SAXOMBuilderSAXParserTest(String name, SAXParserFactory factory, 
String file) {
+        super(name);
+        this.factory = factory;
+        this.file = file;
     }
 
-    private InputSource toInputSource(OMElement element) throws Exception {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        element.serialize(baos);
-        return new InputSource(new ByteArrayInputStream(baos.toByteArray()));
+    private Document toDocument(InputStream in) throws Exception {
+        Document doc = 
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
+        DocumentType docType = doc.getDoctype();
+        if (docType != null) {
+            doc.removeChild(docType);
+        }
+        return doc;
     }
     
-    @Test
-    public void test() throws Exception {
+    @Override
+    protected void runTest() throws Throwable {
         factory.setNamespaceAware(true);
         XMLReader reader = factory.newSAXParser().getXMLReader();
         SAXOMBuilder builder = new SAXOMBuilder();
         reader.setContentHandler(builder);
         reader.setProperty("http://xml.org/sax/properties/lexical-handler";, 
builder);
-        InputStream in = 
SAXOMBuilderSAXParserTest.class.getResourceAsStream("test.xml");
+        InputStream in = getTestResource(file);
         try {
             reader.parse(new InputSource(in));
         } finally {
             in.close();
         }
-        in = SAXOMBuilderSAXParserTest.class.getResourceAsStream("test.xml");
+        in = getTestResource(file);
         try {
-            assertXMLIdentical(compareXML(new InputSource(in), 
toInputSource(builder.getRootElement())), true);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            builder.getDocument().serialize(baos);
+            assertXMLIdentical(compareXML(
+                    toDocument(in),
+                    toDocument(new ByteArrayInputStream(baos.toByteArray()))), 
true);
         } finally {
             in.close();
         }
     }
+    
+    private static void addTests(TestSuite suite, SAXParserFactory factory, 
String name) throws Exception {
+        for (String file : getConformanceTestFiles()) {
+            suite.addTest(new SAXOMBuilderSAXParserTest(
+                    file.substring(file.lastIndexOf('/')+1) + " - " + name, 
factory, file));
+        }
+    }
+    
+    public static TestSuite suite() throws Exception {
+        TestSuite suite = new TestSuite();
+        addTests(suite, new org.apache.crimson.jaxp.SAXParserFactoryImpl(), 
"crimson");
+        addTests(suite, new org.apache.xerces.jaxp.SAXParserFactoryImpl(), 
"xerces");
+        return suite;
+    }
 }


Reply via email to