public class Element extends Object implements Iterable<Element>
Modifier and Type | Field and Description |
---|---|
static int |
DEPTH_INFINITE
A constant that can be used for the get descendants methods to denote infinite recursion
|
Modifier | Constructor and Description |
---|---|
protected |
Element()
Constructs and Element without specifying a name
This can be used by subclasses that do not require name assigments.
|
|
Element(String tagName)
Constructs an Element with the specified name
|
|
Element(String tagName,
boolean isTextElement)
Constructs an Element (or a text element) with the specified name or text.
|
Modifier and Type | Method and Description |
---|---|
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)
|
String |
getAttribute(String name)
Returns the attribute value by its name (or null if it wasn't defined for this element)
|
int |
getAttributeAsInt(String name,
int def)
Helper method to return the attribute as an integer
|
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 Vector |
getChildren()
Deprecated.
this uses the old vector API instead of the more modern Collection/List
|
Vector |
getChildrenByTagName(String name)
Returns all children with the specified tag name
|
Vector |
getDescendantsByTagName(String name)
Returns all descendants with the specified tag name
|
Vector |
getDescendantsByTagName(String name,
int depth)
Returns all descendants with the specified tag name
|
Vector |
getDescendantsByTagNameAndAttribute(String name,
String attributeName,
int depth)
Returns all descendants with the specified tag name and the none empty attribute
|
Element |
getElementById(String id)
Returns the element with the specified ID
|
Element |
getFirstChildByTagName(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
|
String |
getTagName()
Returns this Element's tag name
|
String |
getText()
Returns the text of this element (for text elements only)
|
Vector |
getTextChildren(String text,
boolean caseSensitive)
Returns all children with the specified text
|
Vector |
getTextDescendants(String text,
boolean caseSensitive)
Returns all text descendants containing the specified text
|
Vector |
getTextDescendants(String text,
boolean caseSensitive,
int depth)
Returns all text descendants containing the specified text
|
boolean |
hasTextChild()
Determines whether or not this Element has any text children.
|
void |
insertChildAt(Element child,
int index)
Inserts the given child at the specified index
|
boolean |
isEmpty()
Determines whether or not this Element has no children.
|
boolean |
isTextElement()
Returns true if this is a text element, false otherwise
|
Iterator<Element> |
iterator()
Iterable for children of this entry making tree walking easier, this makes for(Element child : base) {} possible
|
protected void |
removeAttribute(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(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(Object id,
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(String attribute,
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(Vector children)
Deprecated.
this uses the old vector API instead of the more modern Collection/List
|
protected void |
setParent(Element parent)
Sets this element parent, done interanlly in addChild
|
protected void |
setTagName(String name)
Sets the name or text (for text elements) of this tag
|
void |
setText(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
|
String |
toString()
Returns a printable string representing this element
|
String |
toString(String spacing)
A recursive method for creating a printout of a full tag with its entire hierarchy.
|
public static final int DEPTH_INFINITE
protected Element()
public Element(String tagName)
tagName
- The tag namepublic Element(String tagName, boolean isTextElement)
tagName
- The tag name, or in the case of a text element the element's textisTextElement
- true for a text element, false otherwisepublic boolean isTextElement()
public String getTagName()
IllegalStateException
- if this is a text elementpublic Hashtable getAttributes()
protected void setAttribute(Object id, String value)
id
- The attribute IDvalue
- The attribute valuepublic void addChild(Element childElement)
childElement
- The child elementprotected void setParent(Element parent)
parent
- The element's parentpublic Element getParent()
public int getNumChildren()
protected Vector getChildren()
protected void setChildren(Vector children)
children
- The vector to set as this Element's childrenprotected void setTagName(String name)
name
- The name or text of this tagprotected void setTextElement(boolean textElement)
textElement
- true to set this as a text element, false otherwisepublic Element getChildAt(int index)
index
- The requested indexArrayIndexOutOfBoundsException
- if the index is bigger than the children's count or smaller than 0public Element getFirstChildByTagName(String name)
name
- The child's tag namepublic Element getElementById(String id)
id
- The id to findpublic Vector getDescendantsByTagNameAndAttribute(String name, String attributeName, int depth)
name
- The tag name to look forattributeName
- the attribute that must exist on the tagdepth
- The search depth (1 - children, 2 - grandchildren .... DEPTH_INFINITE - for all descendants)public Vector getDescendantsByTagName(String name, int depth)
name
- The tag name to look fordepth
- The search depth (1 - children, 2 - grandchildren .... DEPTH_INFINITE - for all descendants)public Vector getDescendantsByTagName(String name)
name
- The tag name to look forpublic Vector getChildrenByTagName(String name)
name
- The tag name to look forpublic Vector getTextDescendants(String text, boolean caseSensitive, int depth)
text
- The text to look for (null to return all text children)caseSensitive
- true to perform a case sensitive match, false to ignore casedepth
- The search depth (1 - children, 2 - grandchildren .... DEPTH_INFINITE - for all descendants)public Vector getTextDescendants(String text, boolean caseSensitive)
text
- The text to look for (null to return all text children)caseSensitive
- true to perform a case sensitive match, false to ignore casepublic Vector getTextChildren(String text, boolean caseSensitive)
text
- The text to look for (null to return all text children)caseSensitive
- true to perform a case sensitive match, false to ignore casepublic boolean contains(Element element)
element
- The element to look forpublic int setAttribute(String attribute, String value)
attribute
- The attribute's namevalue
- The attribute's valuepublic void removeAttribute(String attribute)
attribute
- The attribute to removeprotected void removeAttribute(Object id)
id
- The attribute IDpublic int getAttributeAsInt(String name, int def)
name
- the name of the attributedef
- default valuepublic String getAttribute(String name)
name
- The attribute idpublic void removeChildAt(int index)
index
- The child's indexpublic int getChildIndex(Element child)
child
- The child element to look forpublic void insertChildAt(Element child, int index)
child
- The child to insertindex
- The index to insert it atpublic void replaceChild(Element oldChild, Element newChild)
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 withpublic String getText()
IllegalStateException
- if this is not a text elementpublic void setText(String str)
str
- The text to setIllegalStateException
- if this is not a text elementpublic String toString()
public String toString(String spacing)
spacing
- Increased by one in each recursion phase to provide with indentationpublic boolean hasTextChild()
public boolean isEmpty()