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

NamedNodeMapImpl

public class NamedNodeMapImpl extends Object implements NamedNodeMap
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 Class
type
private List
list
Constructors Summary
NamedNodeMapImpl(Class type)

        list = new ArrayList<NodeImpl>();
        this.type = type;
    
NamedNodeMapImpl(List list, Class type)

        this.list = list;
        this.type = type;
    
Methods Summary
public intgetLength()

        return list.size();
    
public org.w3c.dom.NodegetNamedItem(java.lang.String name)

        int i = indexOfItem(name);
        
        return (i == -1 ? null : item(i));
    
public org.w3c.dom.NodegetNamedItemNS(java.lang.String namespaceURI, java.lang.String localName)

        int i = indexOfItemNS(namespaceURI, localName);
        
        return (i == -1 ? null : item(i));
    
private intindexOfItem(java.lang.String name)

        for (int i = 0; i < list.size(); i++) {
            NodeImpl node = list.get(i);
            if (node.matchesName(name, false)) {
                return i;
            }
        }
        
        return -1;
    
private intindexOfItemNS(java.lang.String namespaceURI, java.lang.String localName)

        for (int i = 0; i < list.size(); i++) {
            NodeImpl node = list.get(i);
            if (node.matchesNameNS(namespaceURI, localName, false)) {
                return i;
            }
        }
        
        return -1;
    
public org.w3c.dom.Nodeitem(int index)

        return list.get(index);
    
public org.w3c.dom.NoderemoveNamedItem(java.lang.String name)

        int i = indexOfItem(name);
        
        if (i == -1) {
            throw new DOMException(DOMException.NOT_FOUND_ERR, null);
        }

        return list.remove(i);
    
public org.w3c.dom.NoderemoveNamedItemNS(java.lang.String namespaceURI, java.lang.String localName)

        int i = indexOfItemNS(namespaceURI, localName);
        
        if (i == -1) {
            throw new DOMException(DOMException.NOT_FOUND_ERR, null);
        }

        return list.remove(i);
    
public org.w3c.dom.NodesetNamedItem(org.w3c.dom.Node arg)

        // Ensure we only accept nodes of the correct type.
        if (!type.isAssignableFrom(arg.getClass())) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
        }
        
        // All nodes in the map must belong to the same document.
        if (list.size() != 0) {
            Document document = list.get(0).getOwnerDocument();

            if (document != null && arg.getOwnerDocument() != document) {
                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
            }
        }

// TODO Theoretically we should ensure that the nodes don't have a parent.
//        if (newAttrImpl.getOwnerElement() != null) {
//            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, null);
//        }

        int i = indexOfItem(arg.getNodeName());
        
        if (i != -1) {
            list.remove(i);
        }
        
        list.add((NodeImpl)arg);
        return arg;
    
public org.w3c.dom.NodesetNamedItemNS(org.w3c.dom.Node arg)

        // Ensure we only accept nodes of the correct type.
        if (!type.isAssignableFrom(arg.getClass())) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
        }
        
        // All nodes in the map must belong to the same document.
        if (list.size() != 0) {
            Document document = list.get(0).getOwnerDocument();

            if (document != null && arg.getOwnerDocument() != document) {
                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
            }
        }

// TODO Theoretically we should ensure that the nodes don't have a parent.
//        if (newAttrImpl.getOwnerElement() != null) {
//            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, null);
//        }

        int i = indexOfItemNS(arg.getNamespaceURI(), arg.getLocalName());
        
        if (i != -1) {
            list.remove(i);
        }
        
        list.add((NodeImpl)arg);
        return arg;