nu.staldal.xtree
Class Element

java.lang.Object
  extended by nu.staldal.xtree.Node
      extended by nu.staldal.xtree.NodeWithChildren
          extended by nu.staldal.xtree.Element
All Implemented Interfaces:
java.io.Serializable, org.xml.sax.Locator

public class Element
extends NodeWithChildren

An XML Element.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class nu.staldal.xtree.Node
parent, XML_NS
 
Constructor Summary
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 namespaceURI, java.lang.String localName, java.lang.String type, 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 getAttributeLocalName(int index)
          Get the name of the attribute at the specified index.
 java.lang.String getAttributeNamespaceURI(int index)
          Get the namespace URI for the attribute at the specified index.
 java.lang.String getAttributeType(int index)
          Get the type of the attribute at the specified index.
 java.lang.String getAttributeValue(int index)
          Get the value of the attribute at the specified index.
 java.lang.String getAttrValue(java.lang.String localName)
          Shortcut method for getting the value of an attribute without namespace.
 java.lang.String getAttrValue(java.lang.String namespaceURI, java.lang.String localName)
          Shortcut method for getting the value of an attribute with namespace.
 java.lang.String getAttrValueOrNull(java.lang.String localName)
          Shortcut method for getting the value of an attribute without namespace.
 java.lang.String getAttrValueOrNull(java.lang.String namespaceURI, java.lang.String localName)
          Shortcut method for getting the value of an attribute with namespace.
 java.net.URL getBaseURI()
          Returns the absolute base URI of this node.
 Element getFirstChildElement()
          Shortcut method for getting the first Element children with any name.
 Element getFirstChildElement(java.lang.String namespaceURI, java.lang.String localName)
          Shortcut method for getting the first Element child with a specified name.
 Element getFirstChildElementOrNull()
          Shortcut method for getting the first Element children with any name.
 Element getFirstChildElementOrNull(java.lang.String namespaceURI, java.lang.String localName)
          Shortcut method for getting the first Element child with a specified name.
 java.lang.String getInheritedAttribute(java.lang.String namespaceURI, java.lang.String localName)
          Return the value of an inherited attribute.
 java.lang.String getLocalName()
          Get the 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.
 int lookupAttribute(java.lang.String namespaceURI, java.lang.String localName)
          Lookup the index of an attribute to this 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 numberOfAttributes()
          Return the number of attributes this element have.
 int numberOfNamespaceMappings()
          Return the number of namespace mapping 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 removeAttribute(int index)
          Remove an attribute at the specified index.
 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.xtree.NodeWithChildren
addChild, getChild, insertChild, numberOfChildren, removeChild, replaceChild
 
Methods inherited from class nu.staldal.xtree.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
 

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
Method Detail

getNamespaceURI

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


getLocalName

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


lookupAttribute

public int lookupAttribute(java.lang.String namespaceURI,
                           java.lang.String localName)
Lookup the index of an attribute to this element. The returned index may be used as argument to other methods in this class.

Parameters:
namespaceURI - the namespace URI, may be the empty string
localName - the name
Returns:
the index of the attribute, or -1 if no such attribute exists
See Also:
getAttributeValue(int), getAttributeType(int), removeAttribute(int)

addAttribute

public void addAttribute(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String type,
                         java.lang.String value)
Add an attribute to this element. The attribute type is one of the strings "CDATA", "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES", or "NOTATION" (always in upper case).

Parameters:
namespaceURI - the namespace URI, may be the empty string
localName - the name
type - the type (use "CDATA" if the type is irrelevant)
value - the value

removeAttribute

public void removeAttribute(int index)
                     throws java.lang.IndexOutOfBoundsException
Remove an attribute at the specified index. This method is a bit inefficient.

Parameters:
index - the index as returned from lookupAttribute(java.lang.String, java.lang.String)
Throws:
IndexOutOfBoundException - if no such attribute exist.
java.lang.IndexOutOfBoundsException

numberOfAttributes

public int numberOfAttributes()
Return the number of attributes this element have.


getAttributeNamespaceURI

public java.lang.String getAttributeNamespaceURI(int index)
                                          throws java.lang.IndexOutOfBoundsException
Get the namespace URI for the attribute at the specified index.

Parameters:
index - the index as returned from lookupAttribute(java.lang.String, java.lang.String)
Returns:
the namespace URI, may be (and is usually) the empty string, or null if index is -1
Throws:
java.lang.IndexOutOfBoundsException - if no such attribute exist.

getAttributeLocalName

public java.lang.String getAttributeLocalName(int index)
                                       throws java.lang.IndexOutOfBoundsException
Get the name of the attribute at the specified index.

Parameters:
index - the index as returned from lookupAttribute(java.lang.String, java.lang.String)
Returns:
the localName, or null if index is -1
Throws:
java.lang.IndexOutOfBoundsException - if no such attribute exist.

getAttributeType

public java.lang.String getAttributeType(int index)
                                  throws java.lang.IndexOutOfBoundsException
Get the type of the attribute at the specified index. The attribute type is one of the strings "CDATA", "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES", or "NOTATION" (always in upper case).

Parameters:
index - the index as returned from lookupAttribute(java.lang.String, java.lang.String)
Returns:
the attribute type, or null if index is -1
Throws:
java.lang.IndexOutOfBoundsException - if no such attribute exist.

getAttributeValue

public java.lang.String getAttributeValue(int index)
                                   throws java.lang.IndexOutOfBoundsException
Get the value of the attribute at the specified index.

Parameters:
index - the index as returned from lookupAttribute(java.lang.String, java.lang.String)
Returns:
the attribute value, or null if index is -1
Throws:
java.lang.IndexOutOfBoundsException - if no such attribute exist.

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 mapping for this element.


getNamespaceMapping

public java.lang.String[] getNamespaceMapping(int index)
                                       throws java.lang.IndexOutOfBoundsException
Return a namespace mapping at the specified 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 namespaceURI,
                                              java.lang.String localName)
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:
namespaceURI - the namespace URI, may be the empty string
localName - 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.

Throws:
org.xml.sax.SAXException

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.

Throws:
org.xml.sax.SAXException

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

getAttrValueOrNull

public java.lang.String getAttrValueOrNull(java.lang.String localName)
Shortcut method for getting the value of an attribute without namespace.

Returns:
the attrubute value, or null if the attribute doesn't exist

getAttrValue

public java.lang.String getAttrValue(java.lang.String localName)
                              throws org.xml.sax.SAXParseException
Shortcut method for getting the value of an attribute without namespace.

Returns:
the attrubute value, never null
Throws:
org.xml.sax.SAXParseException - if the attribute doesn't exist

getAttrValueOrNull

public java.lang.String getAttrValueOrNull(java.lang.String namespaceURI,
                                           java.lang.String localName)
Shortcut method for getting the value of an attribute with namespace.

Returns:
the attrubute value, or null if the attribute doesn't exist

getAttrValue

public java.lang.String getAttrValue(java.lang.String namespaceURI,
                                     java.lang.String localName)
                              throws org.xml.sax.SAXParseException
Shortcut method for getting the value of an attribute with namespace.

Returns:
the attrubute value, never null
Throws:
org.xml.sax.SAXParseException - if the attribute doesn't exist

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

getFirstChildElementOrNull

public Element getFirstChildElementOrNull(java.lang.String namespaceURI,
                                          java.lang.String localName)
Shortcut method for getting the first Element child with a specified name.

Returns:
the first child Element with the specified name, or null if there is no such child.

getFirstChildElement

public Element getFirstChildElement(java.lang.String namespaceURI,
                                    java.lang.String localName)
                             throws org.xml.sax.SAXParseException
Shortcut method for getting the first Element child with a specified name.

Returns:
the first child Element with the specified name, never null.
Throws:
org.xml.sax.SAXParseException - if there is no such child.

getFirstChildElementOrNull

public Element getFirstChildElementOrNull()
Shortcut method for getting the first Element children with any name.

Returns:
the first child Element or null if there are no Element children.

getFirstChildElement

public Element getFirstChildElement()
                             throws org.xml.sax.SAXParseException
Shortcut method for getting the first Element children with any name.

Returns:
the first child Element never null.
Throws:
org.xml.sax.SAXParseException - if there are no Element children.