attrimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,236 行 · 第 1/3 页

JAVA
1,236
字号
        if (ownerDocument.getMutationEvents()) {            // if there are any event handlers create a real node            internalInsertBefore(ownerDocument.createTextNode(newvalue),                                 null, true);            hasStringValue(false);            // notify document            ownerDocument.modifiedAttrValue(this, oldvalue);        } else {            // directly store the string            value = newvalue;            hasStringValue(true);            changed();        }        if (isIdAttribute() && ownerElement != null) {            ownerDocument.putIdentifier(newvalue, ownerElement);        }    } // setValue(String)    /**     * The "string value" of an Attribute is its text representation,     * which in turn is a concatenation of the string values of its children.     */    public String getValue() {        if (needsSyncData()) {            synchronizeData();        }        if (needsSyncChildren()) {            synchronizeChildren();        }        if (value == null) {            return "";        }        if (hasStringValue()) {            return (String) value;        }                ChildNode firstChild = ((ChildNode) value);        String data = null;        if (firstChild.getNodeType() == Node.ENTITY_REFERENCE_NODE){                data = ((EntityReferenceImpl)firstChild).getEntityRefValue();        }        else {                data =  firstChild.getNodeValue();        }                ChildNode node = firstChild.nextSibling;                if (node == null || data == null)  return (data == null)?"":data;                StringBuffer value = new StringBuffer(data);    	while (node != null) {            if (node.getNodeType()  == Node.ENTITY_REFERENCE_NODE){                data = ((EntityReferenceImpl)node).getEntityRefValue();                if (data == null) return "";                value.append(data);            }            else {                value.append(node.getNodeValue());            }            node = node.nextSibling;    	}    	return value.toString();    } // getValue():String            /**     * The "specified" flag is true if and only if this attribute's     * value was explicitly specified in the original document. Note that     * the implementation, not the user, is in charge of this     * property. If the user asserts an Attribute value (even if it ends     * up having the same value as the default), it is considered a     * specified attribute. If you really want to revert to the default,     * delete the attribute from the Element, and the Implementation will     * re-assert the default (if any) in its place, with the appropriate     * specified=false setting.     */    public boolean getSpecified() {        if (needsSyncData()) {            synchronizeData();        }    	return isSpecified();    } // getSpecified():boolean    //    // Attr2 methods    //    /**     * Returns the element node that this attribute is associated with,     * or null if the attribute has not been added to an element.     *     * @see #getOwnerElement     *     * @deprecated Previous working draft of DOM Level 2. New method     *             is <tt>getOwnerElement()</tt>.     */    public Element getElement() {        // if we have an owner, ownerNode is our ownerElement, otherwise it's        // our ownerDocument and we don't have an ownerElement        return (Element) (isOwned() ? ownerNode : null);    }    /**     * Returns the element node that this attribute is associated with,     * or null if the attribute has not been added to an element.     *     * @since WD-DOM-Level-2-19990719     */    public Element getOwnerElement() {        // if we have an owner, ownerNode is our ownerElement, otherwise it's        // our ownerDocument and we don't have an ownerElement        return (Element) (isOwned() ? ownerNode : null);    }        public void normalize() {        // No need to normalize if already normalized or        // if value is kept as a String.        if (isNormalized() || hasStringValue())            return;        Node kid, next;        ChildNode firstChild = (ChildNode)value;        for (kid = firstChild; kid != null; kid = next) {            next = kid.getNextSibling();            // If kid is a text node, we need to check for one of two            // conditions:            //   1) There is an adjacent text node            //   2) There is no adjacent text node, but kid is            //      an empty text node.            if ( kid.getNodeType() == Node.TEXT_NODE )            {                // If an adjacent text node, merge it with kid                if ( next!=null && next.getNodeType() == Node.TEXT_NODE )                {                    ((Text)kid).appendData(next.getNodeValue());                    removeChild( next );                    next = kid; // Don't advance; there might be another.                }                else                {                    // If kid is empty, remove it                    if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) {                        removeChild( kid );                    }                }            }        }        isNormalized(true);    } // normalize()    //    // Public methods    //    /** NON-DOM, for use by parser */    public void setSpecified(boolean arg) {        if (needsSyncData()) {            synchronizeData();        }    	isSpecified(arg);    } // setSpecified(boolean)    	/**	 * NON-DOM: used by the parser	 * @param type	 */    public void setType (Object type){        this.type = type;    }    //    // Object methods    //    /** NON-DOM method for debugging convenience */    public String toString() {    	return getName() + "=" + "\"" + getValue() + "\"";    }    /**     * Test whether this node has any children. Convenience shorthand     * for (Node.getFirstChild()!=null)     */    public boolean hasChildNodes() {        if (needsSyncChildren()) {            synchronizeChildren();        }        return value != null;    }    /**     * Obtain a NodeList enumerating all children of this node. If there     * are none, an (initially) empty NodeList is returned.     * <p>     * NodeLists are "live"; as children are added/removed the NodeList     * will immediately reflect those changes. Also, the NodeList refers     * to the actual nodes, so changes to those nodes made via the DOM tree     * will be reflected in the NodeList and vice versa.     * <p>     * In this implementation, Nodes implement the NodeList interface and     * provide their own getChildNodes() support. Other DOMs may solve this     * differently.     */    public NodeList getChildNodes() {        // JKESS: KNOWN ISSUE HERE         if (needsSyncChildren()) {            synchronizeChildren();        }        return this;    } // getChildNodes():NodeList    /** The first child of this Node, or null if none. */    public Node getFirstChild() {        if (needsSyncChildren()) {            synchronizeChildren();        }        makeChildNode();    	return (Node) value;    }   // getFirstChild():Node    /** The last child of this Node, or null if none. */    public Node getLastChild() {        if (needsSyncChildren()) {            synchronizeChildren();        }        return lastChild();    } // getLastChild():Node    final ChildNode lastChild() {        // last child is stored as the previous sibling of first child        makeChildNode();        return value != null ? ((ChildNode) value).previousSibling : null;    }    final void lastChild(ChildNode node) {        // store lastChild as previous sibling of first child        if (value != null) {            ((ChildNode) value).previousSibling = node;        }    }    /**     * Move one or more node(s) to our list of children. Note that this     * implicitly removes them from their previous parent.     *     * @param newChild The Node to be moved to our subtree. As a     * convenience feature, inserting a DocumentNode will instead insert     * all its children.     *     * @param refChild Current child which newChild should be placed     * immediately before. If refChild is null, the insertion occurs     * after all existing Nodes, like appendChild().     *     * @return newChild, in its new state (relocated, or emptied in the case of     * DocumentNode.)     *     * @throws DOMException(HIERARCHY_REQUEST_ERR) if newChild is of a     * type that shouldn't be a child of this node, or if newChild is an     * ancestor of this node.     *     * @throws DOMException(WRONG_DOCUMENT_ERR) if newChild has a     * different owner document than we do.     *     * @throws DOMException(NOT_FOUND_ERR) if refChild is not a child of     * this node.     *     * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if this node is     * read-only.     */    public Node insertBefore(Node newChild, Node refChild)         throws DOMException {        // Tail-call; optimizer should be able to do good things with.        return internalInsertBefore(newChild, refChild, false);    } // insertBefore(Node,Node):Node         /** NON-DOM INTERNAL: Within DOM actions,we sometimes need to be able     * to control which mutation events are spawned. This version of the     * insertBefore operation allows us to do so. It is not intended     * for use by application programs.     */    Node internalInsertBefore(Node newChild, Node refChild, boolean replace)         throws DOMException {        CoreDocumentImpl ownerDocument = ownerDocument();        boolean errorChecking = ownerDocument.errorChecking;        if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {            // SLOW BUT SAFE: We could insert the whole subtree without            // juggling so many next/previous pointers. (Wipe out the            // parent's child-list, patch the parent pointers, set the            // ends of the list.) But we know some subclasses have special-            // case behavior they add to insertBefore(), so we don't risk it.            // This approch also takes fewer bytecodes.            // NOTE: If one of the children is not a legal child of this            // node, throw HIERARCHY_REQUEST_ERR before _any_ of the children            // have been transferred. (Alternative behaviors would be to            // reparent up to the first failure point or reparent all those            // which are acceptable to the target node, neither of which is            // as robust. PR-DOM-0818 isn't entirely clear on which it            // recommends?????            // No need to check kids for right-document; if they weren't,            // they wouldn't be kids of that DocFrag.            if (errorChecking) {                for (Node kid = newChild.getFirstChild(); // Prescan                     kid != null; kid = kid.getNextSibling()) {                    if (!ownerDocument.isKidOK(this, kid)) {                        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);                        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);                    }                }            }            while (newChild.hasChildNodes()) {                insertBefore(newChild.getFirstChild(), refChild);            }            return newChild;        }        if (newChild == refChild) {            // stupid case that must be handled as a no-op triggering events...            refChild = refChild.getNextSibling();            removeChild(newChild);            insertBefore(newChild, refChild);            return newChild;        }        if (needsSyncChildren()) {            synchronizeChildren();        }        if (errorChecking) {            if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);            }            if (newChild.getOwnerDocument() != ownerDocument) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);            }            if (!ownerDocument.isKidOK(this, newChild)) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);            }            // refChild must be a child of this node (or null)            if (refChild != null && refChild.getParentNode() != this) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);                throw new DOMException(DOMException.NOT_FOUND_ERR, msg);            }            // Prevent cycles in the tree            // newChild cannot be ancestor of this Node,            // and actually cannot be this            boolean treeSafe = true;            for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode())            {                treeSafe = newChild != a;            }            if (!treeSafe) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);            }        }        makeChildNode(); // make sure we have a node and not a string        // notify document        ownerDocument.insertingNode(this, replace);        // Convert to internal type, to avoid repeated casting        ChildNode newInternal = (ChildNode)newChild;        Node oldparent = newInternal.parentNode();        if (oldparent != null) {            oldparent.removeChild(newInternal);        }        // Convert to internal type, to avoid repeated casting        ChildNode refInternal = (ChildNode) refChild;        // Attach up        newInternal.ownerNode = this;        newInternal.isOwned(true);        // Attach before and after        // Note: firstChild.previousSibling == lastChild!!        ChildNode firstChild = (ChildNode) value;        if (firstChild == null) {            // this our first and only child            value = newInternal; // firstchild = newInternal;            newInternal.isFirstChild(true);            newInternal.previousSibling = newInternal;        }

⌨️ 快捷键说明

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