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 2012/04/23 12:27:05 UTC

svn commit: r1329164 [7/12] - in /webservices/axiom/branches/AXIOM-201: ./ etc/ legal/ modules/axiom-all/ modules/axiom-api/ modules/axiom-api/src/main/java/org/apache/axiom/attachments/ modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl...

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java Mon Apr 23 10:26:45 2012
@@ -26,13 +26,13 @@ import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.impl.OMElementEx;
+import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.common.NamespaceIterator;
 import org.apache.axiom.om.impl.common.OMChildElementIterator;
-import org.apache.axiom.om.impl.common.OMDescendantsIterator;
+import org.apache.axiom.om.impl.common.OMElementImplUtil;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
-import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
 import org.apache.axiom.om.impl.traverse.OMQNameFilterIterator;
 import org.apache.axiom.om.impl.traverse.OMQualifiedNameFilterIterator;
 import org.apache.axiom.om.impl.util.EmptyIterator;
@@ -47,23 +47,39 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.TypeInfo;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 
 /** Implementation of the org.w3c.dom.Element and org.apache.axiom.om.Element interfaces. */
-public class ElementImpl extends ParentNode implements Element, OMElement,
+public class ElementImpl extends ParentNode implements Element, OMElementEx, OMNodeEx, NamedNode,
         OMConstants {
 
     private static final Log log = LogFactory.getLog(ElementImpl.class);
     
     private int lineNumber;
 
+    /**
+     * The namespace of this element. Possible values:
+     * <ul>
+     * <li><code>null</code> (if the element has no namespace)
+     * <li>any {@link OMNamespace} instance, with the following exceptions:
+     * <ul>
+     * <li>an {@link OMNamespace} instance with a <code>null</code> prefix
+     * <li>an {@link OMNamespace} instance with both prefix and namespace URI set to the empty
+     * string
+     * </ul>
+     * </ul>
+     */
     protected OMNamespace namespace;
 
     protected String localName;
@@ -123,17 +139,16 @@ public class ElementImpl extends ParentN
 
     public ElementImpl(ParentNode parentNode, String tagName, OMNamespaceImpl ns,
                        OMFactory factory) {
-        this((DocumentImpl) parentNode.getOwnerDocument(), tagName, ns, factory);
+        this(null, tagName, null, factory);
         parentNode.addChild(this);
         this.done = true;
+        namespace = handleNamespace(ns);
     }
 
     public ElementImpl(ParentNode parentNode, String tagName, OMNamespaceImpl ns,
                        OMXMLParserWrapper builder, OMFactory factory) {
         this(tagName, ns, builder, factory);
         if (parentNode != null) {
-            this.ownerNode = (DocumentImpl) parentNode.getOwnerDocument();
-            this.isOwned(true);
             parentNode.addChild(this);
         }
 
@@ -152,20 +167,46 @@ public class ElementImpl extends ParentN
 
     public ElementImpl(OMFactory factory) {
         super(factory);
-        this.ownerNode = ((OMDOMFactory) factory).getDocument();
     }
 
     private OMNamespace handleNamespace(OMNamespace ns) {
-        String namespaceURI = ns.getNamespaceURI();
-        String prefix = ns.getPrefix();
+        String namespaceURI = ns == null ? "" : ns.getNamespaceURI();
+        String prefix = ns == null ? "" : ns.getPrefix();
         if (namespaceURI.length() == 0 && prefix != null && prefix.length() > 0) {
             throw new IllegalArgumentException("Cannot create a prefixed element with an empty namespace name");
         }
-        OMNamespace namespace = findNamespace(namespaceURI, prefix);
-        if (namespace == null) {
-            namespace = declareNamespace(ns);
+        if (namespaceURI.length() == 0) {
+            // Special case: no namespace; we need to generate a namespace declaration only if
+            // there is a conflicting namespace declaration (i.e. a declaration for the default
+            // namespace with a non empty URI) is in scope
+            if (getDefaultNamespace() != null) {
+                declareDefaultNamespace("");
+            }
+            return null;
+        } else {
+            OMNamespace namespace = findNamespace(namespaceURI, prefix);
+            if (namespace == null) {
+                namespace = declareNamespace(ns);
+            }
+            return namespace;
+        }
+    }
+
+    OMNamespace handleNamespace(String namespaceURI, String prefix) {
+        if (prefix.length() == 0 && namespaceURI.length() == 0) {
+            OMNamespace namespace = getDefaultNamespace();
+            if (namespace != null) {
+                declareDefaultNamespace("");
+            }
+            return null;
+        } else {
+            OMNamespace namespace = findNamespace(namespaceURI,
+                                                  prefix);
+            if (namespace == null) {
+                namespace = declareNamespace(namespaceURI, prefix.length() > 0 ? prefix : null);
+            }
+            return namespace;
         }
-        return namespace;
     }
 
     // /
@@ -254,14 +295,6 @@ public class ElementImpl extends ParentN
      * @see org.w3c.dom.Element#removeAttribute(String)
      */
     public void removeAttribute(String name) throws DOMException {
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
-
         if (name.startsWith(OMConstants.XMLNS_NS_PREFIX)) {
             String namespacePrefix = DOMUtil.getLocalName(name);
             if (this.findNamespaceURI(namespacePrefix) != null) {
@@ -282,14 +315,6 @@ public class ElementImpl extends ParentN
      */
     public void removeAttributeNS(String namespaceURI, String localName)
             throws DOMException {
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
-
         if (OMConstants.XMLNS_NS_URI.equals(namespaceURI)) {
             //look in the ns list
             if (this.namespaces != null) {
@@ -307,21 +332,13 @@ public class ElementImpl extends ParentN
      * @see org.w3c.dom.Element#removeAttributeNode(org.w3c.dom.Attr)
      */
     public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
-        if (isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
-        if (this.attributes == null
-                || this.attributes.getNamedItem(oldAttr.getName()) == null) {
+        if (oldAttr.getOwnerElement() != this) {
             String msg = DOMMessageFormatter.formatMessage(
                     DOMMessageFormatter.DOM_DOMAIN, DOMException.NOT_FOUND_ERR, null);
             throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
         }
-        return (AttrImpl) this.attributes.removeNamedItem(oldAttr
-                .getName());
+        attributes.remove((AttrImpl)oldAttr, true);
+        return oldAttr;
     }
 
     /*
@@ -391,7 +408,7 @@ public class ElementImpl extends ParentN
             OMNamespace ns = this.findNamespaceURI(localName);
             String nsuri = ns != null ? ns.getNamespaceURI() : "";
 
-            AttrImpl namespaceAttr = new AttrImpl(this.ownerNode,
+            AttrImpl namespaceAttr = new AttrImpl(ownerDocument(),
                                                   localName, nsuri, this.factory);
             OMNamespaceImpl xmlNs = new OMNamespaceImpl(OMConstants.XMLNS_NS_URI, null);
             namespaceAttr.setOMNamespace(xmlNs);
@@ -411,22 +428,7 @@ public class ElementImpl extends ParentN
     public Attr setAttributeNode(Attr attr) throws DOMException {
         AttrImpl attrImpl = (AttrImpl) attr;
 
-        if (attrImpl.isOwned()) {// check for ownership
-            if (!this.getOwnerDocument().equals(attr.getOwnerDocument())) {
-                String msg = DOMMessageFormatter.formatMessage(
-                        DOMMessageFormatter.DOM_DOMAIN, DOMException.WRONG_DOCUMENT_ERR,
-                        null);
-                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
-            }
-        }
-
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
+        checkSameOwnerDocument(attr);
 
         // check whether the attr is in use
         if (attrImpl.isUsed()) {
@@ -476,18 +478,17 @@ public class ElementImpl extends ParentN
         } else if (name.equals(OMConstants.XMLNS_NS_PREFIX)) {
             this.declareDefaultNamespace(value);
         } else {
-            this.setAttributeNode(new AttrImpl(this.ownerNode, name, value,
+            this.setAttributeNode(new AttrImpl(ownerDocument(), name, value,
                                                this.factory));
         }
 
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
-     */
     public Attr setAttributeNodeNS(Attr attr) throws DOMException {
+        return setAttributeNodeNS(attr, true);
+    }
+    
+    private Attr setAttributeNodeNS(Attr attr, boolean useDomSemantics) throws DOMException {
 
         // Check whether the attr is a namespace declaration
         // if so add a namespace NOT an attribute
@@ -498,21 +499,8 @@ public class ElementImpl extends ParentN
         } else {
             AttrImpl attrImpl = (AttrImpl) attr;
 
-            if (attrImpl.isOwned()) {// check for ownership
-                if (!this.getOwnerDocument().equals(attr.getOwnerDocument())) {
-                    String msg = DOMMessageFormatter.formatMessage(
-                            DOMMessageFormatter.DOM_DOMAIN,
-                            DOMException.WRONG_DOCUMENT_ERR, null);
-                    throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
-                }
-            }
-
-            if (this.isReadonly()) {
-                String msg = DOMMessageFormatter.formatMessage(
-                        DOMMessageFormatter.DOM_DOMAIN,
-                        DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-                throw new DOMException(
-                        DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
+            if (useDomSemantics) {
+                checkSameOwnerDocument(attr);
             }
 
             // check whether the attr is in use
@@ -537,7 +525,7 @@ public class ElementImpl extends ParentN
                                                         attr.getPrefix()));
             }
 
-            return (Attr) this.attributes.setNamedItemNS(attr);
+            return (Attr) this.attributes.setAttribute(attr, useDomSemantics);
         }
     }
 
@@ -554,7 +542,7 @@ public class ElementImpl extends ParentN
                 this.declareNamespace(value, DOMUtil
                         .getLocalName(qualifiedName));
             } else {
-                AttrImpl attr = new AttrImpl(this.ownerNode, DOMUtil
+                AttrImpl attr = new AttrImpl(ownerDocument(), DOMUtil
                         .getLocalName(qualifiedName), value, this.factory);
                 attr.setOMNamespace(new OMNamespaceImpl(namespaceURI, DOMUtil
                         .getPrefix(qualifiedName)));
@@ -579,14 +567,6 @@ public class ElementImpl extends ParentN
             throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg);
         }
 
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
-
         if (this.attributes == null) {
             this.attributes = new AttributeMap(this);
         }
@@ -690,7 +670,8 @@ public class ElementImpl extends ParentN
             if (owner == this) {
                 return attr;
             }
-            attr = (OMAttribute)((AttrImpl)attr).cloneNode(false);
+            attr = new AttrImpl(null, attr.getLocalName(), attr.getNamespace(),
+                    attr.getAttributeValue(), attr.getOMFactory());
         }
         
         OMNamespace namespace = attr.getNamespace();
@@ -705,11 +686,7 @@ public class ElementImpl extends ParentN
             }
         }
 
-        if (attr.getNamespace() != null) { // If the attr has a namespace
-            this.setAttributeNodeNS((Attr) attr);
-        } else {
-            this.setAttributeNode((Attr) attr);
-        }
+        this.setAttributeNodeNS((Attr) attr, false);
         return attr;
     }
 
@@ -725,7 +702,16 @@ public class ElementImpl extends ParentN
                 }
             }
         }
-        return addAttribute(new AttrImpl(ownerNode, localName, ns, value, factory));
+        return addAttribute(new AttrImpl(null, localName, ns, value, factory));
+    }
+
+    public OMNamespace addNamespaceDeclaration(String uri, String prefix) {
+        if (namespaces == null) {
+            this.namespaces = new HashMap(5);
+        }
+        OMNamespace ns = new OMNamespaceImpl(uri, prefix);
+        namespaces.put(prefix, ns);
+        return ns;
     }
 
     /**
@@ -740,9 +726,7 @@ public class ElementImpl extends ParentN
 
         if (namespace != null) {
             String prefix = namespace.getPrefix();
-            if ("".equals(prefix)) {
-                namespace = declareDefaultNamespace(namespace.getNamespaceURI());
-            } else if (prefix == null) {
+            if (prefix == null) {
                 prefix = OMSerializerUtil.getNextNSPrefix();
                 namespace = new OMNamespaceImpl(namespace.getNamespaceURI(), prefix);
             }
@@ -764,11 +748,6 @@ public class ElementImpl extends ParentN
         namespaces.put(prefix, new OMNamespaceImpl("", prefix));
     }
 
-    /**
-     * Allows overriding an existing declaration if the same prefix was used.
-     *
-     * @see org.apache.axiom.om.OMElement#declareNamespace(String, String)
-     */
     public OMNamespace declareNamespace(String uri, String prefix) {
         if ("".equals(prefix)) {
             log.warn("Deprecated usage of OMElement#declareNamespace(String,String) with empty prefix");
@@ -779,13 +758,13 @@ public class ElementImpl extends ParentN
         return declareNamespace(ns);
     }
 
-    /**
-     * We use "" to store the default namespace of this element. As one can see user can not give ""
-     * as the prefix, when he declare a usual namespace.
-     *
-     * @param uri
-     */
     public OMNamespace declareDefaultNamespace(String uri) {
+        if (namespace == null && uri.length() > 0
+                || namespace != null && namespace.getPrefix().length() == 0 && !namespace.getNamespaceURI().equals(uri)) {
+            throw new OMException("Attempt to add a namespace declaration that conflicts with " +
+                    "the namespace information of the element");
+        }
+
         OMNamespaceImpl ns = new OMNamespaceImpl(uri, "");
         if (namespaces == null) {
             this.namespaces = new HashMap(5);
@@ -798,13 +777,14 @@ public class ElementImpl extends ParentN
         if (namespaces != null) {
             OMNamespaceImpl defaultNS = (OMNamespaceImpl) namespaces.get("");
             if (defaultNS != null) {
-                return defaultNS;
+                return defaultNS.getNamespaceURI().length() == 0 ? null : defaultNS;
             }
         }
 
+        ParentNode parentNode = parentNode();
         if (parentNode instanceof ElementImpl) {
             ElementImpl element = (ElementImpl) parentNode;
-            element.getDefaultNamespace();
+            return element.getDefaultNamespace();
         }
         return null;
     }
@@ -819,13 +799,18 @@ public class ElementImpl extends ParentN
         }
 
         // go up to check with ancestors
-        if (this.parentNode != null) {
+        ParentNode parentNode = parentNode();
+        if (parentNode != null) {
             // For the OMDocumentImpl there won't be any explicit namespace
             // declarations, so going up the parent chain till the document
             // element should be enough.
             if (parentNode instanceof OMElement) {
                 namespace = ((ElementImpl) parentNode).findNamespace(uri,
                                                                      prefix);
+                // If the prefix has been redeclared, then ignore the binding found on the ancestors
+                if (prefix == null && namespace != null && findDeclaredNamespace(null, namespace.getPrefix()) != null) {
+                    namespace = null;
+                }
             }
         }
 
@@ -844,9 +829,10 @@ public class ElementImpl extends ParentN
                 (OMNamespace) this.namespaces.get(prefix);
 
         if (ns == null) {
-            if (this.parentNode instanceof OMElement) {
+            ParentNode parentNode = parentNode();
+            if (parentNode instanceof OMElement) {
                 // try with the parent
-                return ((OMElement) this.parentNode).findNamespaceURI(prefix);
+                return ((OMElement)parentNode).findNamespaceURI(prefix);
             } else {
                 return null;
             }
@@ -908,8 +894,7 @@ public class ElementImpl extends ParentN
             return null;
         }
 
-        if (qname.getNamespaceURI() == null
-                || qname.getNamespaceURI().equals("")) {
+        if (qname.getNamespaceURI().equals("")) {
             return (AttrImpl) this.getAttributeNode(qname.getLocalPart());
         } else {
             return (AttrImpl) this.getAttributeNodeNS(qname.getNamespaceURI(),
@@ -951,7 +936,7 @@ public class ElementImpl extends ParentN
      * @see org.apache.axiom.om.OMElement#getNamespace()
      */
     public OMNamespace getNamespace() {
-        return namespace != null ? namespace : getDefaultNamespace();
+        return namespace;
     }
 
     /**
@@ -962,79 +947,36 @@ public class ElementImpl extends ParentN
     public QName getQName() {
         QName qName;
         if (namespace != null) {
-            if (namespace.getPrefix() != null) {
-                qName = new QName(namespace.getNamespaceURI(), this.localName,
-                                  namespace.getPrefix());
-            } else {
-                qName = new QName(namespace.getNamespaceURI(), this.localName);
-            }
+            qName = new QName(namespace.getNamespaceURI(), this.localName,
+                              namespace.getPrefix());
         } else {
             qName = new QName(this.localName);
         }
         return qName;
     }
 
-    /**
-     * Gets all the text children and concatinates them to a single string.
-     *
-     * @see org.apache.axiom.om.OMElement#getText()
-     */
     public String getText() {
-        String childText = "";
-        OMNode child = this.getFirstOMChild();
-        OMText textNode;
-
-        while (child != null) {
-            final int type = child.getType();
-            if (type == OMNode.TEXT_NODE || type == OMNode.CDATA_SECTION_NODE) {
-                textNode = (OMText) child;
-                if (textNode.getText() != null
-                        && !"".equals(textNode.getText())) {
-                    childText += textNode.getText();
-                }
-            }
-            child = child.getNextOMSibling();
-        }
+        return OMElementImplUtil.getText(this);
+    }
 
-        return childText;
+    public Reader getTextAsStream(boolean cache) {
+        return OMElementImplUtil.getTextAsStream(this, cache);
     }
 
     public QName getTextAsQName() {
-        String childText = getTrimmedText();
-        if (childText != null) {
-            return resolveQName(childText);
-        }
-        return null;
+        String childText = getText().trim();
+        return childText.length() == 0 ? null : resolveQName(childText);
     }
 
-    public String getTrimmedText() {
-        String childText = null;
-        OMNode child = this.getFirstOMChild();
-        OMText textNode;
-
-        while (child != null) {
-            if (child.getType() == OMNode.TEXT_NODE) {
-                textNode = (OMText) child;
-                String textValue = textNode.getText();
-                if (textValue != null &&
-                        !"".equals(textValue.trim())) {
-                    if (childText == null) childText = "";
-                    childText += textValue.trim();
-                }
-            }
-            child = child.getNextOMSibling();
-        }
-
-        return childText;
+    public void writeTextTo(Writer out, boolean cache) throws IOException {
+        OMElementImplUtil.writeTextTo(this, out, cache);
     }
 
-    /**
-     * Removes an attribute from the element.
-     *
-     * @see org.apache.axiom.om.OMElement#removeAttribute (org.apache.axiom.om.OMAttribute)
-     */
     public void removeAttribute(OMAttribute attr) {
-        this.removeAttributeNode((AttrImpl) attr);
+        if (attr.getOwner() != this) {
+            throw new OMException("The attribute is not owned by this element");
+        }
+        attributes.remove((AttrImpl)attr, false);
     }
 
     /**
@@ -1055,15 +997,16 @@ public class ElementImpl extends ParentN
         this.localName = localName;
     }
 
-    public void setNamespace(OMNamespace namespace) {
-        if (namespace != null) {
-            namespace = handleNamespace(namespace);
-        }
+    public void internalSetNamespace(OMNamespace namespace) {
         this.namespace = namespace;
     }
 
+    public void setNamespace(OMNamespace namespace) {
+        internalSetNamespace(handleNamespace(namespace));
+    }
+
     public void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace) {
-        this.namespace = namespace;
+        internalSetNamespace(namespace);
     }
 
     /**
@@ -1072,30 +1015,27 @@ public class ElementImpl extends ParentN
      * @see org.apache.axiom.om.OMElement#setText(String)
      */
     public void setText(String text) {
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
-
-        // if we already have other text nodes remove them
-        OMNode child = this.getFirstOMChild();
-        while (child != null) {
-            if (child.getType() == OMNode.TEXT_NODE) {
-                child.detach();
-            }
-            child = child.getNextOMSibling();
+        // Remove all existing children
+        OMNode child;
+        while ((child = getFirstOMChild()) != null) {
+            child.detach();
+        }
+        // Add a new text node
+        if (text != null && text.length() > 0) {
+            getOMFactory().createOMText(this, text);
         }
-
-        TextImpl textNode = (TextImpl) (this.ownerNode)
-                .createTextNode(text);
-        this.addChild(textNode);
     }
 
-    public void setText(QName text) {
-        throw new UnsupportedOperationException();
+    public void setText(QName qname) {
+        // Remove all existing children
+        OMNode child;
+        while ((child = getFirstOMChild()) != null) {
+            child.detach();
+        }
+        // Add a new text node
+        if (qname != null) {
+            getOMFactory().createOMText(this, qname);
+        }
     }
 
     public void internalSerialize(XMLStreamWriter writer,
@@ -1153,6 +1093,10 @@ public class ElementImpl extends ParentN
         return new NamespaceIterator(this);
     }
 
+    public NamespaceContext getNamespaceContext(boolean detached) {
+        return OMElementImplUtil.getNamespaceContext(this, detached);
+    }
+
     /** @see org.apache.axiom.om.OMElement#getAllAttributes() */
     public Iterator getAllAttributes() {
         if (attributes == null) {
@@ -1186,16 +1130,17 @@ public class ElementImpl extends ParentN
      * @see org.w3c.dom.Node#getPrefix()
      */
     public String getPrefix() {
-        return (this.namespace == null) ? null : this.namespace.getPrefix();
+        OMNamespace ns = getNamespace();
+        if (ns == null) {
+            return null;
+        } else {
+            String prefix = ns.getPrefix();
+            return prefix.length() == 0 ? null : prefix;
+        }
     }
 
-    /** @see NodeImpl#setOwnerDocument (org.apache.axiom.om.impl.dom.DocumentImpl) */
-    protected void setOwnerDocument(DocumentImpl document) {
-        this.ownerNode = document;
-        this.isOwned(true);
-        if (document.firstChild == null) {
-            document.firstChild = this;
-        }
+    public void setPrefix(String prefix) throws DOMException {
+        NamedNodeHelper.setPrefix(this, prefix);
     }
 
     public QName resolveQName(String qname) {
@@ -1232,7 +1177,7 @@ public class ElementImpl extends ParentN
         ElementImpl newnode = (ElementImpl) super.cloneNode(deep);
         // Replicate NamedNodeMap rather than sharing it.
         if (attributes != null) {
-            newnode.attributes = (AttributeMap) attributes.cloneMap(newnode);
+            newnode.attributes = attributes.cloneMap(newnode);
         }
         return newnode;
 
@@ -1257,11 +1202,11 @@ public class ElementImpl extends ParentN
                     OMNamespace ns = (OMNamespace)this.namespaces.get(prefix);
                     
                     if ("".equals(prefix)) {
-                        AttrImpl attr = new AttrImpl(this.ownerNode, "xmlns", ns.getNamespaceURI(), this.factory);
+                        AttrImpl attr = new AttrImpl(ownerDocument(), "xmlns", ns.getNamespaceURI(), this.factory);
                         attr.setOMNamespace(XMLNS_NAMESPACE_WITHOUT_PREFIX);
                         attributeMap.addItem(attr);
                     } else {
-                        AttrImpl attr = new AttrImpl(this.ownerNode, prefix, ns.getNamespaceURI(), this.factory);
+                        AttrImpl attr = new AttrImpl(ownerDocument(), prefix, ns.getNamespaceURI(), this.factory);
                         attr.setOMNamespace(XMLNS_NAMESPACE_WITH_PREFIX);
                         attributeMap.addItem(attr);
                     }
@@ -1323,13 +1268,6 @@ public class ElementImpl extends ParentN
      */
 
     public void setIdAttribute(String name, boolean isId) throws DOMException {
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
         //find the attr
         AttrImpl tempAttr = (AttrImpl) this.getAttributeNode(name);
         if (tempAttr == null) {
@@ -1345,13 +1283,6 @@ public class ElementImpl extends ParentN
 
     public void setIdAttributeNS(String namespaceURI, String localName, boolean isId)
             throws DOMException {
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
         //find the attr
         AttrImpl tempAttr = (AttrImpl) this.getAttributeNodeNS(namespaceURI, localName);
         if (tempAttr == null) {
@@ -1366,13 +1297,6 @@ public class ElementImpl extends ParentN
     }
 
     public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
-        if (this.isReadonly()) {
-            String msg = DOMMessageFormatter.formatMessage(
-                    DOMMessageFormatter.DOM_DOMAIN,
-                    DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   msg);
-        }
         //find the attr
         Iterator attrIter = this.getAllAttributes();
         AttrImpl tempAttr = null;
@@ -1404,9 +1328,9 @@ public class ElementImpl extends ParentN
     private void updateIsId(boolean isId, AttrImpl tempAttr) {
         tempAttr.isId = isId;
         if (isId) {
-            this.ownerNode.addIdAttr(tempAttr);
+            ownerDocument().addIdAttr(tempAttr);
         } else {
-            this.ownerNode.removeIdAttr(tempAttr);
+            ownerDocument().removeIdAttr(tempAttr);
         }
     }
 
@@ -1428,4 +1352,21 @@ public class ElementImpl extends ParentN
             node.buildWithAttachments();
         }
     }
+
+    void normalize(DOMConfigurationImpl config) {
+        if (config.isEnabled(DOMConfigurationImpl.NAMESPACES)) {
+            OMNamespace namespace = getNamespace();
+            if (namespace == null) {
+                if (getDefaultNamespace() != null) {
+                    declareDefaultNamespace("");
+                }
+            } else {
+                OMNamespace namespaceForPrefix = findNamespaceURI(namespace.getPrefix());
+                if (namespaceForPrefix == null || !namespaceForPrefix.getNamespaceURI().equals(namespace.getNamespaceURI())) {
+                    declareNamespace(namespace);
+                }
+            }
+        }
+        super.normalize(config);
+    }
 }

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java Mon Apr 23 10:26:45 2012
@@ -19,30 +19,17 @@
 
 package org.apache.axiom.om.impl.dom;
 
-import org.apache.axiom.om.OMContainer;
-import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
-import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMXMLParserWrapper;
-import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
-import org.apache.axiom.om.impl.OMNodeEx;
-import org.apache.axiom.om.impl.builder.StAXBuilder;
-import org.apache.axiom.om.util.StAXUtils;
 import org.w3c.dom.DOMException;
-import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.UserDataHandler;
 
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-import java.io.OutputStream;
-import java.io.Writer;
 import java.util.Hashtable;
 
-public abstract class NodeImpl implements Node, NodeList, OMNodeEx, Cloneable {
+public abstract class NodeImpl implements Node, NodeList, Cloneable {
 
     /** Holds the user data objects */
     private Hashtable userData; // Will be initialized in setUserData()
@@ -53,8 +40,6 @@ public abstract class NodeImpl implement
     /** Field done */
     protected boolean done = false;
 
-    protected DocumentImpl ownerNode;
-
     /** Factory that created this node */
     protected final OMFactory factory;
 
@@ -62,32 +47,30 @@ public abstract class NodeImpl implement
 
     protected short flags;
 
-    protected final static short OWNED = 0x1 << 1;
-
+    /**
+     * Used by {@link ChildNode} to determine the meaning of the <code>ownerNode</code> attribute.
+     * If the flag is set, then the attribute contains the reference to the parent node. If the flag
+     * is not set, then the node has no parent and the attribute stores a reference to the owner
+     * document (which may be <code>null</code> if the owner document has not been created yet).
+     */
+    protected final static short HAS_PARENT = 0x1 << 1;
+    
     protected final static short FIRSTCHILD = 0x1 << 2;
 
-    protected final static short READONLY = 0x1 << 3;
-
     protected final static short SPECIFIED = 0x1 << 4;
 
-    protected final static short NORMALIZED = 0x1 << 5;
-
     //
     // Constructors
     //
 
-    protected NodeImpl(DocumentImpl ownerDocument, OMFactory factory) {
-        //this(factory);
-        this.factory = factory;
-        this.ownerNode = ownerDocument;
-        // this.isOwned(true);
-
-    }
-
     protected NodeImpl(OMFactory factory) {
         this.factory = factory;
     }
 
+    void normalize(DOMConfigurationImpl config) {
+        // Default: do nothing
+    }
+    
     public void normalize() {
         //Parent node should override this 
     }
@@ -119,7 +102,7 @@ public abstract class NodeImpl implement
         return null;
     }
 
-    public void setNodeValue(String arg0) throws DOMException {
+    public void setNodeValue(String nodeValue) throws DOMException {
         // Don't do anything, to be overridden in SOME Child classes
     }
 
@@ -130,14 +113,6 @@ public abstract class NodeImpl implement
     }
 
     /**
-     * Finds the document that this Node belongs to (the document in whose context the Node was
-     * created). The Node may or may not
-     */
-    public Document getOwnerDocument() {
-        return this.ownerNode;
-    }
-
-    /**
      * Returns the collection of attributes associated with this node, or null if none. At this
      * writing, Element is the only type of node which will ever have attributes.
      *
@@ -174,18 +149,6 @@ public abstract class NodeImpl implement
         return null; // default behavior, overriden in ChildNode
     }
 
-    public Node getParentNode() {
-        return null; // overriden by ChildNode
-        // Document, DocumentFragment, and Attribute will never have parents.
-    }
-
-    /*
-     * Same as getParentNode but returns internal type NodeImpl.
-     */
-    NodeImpl parentNode() {
-        return null;
-    }
-
     /** Returns the previous child of this node's parent, or null if none. */
     public Node getPreviousSibling() {
         return null; // default behavior, overriden in ChildNode
@@ -209,10 +172,6 @@ public abstract class NodeImpl implement
         } catch (CloneNotSupportedException e) {
             throw new RuntimeException("**Internal Error**" + e);
         }
-        newnode.ownerNode = this.ownerNode;
-        newnode.isOwned(false);
-
-        newnode.isReadonly(false);
 
         return newnode;
     }
@@ -311,12 +270,12 @@ public abstract class NodeImpl implement
      * Flags setters and getters
      */
 
-    final boolean isOwned() {
-        return (flags & OWNED) != 0;
+    final boolean hasParent() {
+        return (flags & HAS_PARENT) != 0;
     }
 
-    final void isOwned(boolean value) {
-        flags = (short) (value ? flags | OWNED : flags & ~OWNED);
+    final void hasParent(boolean value) {
+        flags = (short) (value ? flags | HAS_PARENT : flags & ~HAS_PARENT);
     }
 
     final boolean isFirstChild() {
@@ -327,14 +286,6 @@ public abstract class NodeImpl implement
         flags = (short) (value ? flags | FIRSTCHILD : flags & ~FIRSTCHILD);
     }
 
-    final boolean isReadonly() {
-        return (flags & READONLY) != 0;
-    }
-
-    final void isReadonly(boolean value) {
-        flags = (short) (value ? flags | READONLY : flags & ~READONLY);
-    }
-
     final boolean isSpecified() {
         return (flags & SPECIFIED) != 0;
     }
@@ -343,169 +294,6 @@ public abstract class NodeImpl implement
         flags = (short) (value ? flags | SPECIFIED : flags & ~SPECIFIED);
     }
 
-    final boolean isNormalized() {
-        return (flags & NORMALIZED) != 0;
-    }
-
-    final void isNormalized(boolean value) {
-        // See if flag should propagate to parent.
-        if (!value && isNormalized() && ownerNode != null) {
-            ownerNode.isNormalized(false);
-        }
-        flags = (short) (value ? flags | NORMALIZED : flags & ~NORMALIZED);
-    }
-
-    // /
-    // /OM Methods
-    // /
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.axis2.om.OMNode#getParent()
-     */
-    public OMContainer getParent() throws OMException {
-        return null; // overriden by ChildNode
-        // Document, DocumentFragment, and Attribute will never have parents.
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.axis2.om.OMNode#isComplete()
-     */
-    public boolean isComplete() {
-        return this.done;
-    }
-
-    public void setComplete(boolean state) {
-        this.done = state;
-
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.axis2.om.OMNode#insertSiblingAfter
-     * (org.apache.axis2.om.OMNode)
-     */
-    public void insertSiblingAfter(OMNode sibling) throws OMException {
-        // Overridden in ChildNode
-        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
-                               DOMMessageFormatter.formatMessage(
-                                       DOMMessageFormatter.DOM_DOMAIN,
-                                       DOMException.HIERARCHY_REQUEST_ERR, null));
-
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.axis2.om.OMNode#insertSiblingBefore
-     * (org.apache.axis2.om.OMNode)
-     */
-    public void insertSiblingBefore(OMNode sibling) throws OMException {
-        // Overridden in ChildNode
-        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
-                               DOMMessageFormatter.formatMessage(
-                                       DOMMessageFormatter.DOM_DOMAIN,
-                                       DOMException.HIERARCHY_REQUEST_ERR, null));
-
-    }
-
-    /** Default behavior returns null, overriden in ChildNode. */
-    public OMNode getPreviousOMSibling() {
-        return null;
-    }
-
-    /** Default behavior returns null, overriden in ChildNode. */
-    public OMNode getNextOMSibling() {
-        return null;
-    }
-
-    public OMNode getNextOMSiblingIfAvailable() {
-        return null;
-    }
-
-    public void setPreviousOMSibling(OMNode previousSibling) {
-        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
-                               DOMMessageFormatter.formatMessage(
-                                       DOMMessageFormatter.DOM_DOMAIN,
-                                       DOMException.HIERARCHY_REQUEST_ERR, null));
-    }
-
-    public void setNextOMSibling(OMNode previousSibling) {
-        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
-                               DOMMessageFormatter.formatMessage(
-                                       DOMMessageFormatter.DOM_DOMAIN,
-                                       DOMException.HIERARCHY_REQUEST_ERR, null));
-    }
-
-    /** Builds next element. */
-    public void build() {
-        while (!done)
-            this.builder.next();
-    }
-
-    /**
-     * Parses this node and builds the object structure in memory. AXIOM supports two levels of
-     * deffered building. First is deffered building of AXIOM using StAX. Second level is the deffered
-     * building of attachments. AXIOM reads in the attachements from the stream only when user asks by
-     * calling getDataHandler(). build() method builds the OM without the attachments. buildAll()
-     * builds the OM together with attachement data. This becomes handy when user wants to free the
-     * input stream.
-     */
-    public void buildWithAttachments() {
-        if (!this.done) {
-            this.build();
-        }
-    }
-
-    public void close(boolean build) {
-        if (build) {
-            this.build();
-        }
-        this.done = true;
-        
-        // If this is a StAXBuilder, close it.
-        if (builder instanceof StAXBuilder &&
-            !((StAXBuilder) builder).isClosed()) {
-            ((StAXBuilder) builder).releaseParserOnClose(true);
-            ((StAXBuilder) builder).close();
-        }
-    }
-    
-    /**
-     * Sets the owner document.
-     *
-     * @param document
-     */
-    protected void setOwnerDocument(DocumentImpl document) {
-        this.ownerNode = document;
-        this.isOwned(true);
-    }
-
-    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
-        serialize(xmlWriter, true);
-    }
-
-    public void serializeAndConsume(XMLStreamWriter xmlWriter) throws XMLStreamException {
-        serialize(xmlWriter, false);
-    }
-
-    public void serialize(XMLStreamWriter xmlWriter, boolean cache) throws XMLStreamException {
-        MTOMXMLStreamWriter writer = xmlWriter instanceof MTOMXMLStreamWriter ?
-                (MTOMXMLStreamWriter) xmlWriter : 
-                    new MTOMXMLStreamWriter(xmlWriter);
-        internalSerialize(writer, cache);
-        writer.flush();
-    }
-
-    public OMNode detach() {
-        throw new OMException(
-                "Elements that doesn't have a parent can not be detached");
-    }
-
     /*
      * DOM-Level 3 methods
      */
@@ -544,17 +332,17 @@ public abstract class NodeImpl implement
         return this == node;
     }
 
-    public String lookupPrefix(String arg0) {
+    public String lookupPrefix(String namespaceURI) {
         // TODO TODO
         throw new UnsupportedOperationException("TODO");
     }
 
-    public boolean isDefaultNamespace(String arg0) {
+    public boolean isDefaultNamespace(String namespaceURI) {
         // TODO TODO
         throw new UnsupportedOperationException("TODO");
     }
 
-    public String lookupNamespaceURI(String arg0) {
+    public String lookupNamespaceURI(String prefix) {
         // TODO TODO
         throw new UnsupportedOperationException("TODO");
     }
@@ -736,7 +524,7 @@ public abstract class NodeImpl implement
         return equal;
     }
 
-    public Object getFeature(String arg0, String arg1) {
+    public Object getFeature(String feature, String version) {
         // TODO TODO
         throw new UnsupportedOperationException("TODO");
     }
@@ -770,105 +558,8 @@ public abstract class NodeImpl implement
         return null;
     }
 
-    public void serialize(OutputStream output) throws XMLStreamException {
-        XMLStreamWriter xmlStreamWriter = StAXUtils.createXMLStreamWriter(output);
-        try {
-            serialize(xmlStreamWriter);
-        } finally {
-            xmlStreamWriter.close();
-        }
-    }
-
-    public void serialize(Writer writer) throws XMLStreamException {
-        XMLStreamWriter xmlStreamWriter = StAXUtils.createXMLStreamWriter(writer);
-        try {
-            serialize(xmlStreamWriter);
-        } finally {
-            xmlStreamWriter.close();
-        }
-    }
-
-    public void serializeAndConsume(OutputStream output)
-            throws XMLStreamException {
-        XMLStreamWriter xmlStreamWriter = StAXUtils.createXMLStreamWriter(output);
-        try {
-            serializeAndConsume(xmlStreamWriter);
-        } finally {
-            xmlStreamWriter.close();
-        }
-    }
-
-    public void serializeAndConsume(Writer writer) throws XMLStreamException {
-        XMLStreamWriter xmlStreamWriter = StAXUtils.createXMLStreamWriter(writer);
-        try {
-            serializeAndConsume(xmlStreamWriter);
-        } finally {
-            xmlStreamWriter.close();
-        }
-    }
-
-    public void serialize(OutputStream output, OMOutputFormat format)
-            throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format, true);
-        try {
-            internalSerialize(writer, true);
-            // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter
-            writer.flush();
-        } finally {
-            writer.close();
-        }
-    }
-
-    public void serialize(Writer writer2, OMOutputFormat format)
-            throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(StAXUtils
-                .createXMLStreamWriter(writer2));
-        writer.setOutputFormat(format);
-        try {
-            internalSerialize(writer, true);
-            // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter
-            writer.flush();
-        } finally {
-            writer.close();
-        }
-    }
-
-    public void serializeAndConsume(OutputStream output, OMOutputFormat format)
-            throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format, false);
-        try {
-            internalSerialize(writer, false);
-            // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter
-            writer.flush();
-        } finally {
-            writer.close();
-        }
-    }
-
-    public void serializeAndConsume(Writer writer2, OMOutputFormat format)
-            throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(StAXUtils
-                .createXMLStreamWriter(writer2));
-        try {
-            writer.setOutputFormat(format);
-            // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter
-            internalSerialize(writer, false);
-            writer.flush();
-        } finally {
-            writer.close();
-        }
-    }
-
     /** Returns the <code>OMFactory</code> that created this node */
     public OMFactory getOMFactory() {
         return this.factory;
     }
-
-    public void internalSerialize(XMLStreamWriter writer) throws XMLStreamException {
-        internalSerialize(writer, true);
-    }
-
-    public void internalSerializeAndConsume(XMLStreamWriter writer) throws XMLStreamException {
-        internalSerialize(writer, false);
-    }
 }

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/OMDOMException.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/OMDOMException.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/OMDOMException.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/OMDOMException.java Mon Apr 23 10:26:45 2012
@@ -30,16 +30,16 @@ public class OMDOMException extends OMEx
         super();
     }
 
-    public OMDOMException(String arg0) {
-        super(arg0);
+    public OMDOMException(String msg) {
+        super(msg);
     }
 
-    public OMDOMException(Throwable arg0) {
-        super(arg0);
+    public OMDOMException(Throwable cause) {
+        super(cause);
     }
 
-    public OMDOMException(String arg0, Throwable arg1) {
-        super(arg0, arg1);
+    public OMDOMException(String msg, Throwable cause) {
+        super(msg, cause);
     }
 
 }

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java Mon Apr 23 10:26:45 2012
@@ -26,16 +26,18 @@ import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
 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.OMXMLParserWrapper;
+import org.apache.axiom.om.OMXMLStreamReaderConfiguration;
 import org.apache.axiom.om.impl.OMContainerEx;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.om.impl.common.OMChildrenLocalNameIterator;
 import org.apache.axiom.om.impl.common.OMChildrenNamespaceIterator;
 import org.apache.axiom.om.impl.common.OMChildrenQNameIterator;
+import org.apache.axiom.om.impl.common.OMContainerHelper;
 import org.apache.axiom.om.impl.common.OMDescendantsIterator;
-import org.apache.axiom.om.impl.common.OMStAXWrapper;
 import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
 import org.apache.axiom.om.impl.jaxp.OMSource;
 import org.apache.axiom.om.impl.traverse.OMChildrenIterator;
@@ -76,21 +78,16 @@ public abstract class ParentNode extends
 
     public void addChild(OMNode omNode) {
         if (omNode.getOMFactory() instanceof OMDOMFactory) {
-            Node domNode = (Node) omNode;
-            if (this.ownerNode != null && !domNode.getOwnerDocument().equals(this.ownerNode)) {
-                this.appendChild(this.ownerNode.importNode(domNode, true));
-            } else {
-                this.appendChild(domNode);
-            }
+            insertBefore((Node)omNode, null, false);
         } else {
             addChild(importNode(omNode));
         }
-
     }
 
     public void buildNext() {
-        if (!this.done)
+        if (builder != null) {
             builder.next();
+        }
     }
 
     public Iterator getChildren() {
@@ -145,11 +142,11 @@ public abstract class ParentNode extends
         while ((firstChild == null) && !done) {
             buildNext();
         }
-        return firstChild;
+        return (OMNode)firstChild;
     }
 
     public OMNode getFirstOMChildIfAvailable() {
-        return firstChild;
+        return (OMNode)firstChild;
     }
 
     public void setFirstChild(OMNode omNode) {
@@ -205,35 +202,27 @@ public abstract class ParentNode extends
      * last child.
      */
     public Node insertBefore(Node newChild, Node refChild) throws DOMException {
-
+        return insertBefore(newChild, refChild, true);
+    }
+    
+    private Node insertBefore(Node newChild, Node refChild, boolean useDomSemantics) {
         ChildNode newDomChild = (ChildNode) newChild;
         ChildNode refDomChild = (ChildNode) refChild;
 
-        if (this == newChild || !isAncestor(newChild)) {
+        if (useDomSemantics) {
+            checkSameOwnerDocument(newDomChild);
+        }
+
+        if (isAncestorOrSelf(newChild)) {
             throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                                    DOMMessageFormatter.formatMessage(
                                            DOMMessageFormatter.DOM_DOMAIN,
                                            DOMException.HIERARCHY_REQUEST_ERR, null));
         }
 
-        if (newDomChild.parentNode != null && newDomChild.ownerNode == this.ownerNode) {
+        if (newDomChild.parentNode() != null) {
             //If the newChild is already in the tree remove it
-            newDomChild.parentNode.removeChild(newDomChild);
-        }
-
-        if (!(this instanceof Document)
-                && !(this.ownerNode == newDomChild.getOwnerDocument())) {
-            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
-                                   DOMMessageFormatter.formatMessage(
-                                           DOMMessageFormatter.DOM_DOMAIN,
-                                           DOMException.WRONG_DOCUMENT_ERR, null));
-        }
-
-        if (this.isReadonly()) {
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   DOMMessageFormatter.formatMessage(
-                                           DOMMessageFormatter.DOM_DOMAIN,
-                                           DOMException.NO_MODIFICATION_ALLOWED_ERR, null));
+            newDomChild.parentNode().removeChild(newDomChild);
         }
 
         if (this instanceof Document) {
@@ -245,8 +234,8 @@ public abstract class ParentNode extends
                                                    DOMMessageFormatter.DOM_DOMAIN,
                                                    DOMException.HIERARCHY_REQUEST_ERR, null));
                 }
-                if (newDomChild.parentNode == null) {
-                    newDomChild.parentNode = this;
+                if (newDomChild.parentNode() == null) {
+                    newDomChild.setParent(this, useDomSemantics);
                 }
             } else if (!(newDomChild instanceof CommentImpl
                     || newDomChild instanceof ProcessingInstructionImpl
@@ -265,17 +254,16 @@ public abstract class ParentNode extends
                 this.lastChild = newDomChild;
                 this.firstChild = newDomChild;
                 this.firstChild.isFirstChild(true);
-                newDomChild.setParent(this);
+                newDomChild.setParent(this, useDomSemantics);
             } else {
                 this.lastChild.nextSibling = newDomChild;
                 newDomChild.previousSibling = this.lastChild;
                 this.lastChild = newDomChild;
                 this.lastChild.nextSibling = null;
             }
-            if (newDomChild.parentNode == null) {
-                newDomChild.parentNode = this;
+            if (newDomChild.parentNode() == null) {
+                newDomChild.setParent(this, useDomSemantics);
             }
-            return newChild;
         } else {
             Iterator children = this.getChildren();
             boolean found = false;
@@ -294,7 +282,7 @@ public abstract class ParentNode extends
                             
                             ChildNode child = docFrag.firstChild;
                             while (child != null) {
-                                child.parentNode = this;
+                                child.setParent(this, useDomSemantics);
                                 child = child.nextSibling;
                             }
                             
@@ -329,7 +317,7 @@ public abstract class ParentNode extends
 
                             ChildNode child = docFrag.firstChild;
                             while (child != null) {
-                                child.parentNode = this;
+                                child.setParent(this, useDomSemantics);
                                 child = child.nextSibling;
                             }
                             
@@ -363,12 +351,17 @@ public abstract class ParentNode extends
                                                DOMException.NOT_FOUND_ERR, null));
             }
 
-            if (newDomChild.parentNode == null) {
-                newDomChild.parentNode = this;
+            if (newDomChild.parentNode() == null) {
+                newDomChild.setParent(this, useDomSemantics);
             }
 
-            return newChild;
         }
+        
+        if (!newDomChild.isComplete() && !(newDomChild instanceof OMSourcedElement)) {
+            setComplete(false);
+        }
+        
+        return newChild;
     }
 
     /** Replaces the oldChild with the newChild. */
@@ -380,30 +373,14 @@ public abstract class ParentNode extends
             return this.removeChild(oldChild);
         }
 
-        if (this == newChild || !isAncestor(newChild)) {
+        if (isAncestorOrSelf(newChild)) {
             throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                                    DOMMessageFormatter.formatMessage(
                                            DOMMessageFormatter.DOM_DOMAIN,
                                            DOMException.HIERARCHY_REQUEST_ERR, null));
         }
 
-        if (newDomChild != null &&
-                //This is the case where this is an Element in the document
-                (this.ownerNode != null && !this.ownerNode.equals(newDomChild.ownerNode)) ||
-                //This is the case where this is the Document itself
-                (this.ownerNode == null && !this.equals(newDomChild.ownerNode))) {
-            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
-                                   DOMMessageFormatter.formatMessage(
-                                           DOMMessageFormatter.DOM_DOMAIN,
-                                           DOMException.WRONG_DOCUMENT_ERR, null));
-        }
-
-        if (this.isReadonly()) {
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   DOMMessageFormatter.formatMessage(
-                                           DOMMessageFormatter.DOM_DOMAIN,
-                                           DOMException.NO_MODIFICATION_ALLOWED_ERR, null));
-        }
+        checkSameOwnerDocument(newDomChild);
 
         Iterator children = this.getChildren();
         boolean found = false;
@@ -418,7 +395,7 @@ public abstract class ParentNode extends
                     
                     //set the parent of all kids to me
                     while(child != null) {
-                        child.parentNode = this;
+                        child.setParent(this, true);
                         child = child.nextSibling;
                     }
 
@@ -433,7 +410,7 @@ public abstract class ParentNode extends
                         }
 
                         //Cleanup the current first child
-                        this.firstChild.parentNode = null;
+                        this.firstChild.setParent(null, true);
                         this.firstChild.nextSibling = null;
 
                         //Set the new first child
@@ -455,14 +432,14 @@ public abstract class ParentNode extends
 
                     }
 
-                    newDomChild.parentNode = this;
+                    newDomChild.setParent(this, true);
                 }
                 found = true;
 
                 // remove the old child's references to this tree
                 oldDomChild.nextSibling = null;
                 oldDomChild.previousSibling = null;
-                oldDomChild.parentNode = null;
+                oldDomChild.setParent(null, true);
             }
         }
 
@@ -477,77 +454,41 @@ public abstract class ParentNode extends
 
     /** Removes the given child from the DOM Tree. */
     public Node removeChild(Node oldChild) throws DOMException {
-        // Check if this node is readonly
-        if (this.isReadonly()) {
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   DOMMessageFormatter.formatMessage(
-                                           DOMMessageFormatter.DOM_DOMAIN,
-                                           DOMException.NO_MODIFICATION_ALLOWED_ERR, null));
-        }
-
-        // Check if the Child is there
-        Iterator children = this.getChildren();
-        boolean childFound = false;
-        while (!childFound && children.hasNext()) {
-            ChildNode tempNode = (ChildNode) children.next();
-            if (tempNode.equals(oldChild)) {
-
-                if (this.firstChild == tempNode) {
-                    // If this is the first child
-                    ChildNode nextSib = tempNode.nextSibling;
-                    this.firstChild = nextSib;
-                    if (nextSib == null) {
-                        this.lastChild = null;
-                    } else {
-                        nextSib.previousSibling = null;
-                    }
-                    tempNode.parentNode = null;
-                    tempNode.nextSibling = null;
-                } else if (this.lastChild == tempNode) {
-                    // not the first child, but the last child
-                    ChildNode prevSib = tempNode.previousSibling;
-                    this.lastChild = prevSib;
-                    prevSib.nextSibling = null;
-                    tempNode.parentNode = null;
-                    tempNode.previousSibling = null;
-                } else {
-
-                    ChildNode oldDomChild = (ChildNode) oldChild;
-                    ChildNode privChild = oldDomChild.previousSibling;
-
-                    privChild.nextSibling = oldDomChild.nextSibling;
-                    oldDomChild.nextSibling.previousSibling = privChild;
-
-                    // Remove old child's references to this tree
-                    oldDomChild.nextSibling = null;
-                    oldDomChild.previousSibling = null;
-                }
-                // Child found
-                childFound = true;
-            }
-        }
-
-        if (!childFound)
+        if (oldChild.getParentNode() == this) {
+            ((ChildNode)oldChild).detach(true);
+            return oldChild;
+        } else {
             throw new DOMException(DOMException.NOT_FOUND_ERR,
                                    DOMMessageFormatter.formatMessage(
                                            DOMMessageFormatter.DOM_DOMAIN, DOMException.NOT_FOUND_ERR,
                                            null));
-
-        return oldChild;
+        }
     }
 
-    private boolean isAncestor(Node newNode) {
-
-        // TODO isAncestor
-        return true;
+    /**
+     * Checks if the given node is an ancestor (or identical) to this node.
+     * 
+     * @param node
+     *            the node to check
+     * @return <code>true</code> if the node is an ancestor or indentical to this node
+     */
+    private boolean isAncestorOrSelf(Node node) {
+        Node currentNode = this;
+        do {
+            if (currentNode == node) {
+                return true;
+            }
+            currentNode = currentNode.getParentNode();
+        } while (currentNode != null);
+        return false;
     }
 
     public Node cloneNode(boolean deep) {
 
         ParentNode newnode = (ParentNode) super.cloneNode(deep);
 
-        // set owner document
-        newnode.ownerNode = ownerNode;
+        // set parent and owner document
+        newnode.setParent(null, true);
 
         // Need to break the association w/ original kids
         newnode.firstChild = null;
@@ -578,7 +519,7 @@ public abstract class ParentNode extends
                                                           childElement.getXMLStreamReader()))
                         .getDocumentElement();
                 newElement.build();
-                return (OMNode) this.ownerNode.importNode((Element) newElement,
+                return (OMNode)ownerDocument().importNode((Element) newElement,
                                                           true);
             }
             case (OMNode.TEXT_NODE): {
@@ -688,18 +629,35 @@ public abstract class ParentNode extends
     }
 
     public XMLStreamReader getXMLStreamReader(boolean cache) {
-        if ((builder == null) && !cache) {
-            throw new UnsupportedOperationException(
-                    "This element was not created in a manner to be switched");
-        }
-        if (builder != null && builder.isCompleted() && !cache) {
-            throw new UnsupportedOperationException(
-                    "The parser is already consumed!");
-        }
-        return new OMStAXWrapper(builder, this, cache);
+        return OMContainerHelper.getXMLStreamReader(this, cache);
+    }
+    
+    public XMLStreamReader getXMLStreamReader(boolean cache, OMXMLStreamReaderConfiguration configuration) {
+        return OMContainerHelper.getXMLStreamReader(this, cache, configuration);
     }
 
     public SAXSource getSAXSource(boolean cache) {
         return new OMSource(this);
     }
+
+    void notifyChildComplete() {
+        if (!this.done && builder == null) {
+            Iterator iterator = getChildren();
+            while (iterator.hasNext()) {
+                OMNode node = (OMNode) iterator.next();
+                if (!node.isComplete()) {
+                    return;
+                }
+            }
+            this.setComplete(true);
+        }
+    }
+
+    void normalize(DOMConfigurationImpl config) {
+        OMNode child = getFirstOMChild();
+        while (child != null) {
+            ((NodeImpl)child).normalize(config);
+            child = child.getNextOMSibling();
+        }
+    }
 }

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ProcessingInstructionImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ProcessingInstructionImpl.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ProcessingInstructionImpl.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ProcessingInstructionImpl.java Mon Apr 23 10:26:45 2012
@@ -26,11 +26,12 @@ import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMProcessingInstruction;
+import org.apache.axiom.om.impl.OMNodeEx;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Node;
 import org.w3c.dom.ProcessingInstruction;
 
-public class ProcessingInstructionImpl extends ChildNode implements ProcessingInstruction, OMProcessingInstruction {
+public class ProcessingInstructionImpl extends ChildNode implements ProcessingInstruction, OMProcessingInstruction, OMNodeEx {
     private String target;
     private String value;
 
@@ -78,14 +79,7 @@ public class ProcessingInstructionImpl e
     }
     
     public void setData(String data) throws DOMException {
-        if (!isReadonly()) {
-            value = data;
-        } else {
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                                   DOMMessageFormatter.formatMessage(
-                                           DOMMessageFormatter.DOM_DOMAIN,
-                                           DOMException.NO_MODIFICATION_ALLOWED_ERR, null));
-        }
+        value = data;
     }
     
     public String getNodeName() {

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextImpl.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextImpl.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextImpl.java Mon Apr 23 10:26:45 2012
@@ -112,7 +112,7 @@ public class TextImpl extends TextNodeIm
 
     public Node cloneNode(boolean deep) {
         TextImpl textImpl = new TextImpl(this.textValue, this.factory);
-        textImpl.setOwnerDocument(this.ownerNode);
+        textImpl.setOwnerDocument(ownerDocument());
         return textImpl;
     }
 }

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java Mon Apr 23 10:26:45 2012
@@ -28,7 +28,7 @@ import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.OMXMLParserWrapper;
-import org.apache.axiom.om.impl.builder.XOPBuilder;
+import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.util.UIDGenerator;
 import org.apache.axiom.util.base64.Base64Utils;
 import org.apache.axiom.util.stax.XMLStreamWriterUtils;
@@ -221,14 +221,10 @@ public abstract class TextNodeImpl exten
 
     public TextNodeImpl(OMContainer parent, QName text, int nodeType,
                         OMFactory factory) {
-        this(((ElementImpl) parent).ownerNode, factory);
-        if (text != null) {
-            this.textNS =
-                    ((ElementImpl) parent).findNamespace(text.getNamespaceURI(), text.getPrefix());
-        } else {
-
-        }
-        this.textValue = (text == null) ? "" : text.getLocalPart();
+        this(((ElementImpl) parent).ownerDocument(), factory);
+        this.textNS =
+                ((ElementImpl) parent).handleNamespace(text.getNamespaceURI(), text.getPrefix());
+        this.textValue = textNS == null ? text.getLocalPart() : textNS.getPrefix() + ":" + text.getLocalPart();
         this.done = true;
     }
 
@@ -241,12 +237,6 @@ public abstract class TextNodeImpl exten
      * node has no data.
      */
     public Text splitText(int offset) throws DOMException {
-        if (this.isReadonly()) {
-            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
-                    DOMMessageFormatter.formatMessage(
-                            DOMMessageFormatter.DOM_DOMAIN,
-                            DOMException.NO_MODIFICATION_ALLOWED_ERR, null));
-        }
         if (offset < 0 || offset > this.textValue.length()) {
             throw new DOMException(DOMException.INDEX_SIZE_ERR,
                     DOMMessageFormatter.formatMessage(
@@ -259,8 +249,9 @@ public abstract class TextNodeImpl exten
         TextImpl newText = (TextImpl) this.getOwnerDocument().createTextNode(
                 newValue);
 
-        if (this.parentNode != null) {
-            newText.setParent(this.parentNode);
+        ParentNode parentNode = parentNode();
+        if (parentNode != null) {
+            newText.setParent(parentNode);
         }
 
         this.insertSiblingAfter(newText);
@@ -319,9 +310,7 @@ public abstract class TextNodeImpl exten
     }
 
     public String getText() {
-        if (this.textNS != null) {
-            return getTextString();
-        } else if (this.charArray != null || this.textValue != null) {
+        if (this.charArray != null || this.textValue != null) {
             return getTextFromProperPlace();
         } else {
             try {
@@ -405,11 +394,7 @@ public abstract class TextNodeImpl exten
         } else {
 
             if (dataHandlerObject == null) {
-                if (contentID == null) {
-                    throw new RuntimeException("ContentID is null");
-                }
-                dataHandlerObject = ((XOPBuilder) builder)
-                        .getDataHandler(contentID);
+                throw new OMException("No DataHandler available");
             } else if (dataHandlerObject instanceof DataHandlerProvider) {
                 try {
                     dataHandlerObject = ((DataHandlerProvider) dataHandlerObject).getDataHandler();
@@ -464,10 +449,28 @@ public abstract class TextNodeImpl exten
     * DOM-Level 3 methods
     */
 
-
     public String getWholeText() {
-        // TODO TODO
-        throw new UnsupportedOperationException("TODO");
+        // Locate the first sibling that we need to include in the concatenation
+        OMNode sibling = this;
+        OMNode previousSibling;
+        while ((previousSibling = sibling.getPreviousOMSibling()) instanceof OMText) {
+            sibling = previousSibling;
+        }
+        
+        String text = ((OMText)sibling).getText();
+        StringBuffer buffer = null;
+        while ((sibling = sibling.getNextOMSibling()) instanceof OMText) {
+            if (buffer == null) {
+                buffer = new StringBuffer(text);
+            }
+            buffer.append(((OMText)sibling).getText());
+        }
+
+        if (buffer != null) {
+            return buffer.toString();
+        } else {
+            return text;
+        }
     }
 
     public boolean isElementContentWhitespace() {
@@ -475,7 +478,7 @@ public abstract class TextNodeImpl exten
         throw new UnsupportedOperationException("TODO");
     }
 
-    public Text replaceWholeText(String arg0) throws DOMException {
+    public Text replaceWholeText(String content) throws DOMException {
         // TODO TODO
         throw new UnsupportedOperationException("TODO");
     }

Modified: webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java?rev=1329164&r1=1329163&r2=1329164&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java (original)
+++ webservices/axiom/branches/AXIOM-201/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java Mon Apr 23 10:26:45 2012
@@ -41,7 +41,6 @@ import org.apache.axiom.om.impl.common.O
 import org.apache.axiom.om.impl.dom.AttrImpl;
 import org.apache.axiom.om.impl.dom.CDATASectionImpl;
 import org.apache.axiom.om.impl.dom.CommentImpl;
-import org.apache.axiom.om.impl.dom.DocumentFragmentImpl;
 import org.apache.axiom.om.impl.dom.DocumentImpl;
 import org.apache.axiom.om.impl.dom.DocumentTypeImpl;
 import org.apache.axiom.om.impl.dom.ElementImpl;
@@ -50,36 +49,17 @@ import org.apache.axiom.om.impl.dom.Pare
 import org.apache.axiom.om.impl.dom.ProcessingInstructionImpl;
 import org.apache.axiom.om.impl.dom.TextImpl;
 import org.apache.axiom.om.impl.dom.TextNodeImpl;
-import org.w3c.dom.Node;
+import org.apache.axiom.om.impl.util.OMSerializerUtil;
 
 import javax.xml.namespace.QName;
 
 /**
  * OM factory implementation for DOOM. It creates nodes that implement
  * DOM as defined by the interfaces in {@link org.w3c.dom}.
- * <p>
- * Since DOM requires every node to have an owner document even if it has not yet
- * been added to a tree, this factory internally maintains a reference to a
- * {@link DocumentImpl} instance. The document can be set explicitly using the
- * {@link #OMDOMFactory(DocumentImpl)} constructor or the {@link #setDocument(DocumentImpl)}
- * method. If none is set, it will be implicitly created when the first node is created.
- * All nodes created by this factory will have this {@link DocumentImpl} instance as owner
- * document.
- * <p>
- * This has several important consequences:
- * <ul>
- *   <li>The same instance of this class should not be used to parse or construct
- *       multiple documents unless {@link #setDocument(DocumentImpl)} is used
- *       to reset the {@link DocumentImpl} instance before processing the next document.</li>
- *   <li>Instances of this class are not thread safe and using a single instance concurrently
- *       will lead to undefined results.</li>
- * </ul>
  */
 public class OMDOMFactory implements OMFactory {
     private final OMDOMMetaFactory metaFactory;
 
-    protected DocumentImpl document;
-
     public OMDOMFactory(OMDOMMetaFactory metaFactory) {
         this.metaFactory = metaFactory;
     }
@@ -88,97 +68,32 @@ public class OMDOMFactory implements OMF
         this(new OMDOMMetaFactory());
     }
 
-    public OMDOMFactory(DocumentImpl doc) {
-        this(new OMDOMMetaFactory());
-        this.document = doc;
-    }
-
     public OMMetaFactory getMetaFactory() {
         return metaFactory;
     }
 
     public OMDocument createOMDocument() {
-        if (this.document == null)
-            this.document = new DocumentImpl(this);
-
-        return this.document;
-    }
-
-    /**
-     * Configure this factory to use the given document. Use with care.
-     *
-     * @param document
-     */
-    public void setDocument(DocumentImpl document) {
-        this.document = document;
+        return new DocumentImpl(this);
     }
 
     public OMElement createOMElement(String localName, OMNamespace ns) {
-        return new ElementImpl((DocumentImpl) this.createOMDocument(),
+        return new ElementImpl(null,
                                localName, (OMNamespaceImpl) ns, this);
     }
 
     public OMElement createOMElement(String localName, OMNamespace ns,
                                      OMContainer parent) throws OMDOMException {
         if (parent == null) {
-            return new ElementImpl((DocumentImpl) this.createOMDocument(),
-                               localName, (OMNamespaceImpl) ns, this);
-        }
-
-        switch (((ParentNode) parent).getNodeType()) {
-            case Node.ELEMENT_NODE: { // We are adding a new child to an elem
-                ElementImpl parentElem = (ElementImpl) parent;
-                ElementImpl elem = new ElementImpl((DocumentImpl) parentElem
-                        .getOwnerDocument(), localName, (OMNamespaceImpl) ns, this);
-                parentElem.appendChild(elem);
-                return elem;
-            }
-            case Node.DOCUMENT_NODE: {
-                DocumentImpl docImpl = (DocumentImpl) parent;
-                ElementImpl elem = new ElementImpl(docImpl, localName,
-                                       (OMNamespaceImpl) ns, this);
-                docImpl.appendChild(elem);
-                return elem;
-            }
-            case Node.DOCUMENT_FRAGMENT_NODE:
-                DocumentFragmentImpl docFragImpl = (DocumentFragmentImpl) parent;
-                return new ElementImpl((DocumentImpl) docFragImpl
-                        .getOwnerDocument(), localName, (OMNamespaceImpl) ns, this);
-            default:
-                throw new OMDOMException(
-                        "The parent container can only be an ELEMENT, DOCUMENT " +
-                                "or a DOCUMENT FRAGMENT");
+            return createOMElement(localName, ns);
+        } else {
+            return new ElementImpl((ParentNode) parent, localName, (OMNamespaceImpl) ns, this);
         }
     }
 
     /** Creates an OMElement with the builder. */
     public OMElement createOMElement(String localName, OMNamespace ns,
                                      OMContainer parent, OMXMLParserWrapper builder) {
-        switch (((ParentNode) parent).getNodeType()) {
-            case Node.ELEMENT_NODE: // We are adding a new child to an elem
-                ElementImpl parentElem = (ElementImpl) parent;
-                ElementImpl elem = new ElementImpl((DocumentImpl) parentElem
-                        .getOwnerDocument(), localName, (OMNamespaceImpl) ns,
-                                             builder, this);
-                parentElem.appendChild(elem);
-                return elem;
-            case Node.DOCUMENT_NODE:
-                DocumentImpl docImpl = (DocumentImpl) parent;
-                ElementImpl elem2 = new ElementImpl(docImpl, localName,
-                                                    (OMNamespaceImpl) ns, builder, this);
-                docImpl.appendChild(elem2);
-                return elem2;
-
-            case Node.DOCUMENT_FRAGMENT_NODE:
-                DocumentFragmentImpl docFragImpl = (DocumentFragmentImpl) parent;
-                return new ElementImpl((DocumentImpl) docFragImpl
-                        .getOwnerDocument(), localName, (OMNamespaceImpl) ns,
-                                             builder, this);
-            default:
-                throw new OMDOMException(
-                        "The parent container can only be an ELEMENT, DOCUMENT " +
-                                "or a DOCUMENT FRAGMENT");
-        }
+        return new ElementImpl((ParentNode) parent, localName, (OMNamespaceImpl) ns, builder, this);
     }
 
     /* (non-Javadoc)
@@ -266,27 +181,31 @@ public class OMDOMFactory implements OMF
     }
 
     public OMText createOMText(OMContainer parent, QName text) {
-        return new TextImpl(parent, text, this);
+        return createOMText(parent, text, OMNode.TEXT_NODE);
     }
 
     public OMText createOMText(OMContainer parent, QName text, int type) {
-        return new TextImpl(parent, text, type, this);
+        TextImpl txt = new TextImpl(parent, text, type, this);
+        parent.addChild(txt);
+        return txt;
     }
 
     public OMText createOMText(OMContainer parent, String text, int type) {
-        if (parent instanceof DocumentImpl) {
+        if (parent == null) {
+            return createOMText(text, type);
+        } else if (parent instanceof DocumentImpl) {
             throw new OMHierarchyException(
                     "DOM doesn't support text nodes as children of a document");
-        }
-        DocumentImpl ownerDocument = (DocumentImpl)((ElementImpl)parent).getOwnerDocument(); 
-        TextNodeImpl txt;
-        if (type == OMNode.CDATA_SECTION_NODE) {
-            txt = new CDATASectionImpl(ownerDocument, text, this);
         } else {
-            txt = new TextImpl(ownerDocument, text, type, this);
+            TextNodeImpl txt;
+            if (type == OMNode.CDATA_SECTION_NODE) {
+                txt = new CDATASectionImpl(null, text, this);
+            } else {
+                txt = new TextImpl(null, text, type, this);
+            }
+            parent.addChild(txt);
+            return txt;
         }
-        parent.addChild(txt);
-        return txt;
     }
     
     
@@ -295,10 +214,8 @@ public class OMDOMFactory implements OMF
     }
 
     public OMText createOMText(OMContainer parent, char[] charArary, int type) {
-        ElementImpl parentElem = (ElementImpl) parent;
-        TextImpl txt = new TextImpl((DocumentImpl) parentElem
-                .getOwnerDocument(), charArary, this);
-        parentElem.addChild(txt);
+        TextImpl txt = new TextImpl(null, charArary, this);
+        parent.addChild(txt);
         return txt;
     }
 
@@ -308,7 +225,7 @@ public class OMDOMFactory implements OMF
      * @see org.apache.axiom.om.OMFactory#createOMText(String)
      */
     public OMText createOMText(String s) {
-        return new TextImpl(this.document, s, this);
+        return new TextImpl(null, s, this);
     }
 
     /**
@@ -317,11 +234,10 @@ public class OMDOMFactory implements OMF
      * @see org.apache.axiom.om.OMFactory#createOMText(String, int)
      */
     public OMText createOMText(String text, int type) {
-        switch (type) {
-            case OMNode.TEXT_NODE:
-                return new TextImpl(this.document, text, this);
-            default:
-                throw new OMDOMException("Only Text nodes are supported right now");
+        if (type == OMNode.CDATA_SECTION_NODE) {
+            return new CDATASectionImpl(null, text, this);
+        } else {
+            return new TextImpl(null, text, this);
         }
     }
 
@@ -332,7 +248,7 @@ public class OMDOMFactory implements OMF
      * @see org.apache.axiom.om.OMFactory#createOMText(String, String, boolean)
      */
     public OMText createOMText(String text, String mimeType, boolean optimize) {
-        return new TextImpl(this.document, text, mimeType, optimize, this);
+        return new TextImpl(null, text, mimeType, optimize, this);
     }
 
     /**
@@ -342,12 +258,12 @@ public class OMDOMFactory implements OMF
      * @see org.apache.axiom.om.OMFactory#createOMText(Object, boolean)
      */
     public OMText createOMText(Object dataHandler, boolean optimize) {
-        return new TextImpl(this.document, dataHandler, optimize, this);
+        return new TextImpl(null, dataHandler, optimize, this);
     }
 
     public OMText createOMText(String contentID, DataHandlerProvider dataHandlerProvider,
             boolean optimize) {
-        return new TextImpl(this.document, contentID, dataHandlerProvider, optimize, this);
+        return new TextImpl(null, contentID, dataHandlerProvider, optimize, this);
     }
 
     /**
@@ -358,8 +274,7 @@ public class OMDOMFactory implements OMF
      */
     public OMText createOMText(OMContainer parent, String s, String mimeType,
                                boolean optimize) {
-        TextImpl text = new TextImpl((DocumentImpl) ((ElementImpl) parent)
-                .getOwnerDocument(), s, mimeType, optimize, this);
+        TextImpl text = new TextImpl(null, s, mimeType, optimize, this);
         parent.addChild(text);
         return text;
     }
@@ -373,11 +288,19 @@ public class OMDOMFactory implements OMF
 
     public OMAttribute createOMAttribute(String localName, OMNamespace ns,
                                          String value) {
-        return new AttrImpl(this.getDocument(), localName, ns, value, this);
+        if (ns != null && ns.getPrefix() == null) {
+            String namespaceURI = ns.getNamespaceURI();
+            if (namespaceURI.length() == 0) {
+                ns = null;
+            } else {
+                ns = new OMNamespaceImpl(namespaceURI, OMSerializerUtil.getNextNSPrefix());
+            }
+        }
+        return new AttrImpl(null, localName, ns, value, this);
     }
 
     public OMDocType createOMDocType(OMContainer parent, String content) {
-        DocumentTypeImpl docType = new DocumentTypeImpl(this.getDocument(), this);
+        DocumentTypeImpl docType = new DocumentTypeImpl(null, this);
         docType.setValue(content);
         parent.addChild(docType);
         return docType;
@@ -386,7 +309,7 @@ public class OMDOMFactory implements OMF
     public OMProcessingInstruction createOMProcessingInstruction(
             OMContainer parent, String piTarget, String piData) {
         ProcessingInstructionImpl pi =
-            new ProcessingInstructionImpl(getDocumentFromParent(parent), piTarget, piData, this);
+            new ProcessingInstructionImpl(null, piTarget, piData, this);
         if (parent != null) {
             parent.addChild(pi);
         }
@@ -394,30 +317,14 @@ public class OMDOMFactory implements OMF
     }
 
     public OMComment createOMComment(OMContainer parent, String content) {
-        CommentImpl comment = new CommentImpl(getDocumentFromParent(parent), content, this);
+        CommentImpl comment = new CommentImpl(null, content, this);
         if (parent != null) {
             parent.addChild(comment);
         }
         return comment;
     }
 
-    public DocumentImpl getDocument() {
-        return (DocumentImpl) this.createOMDocument();
-    }
-
     public OMDocument createOMDocument(OMXMLParserWrapper builder) {
-        this.document = new DocumentImpl(builder, this);
-        return this.document;
-    }
-
-    private DocumentImpl getDocumentFromParent(OMContainer parent) {
-        if (parent == null) {
-            // TODO: this is really a hack; we should make OMDOMFactory stateless
-            return (DocumentImpl)createOMDocument();
-        } else if (parent instanceof DocumentImpl) {
-            return (DocumentImpl) parent;
-        } else {
-            return (DocumentImpl) ((ParentNode) parent).getOwnerDocument();
-        }
+        return new DocumentImpl(builder, this);
     }
 }