com.sun.lwuit.xml
Class Element

java.lang.Object
  extended by com.sun.lwuit.xml.Element
Direct Known Subclasses:
HTMLElement

public class Element
extends java.lang.Object

The Element class defines a single XML element with its attributes and children. Due to its hierarchial nature, this class can be used for a single "leaf" Element, for more complex elements (with child elements), and up to describing the entire document.


Field Summary
static int DEPTH_INFINITE
          A constant that can be used for the get descendants methods to denote infinite recursion
 
Constructor Summary
protected Element()
          Constructs and Element without specifying a name This can be used by subclasses that do not require name assigments.
  Element(java.lang.String tagName)
          Constructs an Element with the specified name
  Element(java.lang.String tagName, boolean isTextElement)
          Constructs an Element (or a text element) with the specified name or text.
 
Method Summary
 void addChild(Element childElement)
          Adds the specified Element as a child to this element.
 boolean contains(Element element)
          Returns true if the specified element is contained in this element's hierarchy (meaning it is one of its descendants)
 java.lang.String getAttribute(java.lang.String name)
          Returns the attribute value by its name (or null if it wasn't defined for this element)
protected  java.util.Hashtable getAttributes()
          Returns the attributes Hashtable
 Element getChildAt(int index)
          Returns the Element's child positioned at the specified index
 int getChildIndex(Element child)
          Returns the child index
protected  java.util.Vector getChildren()
          Returns the internal children vector
 java.util.Vector getChildrenByTagName(java.lang.String name)
          Returns all children with the specified tag name
 java.util.Vector getDescendantsByTagName(java.lang.String name)
          Returns all descendants with the specified tag name
 java.util.Vector getDescendantsByTagName(java.lang.String name, int depth)
          Returns all descendants with the specified tag name
 Element getElementById(java.lang.String id)
          Returns the element with the specified ID
 Element getFirstChildByTagName(java.lang.String name)
          Returns an Element's child by a tag name
 int getNumChildren()
          Returns the number of this Element's children
 Element getParent()
          Returns this Element's parent
 java.lang.String getTagName()
          Returns this Element's tag name
 java.lang.String getText()
          Returns the text of this element (for text elements only)
 java.util.Vector getTextChildren(java.lang.String text, boolean caseSensitive)
          Returns all children with the specified text
 java.util.Vector getTextDescendants(java.lang.String text, boolean caseSensitive)
          Returns all text descendants containing the specified text
 java.util.Vector getTextDescendants(java.lang.String text, boolean caseSensitive, int depth)
          Returns all text descendants containing the specified text
 void insertChildAt(Element child, int index)
          Inserts the given child at the specified index
 boolean isTextElement()
          Returns true if this is a text element, false otherwise
protected  void removeAttribute(java.lang.Object id)
          Removes the specified attribute if it exist in this Element This method allows creating a key that is non-string to be used by subclasses that optimize attributes retrieval
 void removeAttribute(java.lang.String attribute)
          Removes the specified attribute
 void removeChildAt(int index)
          Removes the child at the given index
 void replaceChild(Element oldChild, Element newChild)
          Replaces one child with another
protected  void setAttribute(java.lang.Object id, java.lang.String value)
          Adds the specified attribute and value to this Element if it is supported for the Element and has a valid value.
 int setAttribute(java.lang.String attribute, java.lang.String value)
          Adds the specified attribute and value to this Element if it is supported for the Element and has a valid value.
protected  void setChildren(java.util.Vector children)
          Sets the children vector of this Element
protected  void setParent(Element parent)
          Sets this element parent, done interanlly in addChild
protected  void setTagName(java.lang.String name)
          Sets the name or text (for text elements) of this tag
 void setText(java.lang.String str)
          Sets the text of this element to the specified string (For text elements only)
protected  void setTextElement(boolean textElement)
          Sets this element as a text element
 java.lang.String toString()
          Returns a printable string representing this element
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEPTH_INFINITE

public static final int DEPTH_INFINITE
A constant that can be used for the get descendants methods to denote infinite recursion

See Also:
Constant Field Values
Constructor Detail

Element

protected Element()
Constructs and Element without specifying a name This can be used by subclasses that do not require name assigments.


Element

public Element(java.lang.String tagName)
Constructs an Element with the specified name

Parameters:
tagName - The tag name

Element

public Element(java.lang.String tagName,
               boolean isTextElement)
Constructs an Element (or a text element) with the specified name or text.

Parameters:
tagName - The tag name, or in the case of a text element the element's text
isTextElement - true for a text element, false otherwise
Method Detail

isTextElement

public boolean isTextElement()
Returns true if this is a text element, false otherwise

Returns:
true if this is a text element, false otherwise

getTagName

public java.lang.String getTagName()
Returns this Element's tag name

Returns:
the Element's tag name
Throws:
java.lang.IllegalStateException - if this is a text element

getAttributes

protected java.util.Hashtable getAttributes()
Returns the attributes Hashtable

Returns:
the attributes Hashtable

setAttribute

protected void setAttribute(java.lang.Object id,
                            java.lang.String value)
Adds the specified attribute and value to this Element if it is supported for the Element and has a valid value. This method allows creating a key that is non-string to be used by subclasses that optimize attributes retrieval

Parameters:
id - The attribute ID
value - The attribute value

addChild

public void addChild(Element childElement)
Adds the specified Element as a child to this element. If the specified element was found to be unsupported (i.e. it's ID is TAG_UNSUPPORTED, it is not added.

Parameters:
childElement - The child element

setParent

protected void setParent(Element parent)
Sets this element parent, done interanlly in addChild

Parameters:
parent - The element's parent

getParent

public Element getParent()
Returns this Element's parent

Returns:
this Element's parent

getNumChildren

public int getNumChildren()
Returns the number of this Element's children

Returns:
the number of this Element's children

getChildren

protected java.util.Vector getChildren()
Returns the internal children vector

Returns:
the children vector

setChildren

protected void setChildren(java.util.Vector children)
Sets the children vector of this Element

Parameters:
children - The vector to set as this Element's children

setTagName

protected void setTagName(java.lang.String name)
Sets the name or text (for text elements) of this tag

Parameters:
name - The name or text of this tag

setTextElement

protected void setTextElement(boolean textElement)
Sets this element as a text element

Parameters:
textElement - true to set this as a text element, false otherwise

getChildAt

public Element getChildAt(int index)
Returns the Element's child positioned at the specified index

Parameters:
index - The requested index
Returns:
child number index of this ELement
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is bigger than the children's count or smaller than 0

getFirstChildByTagName

public Element getFirstChildByTagName(java.lang.String name)
Returns an Element's child by a tag name

Parameters:
name - The child's tag name
Returns:
the first child with the specified name, or null if not found

getElementById

public Element getElementById(java.lang.String id)
Returns the element with the specified ID

Parameters:
id - The id to find
Returns:
An element with the id, or null if none found

getDescendantsByTagName

public java.util.Vector getDescendantsByTagName(java.lang.String name,
                                                int depth)
Returns all descendants with the specified tag name

Parameters:
name - The tag name to look for
depth - The search depth (1 - children, 2 - grandchildren .... DEPTH_INFINITE - for all descendants)
Returns:
A vector containing descendants with the specified tag name

getDescendantsByTagName

public java.util.Vector getDescendantsByTagName(java.lang.String name)
Returns all descendants with the specified tag name

Parameters:
name - The tag name to look for
Returns:
A vector containing descendants with the specified tag name

getChildrenByTagName

public java.util.Vector getChildrenByTagName(java.lang.String name)
Returns all children with the specified tag name

Parameters:
name - The tag name to look for
Returns:
A vector containing children with the specified tag name

getTextDescendants

public java.util.Vector getTextDescendants(java.lang.String text,
                                           boolean caseSensitive,
                                           int depth)
Returns all text descendants containing the specified text

Parameters:
text - The text to look for (null to return all text children)
caseSensitive - true to perform a case sensitive match, false to ignore case
depth - The search depth (1 - children, 2 - grandchildren .... DEPTH_INFINITE - for all descendants)
Returns:
A vector containing decensants containing the specified text

getTextDescendants

public java.util.Vector getTextDescendants(java.lang.String text,
                                           boolean caseSensitive)
Returns all text descendants containing the specified text

Parameters:
text - The text to look for (null to return all text children)
caseSensitive - true to perform a case sensitive match, false to ignore case
Returns:
A vector containing decensants containing the specified text

getTextChildren

public java.util.Vector getTextChildren(java.lang.String text,
                                        boolean caseSensitive)
Returns all children with the specified text

Parameters:
text - The text to look for (null to return all text children)
caseSensitive - true to perform a case sensitive match, false to ignore case
Returns:
A vector containing children containing the specified text

contains

public boolean contains(Element element)
Returns true if the specified element is contained in this element's hierarchy (meaning it is one of its descendants)

Parameters:
element - The element to look for
Returns:
true if this element contains the specified element, false otherwise

setAttribute

public int setAttribute(java.lang.String attribute,
                        java.lang.String value)
Adds the specified attribute and value to this Element if it is supported for the Element and has a valid value.

Parameters:
attribute - The attribute's name
value - The attribute's value
Returns:
a positive error code or -1 if attribute is supported and valid

removeAttribute

public void removeAttribute(java.lang.String attribute)
Removes the specified attribute

Parameters:
attribute - The attribute to remove

removeAttribute

protected void removeAttribute(java.lang.Object id)
Removes the specified attribute if it exist in this Element This method allows creating a key that is non-string to be used by subclasses that optimize attributes retrieval

Parameters:
id - The attribute ID

getAttribute

public java.lang.String getAttribute(java.lang.String name)
Returns the attribute value by its name (or null if it wasn't defined for this element)

Parameters:
name - The attribute id
Returns:
the attribute value by its name (or null if it wasn't defined for this element)

removeChildAt

public void removeChildAt(int index)
Removes the child at the given index

Parameters:
index - The child's index

getChildIndex

public int getChildIndex(Element child)
Returns the child index

Parameters:
child - The child element to look for
Returns:
The child position, or -1 if the child does not belong to this element.

insertChildAt

public void insertChildAt(Element child,
                          int index)
Inserts the given child at the specified index

Parameters:
child - The child to insert
index - The index to insert it at

replaceChild

public void replaceChild(Element oldChild,
                         Element newChild)
Replaces one child with another

Parameters:
oldChild - The child to replace (Must belong to this element, otherwise a call to this method will have no effect)
newChild - The child to replace it with

getText

public java.lang.String getText()
Returns the text of this element (for text elements only)

Returns:
the text of this element (for text elements only)
Throws:
java.lang.IllegalStateException - if this is not a text element

setText

public void setText(java.lang.String str)
Sets the text of this element to the specified string (For text elements only)

Parameters:
str - The text to set
Throws:
java.lang.IllegalStateException - if this is not a text element

toString

public java.lang.String toString()
Returns a printable string representing this element

Overrides:
toString in class java.lang.Object
Returns:
a printable string representing this element