namednodemapimpl.java

来自「JAVA 所有包」· Java 代码 · 共 600 行 · 第 1/2 页

JAVA
600
字号
        }    	int i = findNamePoint(namespaceURI, name);    	if (i < 0) {            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);            throw new DOMException(DOMException.NOT_FOUND_ERR, msg);        }        NodeImpl n = (NodeImpl)nodes.elementAt(i);        nodes.removeElementAt(i);        return n;    } // removeNamedItem(String):Node    //    // Public methods    //    /**     * Cloning a NamedNodeMap is a DEEP OPERATION; it always clones     * all the nodes contained in the map.     */         public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) {    	NamedNodeMapImpl newmap = new NamedNodeMapImpl(ownerNode);        newmap.cloneContent(this);    	return newmap;    }    protected void cloneContent(NamedNodeMapImpl srcmap) {        Vector srcnodes = srcmap.nodes;        if (srcnodes != null) {            int size = srcnodes.size();            if (size != 0) {                if (nodes == null) {                    nodes = new Vector(size);                }                nodes.setSize(size);                for (int i = 0; i < size; ++i) {                    NodeImpl n = (NodeImpl) srcmap.nodes.elementAt(i);                    NodeImpl clone = (NodeImpl) n.cloneNode(true);                    clone.isSpecified(n.isSpecified());                    nodes.setElementAt(clone, i);                }            }        }    } // cloneMap():NamedNodeMapImpl    //    // Package methods    //    /**     * Internal subroutine to allow read-only Nodes to make their contained     * NamedNodeMaps readonly too. I expect that in fact the shallow     * version of this operation will never be     *     * @param readOnly boolean true to make read-only, false to permit editing.     * @param deep boolean true to pass this request along to the contained     * nodes, false to only toggle the NamedNodeMap itself. I expect that     * the shallow version of this operation will never be used, but I want     * to design it in now, while I'm thinking about it.     */    void setReadOnly(boolean readOnly, boolean deep) {        isReadOnly(readOnly);    	if (deep && nodes != null) {            for (int i = nodes.size() - 1; i >= 0; i--) {                ((NodeImpl) nodes.elementAt(i)).setReadOnly(readOnly,deep);            }    	}    } // setReadOnly(boolean,boolean)        /**     * Internal subroutine returns this NodeNameMap's (shallow) readOnly value.     *     */    boolean getReadOnly() {    	return isReadOnly();    } // getReadOnly()        //    // Protected methods    //    /**     * NON-DOM     * set the ownerDocument of this node, and the attributes it contains     */    void setOwnerDocument(CoreDocumentImpl doc) {        if (nodes != null) {            for (int i = 0; i < nodes.size(); i++) {                ((NodeImpl)item(i)).setOwnerDocument(doc);            }        }    }    final boolean isReadOnly() {        return (flags & READONLY) != 0;    }    final void isReadOnly(boolean value) {        flags = (short) (value ? flags | READONLY : flags & ~READONLY);    }    final boolean changed() {        return (flags & CHANGED) != 0;    }    final void changed(boolean value) {        flags = (short) (value ? flags | CHANGED : flags & ~CHANGED);    }    final boolean hasDefaults() {        return (flags & HASDEFAULTS) != 0;    }    final void hasDefaults(boolean value) {        flags = (short) (value ? flags | HASDEFAULTS : flags & ~HASDEFAULTS);    }    //    // Private methods    //    /**     * Subroutine: Locate the named item, or the point at which said item     * should be added.      *     * @param name Name of a node to look up.     *     * @return If positive or zero, the index of the found item.     * If negative, index of the appropriate point at which to insert     * the item, encoded as -1-index and hence reconvertable by subtracting     * it from -1. (Encoding because I don't want to recompare the strings     * but don't want to burn bytes on a datatype to hold a flagged value.)     */    protected int findNamePoint(String name, int start) {    	// Binary search    	int i = 0;    	if (nodes != null) {            int first = start;            int last  = nodes.size() - 1;            while (first <= last) {                i = (first + last) / 2;                int test = name.compareTo(((Node)(nodes.elementAt(i))).getNodeName());                if (test == 0) {                    return i; // Name found                }                else if (test < 0) {                    last = i - 1;                }                else {                    first = i + 1;                }            }            if (first > i) {                i = first;            }    	}    	return -1 - i; // not-found has to be encoded.    } // findNamePoint(String):int        /** This findNamePoint is for DOM Level 2 Namespaces.     */    protected int findNamePoint(String namespaceURI, String name) {                if (nodes == null) return -1;        if (name == null) return -1;        //if namespace uri is empty string, set it to 'null'        if (namespaceURI != null) {            namespaceURI = (namespaceURI.length() == 0)? null : namespaceURI;                    }                // This is a linear search through the same nodes Vector.        // The Vector is sorted on the DOM Level 1 nodename.        // The DOM Level 2 NS keys are namespaceURI and Localname,         // so we must linear search thru it.        // In addition, to get this to work with nodes without any namespace        // (namespaceURI and localNames are both null) we then use the nodeName        // as a seconday key.        for (int i = 0; i < nodes.size(); i++) {            NodeImpl a = (NodeImpl)nodes.elementAt(i);            String aNamespaceURI = a.getNamespaceURI();            String aLocalName = a.getLocalName();            if (namespaceURI == null) {              if (aNamespaceURI == null                  &&                  (name.equals(aLocalName)                   ||                   (aLocalName == null && name.equals(a.getNodeName()))))                return i;            } else {              if (namespaceURI.equals(aNamespaceURI)                  &&                  name.equals(aLocalName))                return i;            }        }        return -1;    }    // compare 2 nodes in the map.  If a precedes b, return true, otherwise     // return false    protected boolean precedes(Node a, Node b) {       if (nodes != null) {          for (int i = 0; i < nodes.size(); i++) {              Node n = (Node)nodes.elementAt(i);              if (n==a) return true;              if (n==b) return false;          }       }       return false;    }    /**      * NON-DOM: Remove attribute at specified index      */    protected void removeItem(int index) {       if (nodes != null && index < nodes.size()){           nodes.removeElementAt(index);       }    }    protected Object getItem (int index){        if (nodes !=null) {            return nodes.elementAt(index);        }        return null;    }    protected int addItem (Node arg) {    	int i = findNamePoint(arg.getNamespaceURI(), arg.getLocalName());    	if (i >= 0) {            nodes.setElementAt(arg,i);    	}         else {    	    // If we can't find by namespaceURI, localName, then we find by    	    // nodeName so we know where to insert.    	    i = findNamePoint(arg.getNodeName(),0);            if (i >= 0) {                nodes.insertElementAt(arg,i);            }             else {                i = -1 - i; // Insert point (may be end of list)                if (null == nodes) {                    nodes = new Vector(5, 10);                }                nodes.insertElementAt(arg, i);            }        }        return i;            }    /**     * NON-DOM: copy content of this map into the specified vector     *      * @param list   Vector to copy information into.     * @return A copy of this node named map     */    protected Vector cloneMap(Vector list){        if (list == null) {            list = new Vector(5, 10);        }        list.setSize(0);        if (nodes != null) {            for (int i=0; i<nodes.size(); i++) {                list.insertElementAt(nodes.elementAt(i), i);            }        }                return list;    }         protected int getNamedItemIndex(String namespaceURI, String localName) {        return findNamePoint(namespaceURI, localName);     }    /**      * NON-DOM remove all elements from this map      */    public void removeAll (){        if (nodes != null) {            nodes.removeAllElements();        }    }    } // class NamedNodeMapImpl

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?