net.n3.nanoxml
Class XMLElement

java.lang.Object
  extended by net.n3.nanoxml.XMLElement
All Implemented Interfaces:
java.io.Serializable

public class XMLElement
extends java.lang.Object
implements java.io.Serializable

XMLElement is an XML element. The standard NanoXML builder generates a tree of such elements.

Version:
$Name$, $Revision: 1263 $
Author:
Marc De Scheemaecker
See Also:
StdXMLBuilder, Serialized Form

Field Summary
private  java.util.Properties attributes
          The attributes of the element.
private  java.util.Vector children
          The child elements.
private  java.lang.String content
          The content of the element.
private  int lineNr
          The line in the source data where this element starts.
private  java.lang.String name
          The name of the element.
static int NO_LINE
          No line number defined.
(package private) static long serialVersionUID
          Necessary for serialization.
private  java.lang.String systemID
          The system ID of the source data where this element is located.
 
Constructor Summary
XMLElement()
          Creates an empty element to be used for #PCDATA content.
XMLElement(java.lang.String name)
          Creates an empty element.
XMLElement(java.lang.String name, java.lang.String systemID, int lineNr)
          Creates an empty element.
 
Method Summary
 void addChild(XMLElement child)
          Adds a child element.
 java.util.Enumeration enumerateAttributeNames()
          Returns an enumeration of all attribute names.
 java.util.Enumeration enumerateChildren()
          Returns an enumeration of all child elements.
protected  void finalize()
          Cleans up the object when it's destroyed.
 java.lang.String getAttribute(java.lang.String name)
          Returns the value of an attribute.
 java.lang.String getAttribute(java.lang.String name, java.lang.String defaultValue)
          Returns the value of an attribute.
 java.util.Properties getAttributes()
          Returns all attributes as a Properties object.
 XMLElement getChildAtIndex(int index)
          Returns the child at a specific index.
 java.util.Vector getChildren()
          Returns a vector containing all the child elements.
 int getChildrenCount()
          Returns the number of children.
 java.util.Vector getChildrenNamed(java.lang.String name)
          Returns a vector of all child elements named name.
 java.lang.String getContent()
          Return the #PCDATA content of the element.
 XMLElement getFirstChildNamed(java.lang.String name)
          Searches a child element.
 int getLineNr()
          Returns the line number in the data where the element started.
 java.lang.String getName()
          Returns the name of the element.
 java.lang.String getSystemID()
          Returns the system ID of the data where the element started.
 boolean hasAttribute(java.lang.String name)
          Returns whether an attribute exists.
 boolean hasChildren()
          Returns whether the element has children.
 boolean isLeaf()
          Returns whether the element is a leaf element.
 void removeAttribute(java.lang.String name)
          Removes an attribute.
 void removeChild(XMLElement child)
          Removes a child element.
 void removeChildAtIndex(int index)
          Removes the child located at a certain index.
 void setAttribute(java.lang.String name, java.lang.String value)
          Sets an attribute.
 void setContent(java.lang.String content)
          Sets the #PCDATA content.
 void setName(java.lang.String name)
          Sets the name.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

static final long serialVersionUID
Necessary for serialization.

See Also:
Constant Field Values

NO_LINE

public static final int NO_LINE
No line number defined.

See Also:
Constant Field Values

attributes

private java.util.Properties attributes
The attributes of the element.


children

private java.util.Vector children
The child elements.


name

private java.lang.String name
The name of the element.


content

private java.lang.String content
The content of the element.


systemID

private java.lang.String systemID
The system ID of the source data where this element is located.


lineNr

private int lineNr
The line in the source data where this element starts.

Constructor Detail

XMLElement

public XMLElement()
Creates an empty element to be used for #PCDATA content.


XMLElement

public XMLElement(java.lang.String name)
Creates an empty element.

Parameters:
name - the name of the element.

XMLElement

public XMLElement(java.lang.String name,
                  java.lang.String systemID,
                  int lineNr)
Creates an empty element.

Parameters:
name - the name of the element.
systemID - the system ID of the XML data where the element starts.
lineNr - the line in the XML data where the element starts.
Method Detail

finalize

protected void finalize()
                 throws java.lang.Throwable
Cleans up the object when it's destroyed.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

getName

public java.lang.String getName()
Returns the name of the element.

Returns:
the name, or null if the element only contains #PCDATA.

setName

public void setName(java.lang.String name)
Sets the name.

Parameters:
name - the non-null name.

addChild

public void addChild(XMLElement child)
Adds a child element.

Parameters:
child - the non-null child to add.

removeChild

public void removeChild(XMLElement child)
Removes a child element.

Parameters:
child - the non-null child to remove.

removeChildAtIndex

public void removeChildAtIndex(int index)
Removes the child located at a certain index.

Parameters:
index - the index of the child, where the first child has index 0.

enumerateChildren

public java.util.Enumeration enumerateChildren()
Returns an enumeration of all child elements.

Returns:
the non-null enumeration

isLeaf

public boolean isLeaf()
Returns whether the element is a leaf element.

Returns:
true if the element has no children.

hasChildren

public boolean hasChildren()
Returns whether the element has children.

Returns:
true if the element has children.

getChildrenCount

public int getChildrenCount()
Returns the number of children.

Returns:
the count.

getChildren

public java.util.Vector getChildren()
Returns a vector containing all the child elements.

Returns:
the vector.

getChildAtIndex

public XMLElement getChildAtIndex(int index)
                           throws java.lang.ArrayIndexOutOfBoundsException
Returns the child at a specific index.

Returns:
the non-null child
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is out of bounds.

getFirstChildNamed

public XMLElement getFirstChildNamed(java.lang.String name)
Searches a child element.

Parameters:
name - the name of the child to search for.
Returns:
the child element, or null if no such child was found.

getChildrenNamed

public java.util.Vector getChildrenNamed(java.lang.String name)
Returns a vector of all child elements named name.

Parameters:
name - the name of the children to search for.
Returns:
the non-null vector of child elements.

getAttribute

public java.lang.String getAttribute(java.lang.String name)
Returns the value of an attribute.

Parameters:
name - the non-null name of the attribute.
Returns:
the value, or null if the attribute does not exist.

getAttribute

public java.lang.String getAttribute(java.lang.String name,
                                     java.lang.String defaultValue)
Returns the value of an attribute.

Parameters:
name - the non-null name of the attribute.
defaultValue - the default value of the attribute.
Returns:
the value, or defaultValue if the attribute does not exist.

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.String value)
Sets an attribute.

Parameters:
name - the non-null name of the attribute.
value - the non-null value of the attribute.

removeAttribute

public void removeAttribute(java.lang.String name)
Removes an attribute.

Parameters:
name - the non-null name of the attribute.

enumerateAttributeNames

public java.util.Enumeration enumerateAttributeNames()
Returns an enumeration of all attribute names.

Returns:
the non-null enumeration.

hasAttribute

public boolean hasAttribute(java.lang.String name)
Returns whether an attribute exists.

Returns:
true if the attribute exists.

getAttributes

public java.util.Properties getAttributes()
Returns all attributes as a Properties object.

Returns:
the non-null set.

getSystemID

public java.lang.String getSystemID()
Returns the system ID of the data where the element started.

Returns:
the system ID, or null if unknown.
See Also:
getLineNr()

getLineNr

public int getLineNr()
Returns the line number in the data where the element started.

Returns:
the line number, or NO_LINE if unknown.
See Also:
NO_LINE, getSystemID()

getContent

public java.lang.String getContent()
Return the #PCDATA content of the element. If the element has a combination of #PCDATA content and child elements, the #PCDATA sections can be retrieved as unnamed child objects. In this case, this method returns null.

Returns:
the content.

setContent

public void setContent(java.lang.String content)
Sets the #PCDATA content. It is an error to call this method with a non-null value if there are child objects.

Parameters:
content - the (possibly null) content.