nu.staldal.xmltree
Class Element

java.lang.Object
  extended by nu.staldal.xmltree.Node
      extended by nu.staldal.xmltree.NodeWithChildren
          extended by nu.staldal.xmltree.Element
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<Node>, java.util.Collection<Node>, java.util.List<Node>, org.xml.sax.Locator

public class Element
extends NodeWithChildren

An XML Element.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class nu.staldal.xmltree.Node
parent
 
Constructor Summary
Element(Element element)
          Pseudo-copy constructor, do not copy the children, but allocate room for them.
Element(java.lang.String namespaceURI, java.lang.String localName)
          Construct an element.
Element(java.lang.String namespaceURI, java.lang.String localName, int numberOfAttributes)
          Construct an element.
Element(java.lang.String namespaceURI, java.lang.String localName, int numberOfAttributes, int numberOfChildren)
          Construct an element.
 
Method Summary
 void addAttribute(java.lang.String name, java.lang.String value)
          Add an attribute to this element.
 void addNamespaceMapping(java.lang.String prefix, java.lang.String URI)
          Add a namespace mapping to this element.
 java.lang.String getAttribute(java.lang.String name)
          Shortcut method for getting the value of an attribute.
 java.lang.String getAttributeOrNull(java.lang.String name)
          Shortcut method for getting the value of an attribute.
 java.util.Map<java.lang.String,java.lang.String> getAttributes()
          Get the Map with attributes for this element.
 java.net.URL getBaseURI()
          Returns the absolute base URI of this node.
 java.lang.String getInheritedAttribute(java.lang.String name)
          Return the value of an inherited attribute.
 java.lang.String getLocalName()
          Get the name of this element.
 java.lang.String getName()
          Get the qualified name of this element.
 java.lang.String[] getNamespaceMapping(int index)
          Return a namespace mapping at the specified index.
 java.lang.String getNamespaceURI()
          Get the namespace URI for this element.
 boolean getPreserveSpace()
          Return the value of any xml:space attribute in force for this node.
 java.lang.String getTextContent()
          Shortcut method for getting the text content of an Element.
 java.lang.String getTextContentOrNull()
          Shortcut method for getting the text content of an Element.
 java.lang.String lookupNamespacePrefix(java.lang.String URI)
          Lookup a prefix which has been mapped to a namespace URI.
 java.lang.String lookupNamespaceURI(java.lang.String prefix)
          Lookup the namespace URI which has been mapped to a prefix.
 int numberOfNamespaceMappings()
          Return the number of namespace mappings for this element.
 void outputEndElement(org.xml.sax.ContentHandler sax)
          Fire the endElement event to the given SAX2 ContentHandler.
 void outputStartElement(org.xml.sax.ContentHandler sax)
          Fire the startElement event to the given SAX2 ContentHandler.
 void outputStartElement(org.xml.sax.ContentHandler sax, java.util.Map<java.lang.String,java.lang.String> attributes)
          Fire the startElement event to the given SAX2 ContentHandler.
 void setBaseURI(java.net.URL URI)
          Set the baseURI property of this element.
 void toSAX(org.xml.sax.ContentHandler sax)
          Serialize this node, and recursively the (sub)tree beneath, into SAX2 events.
 
Methods inherited from class nu.staldal.xmltree.NodeWithChildren
add, add, addAll, addAll, clear, contains, containsAll, get, getChildElements, getFirstChildElement, getFirstChildElement, getFirstChildElementOrNull, getFirstChildElementOrNull, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, set, size, subList, toArray, toArray
 
Methods inherited from class nu.staldal.xmltree.Node
getColumnNumber, getLineNumber, getParent, getPublicId, getSystemId, isWhitespaceNode, setColumn, setLine, setPublicId, setSystemId
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

Element

public Element(java.lang.String namespaceURI,
               java.lang.String localName)
Construct an element.

Parameters:
namespaceURI - the namespace URI for this element, may be the empty string
localName - the element name

Element

public Element(java.lang.String namespaceURI,
               java.lang.String localName,
               int numberOfAttributes)
Construct an element.

Parameters:
namespaceURI - the namespace URI for this element, may be the empty string
localName - the element name
numberOfAttributes - the number of attributes this element should have

Element

public Element(java.lang.String namespaceURI,
               java.lang.String localName,
               int numberOfAttributes,
               int numberOfChildren)
Construct an element.

Parameters:
namespaceURI - the namespace URI for this element, may be the empty string
localName - the name of this element (no namespace)
numberOfAttributes - the number of attributes this element should have
numberOfChildren - the number of children this element should have

Element

public Element(Element element)
Pseudo-copy constructor, do not copy the children, but allocate room for them.

Parameters:
element - the element to copy
Method Detail

getNamespaceURI

public java.lang.String getNamespaceURI()
Get the namespace URI for this element. May be the empty string.

Returns:
the namespace URI for this element

getLocalName

public java.lang.String getLocalName()
Get the name of this element. The name does not include namespace URI or prefix.

Returns:
the name of this element

getName

public java.lang.String getName()
Get the qualified name of this element.

Returns:
the qualified name of this element

addNamespaceMapping

public void addNamespaceMapping(java.lang.String prefix,
                                java.lang.String URI)
Add a namespace mapping to this element.

Parameters:
prefix - the prefix
URI - the namespace URI

numberOfNamespaceMappings

public int numberOfNamespaceMappings()
Return the number of namespace mappings for this element.

Returns:
the number of namespace mappings

getNamespaceMapping

public java.lang.String[] getNamespaceMapping(int index)
                                       throws java.lang.IndexOutOfBoundsException
Return a namespace mapping at the specified index.

Parameters:
index - the index
Returns:
a String[] with [0] = prefix, [1] = namespace URI
Throws:
java.lang.IndexOutOfBoundsException - if no such mapping exist.

lookupNamespaceURI

public java.lang.String lookupNamespaceURI(java.lang.String prefix)
Description copied from class: Node
Lookup the namespace URI which has been mapped to a prefix.

Overrides:
lookupNamespaceURI in class Node
Parameters:
prefix - the prefix, may be the empty string which denotes the default namespace.
Returns:
the namespace URI, or null if the prefix is not mapped to any namespace URI, or the empty string of prefix is the empty string and there is no default namespace mapping.

lookupNamespacePrefix

public java.lang.String lookupNamespacePrefix(java.lang.String URI)
Description copied from class: Node
Lookup a prefix which has been mapped to a namespace URI.

Overrides:
lookupNamespacePrefix in class Node
Parameters:
URI - the namespace URI
Returns:
any of the prefixes which has been mapped to the namespace URI, or null if no prefix is mapped to the namespace URI.

setBaseURI

public void setBaseURI(java.net.URL URI)
Set the baseURI property of this element.

Parameters:
URI - the base URI, must be absolute

getBaseURI

public java.net.URL getBaseURI()
Description copied from class: Node
Returns the absolute base URI of this node.

Overrides:
getBaseURI in class Node
Returns:
the absolute base URI of this node, or null if unknown.

getPreserveSpace

public boolean getPreserveSpace()
Description copied from class: Node
Return the value of any xml:space attribute in force for this node.

Overrides:
getPreserveSpace in class Node
Returns:
true if an xml:space="preserve" is in effect

getInheritedAttribute

public java.lang.String getInheritedAttribute(java.lang.String name)
Description copied from class: Node
Return the value of an inherited attribute. If the given attribute occurs on this node, return its value, otherwise recursivley search the parent of this node (return null if the root is reached without finding the attribute). Useful for e.g. xml:lang.

Overrides:
getInheritedAttribute in class Node
Parameters:
name - the attribute name
Returns:
null if no such attribute is found

outputStartElement

public void outputStartElement(org.xml.sax.ContentHandler sax)
                        throws org.xml.sax.SAXException
Fire the startElement event to the given SAX2 ContentHandler. Will also fire startPrefixMapping events.

Parameters:
sax - the ContentHandler
Throws:
org.xml.sax.SAXException - if any of the ContentHandler methods throw it

outputStartElement

public void outputStartElement(org.xml.sax.ContentHandler sax,
                               java.util.Map<java.lang.String,java.lang.String> attributes)
                        throws org.xml.sax.SAXException
Fire the startElement event to the given SAX2 ContentHandler. Will also fire startPrefixMapping events. Override attributes.

Parameters:
sax - the ContentHandler
attributes - the attributes to use instead of the normal ones
Throws:
org.xml.sax.SAXException - if any of the ContentHandler methods throw it

outputEndElement

public void outputEndElement(org.xml.sax.ContentHandler sax)
                      throws org.xml.sax.SAXException
Fire the endElement event to the given SAX2 ContentHandler. Will also fire endPrefixMapping events.

Parameters:
sax - the ContentHandler
Throws:
org.xml.sax.SAXException - if any of the ContentHandler methods throw it

toSAX

public void toSAX(org.xml.sax.ContentHandler sax)
           throws org.xml.sax.SAXException
Description copied from class: Node
Serialize this node, and recursively the (sub)tree beneath, into SAX2 events.

Specified by:
toSAX in class Node
Parameters:
sax - the SAX2 ContentHander to fire events on.
Throws:
org.xml.sax.SAXException - if any of the ContentHandler methods throw it

getTextContentOrNull

public java.lang.String getTextContentOrNull()
Shortcut method for getting the text content of an Element.

Returns:
if there is a single Text child, return its value, if there is no children, return "", or null if there are more than one children or one non-Text child

getTextContent

public java.lang.String getTextContent()
                                throws org.xml.sax.SAXParseException
Shortcut method for getting the text content of an Element.

Returns:
if there is a single Text child, return its value, if there is no children, return "", never null.
Throws:
org.xml.sax.SAXParseException - if there are more than one children or one non-Text child

addAttribute

public void addAttribute(java.lang.String name,
                         java.lang.String value)
Add an attribute to this element.

Parameters:
name - attribute name
value - attribute value

getAttributeOrNull

public java.lang.String getAttributeOrNull(java.lang.String name)
Shortcut method for getting the value of an attribute.

Parameters:
name - the (qualified) name of the attribute
Returns:
the attrubute value, or null if the attribute doesn't exist

getAttribute

public java.lang.String getAttribute(java.lang.String name)
                              throws org.xml.sax.SAXParseException
Shortcut method for getting the value of an attribute.

Parameters:
name - the (qualified) name of the attribute
Returns:
the attrubute value, never null
Throws:
org.xml.sax.SAXParseException - if the attribute doesn't exist

getAttributes

public java.util.Map<java.lang.String,java.lang.String> getAttributes()
Get the Map with attributes for this element.

Returns:
the Map with attributes for this element