FileDocCategorySizeDatePackage
NodeImpl.javaAPI DocApache Axis 1.435176Sat Apr 22 18:57:28 BST 2006org.apache.axis.message

NodeImpl

public class NodeImpl extends Object implements Serializable, Node, Cloneable, Node
This is our implementation of the DOM node

Fields Summary
protected static Log
log
protected String
name
protected String
prefix
protected String
namespaceURI
protected transient Attributes
attributes
protected Document
document
protected NodeImpl
parent
protected ArrayList
children
protected CharacterData
textRep
protected boolean
_isDirty
private static final String
NULL_URI_NAME
Constructors Summary
public NodeImpl()
empty constructor


           
      
    
public NodeImpl(CharacterData text)
constructor which adopts the name and NS of the char data, and its text

param
text

        textRep = text;
        namespaceURI = text.getNamespaceURI();
        name = text.getLocalName();
    
Methods Summary
public org.w3c.dom.NodeappendChild(org.w3c.dom.Node newChild)
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.

param
newChild The node to add.If it is a DocumentFragment object, the entire contents of the document fragment are moved into the child list of this node
return
The node added.
throws
org.w3c.dom.DOMException HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors or this node itself.
WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

        if (newChild == null) {
            throw new DOMException
                    (DOMException.HIERARCHY_REQUEST_ERR,
                            "Can't append a null node.");
        }
        initializeChildren();
        // per DOM spec - must remove from tree. If newChild.parent == null,
        // detachNode() does nothing.  So this shouldn't hurt performace of
        // serializers.
        ((NodeImpl) newChild).detachNode();
        children.add(newChild);
        ((NodeImpl) newChild).parent = this;
        setDirty();
        return newChild;
    
public org.w3c.dom.NodecloneNode(boolean deep)
Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent; ( parentNode is null.).
Cloning an Element copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any text it contains unless it is a deep clone, since the text is contained in a child Text node. Cloning an Attribute directly, as opposed to be cloned as part of an Element cloning operation, returns a specified attribute ( specified is true). Cloning any other type of node simply returns a copy of this node.
Note that cloning an immutable subtree results in a mutable copy, but the children of an EntityReference clone are readonly . In addition, clones of unspecified Attr nodes are specified. And, cloning Document, DocumentType, Entity, and Notation nodes is implementation dependent.

param
deep If true, recursively clone the subtree under the specified node; if false, clone only the node itself (and its attributes, if it is an Element).
return
The duplicate node.

        return new NodeImpl(textRep);
    
protected org.w3c.dom.NamedNodeMapconvertAttrSAXtoDOM(org.xml.sax.Attributes saxAttr)
The internal representation of Attributes cannot help being changed It is because Attribute is not immutible Type, so if we keep out value and just return it in another form, the application may chnae it, which we cannot detect without some kind back track method (call back notifying the chnage.) I am not sure which approach is better.

        try {
            org.w3c.dom.Document doc = org.apache.axis.utils.XMLUtils.newDocument();
            AttributesImpl saxAttrs = (AttributesImpl) saxAttr;
            NamedNodeMap domAttributes = new NamedNodeMapImpl();
            for (int i = 0; i < saxAttrs.getLength(); i++) {
                String uri = saxAttrs.getURI(i);
                String qname = saxAttrs.getQName(i);
                String value = saxAttrs.getValue(i);
                if (uri != null && uri.trim().length() > 0) {
                    // filterring out the tricky method to differentiate the null namespace
                    // -ware case
                    if (NULL_URI_NAME.equals(uri)) {
                        uri = null;
                    }
                    Attr attr = doc.createAttributeNS(uri, qname);
                    attr.setValue(value);
                    domAttributes.setNamedItemNS(attr);
                } else {
                    Attr attr = doc.createAttribute(qname);
                    attr.setValue(value);
                    domAttributes.setNamedItem(attr);
                }
            }
            return domAttributes;
        } catch (Exception ex) {
            log.error(Messages.getMessage("saxToDomFailed00"),ex);

            return null;
        }
    
public voiddetachNode()
Removes this Node object from the tree. Once removed, this node can be garbage collected if there are no application references to it.

        setDirty();
        if (parent != null) {
            parent.removeChild(this);
            parent = null;
        }
    
public org.w3c.dom.NamedNodeMapgetAttributes()
A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

        // make first it is editable.
        makeAttributesEditable();
        return convertAttrSAXtoDOM(attributes);
    
public org.w3c.dom.NodeListgetChildNodes()
A NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes.

        if (children == null) {
            return NodeListImpl.EMPTY_NODELIST;
        } else {
            return new NodeListImpl(children);
        }
    
public org.w3c.dom.NodegetFirstChild()
The first child of this node. If there is no such node, this returns null.

        if (children != null && !children.isEmpty()) {
            return (Node) children.get(0);
        } else {
            return null;
        }
    
public org.w3c.dom.NodegetLastChild()
The last child of this node. If there is no such node, this returns null.

        if (children != null && !children.isEmpty()) {
            return (Node) children.get(children.size() - 1);
        } else {
            return null;
        }
    
public java.lang.StringgetLocalName()
Returns the local part of the qualified name of this node.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.

since
DOM Level 2

        return name;
    
public java.lang.StringgetNamespaceURI()
The namespace URI of this node, or null if it is unspecified.
This is not a computed value that is the result of a namespace lookup based on an examination of the namespace declarations in scope. It is merely the namespace URI given at creation time.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.Per the Namespaces in XML Specification an attribute does not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it simply has no namespace.

since
DOM Level 2

        return (namespaceURI);
    
public org.w3c.dom.NodegetNextSibling()
The node immediately following this node. If there is no such node, this returns null.

        SOAPElement parent = getParentElement();
        if (parent == null) {
            return null;
        }
        Iterator iter = parent.getChildElements();
        Node nextSibling = null;
        while (iter.hasNext()) {
            if (iter.next() == this) {
                if (iter.hasNext()) {
                    return (Node) iter.next();
                } else {
                    return null;
                }
            }
        }
        return nextSibling; // should be null.
    
public java.lang.StringgetNodeName()
The name of this node, depending on its type; see the table above.

        return (prefix != null && prefix.length() > 0) ?
                prefix + ":" + name : name;
    
public shortgetNodeType()
A code representing the type of the underlying object, as defined above.

        if (this.textRep != null) {
            if (textRep instanceof Comment) {
                return COMMENT_NODE;
            } else if (textRep instanceof CDATASection) {
                return CDATA_SECTION_NODE;
            } else {
                return TEXT_NODE;
            }
        } else if (false) {
            return DOCUMENT_FRAGMENT_NODE;
        } else if (false) {
            return Node.ELEMENT_NODE;
        } else { // most often but we cannot give prioeity now
            return Node.ELEMENT_NODE;
        }
    
public java.lang.StringgetNodeValue()
The value of this node, depending on its type; see the table above. When it is defined to be null, setting it has no effect.

throws
org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
throws
org.w3c.dom.DOMException DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in a DOMString variable on the implementation platform.

        if (textRep == null) {
            return null;
        } else {
            return textRep.getData();
        }
    
public org.w3c.dom.DocumentgetOwnerDocument()
The Document object associated with this node. This is also the Document object used to create new nodes. When this node is a Document or a DocumentType which is not used with any Document yet, this is null.

        if(document == null) {
            NodeImpl node = getParent();
            if (node != null) {
              return node.getOwnerDocument();
            }
        }
        return document;
    
protected org.apache.axis.message.NodeImplgetParent()
get the parent node

return
parent node

        return parent;
    
public javax.xml.soap.SOAPElementgetParentElement()
Returns the parent element of this Node object. This method can throw an UnsupportedOperationException if the tree is not kept in memory.

return
the SOAPElement object that is the parent of this Node object or null if this Node object is root
throws
UnsupportedOperationException if the whole tree is not kept in memory
see
#setParentElement(javax.xml.soap.SOAPElement) setParentElement(javax.xml.soap.SOAPElement)

        return (SOAPElement) getParent();
    
public org.w3c.dom.NodegetParentNode()
The parent of this node. All nodes, except Attr, Document, DocumentFragment, Entity, and Notation may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null.

        return (Node) getParent();
    
public java.lang.StringgetPrefix()
The namespace prefix of this node, or null if it is unspecified.
Note that setting this attribute, when permitted, changes the nodeName attribute, which holds the qualified name, as well as the tagName and name attributes of the Element and Attr interfaces, when applicable.
Note also that changing the prefix of an attribute that is known to have a default value, does not make a new attribute with the default value and the original prefix appear, since the namespaceURI and localName do not change.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.

throws
org.w3c.dom.DOMException INVALID_CHARACTER_ERR: Raised if the specified prefix contains an illegal character, per the XML 1.0 specification .
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
NAMESPACE_ERR: Raised if the specified prefix is malformed per the Namespaces in XML specification, if the namespaceURI of this node is null, if the specified prefix is "xml" and the namespaceURI of this node is different from "http://www.w3.org/XML/1998/namespace", if this node is an attribute and the specified prefix is "xmlns" and the namespaceURI of this node is different from " http://www.w3.org/2000/xmlns/", or if this node is an attribute and the qualifiedName of this node is "xmlns" .
since
DOM Level 2

        return (prefix);
    
public org.w3c.dom.NodegetPreviousSibling()
The node immediately preceding this node. If there is no such node, this returns null.

        SOAPElement parent = getParentElement();
        if (parent == null) {
            return null;
        }
        NodeList nl = parent.getChildNodes();
        int len = nl.getLength();
        int i = 0;
        Node previousSibling = null;
        while (i < len) {
            if (nl.item(i) == this) {
                return previousSibling;
            }
            previousSibling = nl.item(i);
            i++;
        }
        return previousSibling; // should be null.
    
public java.lang.StringgetValue()
Returns the the value of the immediate child of this Node object if a child exists and its value is text.

return
a String with the text of the immediate child of this Node object if (1) there is a child and (2) the child is a Text object; null otherwise

        return textRep.getNodeValue();
    
public booleanhasAttributes()
Returns whether this node (if it is an element) has any attributes.

return
true if this node has any attributes, false otherwise.
since
DOM Level 2

        return attributes.getLength() > 0;
    
public booleanhasChildNodes()
Returns whether this node has any children.

return
true if this node has any children, false otherwise.

        return (children != null && !children.isEmpty());
    
protected voidinitializeChildren()
Initialize the children array

        if (children == null) {
            children = new ArrayList();
        }
    
public org.w3c.dom.NodeinsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild)
Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.

param
newChild The node to insert.
param
refChild The reference node, i.e., the node before which the new node must be inserted.
return
The node being inserted.
throws
org.w3c.dom.DOMException HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors or this node itself.
WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the parent of the node being inserted is readonly.
NOT_FOUND_ERR: Raised if refChild is not a child of this node.

        initializeChildren();
        int position = children.indexOf(refChild);
        if (position < 0) {
            position = 0;
        }
        children.add(position, newChild);
        setDirty();
        return newChild;
    
public booleanisDirty()
get the dirty bit

return

        return _isDirty;
    
public booleanisSupported(java.lang.String feature, java.lang.String version)
Tests whether the DOM implementation implements a specific feature and that feature is supported by this node.

param
feature The name of the feature to test. This is the same name which can be passed to the method hasFeature on DOMImplementation.
param
version This is the version number of the feature to test. In Level 2, version 1, this is the string "2.0". If the version is not specified, supporting any version of the feature will cause the method to return true.
return
Returns true if the specified feature is supported on this node, false otherwise.
since
DOM Level 2

        return false;  //TODO: Fix this for SAAJ 1.2 Implementation
    
protected org.xml.sax.helpers.AttributesImplmakeAttributesEditable()
make the attributes editable

return
AttributesImpl

        if (attributes == null || attributes instanceof NullAttributes) {
            attributes = new AttributesImpl();
        } else if (!(attributes instanceof AttributesImpl)) {
            attributes = new AttributesImpl(attributes);
        }
        return (AttributesImpl) attributes;
    
public voidnormalize()
Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer lookups) that depend on a particular document tree structure are to be used.In cases where the document contains CDATASections, the normalize operation alone may not be sufficient, since XPointers do not differentiate between Text nodes and CDATASection nodes.

        //TODO: Fix this for SAAJ 1.2 Implementation
    
public voidoutput(org.apache.axis.encoding.SerializationContext context)
print the contents of this node

param
context
throws
Exception

        if (textRep == null)
            return;
        boolean oldPretty = context.getPretty();
        context.setPretty(false);
        if (textRep instanceof CDATASection) {
            context.writeString("<![CDATA[");
            context.writeString(((org.w3c.dom.Text) textRep).getData());
            context.writeString("]]>");
        } else if (textRep instanceof Comment) {
            context.writeString("<!--");
            context.writeString(((CharacterData) textRep).getData());
            context.writeString("-->");
        } else if (textRep instanceof Text) {
            context.writeSafeString(((Text) textRep).getData());
        }
        context.setPretty(oldPretty);
    
public voidrecycleNode()
Notifies the implementation that this Node object is no longer being used by the application and that the implementation is free to reuse this object for nodes that may be created later.

Calling the method recycleNode implies that the method detachNode has been called previously.

        //TODO: Fix this for SAAJ 1.2 Implementation        
    
public org.w3c.dom.NoderemoveChild(org.w3c.dom.Node oldChild)
Removes the child node indicated by oldChild from the list of children, and returns it.

param
oldChild The node being removed.
return
The node removed.
throws
org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
NOT_FOUND_ERR: Raised if oldChild is not a child of this node.

        if (removeNodeFromChildList((NodeImpl) oldChild)) {
            setDirty();
            return oldChild;
        }
        throw new DOMException(DOMException.NOT_FOUND_ERR,
                "NodeImpl Not found");
    
private booleanremoveNodeFromChildList(org.apache.axis.message.NodeImpl n)

        boolean removed = false;
        initializeChildren();
        final Iterator itr = children.iterator();
        while (itr.hasNext()) {
            final NodeImpl node = (NodeImpl) itr.next();
            if (node == n) {
                removed = true;
                itr.remove();
            }
        }
        return removed;
    
public org.w3c.dom.NodereplaceChild(org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild)
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
If newChild is a DocumentFragment object, oldChild is replaced by all of the DocumentFragment children, which are inserted in the same order. If the newChild is already in the tree, it is first removed.

param
newChild The new node to put in the child list.
param
oldChild The node being replaced in the list.
return
The node replaced.
throws
org.w3c.dom.DOMException HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to put in is one of this node's ancestors or this node itself.
WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of the new node is readonly.
NOT_FOUND_ERR: Raised if oldChild is not a child of this node.

        initializeChildren();
        int position = children.indexOf(oldChild);
        if (position < 0) {
            throw new DOMException(DOMException.NOT_FOUND_ERR,
                    "NodeImpl Not found");
        }
        children.remove(position);
        children.add(position, newChild);
        setDirty();
        return oldChild;
    
public voidreset()

        if (children != null) {
        	for (int i=0; i<children.size(); i++) {
                ((NodeImpl) children.get(i)).reset();
            }
        }	
        this._isDirty = false;
    
public voidsetDirty(boolean dirty)
set the dirty bit. will also set our parent as dirty, if there is one. Note that clearing the dirty bit does not propagate upwards.

param
dirty new value of the dirty bit

        _isDirty = dirty;
        if (_isDirty && parent != null) {
            ((NodeImpl) parent).setDirty();
        }
    
public voidsetDirty()

        _isDirty = true;
        if (parent != null) {
            ((NodeImpl) parent).setDirty();
        }
    
public voidsetNodeValue(java.lang.String nodeValue)
The value of this node, depending on its type; see the table above. When it is defined to be null, setting it has no effect.

throws
org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
throws
org.w3c.dom.DOMException DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in a DOMString variable on the implementation platform.

        throw new DOMException(DOMException.NO_DATA_ALLOWED_ERR,
                "Cannot use TextNode.set in " + this);
    
public voidsetOwnerDocument(org.w3c.dom.Document doc)
Set the owner document

param
doc

        document = doc;
    
protected voidsetParent(org.apache.axis.message.NodeImpl parent)
Set the parent node and invoke appendChild(this) to add this node to the parent's list of children.

param
parent
throws
SOAPException

        if (this.parent == parent) {
            return;
        }
        if (this.parent != null) {
            this.parent.removeChild(this);
        }
        if (parent != null) {
            parent.appendChild(this);
        }
        this.setDirty();
        this.parent = parent;
    
public voidsetParentElement(javax.xml.soap.SOAPElement parent)
Sets the parent of this Node object to the given SOAPElement object.

param
parent the SOAPElement object to be set as the parent of this Node object
throws
javax.xml.soap.SOAPException if there is a problem in setting the parent to the given element
see
#getParentElement() getParentElement()

        if (parent == null)
            throw new IllegalArgumentException(
                    Messages.getMessage("nullParent00"));
        try {
            setParent((NodeImpl) parent);
        } catch (Throwable t) {
            throw new SOAPException(t);
        }
    
public voidsetPrefix(java.lang.String prefix)
The namespace prefix of this node, or null if it is unspecified.
Note that setting this attribute, when permitted, changes the nodeName attribute, which holds the qualified name, as well as the tagName and name attributes of the Element and Attr interfaces, when applicable.
Note also that changing the prefix of an attribute that is known to have a default value, does not make a new attribute with the default value and the original prefix appear, since the namespaceURI and localName do not change.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.

throws
org.w3c.dom.DOMException INVALID_CHARACTER_ERR: Raised if the specified prefix contains an illegal character, per the XML 1.0 specification .
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
NAMESPACE_ERR: Raised if the specified prefix is malformed per the Namespaces in XML specification, if the namespaceURI of this node is null, if the specified prefix is "xml" and the namespaceURI of this node is different from "http://www.w3.org/XML/1998/namespace", if this node is an attribute and the specified prefix is "xmlns" and the namespaceURI of this node is different from " http://www.w3.org/2000/xmlns/", or if this node is an attribute and the qualifiedName of this node is "xmlns" .
since
DOM Level 2

        this.prefix = prefix;
    
public voidsetValue(java.lang.String value)
If this is a Text node then this method will set its value, otherwise it sets the value of the immediate (Text) child of this node. The value of the immediate child of this node can be set only if, there is one child node and that node is a Text node, or if there are no children in which case a child Text node will be created.

param
value the text to set
throws
IllegalStateException if the node is not a Text node and either has more than one child node or has a child node that is not a Text node

        if (this instanceof org.apache.axis.message.Text) {
            setNodeValue(value);
        } else if (children != null) {
            if (children.size() != 1) {
                throw new IllegalStateException( "setValue() may not be called on a non-Text node with more than one child." );
            }
            javax.xml.soap.Node child = (javax.xml.soap.Node) children.get(0);
            if (!(child instanceof org.apache.axis.message.Text)) {
                throw new IllegalStateException( "setValue() may not be called on a non-Text node with a non-Text child." );
            }
            ((javax.xml.soap.Text)child).setNodeValue(value);
        } else {
            appendChild(new org.apache.axis.message.Text(value));
        }