You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2017/05/13 21:47:41 UTC

svn commit: r1795083 - in /webservices/axiom/trunk: aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/ aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/ aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixi...

Author: veithen
Date: Sat May 13 21:47:41 2017
New Revision: 1795083

URL: http://svn.apache.org/viewvc?rev=1795083&view=rev
Log:
Add a method to OMFactory to import nodes created by another implementation.

Added:
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/TestImportInformationItem.java   (with props)
Removed:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/OMFactoryEx.java
Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/OMFactoryImpl.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMFactory.java
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/CrossOMTestSuiteBuilder.java

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/OMFactoryImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/OMFactoryImpl.java?rev=1795083&r1=1795082&r2=1795083&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/OMFactoryImpl.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/OMFactoryImpl.java Sat May 13 21:47:41 2017
@@ -20,6 +20,8 @@ package org.apache.axiom.om.impl.common.
 
 import static org.apache.axiom.util.xml.NSUtils.generatePrefix;
 
+import java.util.Iterator;
+
 import javax.activation.DataHandler;
 import javax.xml.namespace.QName;
 
@@ -36,32 +38,36 @@ import org.apache.axiom.om.OMDocType;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMEntityReference;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMInformationItem;
 import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNamedInformationItem;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMProcessingInstruction;
 import org.apache.axiom.om.OMSourcedElement;
 import org.apache.axiom.om.OMText;
-import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.om.impl.common.AxiomExceptionTranslator;
 import org.apache.axiom.om.impl.common.AxiomSemantics;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.intf.AxiomAttribute;
 import org.apache.axiom.om.impl.intf.AxiomCDATASection;
 import org.apache.axiom.om.impl.intf.AxiomCharacterDataNode;
+import org.apache.axiom.om.impl.intf.AxiomChildNode;
 import org.apache.axiom.om.impl.intf.AxiomComment;
 import org.apache.axiom.om.impl.intf.AxiomContainer;
 import org.apache.axiom.om.impl.intf.AxiomDocType;
 import org.apache.axiom.om.impl.intf.AxiomDocument;
 import org.apache.axiom.om.impl.intf.AxiomElement;
 import org.apache.axiom.om.impl.intf.AxiomEntityReference;
+import org.apache.axiom.om.impl.intf.AxiomNamedInformationItem;
+import org.apache.axiom.om.impl.intf.AxiomNamespaceDeclaration;
 import org.apache.axiom.om.impl.intf.AxiomProcessingInstruction;
 import org.apache.axiom.om.impl.intf.AxiomSourcedElement;
 import org.apache.axiom.om.impl.intf.AxiomText;
-import org.apache.axiom.om.impl.intf.OMFactoryEx;
 import org.apache.axiom.om.impl.intf.TextContent;
 
-public class OMFactoryImpl implements OMFactoryEx {
+public class OMFactoryImpl implements OMFactory {
     private final OMMetaFactory metaFactory;
     private final NodeFactory nodeFactory;
     
@@ -373,53 +379,104 @@ public class OMFactoryImpl implements OM
         return attr;
     }
 
-    // <old-and-buggy-code>
-    @Override
-    public final OMNode importNode(OMNode child) {
+    private AxiomChildNode importChildNode(OMNode child) {
         int type = child.getType();
         switch (type) {
             case OMNode.ELEMENT_NODE: {
-                OMElement childElement = (OMElement) child;
-                OMElement newElement = (OMXMLBuilderFactory.createStAXOMBuilder(this, childElement
-                        .getXMLStreamReader())).getDocumentElement();
-                newElement.buildWithAttachments();
-                return newElement;
+                OMElement element = (OMElement)child;
+                AxiomElement importedElement = createNode(AxiomElement.class);
+                copyName(element, importedElement);
+                for (Iterator<OMAttribute> it = element.getAllAttributes(); it.hasNext(); ) {
+                    importedElement.coreAppendAttribute(importAttribute(it.next()));
+                }
+                for (Iterator<OMNamespace> it = element.getAllDeclaredNamespaces(); it.hasNext(); ) {
+                    OMNamespace ns = it.next();
+                    AxiomNamespaceDeclaration nsDecl = createNode(AxiomNamespaceDeclaration.class);
+                    nsDecl.coreSetDeclaredNamespace(ns.getPrefix(), ns.getNamespaceURI());
+                    importedElement.coreAppendAttribute(nsDecl);
+                }
+                importChildren(element, importedElement);
+                return importedElement;
             }
-            case OMNode.TEXT_NODE: {
-                OMText importedText = (OMText) child;
-                OMText newText;
-                if (importedText.isBinary()) {
-                    boolean isOptimize = importedText.isOptimized();
-                    newText = createOMText(importedText
-                            .getDataHandler(), isOptimize);
+            case OMNode.TEXT_NODE:
+            case OMNode.SPACE_NODE:
+            case OMNode.CDATA_SECTION_NODE: {
+                OMText text = (OMText)child;
+                Object content;
+                if (text.isBinary()) {
+                    content = new TextContent(text.getContentID(), text.getDataHandler(), text.isOptimized());
                 } else {
-                    newText = createOMText(null, importedText
-                            .getText()/*, importedText.getOMNodeType()*/);
+                    content = text.getText();
                 }
-                return newText;
+                return createAxiomText(null, content, type);
             }
-
             case OMNode.PI_NODE: {
-                OMProcessingInstruction importedPI = (OMProcessingInstruction) child;
-                return createOMProcessingInstruction(null,
-                                                                  importedPI.getTarget(),
-                                                                  importedPI.getValue());
+                OMProcessingInstruction pi = (OMProcessingInstruction)child;
+                AxiomProcessingInstruction importedPI = createNode(AxiomProcessingInstruction.class);
+                importedPI.setTarget(pi.getTarget());
+                importedPI.setValue(pi.getValue());
+                return importedPI;
             }
             case OMNode.COMMENT_NODE: {
-                OMComment importedComment = (OMComment) child;
-                return createOMComment(null, importedComment.getValue());
-            }
-            case OMNode.DTD_NODE : {
-                OMDocType importedDocType = (OMDocType) child;
-                return createOMDocType(null, importedDocType.getRootName(),
-                        importedDocType.getPublicId(), importedDocType.getSystemId(),
-                        importedDocType.getInternalSubset());
-            }
-            default: {
-                throw new UnsupportedOperationException(
-                        "Not Implemented Yet for the given node type");
+                OMComment comment = (OMComment)child;
+                AxiomComment importedComment = createNode(AxiomComment.class);
+                importedComment.setValue(comment.getValue());
+                return importedComment;
+            }
+            case OMNode.DTD_NODE: {
+                OMDocType docType = (OMDocType)child;
+                AxiomDocType importedDocType = createNode(AxiomDocType.class);
+                importedDocType.coreSetRootName(docType.getRootName());
+                importedDocType.coreSetPublicId(docType.getPublicId());
+                importedDocType.coreSetSystemId(docType.getSystemId());
+                importedDocType.coreSetInternalSubset(docType.getInternalSubset());
+                return importedDocType;
+            }
+            case OMNode.ENTITY_REFERENCE_NODE:
+                AxiomEntityReference importedEntityRef = createNode(AxiomEntityReference.class);
+                importedEntityRef.coreSetName(((OMEntityReference)child).getName());
+                return importedEntityRef;
+            default:
+                throw new IllegalArgumentException("Unsupported node type");
+        }
+    }
+
+    private void copyName(OMNamedInformationItem node, AxiomNamedInformationItem importedNode) {
+        importedNode.internalSetNamespace(node.getNamespace());
+        importedNode.internalSetLocalName(node.getLocalName());
+    }
+
+    private void importChildren(OMContainer node, AxiomContainer importedNode) {
+        for (OMNode child = node.getFirstOMChild(); child != null; child = child.getNextOMSibling()) {
+            try {
+                importedNode.coreAppendChild(importChildNode(child));
+            } catch (CoreModelException ex) {
+                throw AxiomExceptionTranslator.translate(ex);
             }
         }
     }
-    // </old-and-buggy-code>
+
+    private AxiomAttribute importAttribute(OMAttribute attribute) {
+        AxiomAttribute importedAttribute = createNode(AxiomAttribute.class);
+        copyName(attribute, importedAttribute);
+        importedAttribute.setAttributeValue(attribute.getAttributeValue());
+        return importedAttribute;
+    }
+
+    @Override
+    public final OMInformationItem importInformationItem(OMInformationItem node) {
+        if (node instanceof OMNode) {
+            return importChildNode((OMNode)node);
+        } else if (node instanceof OMDocument) {
+            OMDocument document = (OMDocument)node;
+            AxiomDocument importedDocument = createNode(AxiomDocument.class);
+            // TODO: other attributes
+            importChildren(document, importedDocument);
+            return importedDocument;
+        } else if (node instanceof OMAttribute) {
+            return importAttribute((OMAttribute)node);
+        } else {
+            throw new IllegalArgumentException("Unsupported node type");
+        }
+    }
 }

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj?rev=1795083&r1=1795082&r2=1795083&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj Sat May 13 21:47:41 2017
@@ -70,7 +70,6 @@ import org.apache.axiom.om.impl.common.b
 import org.apache.axiom.om.impl.intf.AxiomChildNode;
 import org.apache.axiom.om.impl.intf.AxiomContainer;
 import org.apache.axiom.om.impl.intf.AxiomElement;
-import org.apache.axiom.om.impl.intf.OMFactoryEx;
 import org.apache.axiom.om.impl.jaxb.AttachmentUnmarshallerImpl;
 import org.apache.axiom.om.impl.stream.NamespaceContextPreservationFilterHandler;
 import org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler;
@@ -166,7 +165,7 @@ public aspect AxiomContainerSupport {
         if (omNode.getOMFactory().getMetaFactory().equals(getOMFactory().getMetaFactory())) {
             child = (AxiomChildNode)omNode;
         } else {
-            child = (AxiomChildNode)((OMFactoryEx)getOMFactory()).importNode(omNode);
+            child = (AxiomChildNode)getOMFactory().importInformationItem(omNode);
         }
         checkChild(omNode);
         return child;

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMFactory.java?rev=1795083&r1=1795082&r2=1795083&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMFactory.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMFactory.java Sat May 13 21:47:41 2017
@@ -375,4 +375,16 @@ public interface OMFactory {
      * @return the newly created {@link OMEntityReference} node
      */
     OMEntityReference createOMEntityReference(OMContainer parent, String name);
+
+    /**
+     * Create a copy of the given information item using this factory. This method can be used to
+     * import information items created by other Axiom implementations. A copy will be created even
+     * if the original information item was created by this factory. Both {@link OMSourcedElement}
+     * instances and model specific elements are always copied to plain {@link OMElement} instances.
+     * 
+     * @param informationItem
+     *            the information item to copy
+     * @return the imported information item
+     */
+    OMInformationItem importInformationItem(OMInformationItem informationItem);
 }

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java?rev=1795083&r1=1795082&r2=1795083&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java Sat May 13 21:47:41 2017
@@ -25,9 +25,9 @@ import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMInformationItem;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.om.ds.BlobOMDataSource;
 import org.apache.axiom.soap.SOAPFactory;
 import org.apache.axiom.soap.SOAPHeaderBlock;
@@ -185,16 +185,15 @@ public class ElementHelper {
      *
      * @see AttributeHelper#importOMAttribute(OMAttribute, OMElement) to convert instances of
      *      OMAttribute
+     * 
+     * @deprecated Use {@link OMFactory#importInformationItem(OMInformationItem)} instead.
      */
     public static OMElement importOMElement(OMElement omElement, OMFactory omFactory) {
         // first check whether the given OMElement has the same omFactory
         if (omElement.getOMFactory().getMetaFactory() == omFactory.getMetaFactory()) {
             return omElement;
         } else {
-            OMElement documentElement = OMXMLBuilderFactory.createStAXOMBuilder(
-                    omFactory, omElement.getXMLStreamReader()).getDocumentElement();
-            documentElement.build();
-            return documentElement;
+            return (OMElement)omFactory.importInformationItem(omElement);
         }
     }
 

Modified: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/CrossOMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/CrossOMTestSuiteBuilder.java?rev=1795083&r1=1795082&r2=1795083&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/CrossOMTestSuiteBuilder.java (original)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/CrossOMTestSuiteBuilder.java Sat May 13 21:47:41 2017
@@ -18,8 +18,11 @@
  */
 package org.apache.axiom.ts.om.cross;
 
+import static org.apache.axiom.testing.multiton.Multiton.getInstances;
+
 import org.apache.axiom.om.OMMetaFactory;
 import org.apache.axiom.testutils.suite.MatrixTestSuiteBuilder;
+import org.apache.axiom.ts.xml.XMLSample;
 
 public class CrossOMTestSuiteBuilder extends MatrixTestSuiteBuilder {
     private final OMMetaFactory metaFactory;
@@ -33,6 +36,9 @@ public class CrossOMTestSuiteBuilder ext
     @Override
     protected void addTests() {
         addTest(new TestAddChild(metaFactory, altMetaFactory));
+        for (XMLSample file : getInstances(XMLSample.class)) {
+            addTest(new TestImportInformationItem(metaFactory, altMetaFactory, file));
+        }
         addTest(new TestInsertSibling(metaFactory, altMetaFactory, false));
         addTest(new TestInsertSibling(metaFactory, altMetaFactory, true));
     }

Added: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/TestImportInformationItem.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/TestImportInformationItem.java?rev=1795083&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/TestImportInformationItem.java (added)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/TestImportInformationItem.java Sat May 13 21:47:41 2017
@@ -0,0 +1,47 @@
+/*
+ * 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.ts.om.cross;
+
+import static com.google.common.truth.Truth.assertAbout;
+import static org.apache.axiom.truth.xml.XMLTruth.xml;
+
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMMetaFactorySPI;
+import org.apache.axiom.ts.xml.XMLSample;
+import org.xml.sax.InputSource;
+
+public class TestImportInformationItem extends CrossOMTestCase {
+    private final XMLSample file;
+    
+    public TestImportInformationItem(OMMetaFactory metaFactory, OMMetaFactory altMetaFactory, XMLSample file) {
+        super(metaFactory, altMetaFactory);
+        this.file = file;
+        addTestParameter("file", file.getName());
+    }
+
+    @Override
+    protected void runTest() throws Throwable {
+        OMDocument original = ((OMMetaFactorySPI)metaFactory).createOMBuilder(TEST_PARSER_CONFIGURATION,
+                new InputSource(file.getUrl().toString())).getDocument();
+        assertAbout(xml())
+                .that(xml(OMDocument.class, (OMDocument)metaFactory.getOMFactory().importInformationItem(original)))
+                .hasSameContentAs(xml(OMDocument.class, original));
+    }
+}

Propchange: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/cross/TestImportInformationItem.java
------------------------------------------------------------------------------
    svn:eol-style = native