org.objectweb.fractal.adl
Class XMLElement

java.lang.Object
  extended byorg.objectweb.fractal.adl.XMLElement

public class XMLElement
extends Object

An XML element. This class can also parse XML files, and can pretty print XML elements. It also offer some methods to help manually validate XML elements. But it does not handle XML entities, PCDATA and CDATA elements, XML DTDs, namespaces...


Field Summary
 int line
          The line number where this XML element starts.
 String source
          The file from which this XML element was parsed.
 String tag
          The tag of this XML element.
 
Constructor Summary
XMLElement(String tag)
          Creates a new empty XML element.
XMLElement(XMLElement elem)
          Creates a deep copy of the given XML element.
 
Method Summary
 void addSubElement(int index, XMLElement subElement)
          Adds a sub element to this XML element at the given index.
 void addSubElement(XMLElement subElement)
          Adds a sub element to this XML element.
 void checkAttributes(String attrs)
          Checks the attributes of this element.
 void checkSubElements(String subElements)
          Checks the sub-elements of this element.
 String getAttribute(String key, String defaultValue)
          Returns an attribute of this XML element.
 String[] getAttributes()
          Returns the names of the attributes of this XMLElement.
 XMLElement getSubElement(int index)
          Returns the sub element of this XML element whose index is given.
 XMLElement getSubElement(String tag)
          Returns the first sub element of this XML element whose tag is equal to the given tag.
 int getSubElementCount()
          Returns the number of sub elements of this XML element.
static XMLElement parse(LineNumberReader reader, boolean parseComment)
          Parses an XML element from a LineNumberReader.
 void removeSubElement(int index)
          Removes the sub element of this XML element whose index is given.
 void setAttribute(String key, String value)
          Sets an attribute of this XML element.
 void write(Writer writer)
          Writes the XML element to a writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tag

public String tag
The tag of this XML element.


source

public String source
The file from which this XML element was parsed. May be null.


line

public int line
The line number where this XML element starts.

Constructor Detail

XMLElement

public XMLElement(String tag)
Creates a new empty XML element.

Parameters:
tag - the tag of the element.

XMLElement

public XMLElement(XMLElement elem)
Creates a deep copy of the given XML element.

Parameters:
elem - the element to be cloned.
Method Detail

getAttributes

public final String[] getAttributes()
Returns the names of the attributes of this XMLElement.

Returns:
the names of the attributes of this XMLElement.

getAttribute

public final String getAttribute(String key,
                                 String defaultValue)
                          throws ParserException
Returns an attribute of this XML element.

Parameters:
key - the name of an attribute of this element.
defaultValue - the default value of this attribute, or null if there is no default value.
Returns:
the value of the given attribute of this element.
Throws:
ParserException - if this element does not have such an attribute, and the default value is null.
See Also:
setAttribute(String, String)

setAttribute

public final void setAttribute(String key,
                               String value)
Sets an attribute of this XML element.

Parameters:
key - the name of an attribute of this element.
value - the value of this attribute. May be null to remove the attribute.
See Also:
getAttribute(String, String)

getSubElementCount

public final int getSubElementCount()
Returns the number of sub elements of this XML element.

Returns:
the number of sub elements of this XML element.

getSubElement

public final XMLElement getSubElement(int index)
Returns the sub element of this XML element whose index is given.

Parameters:
index - the index of a sub element of this XML element.
Returns:
the sub element of this XML element whose index is given.

getSubElement

public final XMLElement getSubElement(String tag)
Returns the first sub element of this XML element whose tag is equal to the given tag.

Parameters:
tag - a tag name.
Returns:
the first sub element of this XML element whose tag is equal to the given tag, or null if there is no such sub element.

addSubElement

public final void addSubElement(XMLElement subElement)
Adds a sub element to this XML element.

Parameters:
subElement - the sub element to be added to this XML element.

addSubElement

public final void addSubElement(int index,
                                XMLElement subElement)
Adds a sub element to this XML element at the given index.

Parameters:
index - the index at which the given element must be added.
subElement - the sub element to be added to this XML element.

removeSubElement

public final void removeSubElement(int index)
Removes the sub element of this XML element whose index is given.

Parameters:
index - the index of a sub element to be removed.

checkAttributes

public void checkAttributes(String attrs)
                     throws ParserException
Checks the attributes of this element.

Parameters:
attrs - the name of the allowed attributes for this element, separated by commas (for example "name,value,type").
Throws:
ParserException - if an attribute of this element is not specified in the given allowed attributes list.

checkSubElements

public void checkSubElements(String subElements)
                      throws ParserException
Checks the sub-elements of this element.

Parameters:
subElements - the allowed sequences of sub element tags for this element, given as a simplified regular expression: tag | tag* | tag? | tag,tag. An example is "title,author,chapter*,annex?".
Throws:
ParserException - if the tags of the sub elements of this element do not conform to the specified regular expression.

write

public void write(Writer writer)
           throws IOException
Writes the XML element to a writer.

Parameters:
writer - the writer into which this element must be written.
Throws:
IOException - if an IO error occurs.

parse

public static XMLElement parse(LineNumberReader reader,
                               boolean parseComment)
                        throws IOException,
                               ParserException
Parses an XML element from a LineNumberReader.

Parameters:
reader - where to read the XML element.
parseComment - true to parse comments, or false to skip them.
Returns:
the parsed XML element.
Throws:
IOException - if an IO error occurs.
ParserException - if an XML syntax error is found.