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
setSchema(URL)
. Please consult
test case XMLReaderTest to see how this class is intended to be used.Constructor and Description |
---|
XMLReader(java.io.File file,
java.nio.charset.Charset encoding,
IXMLResolver<E,A> xmlResolver)
Create reader.
|
XMLReader(java.io.File file,
IXMLResolver<E,A> xmlResolver)
Create new reader.
|
XMLReader(java.io.Reader reader,
IXMLResolver<E,A> xmlResolver)
Create reader.
|
XMLReader(java.lang.String content,
IXMLResolver<E,A> xmlResolver)
Create reader.
|
Modifier and Type | Method and Description |
---|---|
protected void |
apply(IXMLElementProcessor<E,X>... processors)
This works similar to the template mechanism known from XSLT.
|
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>> |
getEnumAttribute(A attribute,
java.lang.Class<T> enumClass)
Translate attribute value to an enumeration element.
|
protected <T extends java.lang.Enum<T>> |
getEnumAttributeIgnoreCase(A attribute,
java.lang.Class<T> enumClass,
T defaultValue)
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 |
getNonEmptyStringAttribute(A attribute)
Get non-empty attribute value.
|
protected java.lang.String |
getStringAttribute(A attribute)
Get attribute value.
|
protected java.lang.String |
getText()
Get text content of current node.
|
protected boolean |
hasAttribute(A attribute)
Returns true if the current element has a given attribute.
|
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.
|
protected void |
setSchema(java.net.URL schemaURL)
Sets the URL pointing to the schema that is used for validation.
|
public XMLReader(java.io.File file, IXMLResolver<E,A> xmlResolver) throws java.io.IOException
file
- the file to be readxmlResolver
- resolvers used by this readerjava.io.IOException
public XMLReader(java.io.File file, java.nio.charset.Charset encoding, IXMLResolver<E,A> xmlResolver) throws java.io.IOException
file
- the file to be readencoding
- XML encoding of the file. No encoding is set if null
.xmlResolver
- resolvers used by this readerjava.io.IOException
public XMLReader(java.lang.String content, IXMLResolver<E,A> xmlResolver)
content
- XML string that gets parsed validation will be performed if
null
.xmlResolver
- resolvers used by this readerpublic XMLReader(java.io.Reader reader, IXMLResolver<E,A> xmlResolver)
reader
- the reader used to access the XML document.xmlResolver
- resolvers used by this readerprotected void setSchema(java.net.URL schemaURL)
protected boolean getBooleanAttribute(A attribute)
boolean
value of an attribute.Boolean.parseBoolean(String)
.protected java.lang.String getChildText(E childElement)
childElement
- the child elementnull
if the current element doesn't have the
requested child elementprotected <T extends java.lang.Enum<T>> T getEnumAttribute(A attribute, java.lang.Class<T> enumClass)
attribute
- the attributeenumClass
- the enumeration classEnum.valueOf(Class, String)
.protected <T extends java.lang.Enum<T>> T getEnumAttributeIgnoreCase(A attribute, java.lang.Class<T> enumClass, T defaultValue)
attribute
- the attributeenumClass
- the enumeration classdefaultValue
- the default value to return in case the attribute is not specified
or the enumeration does not contain the specified value.EnumUtils.valueOfIgnoreCase(Class, String)
.protected int getIntAttribute(A attribute)
int
value of an attribute.Integer.parseInt(String)
.protected long getLongAttribute(A attribute)
long
value of an attribute.Long.parseLong(String)
.protected java.lang.String getStringAttribute(A attribute)
protected java.lang.String getNonEmptyStringAttribute(A attribute) throws java.io.IOException
IOException
will be thrown.java.io.IOException
protected boolean hasAttribute(A attribute)
protected java.lang.String getText()
protected void parseFile() throws org.xml.sax.SAXException, java.io.IOException
Sub classes should typically wrap this method with a proper error handling mechanism.
org.xml.sax.SAXException
- if a parsing exceptions occursjava.io.IOException
- if an IO exception occurs.protected void processChildElements(IXMLElementProcessor<E,X> processor) throws X extends java.lang.Exception
IXMLElementProcessor.getTargetElement()
.processor
- the processor used to process the elementsX
- if the processor throws an exceptionX extends java.lang.Exception
protected void processDecendantElements(IXMLElementProcessor<E,X> processor) throws X extends java.lang.Exception
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()
.
processor
- the processor used to process the elementsX
- if the processor throws an exceptionX extends java.lang.Exception
protected void apply(IXMLElementProcessor<E,X>... processors) throws X extends java.lang.Exception
java.lang.AssertionError
- if multiple processors apply to the same target elementX extends java.lang.Exception