XML for Java 2.0.15

com.ibm.xml.dom
Class ElementImpl

java.lang.Object
  |
  +--com.ibm.xml.dom.NodeImpl
        |
        +--com.ibm.xml.dom.ElementImpl
Direct Known Subclasses:
DeferredElementImpl

public class ElementImpl
extends NodeImpl
implements Element

Elements represent most of the "markup" and structure of the document. They contain both the data for the element itself (element name and attributes), and any contained nodes, including document text (as children).

Elements may have Attributes associated with them; the API for this is defined in Node, but the function is implemented here. In general, XML applications should retrive Attributes as Nodes, since they may contain entity references and hence be a fairly complex sub-tree. HTML users will be dealing with simple string values, and convenience methods are provided to work in terms of Strings.

Since:
PR-DOM-Level-1-19980818.
Version:
Revision: 61 1.2 src/com/ibm/xml/dom/ElementImpl.java, parser, xml4j2
See Also:
Serialized Form

Field Summary
protected  NamedNodeMapImpl attributes
          Attributes.
 
Fields inherited from class com.ibm.xml.dom.NodeImpl
ELEMENT_DEFINITION_NODE, firstChild, kidOK, lastChild, name, nextSibling, ownerDocument, parentNode, previousSibling, readOnly, syncChildren, syncData, userData, value
 
Constructor Summary
ElementImpl(DocumentImpl ownerDoc, java.lang.String name)
          Factory constructor.
 
Method Summary
 Node cloneNode(boolean deep)
          Return a duplicate copy of this Element.
 java.lang.String getAttribute(java.lang.String name)
          Look up a single Attribute by name.
 Attr getAttributeNode(java.lang.String name)
          Look up a single Attribute by name.
 NamedNodeMap getAttributes()
          Retrieve all the Attributes as a set.
 NodeList getElementsByTagName(java.lang.String tagname)
          Returns a NodeList of all descendent nodes (children, grandchildren, and so on) which are Elements and which have the specified tag name.
 short getNodeType()
          A short integer indicating what type of node this is.
 java.lang.String getNodeValue()
          Returns the node value.
 java.lang.String getTagName()
          Returns the name of the Element.
 java.lang.String getValue()
          Returns the element value.
 void normalize()
          In "normal form" (as read from a source file), there will never be two Text children in succession.
 void removeAttribute(java.lang.String name)
          Remove the named attribute from this Element.
 Attr removeAttributeNode(Attr oldAttr)
          Remove the specified attribute/value pair.
 void setAttribute(java.lang.String name, java.lang.String value)
          Add a new name/value pair, or replace the value of the existing attribute having that name.
 Attr setAttributeNode(Attr newAttr)
          Add a new attribute/value pair, or replace the value of the existing attribute with that name.
 void setNodeValue(java.lang.String value)
          Elements never have a nodeValue.
 void setReadOnly(boolean readOnly, boolean deep)
          NON-DOM: Subclassed to flip the attributes' readonly switch as well.
protected  void setupDefaultAttributes()
          Setup the default attributes.
protected  void synchronizeData()
          Synchronizes the data (name and value) for fast nodes.
 
Methods inherited from class com.ibm.xml.dom.NodeImpl
appendChild, changed, getChildNodes, getFirstChild, getLastChild, getLength, getNextSibling, getNodeName, getOwnerDocument, getParentNode, getPreviousSibling, getReadOnly, getUserData, hasChildNodes, insertBefore, isKidOK, item, removeChild, replaceChild, setUserData, synchronizeChildren, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

attributes

protected NamedNodeMapImpl attributes
Attributes.
Constructor Detail

ElementImpl

public ElementImpl(DocumentImpl ownerDoc,
                   java.lang.String name)
Factory constructor.
Method Detail

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.
Overrides:
getNodeType in class NodeImpl

getNodeValue

public java.lang.String getNodeValue()
Returns the node value.
Overrides:
getNodeValue in class NodeImpl

setNodeValue

public void setNodeValue(java.lang.String value)
                  throws DOMException
Elements never have a nodeValue.
Throws:
DOMException(NO_MODIFICATION_ALLOWED_ERR), - unconditionally.
Overrides:
setNodeValue in class NodeImpl

getAttributes

public NamedNodeMap getAttributes()
Retrieve all the Attributes as a set. Note that this API is inherited from Node rather than specified on Element; in fact only Elements will ever have Attributes, but they want to allow folks to "blindly" operate on the tree as a set of Nodes.
Overrides:
getAttributes in class NodeImpl

cloneNode

public Node cloneNode(boolean deep)
Return a duplicate copy of this Element. Note that its children will not be copied unless the "deep" flag is true, but Attributes are always replicated.
Overrides:
cloneNode in class NodeImpl
See Also:
Node.cloneNode()

getValue

public java.lang.String getValue()
Returns the element value.

getAttribute

public java.lang.String getAttribute(java.lang.String name)
Look up a single Attribute by name. Returns the Attribute's string value, or an empty string (NOT null!) to indicate that the name did not map to a currently defined attribute.

Note: Attributes may contain complex node trees. This method returns the "flattened" string obtained from Attribute.getValue(). If you need the structure information, see getAttributeNode().

Specified by:
getAttribute in interface Element

getAttributeNode

public Attr getAttributeNode(java.lang.String name)
Look up a single Attribute by name. Returns the Attribute Node, so its complete child tree is available. This could be important in XML, where the string rendering may not be sufficient information.

If no matching attribute is available, returns null.

Specified by:
getAttributeNode in interface Element

getElementsByTagName

public NodeList getElementsByTagName(java.lang.String tagname)
Returns a NodeList of all descendent nodes (children, grandchildren, and so on) which are Elements and which have the specified tag name.

Note: NodeList is a "live" view of the DOM. Its contents will change as the DOM changes, and alterations made to the NodeList will be reflected in the DOM.

Specified by:
getElementsByTagName in interface Element
Parameters:
tagname - The type of element to gather. To obtain a list of all elements no matter what their names, use the wild-card tag name "*".
See Also:
com.ibm.domimpl.DeepNodeListImpl

getTagName

public java.lang.String getTagName()
Returns the name of the Element. Note that Element.nodeName() is defined to also return the tag name.

This is case-preserving in XML. HTML should uppercasify it on the way in.

Specified by:
getTagName in interface Element

normalize

public void normalize()
In "normal form" (as read from a source file), there will never be two Text children in succession. But DOM users may create successive Text nodes in the course of manipulating the document. Normalize walks the sub-tree and merges adjacent Texts, as if the DOM had been written out and read back in again. This simplifies implementation of higher-level functions that may want to assume that the document is in standard form.

To normalize a Document, normalize its top-level Element child.

As of PR-DOM-Level-1-19980818, CDATA -- despite being a subclass of Text -- is considered "markup" and will _not_ be merged either with normal Text or with other CDATASections.

Specified by:
normalize in interface Element

removeAttribute

public void removeAttribute(java.lang.String name)
Remove the named attribute from this Element. If the removed Attribute has a default value, it is immediately replaced thereby.

The default logic is actually implemented in NamedNodeMapImpl. PR-DOM-Level-1-19980818 doesn't fully address the DTD, so some of this behavior is likely to change in future versions. ?????

Note that this call "succeeds" even if no attribute by this name existed -- unlike removeAttributeNode, which will throw a not-found exception in that case.

Specified by:
removeAttribute in interface Element
Throws:
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if the node is readonly.

removeAttributeNode

public Attr removeAttributeNode(Attr oldAttr)
                         throws DOMException
Remove the specified attribute/value pair. If the removed Attribute has a default value, it is immediately replaced.

NOTE: Specifically removes THIS NODE -- not the node with this name, nor the node with these contents. If the specific Attribute object passed in is not stored in this Element, we throw a DOMException. If you really want to remove an attribute by name, use removeAttribute().

Specified by:
removeAttributeNode in interface Element
Returns:
the Attribute object that was removed.
Throws:
DOMException(NOT_FOUND_ERR) - if oldattr is not an attribute of this Element.
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if the node is readonly.

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.String value)
Add a new name/value pair, or replace the value of the existing attribute having that name. Note: this method supports only the simplest kind of Attribute, one whose value is a string contained in a single Text node. If you want to assert a more complex value (which XML permits, though HTML doesn't), see setAttributeNode(). The attribute is created with specified=true, meaning it's an explicit value rather than inherited from the DTD as a default. Again, setAttributeNode can be used to achieve other results.
Specified by:
setAttribute in interface Element
Throws:
DOMException(INVALID_NAME_ERR) - if the name is not acceptable. (Attribute factory will do that test for us.)
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if the node is readonly.

setAttributeNode

public Attr setAttributeNode(Attr newAttr)
                      throws DOMException
Add a new attribute/value pair, or replace the value of the existing attribute with that name.

This method allows you to add an Attribute that has already been constructed, and hence avoids the limitations of the simple setAttribute() call. It can handle attribute values that have arbitrarily complex tree structure -- in particular, those which had entity references mixed into their text.

Specified by:
setAttributeNode in interface Element
Throws:
DOMException(INUSE_ATTRIBUTE_ERR) - if the Attribute object has already been assigned to another Element.

setReadOnly

public void setReadOnly(boolean readOnly,
                        boolean deep)
NON-DOM: Subclassed to flip the attributes' readonly switch as well.
Overrides:
setReadOnly in class NodeImpl
See Also:
NodeImpl.setReadOnly

synchronizeData

protected void synchronizeData()
Synchronizes the data (name and value) for fast nodes.
Overrides:
synchronizeData in class NodeImpl

setupDefaultAttributes

protected void setupDefaultAttributes()
Setup the default attributes.

XML for Java 2.0.15