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

DocumentImpl

public class DocumentImpl extends InnerNodeImpl implements Document
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.

Fields Summary
private DOMImplementation
domImplementation
Constructors Summary
DocumentImpl(DOMImplementationImpl impl, String namespaceURI, String qualifiedName, DocumentType doctype)

        super(null);

        this.domImplementation = impl;
        // this.document = this;
        
        if (doctype != null) {
            appendChild(doctype);
        }

        if (qualifiedName != null) {
            appendChild(createElementNS(namespaceURI, qualifiedName));
        }
    
Methods Summary
org.w3c.dom.NodecloneNode(org.w3c.dom.Node node, boolean deep)
Clones a node and (if requested) its children. The source node(s) may have been created by a different DocumentImpl or even DOM implementation.

param
node The node to clone.
param
deep If true, a deep copy is created (including all child nodes).
return
The new node.

        Node target;
        
        switch (node.getNodeType()) {
            case Node.ATTRIBUTE_NODE: {
                Attr source = (Attr)node;
                target = createAttributeNS(source.getNamespaceURI(), source.getLocalName());
                target.setPrefix(source.getPrefix());
                target.setNodeValue(source.getNodeValue());
                break;
            }
            case Node.CDATA_SECTION_NODE: {
                CharacterData source = (CharacterData)node;
                target = createCDATASection(source.getData());
                break;
            }
            case Node.COMMENT_NODE: {
                Comment source = (Comment)node;
                target = createComment(source.getData());
                break;
            }
            case Node.DOCUMENT_FRAGMENT_NODE: {
                // Source is irrelevant in this case.
                target = createDocumentFragment();
                break;
            }
            case Node.DOCUMENT_NODE: {
                throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot clone a Document node");
            }
            case Node.DOCUMENT_TYPE_NODE: {
                throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot clone a DocumentType node");
            }
            case Node.ELEMENT_NODE: {
                Element source = (Element)node;
                target = createElementNS(source.getNamespaceURI(), source.getLocalName());
                target.setPrefix(source.getPrefix());

                NamedNodeMap map = source.getAttributes();
                for (int i = 0; i < map.getLength(); i++) {
                    Attr attr = (Attr)map.item(i);
                    ((Element)target).setAttributeNodeNS((Attr)cloneNode(attr, deep));
                }
                break;
            }
            case Node.ENTITY_NODE: {
                throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot clone an Entity node");
            }
            case Node.ENTITY_REFERENCE_NODE: {
                EntityReference source = (EntityReference)node;
                target = createEntityReference(source.getNodeName());
                break;
            }
            case Node.NOTATION_NODE: {
                throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot clone a Notation node");
            }
            case Node.PROCESSING_INSTRUCTION_NODE: {
                ProcessingInstruction source = (ProcessingInstruction)node;
                target = createProcessingInstruction(source.getTarget(), source.getData());
                break;
            }
            case Node.TEXT_NODE: {
                Text source = (Text)node;
                target = createTextNode(source.getData());
                break;
            }
            default: {
                throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot clone unknown node type " + node.getNodeType() + " (" + node.getClass().getSimpleName() + ")");
            }
        }

        if (deep) {
            NodeList list = node.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node child = cloneNode(list.item(i), deep);
                target.appendChild(child);
            }
        }
        
        return target;
    
public AttrImplcreateAttribute(java.lang.String name)

        return new AttrImpl(this, name);
    
public org.w3c.dom.AttrcreateAttributeNS(java.lang.String namespaceURI, java.lang.String qualifiedName)

        return new AttrImpl(this, namespaceURI, qualifiedName);
    
public org.w3c.dom.CDATASectioncreateCDATASection(java.lang.String data)

        return new CDATASectionImpl(this, data);
    
public org.w3c.dom.CommentcreateComment(java.lang.String data)

        return new CommentImpl(this, data);
    
public org.w3c.dom.DocumentFragmentcreateDocumentFragment()

        return new DocumentFragmentImpl(this);
    
public org.w3c.dom.ElementcreateElement(java.lang.String tagName)

        return new ElementImpl(this, tagName);
    
public org.w3c.dom.ElementcreateElementNS(java.lang.String namespaceURI, java.lang.String qualifiedName)

        return new ElementImpl(this, namespaceURI, qualifiedName);
    
public org.w3c.dom.EntityReferencecreateEntityReference(java.lang.String name)

        return new EntityReferenceImpl(this, name);
    
public org.w3c.dom.ProcessingInstructioncreateProcessingInstruction(java.lang.String target, java.lang.String data)

        return new ProcessingInstructionImpl(this, target, data);
    
public org.w3c.dom.TextcreateTextNode(java.lang.String data)

        return new TextImpl(this, data);
    
public org.w3c.dom.DocumentTypegetDoctype()

        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) instanceof DocumentType) {
                return (DocumentType) children.get(i);
            }
        }

        return null;
    
public org.w3c.dom.ElementgetDocumentElement()

        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) instanceof Element) {
                return (Element) children.get(i);
            }
        }

        return null;
    
public org.w3c.dom.ElementgetElementById(java.lang.String elementId)

        ElementImpl root = (ElementImpl) getDocumentElement();

        return (root == null ? null : root.getElementById(elementId));
    
public org.w3c.dom.NodeListgetElementsByTagName(java.lang.String tagname)

        ElementImpl root = (ElementImpl) getDocumentElement();

        return (root == null ? new NodeListImpl()
                : root.getElementsByTagName(tagname));
    
public org.w3c.dom.NodeListgetElementsByTagNameNS(java.lang.String namespaceURI, java.lang.String localName)

        ElementImpl root = (ElementImpl) getDocumentElement();

        return (root == null ? new NodeListImpl() : root.getElementsByTagNameNS(
                namespaceURI, localName));
    
public org.w3c.dom.DOMImplementationgetImplementation()

        return domImplementation;
    
public java.lang.StringgetNodeName()

        return "#document";
    
public shortgetNodeType()

        return Node.DOCUMENT_NODE;
    
public org.w3c.dom.NodeimportNode(org.w3c.dom.Node importedNode, boolean deep)

        return cloneNode(importedNode, deep);
    
public org.w3c.dom.NodeinsertChildAt(org.w3c.dom.Node newChild, int index)

        // Make sure we have at most one root element and one DTD element.
        if (newChild instanceof Element && getDocumentElement() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                    "Only one root element allowed");
        } else if (newChild instanceof DocumentType && getDoctype() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                    "Only one DOCTYPE element allowed");
        }

        return super.insertChildAt(newChild, index);
    
static booleanisXMLIdentifier(java.lang.String s)

        if (s.length() == 0) {
            return false;
        }
        
        if (!isXMLIdentifierStart(s.charAt(0))) {
            return false;
        }
        
        for (int i = 1; i < s.length(); i++) {
            if (!isXMLIdentifierPart(s.charAt(i))) {
                return false;
            }
        }
        
        return true;
    
private static booleanisXMLIdentifierPart(char c)

        return isXMLIdentifierStart(c) || (c >= '0" && c <= '9") || (c == '-") || (c == '.");
    
private static booleanisXMLIdentifierStart(char c)

        return (c >= 'A" && c <= 'Z") || (c >= 'a" && c <= 'z") || (c == '_");