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 ch...@apache.org on 2006/09/11 16:41:41 UTC

svn commit: r442233 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/util/ axiom-tests/src/test/java/org/apache/axiom/om/util/

Author: chinthaka
Date: Mon Sep 11 07:41:40 2006
New Revision: 442233

URL: http://svn.apache.org/viewvc?view=rev&rev=442233
Log:
Adding a method to import nodes as a util method. Whether to move this to OMElement interface is still under discussion in http://issues.apache.org/jira/browse/WSCOMMONS-90

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/OMElementHelperTest.java
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java?view=diff&rev=442233&r1=442232&r2=442233
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java Mon Sep 11 07:41:40 2006
@@ -15,16 +15,15 @@
  */
 package org.apache.axiom.om.util;
 
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNamespace;
-import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.*;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
-import java.util.Iterator;
-import java.net.URLDecoder;
 import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.Iterator;
+
 /**
  * Helper class to provide extra utility stuff against elements.
  * The code is designed to work with any element implementation.
@@ -36,6 +35,7 @@
 
     /**
      * Constructs and binds to an element.
+     *
      * @param element element to work with
      */
     public ElementHelper(OMElement element) {
@@ -135,6 +135,26 @@
                     "Href attribute not found in XOP:Include element");
         }
         return contentID;
+    }
+
+    /**
+     * Some times two OMElements needs to be added to the same object tree. But in Axiom, a single tree should always
+     * contain object created from the same type of factory (eg: LinkedListImplFactory, DOMFactory, etc.,). If one OMElement
+     * is created from a different factory than that of the factory which was used to create the object in the existing
+     * tree, we need to convert the new OMElement to match to the factory of existing object tree.
+     * This method will oonvert omElement to the given omFactory.
+     *
+     */
+    public static OMElement importOMElement(OMElement omElement, OMFactory omFactory) {
+        // first check whether the given OMElement has the same omFactory
+        if (omElement.getOMFactory().getClass().isInstance(omFactory)) {
+            return omElement;
+        }else {
+            OMElement documentElement = new StAXOMBuilder(omFactory, omElement.getXMLStreamReader()).getDocumentElement();
+            documentElement.build();
+            return documentElement;
+        }
+
     }
 
 }

Added: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/OMElementHelperTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/OMElementHelperTest.java?view=auto&rev=442233
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/OMElementHelperTest.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/OMElementHelperTest.java Mon Sep 11 07:41:40 2006
@@ -0,0 +1,57 @@
+package org.apache.axiom.om.util;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
+import org.custommonkey.xmlunit.XMLTestCase;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class OMElementHelperTest extends XMLTestCase {
+
+    private String testXMLFilePath = "test-resources/soap/soapmessage.xml";
+
+
+
+    public void testImportOMElement() {
+        try {
+            XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(testXMLFilePath));
+            OMElement documentElement = new StAXOMBuilder(OMAbstractFactory.getOMFactory(), xmlStreamReader).getDocumentElement();
+
+            // first lets try to import an element created from llom in to llom factory. This should return the same element
+            assertTrue(ElementHelper.importOMElement(documentElement, OMAbstractFactory.getOMFactory()) == documentElement);
+
+            // then lets pass in an OMElement created using llom and pass DOOMFactory
+            OMElement importedElement = ElementHelper.importOMElement(documentElement, DOOMAbstractFactory.getOMFactory());
+            assertTrue(importedElement != documentElement);
+            assertTrue(importedElement.getOMFactory().getClass().isInstance(DOOMAbstractFactory.getOMFactory()));
+
+        } catch (XMLStreamException e) {
+            e.printStackTrace();
+            fail();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org