xmlwise
Class XmlElement

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by java.util.LinkedList<XmlElement>
                  extended by xmlwise.XmlElement
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<XmlElement>, java.util.Collection<XmlElement>, java.util.Deque<XmlElement>, java.util.List<XmlElement>, java.util.Queue<XmlElement>

public class XmlElement
extends java.util.LinkedList<XmlElement>

A simplified XML Element that only has an attribute map, a list of sub elements a text value and a name.

This is the basic entity of the xmlwise library.

Author:
Christoffer Lerno
See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
XmlElement(org.w3c.dom.Element element)
          Creates a new XmlElement given an Element object.
XmlElement(java.lang.String name)
          Creates a new XmlElement with the given name with no inner text.
XmlElement(java.lang.String name, java.lang.String value)
          Creates a new XmlElement with the given name and inner text value.
 
Method Summary
 boolean contains(java.lang.String key)
          Determines if a direct sub-element exists.
 boolean containsAttribute(java.lang.String attribute)
          Determines if an attribute exists.
 boolean equals(java.lang.Object o)
           
 java.util.LinkedList<XmlElement> get(java.lang.String name)
          Get all elements matching the given key.
 java.lang.String getAttribute(java.lang.String attribute)
          Returns the (string) value of an attribute.
 java.lang.String getAttribute(java.lang.String attribute, java.lang.String defaultValue)
          Get a (string) attribute for this element, defaulting to a default value if the attribute is missing.
 XmlElementAttributes getAttributes()
          Returns a map with all attributes of this element.
 boolean getBoolAttribute(java.lang.String attribute)
          Get an boolean attribute for this element.
 boolean getBoolAttribute(java.lang.String attribute, boolean defaultValue)
          Get an boolean attribute for this element, defaulting to the default value if missing.
 double getDoubleAttribute(java.lang.String attribute)
          Get a double attribute for this element.
 double getDoubleAttribute(java.lang.String attribute, double defaultValue)
          Get an double attribute for this element, defaulting to a default value if the attribute is missing.
 int getIntAttribute(java.lang.String attribute)
          Get an integer attribute for this element.
 int getIntAttribute(java.lang.String attribute, int defaultValue)
          Get an integer attribute for this element, defaulting to a default value if the attribute is missing.
 java.lang.String getName()
          Returns the name of this element.
 XmlElement getUnique(java.lang.String name)
          Get the single direct sub-element with the given name.
 java.lang.String getValue()
          Get the string value contained in this element.
 int hashCode()
           
 boolean removeAttribute(java.lang.String attribute)
          Convenience method to remove an attribute from this element.
 void setAttribute(java.lang.String attribute, java.lang.Object value)
          Convenience method to set an attribute on this element.
 java.lang.String toString()
           
 java.lang.String toXml()
          Renders this as XML.
 
Methods inherited from class java.util.LinkedList
add, add, addAll, addAll, addFirst, addLast, clear, clone, contains, descendingIterator, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, set, size, toArray, toArray
 
Methods inherited from class java.util.AbstractSequentialList
iterator
 
Methods inherited from class java.util.AbstractList
listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, isEmpty, iterator, listIterator, removeAll, retainAll, subList
 
Methods inherited from interface java.util.Deque
iterator
 

Constructor Detail

XmlElement

public XmlElement(org.w3c.dom.Element element)
Creates a new XmlElement given an Element object.

Parameters:
element - the document element to construct this object from.

XmlElement

public XmlElement(java.lang.String name,
                  java.lang.String value)
Creates a new XmlElement with the given name and inner text value.

Parameters:
name - the name of the node.
value - the inner text value of the node.

XmlElement

public XmlElement(java.lang.String name)
Creates a new XmlElement with the given name with no inner text.

Parameters:
name - the name of the node.
Method Detail

getUnique

public XmlElement getUnique(java.lang.String name)
                     throws XmlParseException
Get the single direct sub-element with the given name.

Parameters:
name - the name of the sub-element.
Returns:
the sub element.
Throws:
XmlParseException - if there are more than one of the sub element, or if no such element was found.

getIntAttribute

public int getIntAttribute(java.lang.String attribute)
                    throws XmlParseException
Get an integer attribute for this element.

Parameters:
attribute - the name of the attribute.
Returns:
the integer value of the attribute.
Throws:
XmlParseException - if we fail to parse this attribute as an int, or the attribute is missing.

getIntAttribute

public int getIntAttribute(java.lang.String attribute,
                           int defaultValue)
                    throws XmlParseException
Get an integer attribute for this element, defaulting to a default value if the attribute is missing.

Parameters:
attribute - the name of the attribute.
defaultValue - the default value for the attribute, returned if the attribute is missing.
Returns:
the integer value of the attribute or the default value if the attribute is missing.
Throws:
XmlParseException - if we fail to parse this attribute as an int.

getDoubleAttribute

public double getDoubleAttribute(java.lang.String attribute)
                          throws XmlParseException
Get a double attribute for this element.

Parameters:
attribute - the name of the attribute.
Returns:
the double value of the attribute.
Throws:
XmlParseException - if we fail to parse this attribute as an double, or the attribute is missing.

getDoubleAttribute

public double getDoubleAttribute(java.lang.String attribute,
                                 double defaultValue)
                          throws XmlParseException
Get an double attribute for this element, defaulting to a default value if the attribute is missing.

Parameters:
attribute - the name of the attribute.
defaultValue - the default value for the attribute, returned if the attribute is missing.
Returns:
the double value of the attribute or the default value if the attribute is missing.
Throws:
XmlParseException - if we fail to parse this attribute as an double.

getAttribute

public java.lang.String getAttribute(java.lang.String attribute,
                                     java.lang.String defaultValue)
Get a (string) attribute for this element, defaulting to a default value if the attribute is missing.

Parameters:
attribute - the name of the attribute.
defaultValue - the default value for the attribute, returned if the attribute is missing.
Returns:
the value of the attribute or the default value if the attribute is missing.

getAttribute

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

Parameters:
attribute - the attribute name.
Returns:
the value of the attribute.

getBoolAttribute

public boolean getBoolAttribute(java.lang.String attribute)
                         throws XmlParseException
Get an boolean attribute for this element.

"true", "yes" and "y" are all interpreted as true. (Case-independent)

"false", "no" and "no" are all interpreted at false. (Case-independent)

Parameters:
attribute - the name of the attribute.
Returns:
the boolean value of the attribute.
Throws:
XmlParseException - if the attribute value does match true or false as defined, or the attribute is missing.

getBoolAttribute

public boolean getBoolAttribute(java.lang.String attribute,
                                boolean defaultValue)
                         throws XmlParseException
Get an boolean attribute for this element, defaulting to the default value if missing.

"true", "yes" and "y" are all interpreted as true. (Case-independent)

"false", "no" and "no" are all interpreted at false. (Case-independent)

Parameters:
attribute - the name of the attribute.
defaultValue - the default value of the attribute.
Returns:
the boolean value of the attribute or the default value if the attribute is missing.
Throws:
XmlParseException - if the attribute value does match true or false as defined

get

public java.util.LinkedList<XmlElement> get(java.lang.String name)
Get all elements matching the given key.

Parameters:
name - the key to match.
Returns:
a linked list of matching xml elements

setAttribute

public void setAttribute(java.lang.String attribute,
                         java.lang.Object value)
Convenience method to set an attribute on this element.

Parameters:
attribute - the attribute to set.
value - the new value of the attribute.

removeAttribute

public boolean removeAttribute(java.lang.String attribute)
Convenience method to remove an attribute from this element.

Parameters:
attribute - the attribute to remove.
Returns:
true if this attribute existed before it was removed, false otherwise.

contains

public boolean contains(java.lang.String key)
Determines if a direct sub-element exists.

Parameters:
key - the name of the sub-element.
Returns:
true if the element exists, false otherwise.

toXml

public java.lang.String toXml()
Renders this as XML.

Returns:
an xml string based on this element and its sub-elements.

getValue

public java.lang.String getValue()
Get the string value contained in this element.

E.g. the element for <node>foo</node> would return "foo" as its value.

Note that this value will be a concatenation of all strings directly inside the element, even if the element contains sub elements.

Returns:
the string value contained inside this element.

getAttributes

public XmlElementAttributes getAttributes()
Returns a map with all attributes of this element.

Returns:
a map with the attributes for this element.

getName

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

E.g. the element for <node>foo</node> would return "node" as its name.

Returns:
the name of this element.

containsAttribute

public boolean containsAttribute(java.lang.String attribute)
Determines if an attribute exists.

Parameters:
attribute - the attribute to check.
Returns:
true if the attribute exists on this element, false otherwise.

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Collection<XmlElement>
Specified by:
equals in interface java.util.List<XmlElement>
Overrides:
equals in class java.util.AbstractList<XmlElement>

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Collection<XmlElement>
Specified by:
hashCode in interface java.util.List<XmlElement>
Overrides:
hashCode in class java.util.AbstractList<XmlElement>

toString

public java.lang.String toString()
Overrides:
toString in class java.util.AbstractCollection<XmlElement>