Class AttrImpl

  • All Implemented Interfaces:
    Serializable, Cloneable, Attr, EventTarget, Node, NodeList, TypeInfo
    Direct Known Subclasses:
    AttrNSImpl, DeferredAttrImpl

    public class AttrImpl
    extends NodeImpl
    implements Attr, TypeInfo
    Attribute represents an XML-style attribute of an Element. Typically, the allowable values are controlled by its declaration in the Document Type Definition (DTD) governing this kind of document.

    If the attribute has not been explicitly assigned a value, but has been declared in the DTD, it will exist and have that default. Only if neither the document nor the DTD specifies a value will the Attribute really be considered absent and have no value; in that case, querying the attribute will return null.

    Attributes may have multiple children that contain their data. (XML allows attributes to contain entity references, and tokenized attribute types such as NMTOKENS may have a child for each token.) For convenience, the Attribute object's getValue() method returns the string version of the attribute's value.

    Attributes are not children of the Elements they belong to, in the usual sense, and have no valid Parent reference. However, the spec says they _do_ belong to a specific Element, and an INUSE exception is to be thrown if the user attempts to explicitly share them between elements.

    Note that Elements do not permit attributes to appear to be shared (see the INUSE exception), so this object's mutability is officially not an issue.

    Note: The ownerNode attribute is used to store the Element the Attr node is associated with. Attr nodes do not have parent nodes. Besides, the getOwnerElement() method can be used to get the element node this attribute is associated with.

    AttrImpl does not support Namespaces. AttrNSImpl, which inherits from it, does.

    AttrImpl used to inherit from ParentNode. It now directly inherits from NodeImpl and provide its own implementation of the ParentNode's behavior. The reason is that we now try and avoid to always create a Text node to hold the value of an attribute. The DOM spec requires it, so we still have to do it in case getFirstChild() is called for instance. The reason attribute values are stored as a list of nodes is so that they can carry more than a simple string. They can also contain EntityReference nodes. However, most of the times people only have a single string that they only set and get through Element.set/getAttribute or Attr.set/getValue. In this new version, the Attr node has a value pointer which can either be the String directly or a pointer to the first ChildNode. A flag tells which one it currently is. Note that while we try to stick with the direct String as much as possible once we've switched to a node there is no going back. This is because we have no way to know whether the application keeps referring to the node we once returned.

    The gain in memory varies on the density of attributes in the document. But in the tests I've run I've seen up to 12% of memory gain. And the good thing is that it also leads to a slight gain in speed because we allocate fewer objects! I mean, that's until we have to actually create the node...

    To avoid too much duplicated code, I got rid of ParentNode and renamed ChildAndParentNode, which I never really liked, to ParentNode for simplicity, this doesn't make much of a difference in memory usage because there are only very few objects that are only a Parent. This is only true now because AttrImpl now inherits directly from NodeImpl and has its own implementation of the ParentNode's node behavior. So there is still some duplicated code there.

    This class doesn't directly support mutation events, however, it notifies the document when mutations are performed so that the document class do so.

    WARNING: Some of the code here is partially duplicated in ParentNode, be careful to keep these two classes in sync!

    Since:
    PR-DOM-Level-1-19980818.
    Version:
    $Id$
    Author:
    Arnaud Le Hors, IBM, Joe Kesselman, IBM, Andy Clark, IBM
    See Also:
    AttrNSImpl, Serialized Form
    • Field Detail

      • value

        protected Object value
        This can either be a String or the first child node.
      • name

        protected String name
        Attribute name.
    • Constructor Detail

      • AttrImpl

        protected AttrImpl​(CoreDocumentImpl ownerDocument,
                           String name)
        Attribute has no public constructor. Please use the factory method in the Document class.
      • AttrImpl

        protected AttrImpl()
    • Method Detail

      • makeChildNode

        protected void makeChildNode()
      • setIdAttribute

        public void setIdAttribute​(boolean id)
        NON-DOM: set the type of this attribute to be ID type.
        Parameters:
        id -
      • isId

        public boolean isId()
        DOM Level 3: isId
        Specified by:
        isId in interface Attr
      • getNodeType

        public short getNodeType()
        A short integer indicating what type of node this is. The named constants for this value are defined in the org.w3c.dom.Node interface.
        Specified by:
        getNodeType in interface Node
        Specified by:
        getNodeType in class NodeImpl
      • setNodeValue

        public void setNodeValue​(String value)
                          throws DOMException
        Implicit in the rerouting of getNodeValue to getValue is the need to redefine setNodeValue, for symmetry's sake. Note that since we're explicitly providing a value, Specified should be set true.... even if that value equals the default.
        Specified by:
        setNodeValue in interface Node
        Overrides:
        setNodeValue in class NodeImpl
        Throws:
        DOMException
      • getSchemaTypeInfo

        public TypeInfo getSchemaTypeInfo()
        Method getSchemaTypeInfo.
        Specified by:
        getSchemaTypeInfo in interface Attr
        Returns:
        TypeInfo
      • getName

        public String getName()
        In Attributes, NodeName is considered a synonym for the attribute's Name
        Specified by:
        getName in interface Attr
      • setValue

        public void setValue​(String newvalue)
        The DOM doesn't clearly define what setValue(null) means. I've taken it as "remove all children", which from outside should appear similar to setting it to the empty string.
        Specified by:
        setValue in interface Attr
      • getValue

        public String getValue()
        The "string value" of an Attribute is its text representation, which in turn is a concatenation of the string values of its children.
        Specified by:
        getValue in interface Attr
      • getSpecified

        public boolean getSpecified()
        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.
        Specified by:
        getSpecified in interface Attr
      • getElement

        public Element getElement()
        Deprecated.
        Previous working draft of DOM Level 2. New method is getOwnerElement().
        Returns the element node that this attribute is associated with, or null if the attribute has not been added to an element.
        See Also:
        getOwnerElement()
      • getOwnerElement

        public Element getOwnerElement()
        Returns the element node that this attribute is associated with, or null if the attribute has not been added to an element.
        Specified by:
        getOwnerElement in interface Attr
        Since:
        WD-DOM-Level-2-19990719
      • normalize

        public void normalize()
        Description copied from class: NodeImpl
        Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only markup (e.g., tags, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are no adjacent Text nodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer lookups) that depend on a particular document tree structure are to be used.In cases where the document contains CDATASections, the normalize operation alone may not be sufficient, since XPointers do not differentiate between Text nodes and CDATASection nodes.

        Note that this implementation simply calls normalize() on this Node's children. It is up to implementors or Node to override normalize() to take action.

        Specified by:
        normalize in interface Node
        Overrides:
        normalize in class NodeImpl
      • setSpecified

        public void setSpecified​(boolean arg)
        NON-DOM, for use by parser
      • setType

        public void setType​(Object type)
        NON-DOM: used by the parser
        Parameters:
        type -
      • toString

        public String toString()
        NON-DOM method for debugging convenience
        Overrides:
        toString in class NodeImpl
      • hasChildNodes

        public boolean hasChildNodes()
        Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
        Specified by:
        hasChildNodes in interface Node
        Overrides:
        hasChildNodes in class NodeImpl
        See Also:
        ParentNode
      • getChildNodes

        public NodeList getChildNodes()
        Obtain a NodeList enumerating all children of this node. If there are none, an (initially) empty NodeList is returned.

        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.

        In this implementation, Nodes implement the NodeList interface and provide their own getChildNodes() support. Other DOMs may solve this differently.

        Specified by:
        getChildNodes in interface Node
        Overrides:
        getChildNodes in class NodeImpl
      • insertBefore

        public Node insertBefore​(Node newChild,
                                 Node refChild)
                          throws DOMException
        Move one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.
        Specified by:
        insertBefore in interface Node
        Overrides:
        insertBefore in class NodeImpl
        Parameters:
        newChild - The Node to be moved to our subtree. As a convenience feature, inserting a DocumentNode will instead insert all its children.
        refChild - Current child which newChild should be placed immediately before. If refChild is null, the insertion occurs after all existing Nodes, like appendChild().
        Returns:
        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.
        DOMException(WRONG_DOCUMENT_ERR) - if newChild has a different owner document than we do.
        DOMException(NOT_FOUND_ERR) - if refChild is not a child of this node.
        DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.
        DOMException
        See Also:
        ParentNode
      • removeChild

        public Node removeChild​(Node oldChild)
                         throws DOMException
        Remove a child from this Node. The removed child's subtree remains intact so it may be re-inserted elsewhere.
        Specified by:
        removeChild in interface Node
        Overrides:
        removeChild in class NodeImpl
        Returns:
        oldChild, in its new state (removed).
        Throws:
        DOMException(NOT_FOUND_ERR) - if oldChild is not a child of this node.
        DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.
        DOMException
        See Also:
        ParentNode
      • replaceChild

        public Node replaceChild​(Node newChild,
                                 Node oldChild)
                          throws DOMException
        Make newChild occupy the location that oldChild used to have. Note that newChild will first be removed from its previous parent, if any. Equivalent to inserting newChild before oldChild, then removing oldChild.
        Specified by:
        replaceChild in interface Node
        Overrides:
        replaceChild in class NodeImpl
        Returns:
        oldChild, in its new state (removed).
        Throws:
        DOMException(HIERARCHY_REQUEST_ERR) - if newChild is of a type that shouldn't be a child of this node, or if newChild is one of our ancestors.
        DOMException(WRONG_DOCUMENT_ERR) - if newChild has a different owner document than we do.
        DOMException(NOT_FOUND_ERR) - if oldChild is not a child of this node.
        DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.
        DOMException
        See Also:
        ParentNode
      • item

        public Node item​(int index)
        NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
        Specified by:
        item in interface NodeList
        Overrides:
        item in class NodeImpl
        Parameters:
        index - int
        Returns:
        org.w3c.dom.Node
        See Also:
        ParentNode
      • isEqualNode

        public boolean isEqualNode​(Node arg)
        DOM Level 3 WD- Experimental. Override inherited behavior from ParentNode to support deep equal. isEqualNode is always deep on Attr nodes.
        Specified by:
        isEqualNode in interface Node
        Overrides:
        isEqualNode in class NodeImpl
        Parameters:
        arg - The node to compare equality with.
        Returns:
        If the nodes, and possibly subtrees are equal, true otherwise false.
      • isDerivedFrom

        public boolean isDerivedFrom​(String typeNamespaceArg,
                                     String typeNameArg,
                                     int derivationMethod)
        Introduced in DOM Level 3.

        Checks if a type is derived from another by restriction. See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#TypeInfo-isDerivedFrom

        Specified by:
        isDerivedFrom in interface TypeInfo
        Parameters:
        typeNamespaceArg - The namspace of the ancestor type declaration
        typeNameArg - The name of the ancestor type declaration
        derivationMethod - The derivation method
        Returns:
        boolean True if the type is derived by restriciton for the reference type
      • synchronizeChildren

        protected void synchronizeChildren()
        Override this method in subclass to hook in efficient internal data structure.