FileDocCategorySizeDatePackage
InnerNodeImpl.javaAPI DocAndroid 1.5 API6933Wed May 06 22:41:06 BST 2009org.apache.harmony.xml.dom

InnerNodeImpl

public abstract class InnerNodeImpl extends LeafNodeImpl
Provides a straightforward implementation of the corresponding W3C DOM interface. The class is used internally only, thus only notable members that are not in the original interface are documented (the W3C docs are quite extensive). Hope that's ok.

Some of the fields may have package visibility, so other classes belonging to the DOM implementation can easily access them while maintaining the DOM tree structure.

This class represents a Node that has a parent Node as well as (potentially) a number of children.

Fields Summary
List
children
Constructors Summary
public InnerNodeImpl(DocumentImpl document)


       
        super(document);
    
Methods Summary
public org.w3c.dom.NodeappendChild(org.w3c.dom.Node newChild)

        return insertChildAt(newChild, children.size());
    
public org.w3c.dom.NodeListgetChildNodes()

        NodeListImpl list = new NodeListImpl();

        for (NodeImpl node : children) {
            list.add(node);
        }

        return list;
    
public org.w3c.dom.NodegetFirstChild()

        return (!children.isEmpty() ? children.get(0) : null);
    
public org.w3c.dom.NodegetLastChild()

        return (!children.isEmpty() ? children.get(children.size() - 1) : null);
    
public org.w3c.dom.NodegetNextSibling()

        if (parent == null || index >= parent.children.size()) {
            return null;
        }

        return parent.children.get(index + 1);
    
public booleanhasChildNodes()

        return children.size() != 0;
    
public org.w3c.dom.NodeinsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild)

        LeafNodeImpl refChildImpl = (LeafNodeImpl) refChild;

        if (refChildImpl.document != document) {
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
        }

        if (refChildImpl.parent != this) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
        }

        return insertChildAt(newChild, refChildImpl.index);
    
public org.w3c.dom.NodeinsertChildAt(org.w3c.dom.Node newChild, int index)
Inserts a new child node into this node at a given position. If the new node is already child of another node, it is first removed from there. This method is the generalization of the appendChild() and insertBefore() methods.

param
newChild The new child node to add.
param
index The index at which to insert the new child node.
return
The node added.
throws
DOMException If the attempted operation violates the XML/DOM well-formedness rules.

        LeafNodeImpl newChildImpl = (LeafNodeImpl) newChild;

        if (document != null && newChildImpl.document != null && newChildImpl.document != document) {
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
        }

        if (newChildImpl.isParentOf(this)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
        }

        if (newChildImpl.parent != null) {
            int oldIndex = newChildImpl.index;
            newChildImpl.parent.children.remove(oldIndex);
            newChildImpl.parent.refreshIndices(oldIndex);
        }

        children.add(index, newChildImpl);
        newChildImpl.parent = this;
        refreshIndices(index);

        return newChild;
    
public booleanisParentOf(org.w3c.dom.Node node)

        LeafNodeImpl nodeImpl = (LeafNodeImpl) node;

        while (nodeImpl != null) {
            if (nodeImpl == this) {
                return true;
            }

            nodeImpl = nodeImpl.parent;
        }

        return false;
    
public voidnormalize()

        Node nextNode = null;
        
        for (int i = children.size() - 1; i >= 0; i--) {
            Node thisNode = children.get(i);

            thisNode.normalize();
            
            if (thisNode.getNodeType() == Node.TEXT_NODE) {
                if (nextNode != null && nextNode.getNodeType() == Node.TEXT_NODE) {
                    ((Text)thisNode).setData(thisNode.getNodeValue() + nextNode.getNodeValue());
                    removeChild(nextNode);
                }
                
                if ("".equals(thisNode.getNodeValue())) {
                    removeChild(thisNode);
                    nextNode = null;
                } else {
                    nextNode = thisNode;
                }
            }
        }
    
private voidrefreshIndices(int fromIndex)

        for (int i = fromIndex; i < children.size(); i++) {
            children.get(i).index = i;
        }
    
public org.w3c.dom.NoderemoveChild(org.w3c.dom.Node oldChild)

        LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild;

        if (oldChildImpl.document != document) {
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
        }

        if (oldChildImpl.parent != this) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
        }

        int index = oldChildImpl.index;
        children.remove(index);
        oldChildImpl.parent = null;
        refreshIndices(index);

        return oldChild;
    
public org.w3c.dom.NodereplaceChild(org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild)

        LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild;
        LeafNodeImpl newChildImpl = (LeafNodeImpl) newChild;

        if (oldChildImpl.document != document
                || newChildImpl.document != document) {
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
        }

        if (oldChildImpl.parent != this || newChildImpl.isParentOf(this)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
        }

        int index = oldChildImpl.index;
        children.set(index, newChildImpl);
        oldChildImpl.parent = null;
        newChildImpl.parent = this;
        refreshIndices(index);

        return oldChildImpl;