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 sc...@apache.org on 2008/08/26 02:37:18 UTC

svn commit: r688927 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/ axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ axiom-api/src/main/java/org/apache/axiom/om/util/ axiom-api/src/main/...

Author: scheu
Date: Mon Aug 25 17:37:16 2008
New Revision: 688927

URL: http://svn.apache.org/viewvc?rev=688927&view=rev
Log:
WSCOMMONS-374
Contributor:Rich Scheuerle
Summary of changes:
Upgraded the OMElementImpl.clone method to (a) read optimized xop:includes from the source and (b) use a xop aware builder to create the target tree.  This effectively retains the content-id and datahandler information of the original tree.
Other changes (XOPBuilder, etc) are minor additions or debug changes to support the new clone implementation.

I also added two validation tests (in MTOMStAXSOAPModelBuilderTest).

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CopyUtils.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java Mon Aug 25 17:37:16 2008
@@ -69,6 +69,9 @@
 
     public MTOMXMLStreamWriter(XMLStreamWriter xmlWriter) {
         this.xmlWriter = xmlWriter;
+        if (isDebugEnabled) {
+            log.debug("Call Stack =" + CommonUtils.callStackToString());
+        }
     }
 
     /**

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Mon Aug 25 17:37:16 2008
@@ -319,11 +319,13 @@
                 break;
             case XMLStreamConstants.CHARACTERS:
                 log.trace("CHARACTERS: ");
-                log.trace(   "[" + parser.getText() + "]");
+                // This can bust up a datahandler
+                //log.trace(   "[" + parser.getText() + "]");
                 break;
             case XMLStreamConstants.CDATA:
                 log.trace("CDATA: ");
-                log.trace(   "[" + parser.getText() + "]");
+                // This can but
+                //log.trace(   "[" + parser.getText() + "]");
                 break;
             case XMLStreamConstants.END_ELEMENT:
                 log.trace("END_ELEMENT: ");
@@ -334,11 +336,11 @@
                 break;
             case XMLStreamConstants.SPACE:
                 log.trace("SPACE: ");
-                log.trace(   "[" + parser.getText() + "]");
+                //log.trace(   "[" + parser.getText() + "]");
                 break;
             case XMLStreamConstants.COMMENT:
                 log.trace("COMMENT: ");
-                log.trace(   "[" + parser.getText() + "]");
+                //log.trace(   "[" + parser.getText() + "]");
                 break;
             case XMLStreamConstants.DTD:
                 log.trace("DTD: ");

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java Mon Aug 25 17:37:16 2008
@@ -1,141 +1,158 @@
-/*
- * 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.builder;
-
-import org.apache.axiom.attachments.Attachments;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.OMFactory;
-import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMText;
-import org.apache.axiom.om.impl.MTOMConstants;
-import org.apache.axiom.om.impl.OMContainerEx;
-import org.apache.axiom.om.impl.OMNodeEx;
-import org.apache.axiom.om.util.ElementHelper;
-
-import javax.activation.DataHandler;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-
-public class XOPAwareStAXOMBuilder 
-    extends StAXOMBuilder implements XOPBuilder {
-
-    /** <code>Attachments</code> handles deferred parsing of incoming MIME Messages. */
-    Attachments attachments;
-
-    /**
-     * Constructor StAXOMBuilder.
-     *
-     * @param ombuilderFactory
-     * @param parser
-     */
-    public XOPAwareStAXOMBuilder(OMFactory ombuilderFactory,
-                                 XMLStreamReader parser, Attachments attachments) {
-        super(ombuilderFactory, parser);
-        this.attachments = attachments;
-    }
-
-    /**
-     * Constructor linked to existing element.
-     *
-     * @param factory
-     * @param parser
-     * @param element
-     */
-    public XOPAwareStAXOMBuilder(OMFactory factory, XMLStreamReader parser,
-                                 OMElement element, Attachments attachments) {
-        super(factory, parser, element);
-        this.attachments = attachments;
-    }
-
-    /**
-     * @param filePath - Path to the XML file
-     * @throws XMLStreamException
-     * @throws FileNotFoundException
-     */
-    public XOPAwareStAXOMBuilder(String filePath, Attachments attachments)
-            throws XMLStreamException,
-            FileNotFoundException {
-        super(filePath);
-        this.attachments = attachments;
-    }
-
-    /**
-     * @param inStream - instream which contains the XML
-     * @throws XMLStreamException
-     */
-    public XOPAwareStAXOMBuilder(InputStream inStream, Attachments attachments)
-            throws XMLStreamException {
-        super(inStream);
-        this.attachments = attachments;
-    }
-
-    /**
-     * Constructor StAXXOPAwareOMBuilder.
-     *
-     * @param parser
-     */
-    public XOPAwareStAXOMBuilder(XMLStreamReader parser, Attachments attachments) {
-        super(parser);
-        this.attachments = attachments;
-    }
-
-    /**
-     * Method createOMElement. Overriding the createOMElement of StAXOMBuilder to to XOP aware
-     * building
-     *
-     * @return Returns OMNode.
-     * @throws OMException
-     */
-    protected OMNode createOMElement() throws OMException {
-
-        String elementName = parser.getLocalName();
-        String namespaceURI = parser.getNamespaceURI();
-        if (MTOMConstants.XOP_INCLUDE.equals(elementName)
-                && MTOMConstants.XOP_NAMESPACE_URI.equals(namespaceURI)) {
-            OMText node;
-            String contentID = ElementHelper.getContentID(parser, getDocument()
-                    .getCharsetEncoding());
-
-            if (lastNode == null) {
-                throw new OMException(
-                        "XOP:Include element is not supported here");
-            } else if (lastNode.isComplete() & lastNode.getParent() != null) {
-                node = omfactory.createOMText(contentID, (OMElement) lastNode
-                        .getParent(), this);
-                ((OMNodeEx) lastNode).setNextOMSibling(node);
-                ((OMNodeEx) node).setPreviousOMSibling(lastNode);
-            } else {
-                OMContainerEx e = (OMContainerEx) lastNode;
-                node = omfactory.createOMText(contentID, (OMElement) lastNode, this);
-                e.setFirstChild(node);
-            }
-            return node;
-        } else {
-            return super.createOMElement();
-        }
-    }
-
-    public DataHandler getDataHandler(String blobContentID) throws OMException {
-        return attachments.getDataHandler(blobContentID);
-	}
-}
+/*
+ * 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.builder;
+
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.impl.MTOMConstants;
+import org.apache.axiom.om.impl.OMContainerEx;
+import org.apache.axiom.om.impl.OMNodeEx;
+import org.apache.axiom.om.util.ElementHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.activation.DataHandler;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+public class XOPAwareStAXOMBuilder 
+    extends StAXOMBuilder implements XOPBuilder {
+    
+    private static final Log log = LogFactory.getLog(XOPAwareStAXOMBuilder.class);
+
+    /** <code>Attachments</code> handles deferred parsing of incoming MIME Messages. */
+    Attachments attachments;
+
+    /**
+     * Constructor StAXOMBuilder.
+     *
+     * @param ombuilderFactory
+     * @param parser
+     */
+    public XOPAwareStAXOMBuilder(OMFactory ombuilderFactory,
+                                 XMLStreamReader parser, Attachments attachments) {
+        super(ombuilderFactory, parser);
+        this.attachments = attachments;
+    }
+
+    /**
+     * Constructor linked to existing element.
+     *
+     * @param factory
+     * @param parser
+     * @param element
+     */
+    public XOPAwareStAXOMBuilder(OMFactory factory, XMLStreamReader parser,
+                                 OMElement element, Attachments attachments) {
+        super(factory, parser, element);
+        this.attachments = attachments;
+    }
+
+    /**
+     * @param filePath - Path to the XML file
+     * @throws XMLStreamException
+     * @throws FileNotFoundException
+     */
+    public XOPAwareStAXOMBuilder(String filePath, Attachments attachments)
+            throws XMLStreamException,
+            FileNotFoundException {
+        super(filePath);
+        this.attachments = attachments;
+    }
+
+    /**
+     * @param inStream - instream which contains the XML
+     * @throws XMLStreamException
+     */
+    public XOPAwareStAXOMBuilder(InputStream inStream, Attachments attachments)
+            throws XMLStreamException {
+        super(inStream);
+        this.attachments = attachments;
+    }
+
+    /**
+     * Constructor StAXXOPAwareOMBuilder.
+     *
+     * @param parser
+     */
+    public XOPAwareStAXOMBuilder(XMLStreamReader parser, Attachments attachments) {
+        super(parser);
+        this.attachments = attachments;
+    }
+
+    /**
+     * Method createOMElement. Overriding the createOMElement of StAXOMBuilder to to XOP aware
+     * building
+     *
+     * @return Returns OMNode.
+     * @throws OMException
+     */
+    protected OMNode createOMElement() throws OMException {
+
+        String elementName = parser.getLocalName();
+        String namespaceURI = parser.getNamespaceURI();
+        if (MTOMConstants.XOP_INCLUDE.equals(elementName)
+                && MTOMConstants.XOP_NAMESPACE_URI.equals(namespaceURI)) {
+            OMText node;
+            String contentID = ElementHelper.getContentID(parser, getDocument()
+                    .getCharsetEncoding());
+            if (log.isDebugEnabled()) {
+                log.debug("Encountered xop:include for cid:" + contentID);
+            }
+
+            if (lastNode == null) {
+                throw new OMException(
+                        "XOP:Include element is not supported here");
+            } else if (lastNode.isComplete() & lastNode.getParent() != null) {
+                node = omfactory.createOMText(contentID, (OMElement) lastNode
+                        .getParent(), this);
+                if (log.isDebugEnabled()) {
+                    log.debug("Create createOMText for cid:" + contentID);
+                    Object dh = node.getDataHandler();
+                    String dhClass = (dh==null) ? "null" : dh.getClass().toString();
+                    log.debug("The datahandler is " + dhClass);
+                }
+                ((OMNodeEx) lastNode).setNextOMSibling(node);
+                ((OMNodeEx) node).setPreviousOMSibling(lastNode);
+            } else {
+                OMContainerEx e = (OMContainerEx) lastNode;
+                node = omfactory.createOMText(contentID, (OMElement) lastNode, this);
+                e.setFirstChild(node);
+            }
+            return node;
+        } else {
+            return super.createOMElement();
+        }
+    }
+
+    public DataHandler getDataHandler(String blobContentID) throws OMException {
+        return attachments.getDataHandler(blobContentID);
+    }
+    
+    public Attachments getAttachments() {
+        return attachments;
+    }
+}

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPBuilder.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPBuilder.java Mon Aug 25 17:37:16 2008
@@ -1,32 +1,34 @@
-/*
- * 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.builder;
-
-import org.apache.axiom.om.OMAttachmentAccessor;
-import org.apache.axiom.om.OMException;
-
-import javax.activation.DataHandler;
-
-public interface XOPBuilder extends OMAttachmentAccessor {
-
-    DataHandler getDataHandler(String blobContentID)
-            throws OMException;
-
+/*
+ * 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.builder;
+
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.om.OMAttachmentAccessor;
+import org.apache.axiom.om.OMException;
+
+import javax.activation.DataHandler;
+
+public interface XOPBuilder extends OMAttachmentAccessor {
+
+    DataHandler getDataHandler(String blobContentID)
+            throws OMException;
+
+    Attachments getAttachments();
 }
\ No newline at end of file

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CopyUtils.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CopyUtils.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CopyUtils.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CopyUtils.java Mon Aug 25 17:37:16 2008
@@ -45,7 +45,10 @@
 import org.apache.axiom.soap.SOAPFaultValue;
 import org.apache.axiom.soap.SOAPHeader;
 import org.apache.axiom.soap.SOAPHeaderBlock;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
+import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
@@ -62,7 +65,10 @@
  */
 public class CopyUtils {
 
-
+    private static Log log = LogFactory.getLog(CopyUtils.class);
+    private static final boolean IS_DEBUG_ENABLED = log.isDebugEnabled();
+    
+    
     /**
      * Private Constructor
      */
@@ -80,6 +86,11 @@
      */
     public static SOAPEnvelope copy(SOAPEnvelope sourceEnv) {
 
+        // Make sure to build the whole sourceEnv
+        if (log.isDebugEnabled()) {
+            log.debug("start copy SOAPEnvelope");
+        }
+        
         SOAPFactory factory = (SOAPFactory) sourceEnv.getOMFactory();
         // Create envelope with the same prefix
         SOAPEnvelope targetEnv = factory.createSOAPEnvelope(sourceEnv.getNamespace());
@@ -114,6 +125,9 @@
             }  
         }
 
+        if (log.isDebugEnabled()) {
+            log.debug("end copy SOAPEnvelope");
+        }
         return targetEnv;
     }
 
@@ -195,7 +209,21 @@
     private static void copyOMText(SOAPFactory factory, 
                                    OMContainer targetParent, 
                                    OMText sourceText) {
+        if (IS_DEBUG_ENABLED) {
+            log.debug("start copyOMText");
+        }
+        if (sourceText.isBinary()) {
+            // This forces a load of the datahandler so that it is saved on the copy.
+            Object dh = sourceText.getDataHandler();
+            if (IS_DEBUG_ENABLED) {
+                String dhclass = (dh == null) ? "null" : dh.getClass().toString();
+                log.debug("The source text's binary data handler is " + dhclass);
+            }
+        }
         factory.createOMText(targetParent, sourceText);
+        if (IS_DEBUG_ENABLED) {
+            log.debug("end copyOMText");
+        }
     }
 
     /**

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java Mon Aug 25 17:37:16 2008
@@ -30,12 +30,16 @@
 import org.apache.axiom.om.impl.builder.XOPBuilder;
 import org.apache.axiom.om.util.ElementHelper;
 import org.apache.axiom.soap.SOAPFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamReader;
 
 public class MTOMStAXSOAPModelBuilder extends StAXSOAPModelBuilder implements
         MTOMConstants, XOPBuilder {
+    
+    private static final Log log = LogFactory.getLog(MTOMStAXSOAPModelBuilder.class);
 
     /** <code>Attachments</code> handles deferred parsing of incoming MIME Messages. */
     Attachments attachments;
@@ -75,6 +79,10 @@
             OMText node;
             String contentID = ElementHelper.getContentID(parser, getDocument()
                     .getCharsetEncoding());
+            
+            if (log.isDebugEnabled()) {
+                log.debug("Encountered xop:include for cid:" + contentID);
+            }
 
             if (lastNode == null) {
                 throw new OMException(
@@ -83,6 +91,12 @@
                 node = omfactory.createOMText(contentID, lastNode.getParent(), this);
                 ((OMNodeEx) lastNode).setNextOMSibling(node);
                 ((OMNodeEx) node).setPreviousOMSibling(lastNode);
+                if (log.isDebugEnabled()) {
+                    log.debug("Create createOMText for cid:" + contentID);
+                    Object dh = node.getDataHandler();
+                    String dhClass = (dh==null) ? "null" : dh.getClass().toString();
+                    log.debug("The datahandler is " + dhClass);
+                }
             } else {
                 OMContainerEx e = (OMContainerEx) lastNode;
                 node = omfactory.createOMText(contentID, (OMElement) lastNode,
@@ -112,4 +126,8 @@
         */
         return dataHandler;
     }
+    
+    public Attachments getAttachments() {
+        return attachments;
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java Mon Aug 25 17:37:16 2008
@@ -19,6 +19,7 @@
 
 package org.apache.axiom.om.impl.llom;
 
+import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMConstants;
@@ -36,6 +37,8 @@
 import org.apache.axiom.om.impl.OMNamespaceImpl;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.impl.builder.XOPAwareStAXOMBuilder;
+import org.apache.axiom.om.impl.builder.XOPBuilder;
 import org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory;
 import org.apache.axiom.om.impl.traverse.OMChildElementIterator;
 import org.apache.axiom.om.impl.traverse.OMChildrenIterator;
@@ -1138,7 +1141,7 @@
         
         if (log.isDebugEnabled()) {
             log.debug("cloneOMElement start");
-            log.debug("  element string =" + this.toString());
+            log.debug("  element string =" + getLocalName());
             log.debug(" isComplete = " + isComplete());
             log.debug("  builder = " + builder);
         }
@@ -1149,10 +1152,32 @@
         
         // Now get a parser for the full tree
         XMLStreamReader xmlStreamReader = this.getXMLStreamReader(true);
+        if (log.isDebugEnabled()) {
+            log.debug("  reader = " + xmlStreamReader);
+        }
+        
+        // Get a new builder.  Use an xop aware builder if the original
+        // builder is xop aware
+        StAXOMBuilder newBuilder = null;
+        if (builder instanceof XOPBuilder) {
+            Attachments attachments = ((XOPBuilder)builder).getAttachments();
+            attachments.getAllContentIDs();
+            if (xmlStreamReader instanceof OMXMLStreamReader) {
+                if (log.isDebugEnabled()) {
+                    log.debug("  read optimized xop:include");
+                }
+                ((OMXMLStreamReader)xmlStreamReader).setInlineMTOM(false);
+            }
+            newBuilder = new XOPAwareStAXOMBuilder(xmlStreamReader, attachments);
+        } else {
+            newBuilder = new StAXOMBuilder(xmlStreamReader);
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("  newBuilder = " + newBuilder);
+        }
         
         // Build the (target) clonedElement from the parser
-        OMElement clonedElement =
-                new StAXOMBuilder(xmlStreamReader).getDocumentElement();
+        OMElement clonedElement = newBuilder.getDocumentElement();
         clonedElement.build();
         return clonedElement;
     }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java?rev=688927&r1=688926&r2=688927&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java Mon Aug 25 17:37:16 2008
@@ -96,6 +96,11 @@
         //  assertEquals("Object check", expectedObject[5],actualObject[5] );
     }
     
+    /**
+     * Test reading a message containing XOP and ensuring that the
+     * the XOP is preserved when it is serialized.
+     * @throws Exception
+     */
     public void testCreateAndSerializeOptimized() throws Exception {
         String contentTypeString =
                 "multipart/Related; charset=\"UTF-8\"; type=\"application/xop+xml\"; boundary=\"----=_AxIs2_Def_boundary_=42214532\"; start=\"SOAPPart\"";
@@ -119,7 +124,103 @@
         assertTrue(msg.indexOf("Content-ID: <cid:-1609420109260943731>") > 0);
     }
     
+    /**
+     * Test reading a message containing XOP.
+     * Then make a copy of the message.
+     * Then ensure that the XOP is preserved when it is serialized.
+     * @throws Exception
+     */
+    public void testCreateCloneAndSerializeOptimized() throws Exception {
+        String contentTypeString =
+                "multipart/Related; charset=\"UTF-8\"; type=\"application/xop+xml\"; boundary=\"----=_AxIs2_Def_boundary_=42214532\"; start=\"SOAPPart\"";
+        String inFileName = "mtom/MTOMBuilderTestIn.txt";
+        InputStream inStream = new FileInputStream(getTestResourceFile(inFileName));
+        Attachments attachments = new Attachments(inStream, contentTypeString);
+        XMLStreamReader reader = XMLInputFactory.newInstance()
+                .createXMLStreamReader(new BufferedReader(new InputStreamReader(attachments
+                        .getSOAPPartInputStream())));
+        OMXMLParserWrapper builder = new MTOMStAXSOAPModelBuilder(reader, attachments,
+                                               SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+        OMElement root = builder.getDocumentElement();
+        
+        // Create a clone of root
+        OMElement root2 = root.cloneOMElement();
+        
+        // Write out the source
+        OMOutputFormat format = new OMOutputFormat();
+        format.setDoOptimize(true);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        root.serializeAndConsume(baos, format);
+        String msg = baos.toString();
+        // Make sure there is an xop:Include element and an optimized attachment
+        assertTrue(msg.indexOf("xop:Include") > 0);
+        assertTrue(msg.indexOf("Content-ID: <-1609420109260943731>") > 0);
+        
+        // Write out the clone
+        format = new OMOutputFormat();
+        format.setDoOptimize(true);
+        baos = new ByteArrayOutputStream();
+        root2.serializeAndConsume(baos, format);
+        msg = baos.toString();
+        // Make sure there is an xop:Include element and an optimized attachment
+        assertTrue(msg.indexOf("xop:Include") > 0);
+        assertTrue(msg.indexOf("Content-ID: <-1609420109260943731>") > 0);
+    }
+    
+    /**
+     * Test reading a message containing XOP.
+     * Fully build the tree.
+     * Then make a copy of the message.
+     * Then ensure that the XOP is preserved when it is serialized.
+     * @throws Exception
+     */
+    public void testCreateBuildCloneAndSerializeOptimized() throws Exception {
+        String contentTypeString =
+                "multipart/Related; charset=\"UTF-8\"; type=\"application/xop+xml\"; boundary=\"----=_AxIs2_Def_boundary_=42214532\"; start=\"SOAPPart\"";
+        String inFileName = "mtom/MTOMBuilderTestIn.txt";
+        InputStream inStream = new FileInputStream(getTestResourceFile(inFileName));
+        Attachments attachments = new Attachments(inStream, contentTypeString);
+        XMLStreamReader reader = XMLInputFactory.newInstance()
+                .createXMLStreamReader(new BufferedReader(new InputStreamReader(attachments
+                        .getSOAPPartInputStream())));
+        OMXMLParserWrapper builder = new MTOMStAXSOAPModelBuilder(reader, attachments,
+                                               SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+        OMElement root = builder.getDocumentElement();
+        
+        // Fully build the root
+        root.buildWithAttachments();
+        
+        // Create a clone of root
+        OMElement root2 = root.cloneOMElement();
+        
+        // Write out the source
+        OMOutputFormat format = new OMOutputFormat();
+        format.setDoOptimize(true);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        root.serializeAndConsume(baos, format);
+        String msg = baos.toString();
+        // Make sure there is an xop:Include element and an optimized attachment
+        assertTrue(msg.indexOf("xop:Include") > 0);
+        assertTrue(msg.indexOf("Content-ID: <-1609420109260943731>") > 0);
+        
+        // Write out the clone
+        format = new OMOutputFormat();
+        format.setDoOptimize(true);
+        baos = new ByteArrayOutputStream();
+        root2.serializeAndConsume(baos, format);
+        msg = baos.toString();
+        // Make sure there is an xop:Include element and an optimized attachment
+        assertTrue(msg.indexOf("xop:Include") > 0);
+        assertTrue(msg.indexOf("Content-ID: <-1609420109260943731>") > 0);
+    }
+    
     
+    /**
+     * Test reading a message containing XOP.
+     * Enable inlining serialization
+     * Then ensure that the data is inlined when written
+     * @throws Exception
+     */
     public void testCreateAndSerializeInlined() throws Exception {
         String contentTypeString =
                 "multipart/Related; charset=\"UTF-8\"; type=\"application/xop+xml\"; boundary=\"----=_AxIs2_Def_boundary_=42214532\"; start=\"SOAPPart\"";