com.twicom.qdparser
Class TaggedElement

java.lang.Object
  extended by com.twicom.qdparser.Element
      extended by com.twicom.qdparser.TaggedElement
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class TaggedElement
extends Element
implements java.lang.Cloneable

Represents a tagged XML element and contains all the information that the original had. Attributes may be set, accessed and deleted. Child elements may be set accessed or deleted. This can be converted back to XML, along with its children, in a formatted or unformatted string.

Version:
$Id$
Author:
Kevin Twidle
See Also:
Serialized Form

Field Summary
(package private)  java.util.Map<java.lang.String,java.lang.String> attributes
           
(package private)  java.util.List<Element> elements
           
(package private)  java.lang.String name
           
(package private)  java.lang.String nameSpace
           
(package private)  java.lang.String tagName
           
 
Constructor Summary
TaggedElement(java.lang.String tag)
          Class constructor specifying only the name part of the tag
TaggedElement(java.lang.String tag, java.lang.Object[] children)
          Class constructor specifying only the name part of the tag with an array of objects to be added as child elements
TaggedElement(java.lang.String nameSpace, java.lang.String tagName)
          Class constructor specifying the name space and the tag name
TaggedElement(java.lang.String nameSpace, java.lang.String tag, java.lang.Object[] children)
          Class constructor specifying only the name part of the tag with an array of objects to be added as child elements
 
Method Summary
 void add(Element element)
          adds a child to the end of this element
 void add(int index, Element element)
          inserts a child into this element.
 void add(int index, java.lang.String string)
          inserts a child into this element If the index is out of range, the child is added at the end
 void add(java.lang.String string)
          adds a child to the end of this element
 void clearElements()
          removes the children from this element
 java.lang.Object clone()
           
 void delAttribute(java.lang.String string)
          removes the named attribute
 int elements()
          gets the number of child elements this element has
 TaggedElement find(java.lang.String name)
          Finds a named child tagged element.
 int findIndex(java.lang.String name)
          Finds the index of a named child tagged element.
 java.lang.String getAttribute(java.lang.String name)
          gets the specified attribute of this element
 java.lang.String getAttribute(java.lang.String name, java.lang.String defaultValue)
          gets the specified attribute of this element but returns given default value if the attribute does not exist
 java.util.Map<java.lang.String,java.lang.String> getAttributes()
          gets the attributes in the form of an indexed table
 Element getChild(int i)
          gets a child from the specified place in this element
 int getCol()
          gets the XML source code cloumn number where this element was declared.
 java.lang.String getComment()
          get the comment associated with this element
 java.util.List<Element> getElements()
          gets a list of the children of this element.
 int getLine()
          gets the XML source code line number where this element was declared.
 java.lang.String getName()
          gets the original tag name
 java.lang.String getNameSpace()
          gets the name space name
 java.lang.String getNamespacePrefix()
          Gets the namespace prefix to be used for the namespace attribute when printing out the element
 java.lang.String getTag()
          gets the name part of the tag
 boolean hasAttribute(java.lang.String name)
          finds out whether an attribute exists
 boolean hasAttributes()
          finds out whether this element has any attributes
 boolean hasElements()
          finds out whether this element has any child elements
 java.util.Iterator<Element> iterator()
           
 boolean remove(Element element)
          Removes a child from this element
 Element remove(int index)
          Removes a child from this element
 void replace(int index, Element newElement)
          replaces a child element with the one given
 void setAttribute(java.lang.String name, java.lang.String value)
          sets an attribute of this element
 void setComment(java.lang.String comment)
          set this element's comment
 void setElements(java.util.List<Element> elements)
          sets the list of children of this element.
 void setLocation(int line, int col)
          sets the XML source code location information for this element
 void setNamespacePrefix(java.lang.String namespacePrefix)
          Sets the namespace prefix to be used when converting the XML to a String.
 java.lang.String toString()
           
protected  java.lang.StringBuffer toString(java.lang.StringBuffer sb, boolean formatted, int level)
          Internal method used recursively to format XML with appropriate indentation.
 
Methods inherited from class com.twicom.qdparser.Element
newElement, quote, toString, unquote
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nameSpace

java.lang.String nameSpace

tagName

java.lang.String tagName

name

java.lang.String name

attributes

java.util.Map<java.lang.String,java.lang.String> attributes

elements

java.util.List<Element> elements
Constructor Detail

TaggedElement

public TaggedElement(java.lang.String tag)
Class constructor specifying only the name part of the tag

Parameters:
tag - the name of the tag

TaggedElement

public TaggedElement(java.lang.String nameSpace,
                     java.lang.String tagName)
Class constructor specifying the name space and the tag name

Parameters:
nameSpace - the name space name
tagName - the name of the tag

TaggedElement

public TaggedElement(java.lang.String tag,
                     java.lang.Object[] children)
Class constructor specifying only the name part of the tag with an array of objects to be added as child elements

Parameters:
tag - the name of the tag
children - an array of objects to be added as children

TaggedElement

public TaggedElement(java.lang.String nameSpace,
                     java.lang.String tag,
                     java.lang.Object[] children)
Class constructor specifying only the name part of the tag with an array of objects to be added as child elements

Parameters:
nameSpace - the name space name
tag - the name of the tag
children - an array of objects to be added as children
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class Element
Throws:
java.lang.CloneNotSupportedException

getName

public java.lang.String getName()
gets the original tag name

Returns:
the complete namespace and tag name

getNameSpace

public java.lang.String getNameSpace()
gets the name space name

Returns:
the name space name

getTag

public java.lang.String getTag()
gets the name part of the tag

Returns:
the name part of the tag

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.String value)
sets an attribute of this element

Parameters:
name - the name of the attribute to be set
value - the value of the attribute to be set

getAttribute

public java.lang.String getAttribute(java.lang.String name)
gets the specified attribute of this element

Parameters:
name - the name of the attribute to get
Returns:
the value of the attribute

getAttribute

public java.lang.String getAttribute(java.lang.String name,
                                     java.lang.String defaultValue)
gets the specified attribute of this element but returns given default value if the attribute does not exist

Parameters:
name - the name of the attribute to get
defaultValue - the value to be returned if the attribute doesn't exist
Returns:
the value of the attribute

delAttribute

public void delAttribute(java.lang.String string)
removes the named attribute

Parameters:
string - the name of the attribute to be removed

hasAttribute

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

Parameters:
name - the name of the attribute to look for
Returns:
whether the attribute exists in this element

hasAttributes

public boolean hasAttributes()
finds out whether this element has any attributes

Returns:
whether this element has any attributes

getAttributes

public java.util.Map<java.lang.String,java.lang.String> getAttributes()
gets the attributes in the form of an indexed table

Returns:
the attribute table for this element

elements

public int elements()
gets the number of child elements this element has

Returns:
the number of child elements this element has

hasElements

public boolean hasElements()
finds out whether this element has any child elements

Returns:
whether this element has any child elements

clearElements

public void clearElements()
removes the children from this element


add

public void add(Element element)
adds a child to the end of this element

Parameters:
element - the child element to be added

add

public void add(int index,
                Element element)
inserts a child into this element. If the index is out of range, the child is added at the end

Parameters:
index - the location to insert the child
element - the child element to be added

add

public void add(java.lang.String string)
adds a child to the end of this element

Parameters:
string - the child element to be added

add

public void add(int index,
                java.lang.String string)
inserts a child into this element If the index is out of range, the child is added at the end

Parameters:
index - the location to insert the child
string - the child element to be added

getChild

public Element getChild(int i)
gets a child from the specified place in this element

Parameters:
i - the index where the child is to be added
Returns:
the specified child

replace

public void replace(int index,
                    Element newElement)
replaces a child element with the one given

Parameters:
index - the child element number to replace
newElement - the new element to replace the old one

remove

public Element remove(int index)
Removes a child from this element

Parameters:
index - the index of the child to be removed
Returns:
whether the child was found and removed or not

remove

public boolean remove(Element element)
Removes a child from this element

Parameters:
element - the child to be removed
Returns:
whether the child was found and removed or not

find

public TaggedElement find(java.lang.String name)
Finds a named child tagged element. If there is no such child, null is returned.

Parameters:
name - the name of the child of this TaggedElement to find
Returns:
the name of the found element or null if not found

findIndex

public int findIndex(java.lang.String name)
Finds the index of a named child tagged element. If there is no such child, -1 is returned.

Parameters:
name - the name of the child of this TaggedElement to find
Returns:
the index of the found element or -1 if not found

setElements

public void setElements(java.util.List<Element> elements)
sets the list of children of this element. This method replaces the current children.

Parameters:
elements - the new list of children

getElements

public java.util.List<Element> getElements()
gets a list of the children of this element. This method always returns a List even if it is empty.

Returns:
the List containing the children of this element

iterator

public java.util.Iterator<Element> iterator()

getNamespacePrefix

public java.lang.String getNamespacePrefix()
Gets the namespace prefix to be used for the namespace attribute when printing out the element

Returns:
Returns the namespacePrefix.

setNamespacePrefix

public void setNamespacePrefix(java.lang.String namespacePrefix)
Sets the namespace prefix to be used when converting the XML to a String. If not set, a suitable one will be made up

Parameters:
namespacePrefix - The namespacePrefix to set.

setLocation

public void setLocation(int line,
                        int col)
sets the XML source code location information for this element

Parameters:
line - the line number of the start tag
col - the column number of the start tag

getLine

public int getLine()
gets the XML source code line number where this element was declared. This number will be 0 if it was never set.

Returns:
the XML source code line number of this element's declaration

getCol

public int getCol()
gets the XML source code cloumn number where this element was declared. This number will be 0 if it was never set.

Returns:
the XML source code column number of this element's declaration

getComment

public java.lang.String getComment()
get the comment associated with this element

Returns:
this element's comment

setComment

public void setComment(java.lang.String comment)
set this element's comment

Parameters:
comment - the comment to be stored

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

protected java.lang.StringBuffer toString(java.lang.StringBuffer sb,
                                          boolean formatted,
                                          int level)
Description copied from class: Element
Internal method used recursively to format XML with appropriate indentation.

Specified by:
toString in class Element
Parameters:
sb - TODO
formatted - whether to return formatted XML source. If true, the source is pretty-printed with new lines and indentation. If false, the XML string is returned as one long, unformatted line.
level - the indentation level used to write leading spaces
Returns:
a String containing th XML source