Class AttrImpl
- java.lang.Object
-
- org.htmlunit.cyberneko.xerces.dom.NodeImpl
-
- org.htmlunit.cyberneko.xerces.dom.AttrImpl
-
- All Implemented Interfaces:
java.lang.Cloneable,org.w3c.dom.Attr,org.w3c.dom.events.EventTarget,org.w3c.dom.Node,org.w3c.dom.NodeList,org.w3c.dom.TypeInfo
- Direct Known Subclasses:
AttrNSImpl
public class AttrImpl extends NodeImpl implements org.w3c.dom.Attr, org.w3c.dom.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!
- See Also:
AttrNSImpl
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringDTD_URIDTD namespace.protected java.lang.StringnameAttribute name.protected java.lang.StringtypeType informationprivate java.lang.Objectvalue_This can either be a String or the first child node.-
Fields inherited from class org.htmlunit.cyberneko.xerces.dom.NodeImpl
DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_IS_CONTAINED, DOCUMENT_POSITION_PRECEDING, FIRSTCHILD, HASSTRING, ID, NORMALIZED, OWNED, ownerNode_, READONLY, SPECIFIED, SYNCCHILDREN
-
Fields inherited from interface org.w3c.dom.Node
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAttrImpl(CoreDocumentImpl ownerDocument, java.lang.String name)Attribute has no public constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidcheckNormalizationAfterInsert(ChildNode insertedChild)Checks the normalized state of this node after inserting a child.(package private) voidcheckNormalizationAfterRemove(ChildNode previousSibling)Checks the normalized of this node after removing a child.org.w3c.dom.NodecloneNode(boolean deep)Returns a duplicate of a given node.org.w3c.dom.NodeListgetChildNodes()Obtain a NodeList enumerating all children of this node.org.w3c.dom.NodegetFirstChild()The first child of this Node, or null if none.org.w3c.dom.NodegetLastChild()The first child of this Node, or null if none.intgetLength()NodeList method: Count the immediate children of this nodejava.lang.StringgetName()In Attributes, NodeName is considered a synonym for the attribute's Namejava.lang.StringgetNodeName()the name of this node.shortgetNodeType()A short integer indicating what type of node this is.java.lang.StringgetNodeValue()Returns the node value.org.w3c.dom.ElementgetOwnerElement()Returns the element node that this attribute is associated with, or null if the attribute has not been added to an element.org.w3c.dom.TypeInfogetSchemaTypeInfo()booleangetSpecified()The "specified" flag is true if and only if this attribute's value was explicitly specified in the original document.java.lang.StringgetTypeName()java.lang.StringgetTypeNamespace()java.lang.StringgetValue()The "string value" of an Attribute is its text representation, which in turn is a concatenation of the string values of its children.booleanhasChildNodes()Test whether this node has any children.org.w3c.dom.NodeinsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild)Move one or more node(s) to our list of children.(package private) org.w3c.dom.NodeinternalInsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild, boolean replace)(package private) org.w3c.dom.NodeinternalRemoveChild(org.w3c.dom.Node oldChild, boolean replace)NON-DOM INTERNAL: Within DOM actions,we sometimes need to be able to control which mutation events are spawned.booleanisDerivedFrom(java.lang.String typeNamespaceArg, java.lang.String typeNameArg, int derivationMethod)Introduced in DOM Level 3.booleanisEqualNode(org.w3c.dom.Node arg)DOM Level 3 WD- Experimental.booleanisId()DOM Level 3: isIdorg.w3c.dom.Nodeitem(int index)NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.(package private) ChildNodelastChild()protected voidmakeChildNode()org.w3c.dom.NoderemoveChild(org.w3c.dom.Node oldChild)Remove a child from this Node.(package private) voidrename(java.lang.String name)org.w3c.dom.NodereplaceChild(org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild)Make newChild occupy the location that oldChild used to have.voidsetNodeValue(java.lang.String value)Sets the node value.protected voidsetOwnerDocument(CoreDocumentImpl doc)NON-DOM set the ownerDocument of this node and its childrenvoidsetSpecified(boolean arg)voidsetType(java.lang.String type)voidsetValue(java.lang.String newvalue)The DOM doesn't clearly define what setValue(null) means.java.lang.StringtoString()-
Methods inherited from class org.htmlunit.cyberneko.xerces.dom.NodeImpl
addEventListener, appendChild, changed, changes, compareDocumentPosition, dispatchEvent, getAttributes, getBaseURI, getElementAncestor, getFeature, getLocalName, getNamespaceURI, getNextSibling, getNodeNumber, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getTextContent, getTextContent, getUserData, hasAttributes, hasStringValue, hasStringValue, isDefaultNamespace, isFirstChild, isFirstChild, isIdAttribute, isIdAttribute, isNormalized, isNormalized, isOwned, isOwned, isSameNode, isSpecified, isSpecified, isSupported, lookupNamespacePrefix, lookupNamespaceURI, lookupPrefix, needsSyncChildren, needsSyncChildren, normalize, ownerDocument, parentNode, previousSibling, removeEventListener, setPrefix, setTextContent, setUserData
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.w3c.dom.Node
appendChild, compareDocumentPosition, getAttributes, getBaseURI, getFeature, getLocalName, getNamespaceURI, getNextSibling, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getTextContent, getUserData, hasAttributes, isDefaultNamespace, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, setPrefix, setTextContent, setUserData
-
-
-
-
Field Detail
-
DTD_URI
protected static final java.lang.String DTD_URI
DTD namespace.- See Also:
- Constant Field Values
-
value_
private java.lang.Object value_
This can either be a String or the first child node.
-
name
protected java.lang.String name
Attribute name.
-
type
protected java.lang.String type
Type information
-
-
Constructor Detail
-
AttrImpl
protected AttrImpl(CoreDocumentImpl ownerDocument, java.lang.String name)
Attribute has no public constructor. Please use the factory method in the Document class.- Parameters:
ownerDocument- the owner documentname- the name
-
-
Method Detail
-
rename
void rename(java.lang.String name)
-
makeChildNode
protected void makeChildNode()
-
setOwnerDocument
protected void setOwnerDocument(CoreDocumentImpl doc)
NON-DOM set the ownerDocument of this node and its children- Overrides:
setOwnerDocumentin classNodeImpl
-
isId
public boolean isId()
DOM Level 3: isId- Specified by:
isIdin interfaceorg.w3c.dom.Attr
-
cloneNode
public org.w3c.dom.Node cloneNode(boolean deep)
Description copied from class:NodeImplReturns a duplicate of a given node. You can consider this a generic "copy constructor" for nodes. The newly returned object should be completely independent of the source object's subtree, so changes in one after the clone has been made will not affect the other.Note: since we never have any children deep is meaningless here, ParentNode overrides this behavior.
- Specified by:
cloneNodein interfaceorg.w3c.dom.Node- Overrides:
cloneNodein classNodeImpl- See Also:
Example: Cloning a Text node will copy both the node and the text it contains. Example: Cloning something that has children -- Element or Attr, for example -- will _not_ clone those children unless a "deep clone" has been requested. A shallow clone of an Attr node will yield an empty Attr of the same name. NOTE: Clones will always be read/write, even if the node being cloned is read-only, to permit applications using only the DOM API to obtain editable copies of locked portions of the tree.
-
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:
getNodeTypein interfaceorg.w3c.dom.Node- Specified by:
getNodeTypein classNodeImpl
-
getNodeName
public java.lang.String getNodeName()
the name of this node.- Specified by:
getNodeNamein interfaceorg.w3c.dom.Node- Specified by:
getNodeNamein classNodeImpl
-
setNodeValue
public void setNodeValue(java.lang.String value) throws org.w3c.dom.DOMExceptionSets the node value. 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:
setNodeValuein interfaceorg.w3c.dom.Node- Overrides:
setNodeValuein classNodeImpl- Throws:
org.w3c.dom.DOMException- NO_MODIFICATION_ALLOWED_ERR
-
getTypeName
public java.lang.String getTypeName()
- Specified by:
getTypeNamein interfaceorg.w3c.dom.TypeInfo
-
getTypeNamespace
public java.lang.String getTypeNamespace()
- Specified by:
getTypeNamespacein interfaceorg.w3c.dom.TypeInfo
-
getSchemaTypeInfo
public org.w3c.dom.TypeInfo getSchemaTypeInfo()
- Specified by:
getSchemaTypeInfoin interfaceorg.w3c.dom.Attr
-
getNodeValue
public java.lang.String getNodeValue()
Returns the node value.- Specified by:
getNodeValuein interfaceorg.w3c.dom.Node- Overrides:
getNodeValuein classNodeImpl
-
getName
public java.lang.String getName()
In Attributes, NodeName is considered a synonym for the attribute's Name- Specified by:
getNamein interfaceorg.w3c.dom.Attr
-
setValue
public void setValue(java.lang.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:
setValuein interfaceorg.w3c.dom.Attr
-
getValue
public java.lang.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:
getValuein interfaceorg.w3c.dom.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:
getSpecifiedin interfaceorg.w3c.dom.Attr
-
getOwnerElement
public org.w3c.dom.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:
getOwnerElementin interfaceorg.w3c.dom.Attr
-
setSpecified
public void setSpecified(boolean arg)
-
setType
public void setType(java.lang.String type)
-
hasChildNodes
public boolean hasChildNodes()
Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)By default we do not have any children, ParentNode overrides this. Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
- Specified by:
hasChildNodesin interfaceorg.w3c.dom.Node- Overrides:
hasChildNodesin classNodeImpl- See Also:
ParentNode
-
getChildNodes
public org.w3c.dom.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. 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:
getChildNodesin interfaceorg.w3c.dom.Node- Overrides:
getChildNodesin classNodeImpl
-
getFirstChild
public org.w3c.dom.Node getFirstChild()
The first child of this Node, or null if none.By default we do not have any children, ParentNode overrides this.
- Specified by:
getFirstChildin interfaceorg.w3c.dom.Node- Overrides:
getFirstChildin classNodeImpl- See Also:
ParentNode
-
getLastChild
public org.w3c.dom.Node getLastChild()
The first child of this Node, or null if none.By default we do not have any children, ParentNode overrides this.
- Specified by:
getLastChildin interfaceorg.w3c.dom.Node- Overrides:
getLastChildin classNodeImpl- See Also:
ParentNode
-
lastChild
final ChildNode lastChild()
-
insertBefore
public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild) throws org.w3c.dom.DOMExceptionMove one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.By default we do not accept any children, ParentNode overrides this. Move one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.
- Specified by:
insertBeforein interfaceorg.w3c.dom.Node- Overrides:
insertBeforein classNodeImpl- 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:
org.w3c.dom.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.org.w3c.dom.DOMException- WRONG_DOCUMENT_ERR if newChild has a different owner document than we do.org.w3c.dom.DOMException- NOT_FOUND_ERR if refChild is not a child of this node.org.w3c.dom.DOMException- NO_MODIFICATION_ALLOWED_ERR if this node is read-only.- See Also:
ParentNode
-
internalInsertBefore
org.w3c.dom.Node internalInsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild, boolean replace) throws org.w3c.dom.DOMException- Throws:
org.w3c.dom.DOMException
-
removeChild
public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws org.w3c.dom.DOMExceptionRemove a child from this Node. The removed child's subtree remains intact so it may be re-inserted elsewhere.- Specified by:
removeChildin interfaceorg.w3c.dom.Node- Overrides:
removeChildin classNodeImpl- Returns:
- oldChild, in its new state (removed).
- Throws:
org.w3c.dom.DOMException- NOT_FOUND_ERR if oldChild is not a child of this node.org.w3c.dom.DOMException- NO_MODIFICATION_ALLOWED_ERR if this node is read-only.- See Also:
ParentNode
-
internalRemoveChild
org.w3c.dom.Node internalRemoveChild(org.w3c.dom.Node oldChild, boolean replace) throws org.w3c.dom.DOMExceptionNON-DOM INTERNAL: Within DOM actions,we sometimes need to be able to control which mutation events are spawned. This version of the removeChild operation allows us to do so. It is not intended for use by application programs.- Throws:
org.w3c.dom.DOMException
-
replaceChild
public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild) throws org.w3c.dom.DOMExceptionMake 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:
replaceChildin interfaceorg.w3c.dom.Node- Overrides:
replaceChildin classNodeImpl- Returns:
- oldChild, in its new state (removed).
- Throws:
org.w3c.dom.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.org.w3c.dom.DOMException- WRONG_DOCUMENT_ERR if newChild has a different owner document than we do.org.w3c.dom.DOMException- NOT_FOUND_ERR if oldChild is not a child of this node.org.w3c.dom.DOMException- NO_MODIFICATION_ALLOWED_ERR if this node is read-only.- See Also:
ParentNode
-
getLength
public int getLength()
NodeList method: Count the immediate children of this node
-
item
public org.w3c.dom.Node item(int index)
NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
-
isEqualNode
public boolean isEqualNode(org.w3c.dom.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:
isEqualNodein interfaceorg.w3c.dom.Node- Overrides:
isEqualNodein classNodeImpl- Parameters:
arg- The node to compare equality with.- Returns:
- If the nodes, and possibly subtrees are equal,
trueotherwisefalse.
-
isDerivedFrom
public boolean isDerivedFrom(java.lang.String typeNamespaceArg, java.lang.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:
isDerivedFromin interfaceorg.w3c.dom.TypeInfo- Parameters:
typeNamespaceArg- The namspace of the ancestor type declarationtypeNameArg- The name of the ancestor type declarationderivationMethod- The derivation method- Returns:
- boolean True if the type is derived by restriciton for the reference type
-
checkNormalizationAfterInsert
void checkNormalizationAfterInsert(ChildNode insertedChild)
Checks the normalized state of this node after inserting a child. If the inserted child causes this node to be unnormalized, then this node is flagged accordingly. The conditions for changing the normalized state are:- The inserted child is a text node and one of its adjacent siblings is also a text node.
- The inserted child is is itself unnormalized.
- Parameters:
insertedChild- the child node that was inserted into this node- Throws:
java.lang.NullPointerException- if the inserted child isnull
-
checkNormalizationAfterRemove
void checkNormalizationAfterRemove(ChildNode previousSibling)
Checks the normalized of this node after removing a child. If the removed child causes this node to be unnormalized, then this node is flagged accordingly. The conditions for changing the normalized state are:- The removed child had two adjacent siblings that were text nodes.
- Parameters:
previousSibling- the previous sibling of the removed child, ornull
-
-