com.bmc.mainview.nvbapi
Class XML

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.Vector<XML>
              extended by com.bmc.mainview.nvbapi.XML
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<XML>, java.util.Collection<XML>, java.util.List<XML>, java.util.RandomAccess

public class XML
extends java.util.Vector<XML>

An object containing XML stored as a DOM-like structure.

An XML element may be a top-level element in which case it may have processing instructions, or it may be added to another XML element as a sub-element. Each element may have a set of attributes and either a value or a set of sub-elements, but not both.

Version:
2.0
Author:
F. Schneider
See Also:
Serialized Form

Field Summary
static java.lang.String HEADER
          The header placed at the beginning of each formatted XML string.
static int NOINDENT
          Argument to toString(int) to suppress indentation
static int NOPI
          Argument to toString(int) to suppress indentation and processing instructions
 
Constructor Summary
XML(java.io.Reader source)
          Create a DOM-like structure from an XML input stream.
XML(java.lang.String tag)
          Create an element with the given tag
 
Method Summary
 boolean add(XML subElement)
          Appends the specified sub-element to this XML.
 void addElement(java.lang.String s, java.lang.String parm)
          A utility method that adds a sub-element with the given value to the element
 void addElement(XML element)
          Add a sub-element to the current XML element.
 void addProcessingInstruction(XML pi)
          Add a processing instruction to the current XML element.
 XML clone()
          Creates and returns a copy of this object.
 java.lang.String dump()
          Writes a printable version of the entire XML stream which contains this element.
 boolean equals(java.lang.Object other)
           
 boolean equals(XML other)
          Compare attributes, value, and elements of two XML objects for equality.
 XML findElement(java.lang.String tag)
          Searches for the first sub-element with the given tag and returns it.
 XML findElement(java.lang.String tag, int which)
          Searches for the nth sub-element with the given tag and returns it.
 java.util.Enumeration<XML> findElements(java.lang.String tag)
          Allows sub-elements of a given type to be accessed sequentially
 java.lang.String getAttribute(java.lang.String name)
          Retrieve an attribute of this elemrnt
 int getAttributeCount()
          Determine how many attributes there are
 java.util.Enumeration<java.lang.Object> getAttributeList()
          Get an Enumeration of the attributes
 XML getElement(int n)
          Get the nth sub-element of the element
 int getElementCount(java.lang.String tag)
          Returns number of sub-elements with the given tag
 int getIntAttribute(java.lang.String name)
          Retrieve an attribute of this elemrnt which is an int
 XML getParent()
          Find the XML element of which this is a sub-element.
 java.util.Vector<XML> getProcessingInstructions()
          Retrieve the current set of processing instructions.
 int getSize()
          The number of sub-elements in this element.
 java.lang.String getTag()
          Retrieve the type of this element
 java.lang.String getValue()
          Gets the value contained in this element.
 XML remove(int index)
          Remove a specific element
 boolean removeElement(java.lang.Object element)
          Removes the first occurrence of the specified sub-element in this XML If the XML does not contain the sub-element, it is unchanged.
 void setAttribute(java.lang.String name, int value)
          Add an attribute to this element
 void setAttribute(java.lang.String name, java.lang.String value)
          Add an attribute to this element
 void setDocType(java.lang.String docType)
           
 void setElementAt(XML obj, int index)
           
 void setValue(java.lang.String data)
          Store a value between the beginning and ending tags of this element An element may have either a single value or an ordered list of sub-elements.
 java.lang.String toString()
          Converts the XML to string
 java.lang.String toString(int indent)
          Converts the XML to String
 java.lang.StringBuffer toStringBuffer(int indent)
          Converts the XML to a StringBuffer
 
Methods inherited from class java.util.Vector
add, addAll, addAll, capacity, clear, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, removeAll, removeAllElements, removeElementAt, retainAll, set, setSize, size, subList, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
iterator, listIterator, listIterator
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
iterator, listIterator, listIterator
 

Field Detail

HEADER

public static final java.lang.String HEADER
The header placed at the beginning of each formatted XML string.

See Also:
Constant Field Values

NOINDENT

public static final int NOINDENT
Argument to toString(int) to suppress indentation

See Also:
toString(int), Constant Field Values

NOPI

public static final int NOPI
Argument to toString(int) to suppress indentation and processing instructions

See Also:
toString(int), Constant Field Values
Constructor Detail

XML

public XML(java.lang.String tag)
Create an element with the given tag

Parameters:
tag -

XML

public XML(java.io.Reader source)
    throws java.io.IOException
Create a DOM-like structure from an XML input stream.

Parameters:
source - A Reader such as a StringReader or a FileReader.
Throws:
java.io.IOException - If there are errors in the input stream.
Method Detail

setDocType

public void setDocType(java.lang.String docType)

clone

public XML clone()
Creates and returns a copy of this object. The attributes are shallow copies in that the names and values are not cloned. The value and the elements are cloned.

Overrides:
clone in class java.util.Vector<XML>
Returns:
a clone of this instance.
See Also:
Cloneable

equals

public boolean equals(XML other)
Compare attributes, value, and elements of two XML objects for equality. To be equal, the tags must match, the attributes must match and either the values must match, or for each element there must bevan equal element on the other object.

Parameters:
other -
Returns:
true if they match

equals

public boolean equals(java.lang.Object other)
Specified by:
equals in interface java.util.Collection<XML>
Specified by:
equals in interface java.util.List<XML>
Overrides:
equals in class java.util.Vector<XML>

getTag

public java.lang.String getTag()
Retrieve the type of this element

Returns:
The tag at the beginning of this element

getAttributeCount

public int getAttributeCount()
Determine how many attributes there are

Returns:
A count of the attributes

getAttributeList

public java.util.Enumeration<java.lang.Object> getAttributeList()
Get an Enumeration of the attributes

Returns:
An Enumeration object for the names of the attributes.

getAttribute

public java.lang.String getAttribute(java.lang.String name)
Retrieve an attribute of this elemrnt

Parameters:
name -
Returns:
the value of the attribute

getIntAttribute

public int getIntAttribute(java.lang.String name)
Retrieve an attribute of this elemrnt which is an int

Parameters:
name -
Returns:
the value of the attribute

setAttribute

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

Parameters:
name - The name of the attribute
value - The value to be set. If null, the attribute is removed

setAttribute

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

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

addElement

public void addElement(XML element)
Add a sub-element to the current XML element. An element may have either a single value or an ordered list of sub-elements.

Overrides:
addElement in class java.util.Vector<XML>
Parameters:
element - An XML element

add

public boolean add(XML subElement)
Appends the specified sub-element to this XML.

Specified by:
add in interface java.util.Collection<XML>
Specified by:
add in interface java.util.List<XML>
Overrides:
add in class java.util.Vector<XML>
Parameters:
subElement - sub-element to be appended to this XML.
Returns:
true (as per the general contract of Collection.add).
See Also:
addElement(com.bmc.mainview.nvbapi.XML)

removeElement

public boolean removeElement(java.lang.Object element)
Removes the first occurrence of the specified sub-element in this XML If the XML does not contain the sub-element, it is unchanged.

Overrides:
removeElement in class java.util.Vector<XML>
Parameters:
element - sub-element to be removed from this XML, if present.
Returns:
true if the XML contained the specified sub-element.

remove

public XML remove(int index)
Remove a specific element

Specified by:
remove in interface java.util.List<XML>
Overrides:
remove in class java.util.Vector<XML>
Parameters:
index - the element number to remove
See Also:
Vector.remove(int)

addElement

public void addElement(java.lang.String s,
                       java.lang.String parm)
A utility method that adds a sub-element with the given value to the element

Parameters:
s - The tag of the new sub-element
parm - The value that the sub-element is to contain.

getElement

public XML getElement(int n)
Get the nth sub-element of the element

Parameters:
n - The ordinal number of the desired sub-element
Returns:
The sub-element

setElementAt

public void setElementAt(XML obj,
                         int index)
Overrides:
setElementAt in class java.util.Vector<XML>

findElement

public XML findElement(java.lang.String tag)
Searches for the first sub-element with the given tag and returns it.

Parameters:
tag - The type of the sub-element
Returns:
The requested sub-element, or null if not found.

findElement

public XML findElement(java.lang.String tag,
                       int which)
Searches for the nth sub-element with the given tag and returns it.

Parameters:
tag - The type of the sub-element
which - Zer0-based index of the desired element
Returns:
The requested sub-element, or null if not found.

findElements

public java.util.Enumeration<XML> findElements(java.lang.String tag)
Allows sub-elements of a given type to be accessed sequentially

Parameters:
tag -
Returns:
An Enumeration of the sub-elements with the given tag.

getParent

public XML getParent()
Find the XML element of which this is a sub-element.

Returns:
The containing XML element or null if none.

getSize

public int getSize()
The number of sub-elements in this element.

Returns:
The sub-element count

getProcessingInstructions

public java.util.Vector<XML> getProcessingInstructions()
Retrieve the current set of processing instructions.

Returns:
A Vector of XML objects. Any change to the Vector will change the set of processing instructions in this XML
See Also:
addProcessingInstruction(XML)

addProcessingInstruction

public void addProcessingInstruction(XML pi)
Add a processing instruction to the current XML element. When an XML element is converted to String or StringBuffer the processing instructions will preceed it surrounded by <? and ?> brackets. Only the processing instructions of the topmost element will be output. Those of sub-elements are ignored.

Parameters:
pi - An XML element. The tag will be the tag of the processing instruction and the attributes will be the parameters. Any value or sub-elements will be ignored.
See Also:
toString(), toString(int), toStringBuffer(int)

getValue

public java.lang.String getValue()
Gets the value contained in this element. An element may have either a single value or an ordered list of sub-elements.

Returns:
The value of the element
Throws:
java.lang.IllegalArgumentException - if the element has sub-elements.

setValue

public void setValue(java.lang.String data)
Store a value between the beginning and ending tags of this element An element may have either a single value or an ordered list of sub-elements.

Parameters:
data -
Throws:
java.lang.IllegalArgumentException - if the element has sub-elements.

dump

public java.lang.String dump()
Writes a printable version of the entire XML stream which contains this element.

Returns:
A printable version of the object.

toString

public java.lang.String toString()
Converts the XML to string

Overrides:
toString in class java.util.Vector<XML>
Returns:
An XML stream

toString

public java.lang.String toString(int indent)
Converts the XML to String

Parameters:
indent - The initial indentation.
Returns:
An XML stream
See Also:
NOINDENT, NOPI

toStringBuffer

public java.lang.StringBuffer toStringBuffer(int indent)
Converts the XML to a StringBuffer

Parameters:
indent - The initial indentation.
Returns:
An XML stream
See Also:
NOINDENT, NOPI

getElementCount

public int getElementCount(java.lang.String tag)
Returns number of sub-elements with the given tag

Parameters:
tag -
Returns:
Count of sub-elements with the given tag