TUM CCSM Commons

edu.tum.cs.commons.xml
Class XMLReader<E extends java.lang.Enum<E>,A extends java.lang.Enum<A>,X extends java.lang.Exception>

java.lang.Object
  extended by edu.tum.cs.commons.xml.XMLReader<E,A,X>

public abstract class XMLReader<E extends java.lang.Enum<E>,A extends java.lang.Enum<A>,X extends java.lang.Exception>
extends java.lang.Object

Utility class for reading XML documents. Please consult test case XMLReaderTest to see how this class is intended to be used.

Version:
$Rev: 26268 $
Author:
Florian Deissenboeck, $Author: juergens $
Rating:
GREEN Hash: 069D289898A054917CFA09EFB65AAE80

Constructor Summary
XMLReader(java.io.File file, IXMLResolver<E,A> xmlResolver)
          Create new reader.
XMLReader(java.io.File file, java.lang.String encoding, IXMLResolver<E,A> xmlResolver)
          Create reader.
XMLReader(java.io.File file, java.lang.String encoding, java.net.URL schemaURL, IXMLResolver<E,A> xmlResolver)
          Create reader.
XMLReader(java.io.File file, java.net.URL schemaURL, IXMLResolver<E,A> xmlResolver)
          Create reader.
 
Method Summary
protected  boolean getBooleanAttribute(A attribute)
          Get boolean value of an attribute.
protected  java.lang.String getChildText(E childElement)
          Get the text content of a child element of the current element.
protected
<T extends java.lang.Enum<T>>
T
getEnumAttribute(A attribute, java.lang.Class<T> enumClass)
          Translate attribute value to an enumeration element.
protected  int getIntAttribute(A attribute)
          Get int value of an attribute.
protected  long getLongAttribute(A attribute)
          Get long value of an attribute.
protected  java.lang.String getStringAttribute(A attribute)
          Get attribute value.
protected  java.lang.String getText()
          Get text content of current node.
protected  void parseFile()
          Parse file.
protected  void processChildElements(IXMLElementProcessor<E,X> processor)
          Process the child elements of the current element with a given processor.
protected  void processDecendantElements(IXMLElementProcessor<E,X> processor)
          Process all descendant elements of the current element with a given processor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLReader

public XMLReader(java.io.File file,
                 IXMLResolver<E,A> xmlResolver)
Create new reader.

Parameters:
file - the file to be read
xmlResolver - resolvers used by this reader

XMLReader

public XMLReader(java.io.File file,
                 java.lang.String encoding,
                 IXMLResolver<E,A> xmlResolver)
Create reader.

Parameters:
file - the file to be read
encoding - XML encoding of the file. No encoding is set if null.
xmlResolver - resolvers used by this reader

XMLReader

public XMLReader(java.io.File file,
                 java.net.URL schemaURL,
                 IXMLResolver<E,A> xmlResolver)
Create reader.

Parameters:
file - the file to be read
schemaURL - the URL pointing to the schema that is used for validation. No validation will be performed if null.
xmlResolver - resolvers used by this reader

XMLReader

public XMLReader(java.io.File file,
                 java.lang.String encoding,
                 java.net.URL schemaURL,
                 IXMLResolver<E,A> xmlResolver)
Create reader.

Parameters:
file - the file to be read
encoding - XML encoding of the file. No encoding is set if null.
schemaURL - the URL pointing to the schema that is used for validation. No validation will be performed if null.
xmlResolver - resolvers used by this reader
Method Detail

getBooleanAttribute

protected boolean getBooleanAttribute(A attribute)
Get boolean value of an attribute.

Returns:
the boolean value, semantics for non-translatable or null values is defined by Boolean.valueOf(String).

getChildText

protected java.lang.String getChildText(E childElement)
Get the text content of a child element of the current element.

Parameters:
childElement - the child element
Returns:
the text or null if the current element doesn't have the requested child element

getEnumAttribute

protected <T extends java.lang.Enum<T>> T getEnumAttribute(A attribute,
                                                           java.lang.Class<T> enumClass)
Translate attribute value to an enumeration element.

Parameters:
attribute - the attribute
enumClass - the enumeration class
Returns:
the enum value, semantics for non-translatable or null values is defined by Enum.valueOf(Class, String).

getIntAttribute

protected int getIntAttribute(A attribute)
Get int value of an attribute.

Returns:
the int value, semantics for non-translatable or null values is defined by Integer.valueOf(String).

getLongAttribute

protected long getLongAttribute(A attribute)
Get long value of an attribute.

Returns:
the int value, semantics for non-translatable or null values is defined by Integer.valueOf(String).

getStringAttribute

protected java.lang.String getStringAttribute(A attribute)
Get attribute value.

Returns:
the attribute value or null if attribute is undefined.

getText

protected java.lang.String getText()
Get text content of current node.


parseFile

protected void parseFile()
                  throws org.xml.sax.SAXException,
                         java.io.IOException
Parse file. This sets the current element focus to the document root element. If schema URL was set the document is validated against the schema.

Sub classes should typically wrap this method with a proper error handling mechanism.

Throws:
org.xml.sax.SAXException - if a parsing exceptions occurs
java.io.IOException - if an IO exception occurs.

processChildElements

protected void processChildElements(IXMLElementProcessor<E,X> processor)
                             throws X extends java.lang.Exception
Process the child elements of the current element with a given processor. Target elements are specified by IXMLElementProcessor.getTargetElement().

Parameters:
processor - the processor used to process the elements
Throws:
X - if the processor throws an exception
X extends java.lang.Exception

processDecendantElements

protected void processDecendantElements(IXMLElementProcessor<E,X> processor)
                                 throws X extends java.lang.Exception
Process all descendant elements of the current element with a given processor. In contrast to processChildElements(IXMLElementProcessor), not only direct child elements are processed. Descendant elements are processed in the sequence they are found during a top-down, left-right traversal of the XML document.

Target elements are specified by IXMLElementProcessor.getTargetElement().

Parameters:
processor - the processor used to process the elements
Throws:
X - if the processor throws an exception
X extends java.lang.Exception

TUM CCSM Commons

TUM CCSM Commons - 2.7