286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.dom;
286N/A
286N/Aimport java.io.IOException;
286N/Aimport java.io.ObjectInputStream;
286N/Aimport java.io.ObjectOutputStream;
286N/A
286N/Aimport org.w3c.dom.TypeInfo;
286N/Aimport org.w3c.dom.Attr;
286N/Aimport org.w3c.dom.DOMException;
286N/Aimport org.w3c.dom.Element;
286N/Aimport org.w3c.dom.Node;
286N/Aimport org.w3c.dom.NodeList;
286N/Aimport org.w3c.dom.Text;
286N/A
286N/A/**
286N/A * Attribute represents an XML-style attribute of an
286N/A * Element. Typically, the allowable values are controlled by its
286N/A * declaration in the Document Type Definition (DTD) governing this
286N/A * kind of document.
286N/A * <P>
286N/A * If the attribute has not been explicitly assigned a value, but has
286N/A * been declared in the DTD, it will exist and have that default. Only
286N/A * if neither the document nor the DTD specifies a value will the
286N/A * Attribute really be considered absent and have no value; in that
286N/A * case, querying the attribute will return null.
286N/A * <P>
286N/A * Attributes may have multiple children that contain their data. (XML
286N/A * allows attributes to contain entity references, and tokenized
286N/A * attribute types such as NMTOKENS may have a child for each token.)
286N/A * For convenience, the Attribute object's getValue() method returns
286N/A * the string version of the attribute's value.
286N/A * <P>
286N/A * Attributes are not children of the Elements they belong to, in the
286N/A * usual sense, and have no valid Parent reference. However, the spec
286N/A * says they _do_ belong to a specific Element, and an INUSE exception
286N/A * is to be thrown if the user attempts to explicitly share them
286N/A * between elements.
286N/A * <P>
286N/A * Note that Elements do not permit attributes to appear to be shared
286N/A * (see the INUSE exception), so this object's mutability is
286N/A * officially not an issue.
286N/A * <p>
286N/A * Note: The ownerNode attribute is used to store the Element the Attr
286N/A * node is associated with. Attr nodes do not have parent nodes.
286N/A * Besides, the getOwnerElement() method can be used to get the element node
286N/A * this attribute is associated with.
286N/A * <P>
286N/A * AttrImpl does not support Namespaces. AttrNSImpl, which inherits from
286N/A * it, does.
286N/A *
286N/A * <p>AttrImpl used to inherit from ParentNode. It now directly inherits from
286N/A * NodeImpl and provide its own implementation of the ParentNode's behavior.
286N/A * The reason is that we now try and avoid to always create a Text node to
286N/A * hold the value of an attribute. The DOM spec requires it, so we still have
286N/A * to do it in case getFirstChild() is called for instance. The reason
286N/A * attribute values are stored as a list of nodes is so that they can carry
286N/A * more than a simple string. They can also contain EntityReference nodes.
286N/A * However, most of the times people only have a single string that they only
286N/A * set and get through Element.set/getAttribute or Attr.set/getValue. In this
286N/A * new version, the Attr node has a value pointer which can either be the
286N/A * String directly or a pointer to the first ChildNode. A flag tells which one
286N/A * it currently is. Note that while we try to stick with the direct String as
286N/A * much as possible once we've switched to a node there is no going back. This
286N/A * is because we have no way to know whether the application keeps referring to
286N/A * the node we once returned.
286N/A * <p> The gain in memory varies on the density of attributes in the document.
286N/A * But in the tests I've run I've seen up to 12% of memory gain. And the good
286N/A * thing is that it also leads to a slight gain in speed because we allocate
286N/A * fewer objects! I mean, that's until we have to actually create the node...
286N/A * <p>
286N/A * To avoid too much duplicated code, I got rid of ParentNode and renamed
286N/A * ChildAndParentNode, which I never really liked, to ParentNode for
286N/A * simplicity, this doesn't make much of a difference in memory usage because
286N/A * there are only very few objects that are only a Parent. This is only true
286N/A * now because AttrImpl now inherits directly from NodeImpl and has its own
286N/A * implementation of the ParentNode's node behavior. So there is still some
286N/A * duplicated code there.
286N/A * <p>
286N/A * This class doesn't directly support mutation events, however, it notifies
286N/A * the document when mutations are performed so that the document class do so.
286N/A *
286N/A * <p><b>WARNING</b>: Some of the code here is partially duplicated in
286N/A * ParentNode, be careful to keep these two classes in sync!
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @see AttrNSImpl
286N/A *
286N/A * @author Arnaud Le Hors, IBM
286N/A * @author Joe Kesselman, IBM
286N/A * @author Andy Clark, IBM
286N/A * @version $Id: AttrImpl.java,v 1.5 2008/06/10 00:59:32 joehw Exp $
286N/A * @since PR-DOM-Level-1-19980818.
286N/A *
286N/A */
286N/Apublic class AttrImpl
286N/A extends NodeImpl
286N/A implements Attr, TypeInfo{
286N/A
286N/A //
286N/A // Constants
286N/A //
286N/A
286N/A /** Serialization version. */
286N/A static final long serialVersionUID = 7277707688218972102L;
286N/A
286N/A /** DTD namespace. **/
286N/A static final String DTD_URI = "http://www.w3.org/TR/REC-xml";
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** This can either be a String or the first child node. */
286N/A protected Object value = null;
286N/A
286N/A /** Attribute name. */
286N/A protected String name;
286N/A
286N/A /** Type information */
286N/A // REVISIT: we are losing the type information in DOM during serialization
286N/A transient Object type;
286N/A
286N/A protected TextImpl textNode = null;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /**
286N/A * Attribute has no public constructor. Please use the factory
286N/A * method in the Document class.
286N/A */
286N/A protected AttrImpl(CoreDocumentImpl ownerDocument, String name) {
286N/A super(ownerDocument);
286N/A this.name = name;
286N/A /** False for default attributes. */
286N/A isSpecified(true);
286N/A hasStringValue(true);
286N/A }
286N/A
286N/A // for AttrNSImpl
286N/A protected AttrImpl() {}
286N/A
286N/A // Support for DOM Level 3 renameNode method.
286N/A // Note: This only deals with part of the pb. It is expected to be
286N/A // called after the Attr has been detached for one thing.
286N/A // CoreDocumentImpl does all the work.
286N/A void rename(String name) {
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A this.name = name;
286N/A }
286N/A
286N/A // create a real text node as child if we don't have one yet
286N/A protected void makeChildNode() {
286N/A if (hasStringValue()) {
286N/A if (value != null) {
286N/A TextImpl text =
286N/A (TextImpl) ownerDocument().createTextNode((String) value);
286N/A value = text;
286N/A text.isFirstChild(true);
286N/A text.previousSibling = text;
286N/A text.ownerNode = this;
286N/A text.isOwned(true);
286N/A }
286N/A hasStringValue(false);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * NON-DOM
286N/A * set the ownerDocument of this node and its children
286N/A */
286N/A void setOwnerDocument(CoreDocumentImpl doc) {
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A super.setOwnerDocument(doc);
286N/A if (!hasStringValue()) {
286N/A for (ChildNode child = (ChildNode) value;
286N/A child != null; child = child.nextSibling) {
286N/A child.setOwnerDocument(doc);
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * NON-DOM: set the type of this attribute to be ID type.
286N/A *
286N/A * @param id
286N/A */
286N/A public void setIdAttribute(boolean id){
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A isIdAttribute(id);
286N/A }
286N/A /** DOM Level 3: isId*/
286N/A public boolean isId(){
286N/A // REVISIT: should an attribute that is not in the tree return
286N/A // isID true?
286N/A return isIdAttribute();
286N/A }
286N/A
286N/A
286N/A //
286N/A // Node methods
286N/A //
286N/A
286N/A public Node cloneNode(boolean deep) {
286N/A
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A AttrImpl clone = (AttrImpl) super.cloneNode(deep);
286N/A
286N/A // take care of case where there are kids
286N/A if (!clone.hasStringValue()) {
286N/A
286N/A // Need to break the association w/ original kids
286N/A clone.value = null;
286N/A
286N/A // Cloning an Attribute always clones its children,
286N/A // since they represent its value, no matter whether this
286N/A // is a deep clone or not
286N/A for (Node child = (Node) value; child != null;
286N/A child = child.getNextSibling()) {
286N/A clone.appendChild(child.cloneNode(true));
286N/A }
286N/A }
286N/A clone.isSpecified(true);
286N/A return clone;
286N/A }
286N/A
286N/A /**
286N/A * A short integer indicating what type of node this is. The named
286N/A * constants for this value are defined in the org.w3c.dom.Node interface.
286N/A */
286N/A public short getNodeType() {
286N/A return Node.ATTRIBUTE_NODE;
286N/A }
286N/A
286N/A /**
286N/A * Returns the attribute name
286N/A */
286N/A public String getNodeName() {
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A return name;
286N/A }
286N/A
286N/A /**
286N/A * Implicit in the rerouting of getNodeValue to getValue is the
286N/A * need to redefine setNodeValue, for symmetry's sake. Note that
286N/A * since we're explicitly providing a value, Specified should be set
286N/A * true.... even if that value equals the default.
286N/A */
286N/A public void setNodeValue(String value) throws DOMException {
286N/A setValue(value);
286N/A }
286N/A
286N/A /**
286N/A * @see org.w3c.dom.TypeInfo#getTypeName()
286N/A */
286N/A public String getTypeName() {
286N/A return (String)type;
286N/A }
286N/A
286N/A /**
286N/A * @see org.w3c.dom.TypeInfo#getTypeNamespace()
286N/A */
286N/A public String getTypeNamespace() {
286N/A if (type != null) {
286N/A return DTD_URI;
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Method getSchemaTypeInfo.
286N/A * @return TypeInfo
286N/A */
286N/A public TypeInfo getSchemaTypeInfo(){
286N/A return this;
286N/A }
286N/A
286N/A /**
286N/A * In Attribute objects, NodeValue is considered a synonym for
286N/A * Value.
286N/A *
286N/A * @see #getValue()
286N/A */
286N/A public String getNodeValue() {
286N/A return getValue();
286N/A }
286N/A
286N/A //
286N/A // Attr methods
286N/A //
286N/A
286N/A /**
286N/A * In Attributes, NodeName is considered a synonym for the
286N/A * attribute's Name
286N/A */
286N/A public String getName() {
286N/A
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A return name;
286N/A
286N/A } // getName():String
286N/A
286N/A /**
286N/A * The DOM doesn't clearly define what setValue(null) means. I've taken it
286N/A * as "remove all children", which from outside should appear
286N/A * similar to setting it to the empty string.
286N/A */
286N/A public void setValue(String newvalue) {
286N/A
286N/A CoreDocumentImpl ownerDocument = ownerDocument();
286N/A
286N/A if (ownerDocument.errorChecking && isReadOnly()) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
286N/A throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
286N/A }
286N/A
286N/A Element ownerElement = getOwnerElement();
286N/A String oldvalue = "";
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A if (value != null) {
286N/A if (ownerDocument.getMutationEvents()) {
286N/A // Can no longer just discard the kids; they may have
286N/A // event listeners waiting for them to disconnect.
286N/A if (hasStringValue()) {
286N/A oldvalue = (String) value;
286N/A // create an actual text node as our child so
286N/A // that we can use it in the event
286N/A if (textNode == null) {
286N/A textNode = (TextImpl)
286N/A ownerDocument.createTextNode((String) value);
286N/A }
286N/A else {
286N/A textNode.data = (String) value;
286N/A }
286N/A value = textNode;
286N/A textNode.isFirstChild(true);
286N/A textNode.previousSibling = textNode;
286N/A textNode.ownerNode = this;
286N/A textNode.isOwned(true);
286N/A hasStringValue(false);
286N/A internalRemoveChild(textNode, true);
286N/A }
286N/A else {
286N/A oldvalue = getValue();
286N/A while (value != null) {
286N/A internalRemoveChild((Node) value, true);
286N/A }
286N/A }
286N/A }
286N/A else {
286N/A if (hasStringValue()) {
286N/A oldvalue = (String) value;
286N/A }
286N/A else {
286N/A // simply discard children if any
286N/A oldvalue = getValue();
286N/A // remove ref from first child to last child
286N/A ChildNode firstChild = (ChildNode) value;
286N/A firstChild.previousSibling = null;
286N/A firstChild.isFirstChild(false);
286N/A firstChild.ownerNode = ownerDocument;
286N/A }
286N/A // then remove ref to current value
286N/A value = null;
286N/A needsSyncChildren(false);
286N/A }
286N/A if (isIdAttribute() && ownerElement != null) {
286N/A ownerDocument.removeIdentifier(oldvalue);
286N/A }
286N/A }
286N/A
286N/A // Create and add the new one, generating only non-aggregate events
286N/A // (There are no listeners on the new Text, but there may be
286N/A // capture/bubble listeners on the Attr.
286N/A // Note that aggregate events are NOT dispatched here,
286N/A // since we need to combine the remove and insert.
286N/A isSpecified(true);
286N/A if (ownerDocument.getMutationEvents()) {
286N/A // if there are any event handlers create a real node
286N/A internalInsertBefore(ownerDocument.createTextNode(newvalue),
286N/A null, true);
286N/A hasStringValue(false);
286N/A // notify document
286N/A ownerDocument.modifiedAttrValue(this, oldvalue);
286N/A } else {
286N/A // directly store the string
286N/A value = newvalue;
286N/A hasStringValue(true);
286N/A changed();
286N/A }
286N/A if (isIdAttribute() && ownerElement != null) {
286N/A ownerDocument.putIdentifier(newvalue, ownerElement);
286N/A }
286N/A
286N/A } // setValue(String)
286N/A
286N/A /**
286N/A * The "string value" of an Attribute is its text representation,
286N/A * which in turn is a concatenation of the string values of its children.
286N/A */
286N/A public String getValue() {
286N/A
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A if (value == null) {
286N/A return "";
286N/A }
286N/A if (hasStringValue()) {
286N/A return (String) value;
286N/A }
286N/A
286N/A ChildNode firstChild = ((ChildNode) value);
286N/A
286N/A String data = null;
286N/A if (firstChild.getNodeType() == Node.ENTITY_REFERENCE_NODE){
286N/A data = ((EntityReferenceImpl)firstChild).getEntityRefValue();
286N/A }
286N/A else {
286N/A data = firstChild.getNodeValue();
286N/A }
286N/A
286N/A ChildNode node = firstChild.nextSibling;
286N/A
286N/A if (node == null || data == null) return (data == null)?"":data;
286N/A
286N/A StringBuffer value = new StringBuffer(data);
286N/A while (node != null) {
286N/A if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE){
286N/A data = ((EntityReferenceImpl)node).getEntityRefValue();
286N/A if (data == null) return "";
286N/A value.append(data);
286N/A }
286N/A else {
286N/A value.append(node.getNodeValue());
286N/A }
286N/A node = node.nextSibling;
286N/A }
286N/A return value.toString();
286N/A
286N/A } // getValue():String
286N/A
286N/A
286N/A /**
286N/A * The "specified" flag is true if and only if this attribute's
286N/A * value was explicitly specified in the original document. Note that
286N/A * the implementation, not the user, is in charge of this
286N/A * property. If the user asserts an Attribute value (even if it ends
286N/A * up having the same value as the default), it is considered a
286N/A * specified attribute. If you really want to revert to the default,
286N/A * delete the attribute from the Element, and the Implementation will
286N/A * re-assert the default (if any) in its place, with the appropriate
286N/A * specified=false setting.
286N/A */
286N/A public boolean getSpecified() {
286N/A
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A return isSpecified();
286N/A
286N/A } // getSpecified():boolean
286N/A
286N/A //
286N/A // Attr2 methods
286N/A //
286N/A
286N/A /**
286N/A * Returns the element node that this attribute is associated with,
286N/A * or null if the attribute has not been added to an element.
286N/A *
286N/A * @see #getOwnerElement
286N/A *
286N/A * @deprecated Previous working draft of DOM Level 2. New method
286N/A * is <tt>getOwnerElement()</tt>.
286N/A */
286N/A public Element getElement() {
286N/A // if we have an owner, ownerNode is our ownerElement, otherwise it's
286N/A // our ownerDocument and we don't have an ownerElement
286N/A return (Element) (isOwned() ? ownerNode : null);
286N/A }
286N/A
286N/A /**
286N/A * Returns the element node that this attribute is associated with,
286N/A * or null if the attribute has not been added to an element.
286N/A *
286N/A * @since WD-DOM-Level-2-19990719
286N/A */
286N/A public Element getOwnerElement() {
286N/A // if we have an owner, ownerNode is our ownerElement, otherwise it's
286N/A // our ownerDocument and we don't have an ownerElement
286N/A return (Element) (isOwned() ? ownerNode : null);
286N/A }
286N/A
286N/A public void normalize() {
286N/A
286N/A // No need to normalize if already normalized or
286N/A // if value is kept as a String.
286N/A if (isNormalized() || hasStringValue())
286N/A return;
286N/A
286N/A Node kid, next;
286N/A ChildNode firstChild = (ChildNode)value;
286N/A for (kid = firstChild; kid != null; kid = next) {
286N/A next = kid.getNextSibling();
286N/A
286N/A // If kid is a text node, we need to check for one of two
286N/A // conditions:
286N/A // 1) There is an adjacent text node
286N/A // 2) There is no adjacent text node, but kid is
286N/A // an empty text node.
286N/A if ( kid.getNodeType() == Node.TEXT_NODE )
286N/A {
286N/A // If an adjacent text node, merge it with kid
286N/A if ( next!=null && next.getNodeType() == Node.TEXT_NODE )
286N/A {
286N/A ((Text)kid).appendData(next.getNodeValue());
286N/A removeChild( next );
286N/A next = kid; // Don't advance; there might be another.
286N/A }
286N/A else
286N/A {
286N/A // If kid is empty, remove it
286N/A if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) {
286N/A removeChild( kid );
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A isNormalized(true);
286N/A } // normalize()
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /** NON-DOM, for use by parser */
286N/A public void setSpecified(boolean arg) {
286N/A
286N/A if (needsSyncData()) {
286N/A synchronizeData();
286N/A }
286N/A isSpecified(arg);
286N/A
286N/A } // setSpecified(boolean)
286N/A
286N/A /**
286N/A * NON-DOM: used by the parser
286N/A * @param type
286N/A */
286N/A public void setType (Object type){
286N/A this.type = type;
286N/A }
286N/A
286N/A //
286N/A // Object methods
286N/A //
286N/A
286N/A /** NON-DOM method for debugging convenience */
286N/A public String toString() {
286N/A return getName() + "=" + "\"" + getValue() + "\"";
286N/A }
286N/A
286N/A /**
286N/A * Test whether this node has any children. Convenience shorthand
286N/A * for (Node.getFirstChild()!=null)
286N/A */
286N/A public boolean hasChildNodes() {
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A return value != null;
286N/A }
286N/A
286N/A /**
286N/A * Obtain a NodeList enumerating all children of this node. If there
286N/A * are none, an (initially) empty NodeList is returned.
286N/A * <p>
286N/A * NodeLists are "live"; as children are added/removed the NodeList
286N/A * will immediately reflect those changes. Also, the NodeList refers
286N/A * to the actual nodes, so changes to those nodes made via the DOM tree
286N/A * will be reflected in the NodeList and vice versa.
286N/A * <p>
286N/A * In this implementation, Nodes implement the NodeList interface and
286N/A * provide their own getChildNodes() support. Other DOMs may solve this
286N/A * differently.
286N/A */
286N/A public NodeList getChildNodes() {
286N/A // JKESS: KNOWN ISSUE HERE
286N/A
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A return this;
286N/A
286N/A } // getChildNodes():NodeList
286N/A
286N/A /** The first child of this Node, or null if none. */
286N/A public Node getFirstChild() {
286N/A
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A makeChildNode();
286N/A return (Node) value;
286N/A
286N/A } // getFirstChild():Node
286N/A
286N/A /** The last child of this Node, or null if none. */
286N/A public Node getLastChild() {
286N/A
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A return lastChild();
286N/A
286N/A } // getLastChild():Node
286N/A
286N/A final ChildNode lastChild() {
286N/A // last child is stored as the previous sibling of first child
286N/A makeChildNode();
286N/A return value != null ? ((ChildNode) value).previousSibling : null;
286N/A }
286N/A
286N/A final void lastChild(ChildNode node) {
286N/A // store lastChild as previous sibling of first child
286N/A if (value != null) {
286N/A ((ChildNode) value).previousSibling = node;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Move one or more node(s) to our list of children. Note that this
286N/A * implicitly removes them from their previous parent.
286N/A *
286N/A * @param newChild The Node to be moved to our subtree. As a
286N/A * convenience feature, inserting a DocumentNode will instead insert
286N/A * all its children.
286N/A *
286N/A * @param refChild Current child which newChild should be placed
286N/A * immediately before. If refChild is null, the insertion occurs
286N/A * after all existing Nodes, like appendChild().
286N/A *
286N/A * @return newChild, in its new state (relocated, or emptied in the case of
286N/A * DocumentNode.)
286N/A *
286N/A * @throws DOMException(HIERARCHY_REQUEST_ERR) if newChild is of a
286N/A * type that shouldn't be a child of this node, or if newChild is an
286N/A * ancestor of this node.
286N/A *
286N/A * @throws DOMException(WRONG_DOCUMENT_ERR) if newChild has a
286N/A * different owner document than we do.
286N/A *
286N/A * @throws DOMException(NOT_FOUND_ERR) if refChild is not a child of
286N/A * this node.
286N/A *
286N/A * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if this node is
286N/A * read-only.
286N/A */
286N/A public Node insertBefore(Node newChild, Node refChild)
286N/A throws DOMException {
286N/A // Tail-call; optimizer should be able to do good things with.
286N/A return internalInsertBefore(newChild, refChild, false);
286N/A } // insertBefore(Node,Node):Node
286N/A
286N/A /** NON-DOM INTERNAL: Within DOM actions,we sometimes need to be able
286N/A * to control which mutation events are spawned. This version of the
286N/A * insertBefore operation allows us to do so. It is not intended
286N/A * for use by application programs.
286N/A */
286N/A Node internalInsertBefore(Node newChild, Node refChild, boolean replace)
286N/A throws DOMException {
286N/A
286N/A CoreDocumentImpl ownerDocument = ownerDocument();
286N/A boolean errorChecking = ownerDocument.errorChecking;
286N/A
286N/A if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
286N/A // SLOW BUT SAFE: We could insert the whole subtree without
286N/A // juggling so many next/previous pointers. (Wipe out the
286N/A // parent's child-list, patch the parent pointers, set the
286N/A // ends of the list.) But we know some subclasses have special-
286N/A // case behavior they add to insertBefore(), so we don't risk it.
286N/A // This approch also takes fewer bytecodes.
286N/A
286N/A // NOTE: If one of the children is not a legal child of this
286N/A // node, throw HIERARCHY_REQUEST_ERR before _any_ of the children
286N/A // have been transferred. (Alternative behaviors would be to
286N/A // reparent up to the first failure point or reparent all those
286N/A // which are acceptable to the target node, neither of which is
286N/A // as robust. PR-DOM-0818 isn't entirely clear on which it
286N/A // recommends?????
286N/A
286N/A // No need to check kids for right-document; if they weren't,
286N/A // they wouldn't be kids of that DocFrag.
286N/A if (errorChecking) {
286N/A for (Node kid = newChild.getFirstChild(); // Prescan
286N/A kid != null; kid = kid.getNextSibling()) {
286N/A
286N/A if (!ownerDocument.isKidOK(this, kid)) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
286N/A throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
286N/A }
286N/A }
286N/A }
286N/A
286N/A while (newChild.hasChildNodes()) {
286N/A insertBefore(newChild.getFirstChild(), refChild);
286N/A }
286N/A return newChild;
286N/A }
286N/A
286N/A if (newChild == refChild) {
286N/A // stupid case that must be handled as a no-op triggering events...
286N/A refChild = refChild.getNextSibling();
286N/A removeChild(newChild);
286N/A insertBefore(newChild, refChild);
286N/A return newChild;
286N/A }
286N/A
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A
286N/A if (errorChecking) {
286N/A if (isReadOnly()) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
286N/A throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
286N/A }
286N/A if (newChild.getOwnerDocument() != ownerDocument) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
286N/A throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
286N/A }
286N/A if (!ownerDocument.isKidOK(this, newChild)) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
286N/A throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
286N/A }
286N/A // refChild must be a child of this node (or null)
286N/A if (refChild != null && refChild.getParentNode() != this) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);
286N/A throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
286N/A }
286N/A
286N/A // Prevent cycles in the tree
286N/A // newChild cannot be ancestor of this Node,
286N/A // and actually cannot be this
286N/A boolean treeSafe = true;
286N/A for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode())
286N/A {
286N/A treeSafe = newChild != a;
286N/A }
286N/A if (!treeSafe) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
286N/A throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
286N/A }
286N/A }
286N/A
286N/A makeChildNode(); // make sure we have a node and not a string
286N/A
286N/A // notify document
286N/A ownerDocument.insertingNode(this, replace);
286N/A
286N/A // Convert to internal type, to avoid repeated casting
286N/A ChildNode newInternal = (ChildNode)newChild;
286N/A
286N/A Node oldparent = newInternal.parentNode();
286N/A if (oldparent != null) {
286N/A oldparent.removeChild(newInternal);
286N/A }
286N/A
286N/A // Convert to internal type, to avoid repeated casting
286N/A ChildNode refInternal = (ChildNode) refChild;
286N/A
286N/A // Attach up
286N/A newInternal.ownerNode = this;
286N/A newInternal.isOwned(true);
286N/A
286N/A // Attach before and after
286N/A // Note: firstChild.previousSibling == lastChild!!
286N/A ChildNode firstChild = (ChildNode) value;
286N/A if (firstChild == null) {
286N/A // this our first and only child
286N/A value = newInternal; // firstchild = newInternal;
286N/A newInternal.isFirstChild(true);
286N/A newInternal.previousSibling = newInternal;
286N/A }
286N/A else {
286N/A if (refInternal == null) {
286N/A // this is an append
286N/A ChildNode lastChild = firstChild.previousSibling;
286N/A lastChild.nextSibling = newInternal;
286N/A newInternal.previousSibling = lastChild;
286N/A firstChild.previousSibling = newInternal;
286N/A }
286N/A else {
286N/A // this is an insert
286N/A if (refChild == firstChild) {
286N/A // at the head of the list
286N/A firstChild.isFirstChild(false);
286N/A newInternal.nextSibling = firstChild;
286N/A newInternal.previousSibling = firstChild.previousSibling;
286N/A firstChild.previousSibling = newInternal;
286N/A value = newInternal; // firstChild = newInternal;
286N/A newInternal.isFirstChild(true);
286N/A }
286N/A else {
286N/A // somewhere in the middle
286N/A ChildNode prev = refInternal.previousSibling;
286N/A newInternal.nextSibling = refInternal;
286N/A prev.nextSibling = newInternal;
286N/A refInternal.previousSibling = newInternal;
286N/A newInternal.previousSibling = prev;
286N/A }
286N/A }
286N/A }
286N/A
286N/A changed();
286N/A
286N/A // notify document
286N/A ownerDocument.insertedNode(this, newInternal, replace);
286N/A
286N/A checkNormalizationAfterInsert(newInternal);
286N/A
286N/A return newChild;
286N/A
286N/A } // internalInsertBefore(Node,Node,int):Node
286N/A
286N/A /**
286N/A * Remove a child from this Node. The removed child's subtree
286N/A * remains intact so it may be re-inserted elsewhere.
286N/A *
286N/A * @return oldChild, in its new state (removed).
286N/A *
286N/A * @throws DOMException(NOT_FOUND_ERR) if oldChild is not a child of
286N/A * this node.
286N/A *
286N/A * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if this node is
286N/A * read-only.
286N/A */
286N/A public Node removeChild(Node oldChild)
286N/A throws DOMException {
286N/A // Tail-call, should be optimizable
286N/A if (hasStringValue()) {
286N/A // we don't have any child per say so it can't be one of them!
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);
286N/A throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
286N/A }
286N/A return internalRemoveChild(oldChild, false);
286N/A } // removeChild(Node) :Node
286N/A
286N/A /** NON-DOM INTERNAL: Within DOM actions,we sometimes need to be able
286N/A * to control which mutation events are spawned. This version of the
286N/A * removeChild operation allows us to do so. It is not intended
286N/A * for use by application programs.
286N/A */
286N/A Node internalRemoveChild(Node oldChild, boolean replace)
286N/A throws DOMException {
286N/A
286N/A CoreDocumentImpl ownerDocument = ownerDocument();
286N/A if (ownerDocument.errorChecking) {
286N/A if (isReadOnly()) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
286N/A throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
286N/A }
286N/A if (oldChild != null && oldChild.getParentNode() != this) {
286N/A String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);
286N/A throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
286N/A }
286N/A }
286N/A
286N/A ChildNode oldInternal = (ChildNode) oldChild;
286N/A
286N/A // notify document
286N/A ownerDocument.removingNode(this, oldInternal, replace);
286N/A
286N/A // Patch linked list around oldChild
286N/A // Note: lastChild == firstChild.previousSibling
286N/A if (oldInternal == value) { // oldInternal == firstChild
286N/A // removing first child
286N/A oldInternal.isFirstChild(false);
286N/A // next line is: firstChild = oldInternal.nextSibling
286N/A value = oldInternal.nextSibling;
286N/A ChildNode firstChild = (ChildNode) value;
286N/A if (firstChild != null) {
286N/A firstChild.isFirstChild(true);
286N/A firstChild.previousSibling = oldInternal.previousSibling;
286N/A }
286N/A } else {
286N/A ChildNode prev = oldInternal.previousSibling;
286N/A ChildNode next = oldInternal.nextSibling;
286N/A prev.nextSibling = next;
286N/A if (next == null) {
286N/A // removing last child
286N/A ChildNode firstChild = (ChildNode) value;
286N/A firstChild.previousSibling = prev;
286N/A } else {
286N/A // removing some other child in the middle
286N/A next.previousSibling = prev;
286N/A }
286N/A }
286N/A
286N/A // Save previous sibling for normalization checking.
286N/A ChildNode oldPreviousSibling = oldInternal.previousSibling();
286N/A
286N/A // Remove oldInternal's references to tree
286N/A oldInternal.ownerNode = ownerDocument;
286N/A oldInternal.isOwned(false);
286N/A oldInternal.nextSibling = null;
286N/A oldInternal.previousSibling = null;
286N/A
286N/A changed();
286N/A
286N/A // notify document
286N/A ownerDocument.removedNode(this, replace);
286N/A
286N/A checkNormalizationAfterRemove(oldPreviousSibling);
286N/A
286N/A return oldInternal;
286N/A
286N/A } // internalRemoveChild(Node,int):Node
286N/A
286N/A /**
286N/A * Make newChild occupy the location that oldChild used to
286N/A * have. Note that newChild will first be removed from its previous
286N/A * parent, if any. Equivalent to inserting newChild before oldChild,
286N/A * then removing oldChild.
286N/A *
286N/A * @return oldChild, in its new state (removed).
286N/A *
286N/A * @throws DOMException(HIERARCHY_REQUEST_ERR) if newChild is of a
286N/A * type that shouldn't be a child of this node, or if newChild is
286N/A * one of our ancestors.
286N/A *
286N/A * @throws DOMException(WRONG_DOCUMENT_ERR) if newChild has a
286N/A * different owner document than we do.
286N/A *
286N/A * @throws DOMException(NOT_FOUND_ERR) if oldChild is not a child of
286N/A * this node.
286N/A *
286N/A * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if this node is
286N/A * read-only.
286N/A */
286N/A public Node replaceChild(Node newChild, Node oldChild)
286N/A throws DOMException {
286N/A
286N/A makeChildNode();
286N/A
286N/A // If Mutation Events are being generated, this operation might
286N/A // throw aggregate events twice when modifying an Attr -- once
286N/A // on insertion and once on removal. DOM Level 2 does not specify
286N/A // this as either desirable or undesirable, but hints that
286N/A // aggregations should be issued only once per user request.
286N/A
286N/A // notify document
286N/A CoreDocumentImpl ownerDocument = ownerDocument();
286N/A ownerDocument.replacingNode(this);
286N/A
286N/A internalInsertBefore(newChild, oldChild, true);
286N/A if (newChild != oldChild) {
286N/A internalRemoveChild(oldChild, true);
286N/A }
286N/A
286N/A // notify document
286N/A ownerDocument.replacedNode(this);
286N/A
286N/A return oldChild;
286N/A }
286N/A
286N/A //
286N/A // NodeList methods
286N/A //
286N/A
286N/A /**
286N/A * NodeList method: Count the immediate children of this node
286N/A * @return int
286N/A */
286N/A public int getLength() {
286N/A
286N/A if (hasStringValue()) {
286N/A return 1;
286N/A }
286N/A ChildNode node = (ChildNode) value;
286N/A int length = 0;
286N/A for (; node != null; node = node.nextSibling) {
286N/A length++;
286N/A }
286N/A return length;
286N/A
286N/A } // getLength():int
286N/A
286N/A /**
286N/A * NodeList method: Return the Nth immediate child of this node, or
286N/A * null if the index is out of bounds.
286N/A * @return org.w3c.dom.Node
286N/A * @param Index int
286N/A */
286N/A public Node item(int index) {
286N/A
286N/A if (hasStringValue()) {
286N/A if (index != 0 || value == null) {
286N/A return null;
286N/A }
286N/A else {
286N/A makeChildNode();
286N/A return (Node) value;
286N/A }
286N/A }
286N/A if (index < 0) {
286N/A return null;
286N/A }
286N/A ChildNode node = (ChildNode) value;
286N/A for (int i = 0; i < index && node != null; i++) {
286N/A node = node.nextSibling;
286N/A }
286N/A return node;
286N/A
286N/A } // item(int):Node
286N/A
286N/A //
286N/A // DOM3
286N/A //
286N/A
286N/A /**
286N/A * DOM Level 3 WD- Experimental.
286N/A * Override inherited behavior from ParentNode to support deep equal.
286N/A * isEqualNode is always deep on Attr nodes.
286N/A */
286N/A public boolean isEqualNode(Node arg) {
286N/A return super.isEqualNode(arg);
286N/A }
286N/A
286N/A /**
286N/A * Introduced in DOM Level 3. <p>
286N/A * Checks if a type is derived from another by restriction. See:
286N/A * http://www.w3.org/TR/DOM-Level-3-Core/core.html#TypeInfo-isDerivedFrom
286N/A *
286N/A * @param ancestorNS
286N/A * The namspace of the ancestor type declaration
286N/A * @param ancestorName
286N/A * The name of the ancestor type declaration
286N/A * @param type
286N/A * The reference type definition
286N/A *
286N/A * @return boolean True if the type is derived by restriciton for the
286N/A * reference type
286N/A */
286N/A public boolean isDerivedFrom(String typeNamespaceArg,
286N/A String typeNameArg,
286N/A int derivationMethod) {
286N/A
286N/A return false;
286N/A }
286N/A
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /**
286N/A * Override default behavior so that if deep is true, children are also
286N/A * toggled.
286N/A * @see Node
286N/A * <P>
286N/A * Note: this will not change the state of an EntityReference or its
286N/A * children, which are always read-only.
286N/A */
286N/A public void setReadOnly(boolean readOnly, boolean deep) {
286N/A
286N/A super.setReadOnly(readOnly, deep);
286N/A
286N/A if (deep) {
286N/A
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A
286N/A if (hasStringValue()) {
286N/A return;
286N/A }
286N/A // Recursively set kids
286N/A for (ChildNode mykid = (ChildNode) value;
286N/A mykid != null;
286N/A mykid = mykid.nextSibling) {
286N/A if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) {
286N/A mykid.setReadOnly(readOnly,true);
286N/A }
286N/A }
286N/A }
286N/A } // setReadOnly(boolean,boolean)
286N/A
286N/A //
286N/A // Protected methods
286N/A //
286N/A
286N/A /**
286N/A * Override this method in subclass to hook in efficient
286N/A * internal data structure.
286N/A */
286N/A protected void synchronizeChildren() {
286N/A // By default just change the flag to avoid calling this method again
286N/A needsSyncChildren(false);
286N/A }
286N/A
286N/A /**
286N/A * Checks the normalized state of this node after inserting a child.
286N/A * If the inserted child causes this node to be unnormalized, then this
286N/A * node is flagged accordingly.
286N/A * The conditions for changing the normalized state are:
286N/A * <ul>
286N/A * <li>The inserted child is a text node and one of its adjacent siblings
286N/A * is also a text node.
286N/A * <li>The inserted child is is itself unnormalized.
286N/A * </ul>
286N/A *
286N/A * @param insertedChild the child node that was inserted into this node
286N/A *
286N/A * @throws NullPointerException if the inserted child is <code>null</code>
286N/A */
286N/A void checkNormalizationAfterInsert(ChildNode insertedChild) {
286N/A // See if insertion caused this node to be unnormalized.
286N/A if (insertedChild.getNodeType() == Node.TEXT_NODE) {
286N/A ChildNode prev = insertedChild.previousSibling();
286N/A ChildNode next = insertedChild.nextSibling;
286N/A // If an adjacent sibling of the new child is a text node,
286N/A // flag this node as unnormalized.
286N/A if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) ||
286N/A (next != null && next.getNodeType() == Node.TEXT_NODE)) {
286N/A isNormalized(false);
286N/A }
286N/A }
286N/A else {
286N/A // If the new child is not normalized,
286N/A // then this node is inherently not normalized.
286N/A if (!insertedChild.isNormalized()) {
286N/A isNormalized(false);
286N/A }
286N/A }
286N/A } // checkNormalizationAfterInsert(ChildNode)
286N/A
286N/A /**
286N/A * Checks the normalized of this node after removing a child.
286N/A * If the removed child causes this node to be unnormalized, then this
286N/A * node is flagged accordingly.
286N/A * The conditions for changing the normalized state are:
286N/A * <ul>
286N/A * <li>The removed child had two adjacent siblings that were text nodes.
286N/A * </ul>
286N/A *
286N/A * @param previousSibling the previous sibling of the removed child, or
286N/A * <code>null</code>
286N/A */
286N/A void checkNormalizationAfterRemove(ChildNode previousSibling) {
286N/A // See if removal caused this node to be unnormalized.
286N/A // If the adjacent siblings of the removed child were both text nodes,
286N/A // flag this node as unnormalized.
286N/A if (previousSibling != null &&
286N/A previousSibling.getNodeType() == Node.TEXT_NODE) {
286N/A
286N/A ChildNode next = previousSibling.nextSibling;
286N/A if (next != null && next.getNodeType() == Node.TEXT_NODE) {
286N/A isNormalized(false);
286N/A }
286N/A }
286N/A } // checkNormalizationAfterRemove(ChildNode)
286N/A
286N/A //
286N/A // Serialization methods
286N/A //
286N/A
286N/A /** Serialize object. */
286N/A private void writeObject(ObjectOutputStream out) throws IOException {
286N/A
286N/A // synchronize chilren
286N/A if (needsSyncChildren()) {
286N/A synchronizeChildren();
286N/A }
286N/A // write object
286N/A out.defaultWriteObject();
286N/A
286N/A } // writeObject(ObjectOutputStream)
286N/A
286N/A /** Deserialize object. */
286N/A private void readObject(ObjectInputStream ois)
286N/A throws ClassNotFoundException, IOException {
286N/A
286N/A // perform default deseralization
286N/A ois.defaultReadObject();
286N/A
286N/A // hardset synchildren - so we don't try to sync -
286N/A // it does not make any sense to try to synchildren when we just
286N/A // deserialize object.
286N/A needsSyncChildren(false);
286N/A
286N/A } // readObject(ObjectInputStream)
286N/A
286N/A
286N/A} // class AttrImpl