You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2008/12/18 00:15:07 UTC

svn commit: r727573 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/ axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ axiom-integration/ axiom-integration/src/test/java/org/apache/...

Author: veithen
Date: Wed Dec 17 15:15:05 2008
New Revision: 727573

URL: http://svn.apache.org/viewvc?rev=727573&view=rev
Log:
WSCOMMONS-415: Added implementations of javax.xml.transform.Source and javax.xml.transform.Result.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMResult.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMSource.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/TransformerTest.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/test.xml   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMResult.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMResult.java?rev=727573&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMResult.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMResult.java Wed Dec 17 15:15:05 2008
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.jaxp;
+
+import javax.xml.transform.sax.SAXResult;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.SAXOMBuilder;
+
+/**
+ * Implementation of {@link javax.xml.transform.Result} for AXIOM.
+ * The implementation is based on {@link SAXResult} and transforms the stream
+ * of SAX events to an AXIOM tree using {@link SAXOMBuilder}.
+ */
+public class OMResult extends SAXResult {
+    private final SAXOMBuilder builder;
+    
+    public OMResult() {
+        builder = new SAXOMBuilder();
+        setHandler(builder);
+    }
+
+    public OMElement getRootElement() {
+        return builder.getRootElement();
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMResult.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMSource.java?rev=727573&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMSource.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMSource.java Wed Dec 17 15:15:05 2008
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.jaxp;
+
+import javax.xml.transform.sax.SAXSource;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.serialize.OMXMLReader;
+import org.xml.sax.InputSource;
+
+/**
+ * Implementation of {@link javax.xml.transform.Source} for AXIOM.
+ * The implementation is based on {@link SAXSource} and directly transforms an AXIOM
+ * tree into a stream of SAX events using {@link OMXMLReader}.
+ */
+public class OMSource extends SAXSource {
+    public OMSource(OMElement element) {
+        super(new OMXMLReader(element), new InputSource());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/jaxp/OMSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java?rev=727573&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java Wed Dec 17 15:15:05 2008
@@ -0,0 +1,312 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.serialize;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMComment;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.LexicalHandler;
+
+/**
+ * SAX {@link XMLReader} implementation that traverses a given OM tree and invokes the
+ * callback methods on the configured {@link ContentHandler}. This can be used to
+ * serialize an Axiom tree to SAX.
+ */
+public class OMXMLReader implements XMLReader {
+    private static final String URI_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
+    
+    private final OMElement element;
+    private final AttributesAdapter attributesAdapter = new AttributesAdapter();
+    
+    private boolean namespaces = true;
+    private boolean namespacePrefixes = false;
+    
+    private ContentHandler contentHandler;
+    private LexicalHandler lexicalHandler;
+    private DTDHandler dtdHandler;
+    private EntityResolver entityResolver;
+    private ErrorHandler errorHandler;
+    
+    public OMXMLReader(OMElement element) {
+        this.element = element;
+    }
+
+    public ContentHandler getContentHandler() {
+        return contentHandler;
+    }
+
+    public void setContentHandler(ContentHandler contentHandler) {
+        this.contentHandler = contentHandler;
+    }
+
+    public DTDHandler getDTDHandler() {
+        return dtdHandler;
+    }
+
+    public void setDTDHandler(DTDHandler dtdHandler) {
+        this.dtdHandler = dtdHandler;
+    }
+
+    public EntityResolver getEntityResolver() {
+        return entityResolver;
+    }
+
+    public void setEntityResolver(EntityResolver entityResolver) {
+        this.entityResolver = entityResolver;
+    }
+
+    public ErrorHandler getErrorHandler() {
+        return errorHandler;
+    }
+
+    public void setErrorHandler(ErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
+    }
+
+    public boolean getFeature(String name)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        throw new SAXNotRecognizedException(name);
+    }
+
+    public void setFeature(String name, boolean value)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        
+        if ("http://xml.org/sax/features/namespaces".equals(name)) {
+            namespaces = value;
+        } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) {
+            namespacePrefixes = value;
+        } else {
+            throw new SAXNotRecognizedException(name);
+        }
+    }
+
+    public Object getProperty(String name)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        
+        if ("http://xml.org/sax/features/namespaces".equals(name)) {
+            return Boolean.valueOf(namespaces);
+        } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) {
+            return Boolean.valueOf(namespacePrefixes);
+        } else if (URI_LEXICAL_HANDLER.equals(name)) {
+            return lexicalHandler;
+        } else {
+            throw new SAXNotRecognizedException(name);
+        }
+    }
+
+    public void setProperty(String name, Object value)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        if (URI_LEXICAL_HANDLER.equals(name)) {
+            lexicalHandler = (LexicalHandler)value;
+        } else {
+            throw new SAXNotRecognizedException(name);
+        }
+    }
+
+    public void parse(InputSource input) throws IOException, SAXException {
+        parse();
+    }
+
+    public void parse(String systemId) throws IOException, SAXException {
+        parse();
+    }
+    
+    private void parse() throws SAXException {
+        contentHandler.startDocument();
+        generateEvents(element);
+        contentHandler.endDocument();
+    }
+    
+    private void generatePrefixMappingEvents(OMElement omElement, boolean start)
+            throws SAXException {
+        
+        for (Iterator it = omElement.getAllDeclaredNamespaces(); it.hasNext(); ) {
+            OMNamespace ns = (OMNamespace)it.next();
+            String prefix = ns.getPrefix();
+            if (prefix != null) {
+                if (start) {
+                    contentHandler.startPrefixMapping(prefix, ns.getNamespaceURI());
+                } else {
+                    contentHandler.endPrefixMapping(prefix);
+                }
+            }
+        }
+    }
+    
+    private void generateEvents(OMElement omElement) throws SAXException {
+        generatePrefixMappingEvents(omElement, true);
+        OMNamespace omNamespace = omElement.getNamespace();
+        String uri;
+        String prefix;
+        if (omNamespace != null) {
+            uri = omNamespace.getNamespaceURI();
+            prefix = omNamespace.getPrefix();
+        } else {
+            uri = "";
+            prefix = null;
+        }
+        String localName = omElement.getLocalName();
+        String qName;
+        if (prefix == null || prefix.length() == 0) {
+            qName = localName;
+        } else {
+            qName = prefix + ":" + localName;
+        }
+        // For performance reasons, we always reuse the same instance of AttributesAdapter.
+        // This is explicitely allowed by the specification of the startElement method.
+        attributesAdapter.setAttributes(omElement);
+        contentHandler.startElement(uri, localName, qName, attributesAdapter);
+        for (Iterator it = omElement.getChildren(); it.hasNext(); ) {
+            OMNode node = (OMNode)it.next();
+            switch (node.getType()) {
+                case OMNode.ELEMENT_NODE:
+                    generateEvents((OMElement)node);
+                    break;
+                case OMNode.TEXT_NODE:
+                    generateEvents((OMText)node);
+                    break;
+                case OMNode.CDATA_SECTION_NODE:
+                    if (lexicalHandler != null) {
+                        lexicalHandler.startCDATA();
+                    }
+                    generateEvents((OMText)node);
+                    if (lexicalHandler != null) {
+                        lexicalHandler.endCDATA();
+                    }
+                    break;
+                case OMNode.COMMENT_NODE:
+                    if (lexicalHandler != null) {
+                        char[] ch = ((OMComment)node).getValue().toCharArray();
+                        lexicalHandler.comment(ch, 0, ch.length);
+                    }
+            }
+        }
+        contentHandler.endElement(uri, localName, qName);
+        generatePrefixMappingEvents(omElement, false);
+    }
+    
+    private void generateEvents(OMText omText) throws SAXException {
+        char[] ch = omText.getTextCharacters();
+        contentHandler.characters(ch, 0, ch.length);
+    }
+
+    protected static class AttributesAdapter implements Attributes {
+        private List/*<OMAttribute>*/ attributes = new ArrayList(5);
+
+        public void setAttributes(OMElement element) {
+            attributes.clear();
+            for (Iterator it = element.getAllAttributes(); it.hasNext(); ) {
+                attributes.add(it.next());
+            }
+        }
+
+        public int getLength() {
+            return attributes.size();
+        }
+
+        public int getIndex(String qName) {
+            for (int i=0, len=attributes.size(); i<len; i++) {
+                if (getQName(i).equals(qName)) {
+                    return i;
+                }
+            }
+            return -1;
+        }
+
+        public int getIndex(String uri, String localName) {
+            for (int i=0, len=attributes.size(); i<len; i++) {
+                if (getURI(i).equals(uri) && getLocalName(i).equals(localName)) {
+                    return i;
+                }
+            }
+            return -1;
+        }
+
+        public String getLocalName(int index) {
+            return ((OMAttribute)attributes.get(index)).getLocalName();
+        }
+
+        public String getQName(int index) {
+            OMAttribute attribute = ((OMAttribute)attributes.get(index));
+            OMNamespace ns = attribute.getNamespace();
+            if (ns == null) {
+                return attribute.getLocalName();
+            } else {
+                String prefix = ns.getPrefix();
+                if (prefix == null || prefix.length() == 0) {
+                    return attribute.getLocalName();
+                } else {
+                    return ns.getPrefix() + ":" + attribute.getLocalName();
+                }
+            }
+        }
+
+        public String getType(int index) {
+            return ((OMAttribute)attributes.get(index)).getAttributeType();
+        }
+
+        public String getType(String qName) {
+            int index = getIndex(qName);
+            return index == -1 ? null : getType(index);
+        }
+
+        public String getType(String uri, String localName) {
+            int index = getIndex(uri, localName);
+            return index == -1 ? null : getType(index);
+        }
+
+        public String getURI(int index) {
+            OMNamespace ns = ((OMAttribute)attributes.get(index)).getNamespace();
+            return ns == null ? "" : ns.getNamespaceURI();
+        }
+
+        public String getValue(int index) {
+            return ((OMAttribute)attributes.get(index)).getAttributeValue();
+        }
+
+        public String getValue(String qName) {
+            int index = getIndex(qName);
+            return index == -1 ? null : getValue(index);
+        }
+
+        public String getValue(String uri, String localName) {
+            int index = getIndex(uri, localName);
+            return index == -1 ? null : getValue(index);
+        }
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=727573&r1=727572&r2=727573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml Wed Dec 17 15:15:05 2008
@@ -62,6 +62,16 @@
             <artifactId>jaxb-impl</artifactId>
             <version>2.1.9</version>
         </dependency>
+        <dependency>
+            <groupId>xalan</groupId>
+            <artifactId>xalan</artifactId>
+            <version>2.7.1</version>
+        </dependency>
+        <dependency>
+            <groupId>net.sf.saxon</groupId>
+            <artifactId>saxon</artifactId>
+            <version>8.5.1</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

Added: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/TransformerTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/TransformerTest.java?rev=727573&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/TransformerTest.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/TransformerTest.java Wed Dec 17 15:15:05 2008
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.jaxp;
+
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.custommonkey.xmlunit.XMLTestCase;
+
+public class TransformerTest extends XMLTestCase {
+    private InputStream getInput() {
+        return TransformerTest.class.getResourceAsStream("test.xml");
+    }
+    
+    private void test(TransformerFactory factory) throws Exception {
+        Transformer transformer = factory.newTransformer();
+        
+        OMSource omSource = new OMSource(new StAXOMBuilder(getInput()).getDocumentElement());
+        OMResult omResult = new OMResult();
+        transformer.transform(omSource, omResult);
+        
+        StreamSource streamSource = new StreamSource(getInput());
+        StringWriter out = new StringWriter();
+        StreamResult streamResult = new StreamResult(out);
+        transformer.transform(streamSource, streamResult);
+        
+        assertXMLIdentical(compareXML(out.toString(), omResult.getRootElement().toString()), true);
+    }
+
+    public void testXalan() throws Exception {
+        test(new org.apache.xalan.processor.TransformerFactoryImpl());
+    }
+    
+    public void testSaxon() throws Exception {
+        test(new net.sf.saxon.TransformerFactoryImpl());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/jaxp/TransformerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/test.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/test.xml?rev=727573&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/test.xml (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/test.xml Wed Dec 17 15:15:05 2008
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+    <!-- Some comment -->
+    <element xmlns="urn:ns1" xmlns:ns2="urn:ns2">
+        <ns2:subelement attr1="test" attr2="test2">Some text</ns2:subelement>
+        <otherelement attr1="test" ns2:attr2="test2">more text</otherelement>
+    </element>
+</root>

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/resources/org/apache/axiom/om/impl/jaxp/test.xml
------------------------------------------------------------------------------
    svn:eol-style = native