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

ElementImpl

public class ElementImpl extends InnerNodeImpl implements Element
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 boolean
namespaceAware
private String
namespaceURI
private String
prefix
private String
localName
private List
attributes
Constructors Summary
ElementImpl(DocumentImpl document, String namespaceURI, String qualifiedName)


          
        super(document);

        this.namespaceAware = true;
        this.namespaceURI = namespaceURI;

        if (qualifiedName == null || "".equals(qualifiedName)) {
            throw new DOMException(DOMException.NAMESPACE_ERR, qualifiedName);
        }
        
        int p = qualifiedName.lastIndexOf(":");
        if (p != -1) {
            setPrefix(qualifiedName.substring(0, p));
            qualifiedName = qualifiedName.substring(p + 1);
        }
        
        if (!document.isXMLIdentifier(qualifiedName)) {
            throw new DOMException(DOMException.INVALID_CHARACTER_ERR, qualifiedName);
        }
            
        this.localName = qualifiedName;
    
ElementImpl(DocumentImpl document, String name)

        super(document);

        this.namespaceAware = false;
        
        int p = name.lastIndexOf(":");
        if (p != -1) {
            String prefix = name.substring(0, p);
            String localName = name.substring(p + 1);
            
            if (!document.isXMLIdentifier(prefix) || !document.isXMLIdentifier(localName)) {
                throw new DOMException(DOMException.INVALID_CHARACTER_ERR, name);
            }
        } else {
            if (!document.isXMLIdentifier(name)) {
                throw new DOMException(DOMException.INVALID_CHARACTER_ERR, name);
            }
        }
        
        this.localName = name;
    
Methods Summary
public java.lang.StringgetAttribute(java.lang.String name)

        Attr attr = getAttributeNode(name);

        if (attr == null) {
            return "";
        }

        return attr.getValue();
    
public java.lang.StringgetAttributeNS(java.lang.String namespaceURI, java.lang.String localName)

        Attr attr = getAttributeNodeNS(namespaceURI, localName);

        if (attr == null) {
            return "";
        }

        return attr.getValue();
    
public org.w3c.dom.AttrgetAttributeNode(java.lang.String name)

        int i = indexOfAttribute(name);
        
        if (i == -1) {
            return null;
        }
        
        return attributes.get(i);
    
public org.w3c.dom.AttrgetAttributeNodeNS(java.lang.String namespaceURI, java.lang.String localName)

        int i = indexOfAttributeNS(namespaceURI, localName);
        
        if (i == -1) {
            return null;
        }
        
        return attributes.get(i);
    
public org.w3c.dom.NamedNodeMapgetAttributes()

        return new ElementAttrNamedNodeMapImpl();
    
org.w3c.dom.ElementgetElementById(java.lang.String name)

        if (name.equals(getAttribute("id"))) {
            return this;
        }

        for (NodeImpl node : children) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = ((ElementImpl) node).getElementById(name);
                if (element != null) {
                    return element;
                }
            }
        }

        return null;
    
public org.w3c.dom.NodeListgetElementsByTagName(java.lang.String name)

        NodeListImpl list = new NodeListImpl();
        getElementsByTagName(list, name);
        return list;
    
voidgetElementsByTagName(NodeListImpl list, java.lang.String name)

        if (matchesName(name, true)) {
            list.add(this);
        }

        for (NodeImpl node : children) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                ((ElementImpl) node).getElementsByTagName(list, name);
            }
        }
    
public org.w3c.dom.NodeListgetElementsByTagNameNS(java.lang.String namespaceURI, java.lang.String localName)

        NodeListImpl list = new NodeListImpl();
        getElementsByTagNameNS(list, namespaceURI, localName);
        return list;
    
voidgetElementsByTagNameNS(NodeListImpl list, java.lang.String namespaceURI, java.lang.String localName)

        if (matchesNameNS(namespaceURI, localName, true)) {
            list.add(this);
        }
        
        for (NodeImpl node : children) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                ((ElementImpl) node).getElementsByTagNameNS(list, namespaceURI,
                        localName);
            }
        }
    
public java.lang.StringgetLocalName()

        return namespaceAware ? localName : null;
    
public java.lang.StringgetNamespaceURI()

        return namespaceURI;
    
public java.lang.StringgetNodeName()

        return getTagName();
    
public shortgetNodeType()

        return Node.ELEMENT_NODE;
    
public java.lang.StringgetPrefix()

        return prefix;
    
public java.lang.StringgetTagName()

        return (prefix != null ? prefix + ":" : "") + localName;
    
public booleanhasAttribute(java.lang.String name)

        return indexOfAttribute(name) != -1;
    
public booleanhasAttributeNS(java.lang.String namespaceURI, java.lang.String localName)

        return indexOfAttributeNS(namespaceURI, localName) != -1;
    
public booleanhasAttributes()

        return !attributes.isEmpty();
    
private intindexOfAttribute(java.lang.String name)

        for (int i = 0; i < attributes.size(); i++) {
            AttrImpl attr = attributes.get(i);
            if (attr.matchesName(name, false)) {
                return i;
            }
        }
        
        return -1;
    
private intindexOfAttributeNS(java.lang.String namespaceURI, java.lang.String localName)

        for (int i = 0; i < attributes.size(); i++) {
            AttrImpl attr = attributes.get(i);
            if (attr.matchesNameNS(namespaceURI, localName, false)) {
                return i;
            }
        }
        
        return -1;
    
public voidremoveAttribute(java.lang.String name)

        int i = indexOfAttribute(name);
        
        if (i != -1) {
            attributes.remove(i);
        }
    
public voidremoveAttributeNS(java.lang.String namespaceURI, java.lang.String localName)

        int i = indexOfAttributeNS(namespaceURI, localName);
        
        if (i != -1) {
            attributes.remove(i);
        }
    
public org.w3c.dom.AttrremoveAttributeNode(org.w3c.dom.Attr oldAttr)

        AttrImpl oldAttrImpl = (AttrImpl) oldAttr;

        if (oldAttrImpl.getOwnerElement() != this) {
            throw new DOMException(DOMException.NOT_FOUND_ERR, null);
        }

        attributes.remove(oldAttr);
        oldAttrImpl.ownerElement = null;

        return oldAttrImpl;
    
public voidsetAttribute(java.lang.String name, java.lang.String value)

        Attr attr = getAttributeNode(name);

        if (attr == null) {
            attr = document.createAttribute(name);
            setAttributeNode(attr);
        }

        attr.setValue(value);
    
public voidsetAttributeNS(java.lang.String namespaceURI, java.lang.String qualifiedName, java.lang.String value)

        Attr attr = getAttributeNodeNS(namespaceURI, qualifiedName);

        if (attr == null) {
            attr = document.createAttributeNS(namespaceURI, qualifiedName);
            setAttributeNodeNS(attr);
        }

        attr.setValue(value);
    
public org.w3c.dom.AttrsetAttributeNode(org.w3c.dom.Attr newAttr)

        AttrImpl newAttrImpl = (AttrImpl) newAttr;
        
        if (newAttrImpl.document != this.getOwnerDocument()) {
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
        }

        if (newAttrImpl.getOwnerElement() != null) {
            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, null);
        }

        AttrImpl oldAttrImpl = null;
        
        int i = indexOfAttribute(newAttr.getName());
        if (i != -1) {
            oldAttrImpl = attributes.get(i);
            attributes.remove(i);
        }
        
        attributes.add(newAttrImpl);
        newAttrImpl.ownerElement = this;

        return oldAttrImpl;
    
public org.w3c.dom.AttrsetAttributeNodeNS(org.w3c.dom.Attr newAttr)

        AttrImpl newAttrImpl = (AttrImpl) newAttr;

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

        if (newAttrImpl.getOwnerElement() != null) {
            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, null);
        }

        AttrImpl oldAttrImpl = null;
        
        int i = indexOfAttributeNS(newAttr.getNamespaceURI(), newAttr.getLocalName());
        if (i != -1) {
            oldAttrImpl = attributes.get(i);
            attributes.remove(i);
        }
        
        attributes.add(newAttrImpl);
        newAttrImpl.ownerElement = this;

        return oldAttrImpl;
    
public voidsetPrefix(java.lang.String prefix)

        if (!namespaceAware) {
            throw new DOMException(DOMException.NAMESPACE_ERR, prefix);
        }
        
        if (prefix != null) {
            if (namespaceURI == null || !document.isXMLIdentifier(prefix)) {
                throw new DOMException(DOMException.NAMESPACE_ERR, prefix);
            }
            
            if ("xml".equals(prefix) && !"http://www.w3.org/XML/1998/namespace".equals(namespaceURI)) {
                throw new DOMException(DOMException.NAMESPACE_ERR, prefix);
            }
        }
        
        this.prefix = prefix;