Package cdc.util.xml
Class AbstractStAXParser<R>
- java.lang.Object
-
- cdc.util.xml.AbstractStAXParser<R>
-
- Type Parameters:
R
- The result type.
public abstract class AbstractStAXParser<R> extends Object
Class that can facilitate the writing of an XML parser over an XMLStreamReader.- Author:
- Damien Carbonne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
AbstractStAXParser.ContextElementParser<C>
static interface
AbstractStAXParser.ElementParser
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.logging.log4j.Logger
logger
protected FailureReaction
reaction
protected XMLStreamReader
reader
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractStAXParser(XMLStreamReader reader, FailureReaction reaction)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected InvalidDataException
error(String message)
Throws an InvalidDataExeption.protected void
expect(String context, int eventType)
Checks that current event type is an expected one.protected void
expectEndDocument(String context)
Checks that current event type is END_DOCUMENT.protected void
expectEndElement(String context, String name)
Checks that current event type is END_ELEMNT.protected void
expectEndElement(String context, String... names)
protected void
expectStartDocument(String context)
Checks that current event type is START_DOCUMENT.protected void
expectStartElement(String context, String name)
Checks that current event type is START_ELEMNT.protected void
expectStartElement(String context, String... names)
protected boolean
getAttributeAsBoolean(String name, boolean def)
protected boolean
getAttributeAsBoolean(String name, boolean def, FailureReaction missingReaction, FailureReaction errorReaction)
protected byte
getAttributeAsByte(String name, byte def)
protected byte
getAttributeAsByte(String name, byte def, FailureReaction missingReaction, FailureReaction errorReaction)
protected double
getAttributeAsDouble(String name, double def)
protected double
getAttributeAsDouble(String name, double def, FailureReaction missingReaction, FailureReaction errorReaction)
protected <E extends Enum<E>>
EgetAttributeAsEnum(String name, Class<E> enumClass, E def)
protected <E extends Enum<E>>
EgetAttributeAsEnum(String name, Class<E> enumClass, E def, FailureReaction missingReaction, FailureReaction errorReaction)
protected float
getAttributeAsFloat(String name, float def)
protected float
getAttributeAsFloat(String name, float def, FailureReaction missingReaction, FailureReaction errorReaction)
protected int
getAttributeAsInt(String name, int def)
protected int
getAttributeAsInt(String name, int def, FailureReaction missingReaction, FailureReaction errorReaction)
protected long
getAttributeAsLong(String name, long def)
protected long
getAttributeAsLong(String name, long def, FailureReaction missingReaction, FailureReaction errorReaction)
protected Boolean
getAttributeAsOptionalBoolean(String name, Boolean def)
protected Boolean
getAttributeAsOptionalBoolean(String name, Boolean def, FailureReaction errorReaction)
protected Byte
getAttributeAsOptionalByte(String name, Byte def)
protected Byte
getAttributeAsOptionalByte(String name, Byte def, FailureReaction errorReaction)
protected Double
getAttributeAsOptionalDouble(String name, Double def)
protected Double
getAttributeAsOptionalDouble(String name, Double def, FailureReaction errorReaction)
protected <E extends Enum<E>>
EgetAttributeAsOptionalEnum(String name, Class<E> enumClass, E def)
protected <E extends Enum<E>>
EgetAttributeAsOptionalEnum(String name, Class<E> enumClass, E def, FailureReaction errorReaction)
protected Float
getAttributeAsOptionalFloat(String name, Float def)
protected Float
getAttributeAsOptionalFloat(String name, Float def, FailureReaction errorReaction)
protected Integer
getAttributeAsOptionalInt(String name, Integer def)
protected Integer
getAttributeAsOptionalInt(String name, Integer def, FailureReaction errorReaction)
protected Long
getAttributeAsOptionalLong(String name, Long def)
protected Long
getAttributeAsOptionalLong(String name, Long def, FailureReaction errorReaction)
protected Enum<?>
getAttributeAsOptionalRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def)
protected Enum<?>
getAttributeAsOptionalRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def, FailureReaction errorReaction)
protected Short
getAttributeAsOptionalShort(String name, Short def)
protected Short
getAttributeAsOptionalShort(String name, Short def, FailureReaction errorReaction)
protected Enum<?>
getAttributeAsRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def)
protected Enum<?>
getAttributeAsRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def, FailureReaction missingReaction, FailureReaction errorReaction)
protected short
getAttributeAsShort(String name, short def)
protected short
getAttributeAsShort(String name, short def, FailureReaction missingReaction, FailureReaction errorReaction)
protected String
getAttributeValue(String name, String def)
protected String
getAttributeValue(String name, String def, FailureReaction missingReaction)
Returns an attribute value.protected org.apache.logging.log4j.Logger
getLogger()
FailureReaction
getReaction()
protected void
ignoreElement()
Must be called after a START_ELEMENT has been found.protected boolean
isStartElement(String name)
Returnstrue
if reader is on a START_ELEMENT that has a given name.protected int
next()
Gets the next parsing event.protected int
nextTag()
Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.protected void
onError(String message)
Function that either keeps silent, warns or raises an exception, depending on error reaction.protected <T> T
onError(String message, T def)
Function that either silently returns a value, warns and returns a value or raises an exception, depending on error reaction.protected <T> T
onResult(T result, String message, T def)
Function that returns a computed value if it is notnull
, or silently returns a default value, warns and returns a default value or throws an exception.protected abstract R
parse()
Base method called to parse the stream.protected <C> void
parseChildren(C context, String name, AbstractStAXParser.ContextElementParser<C> childParser)
Utility used to parse an element and all its children.protected void
parseChildren(String name, AbstractStAXParser.ElementParser childParser)
protected void
trace()
protected void
trace(String message)
protected InvalidDataException
unexpectedEvent()
-
-
-
Field Detail
-
logger
protected final org.apache.logging.log4j.Logger logger
-
reader
protected final XMLStreamReader reader
-
reaction
protected final FailureReaction reaction
-
-
Constructor Detail
-
AbstractStAXParser
protected AbstractStAXParser(XMLStreamReader reader, FailureReaction reaction)
-
-
Method Detail
-
getReaction
public final FailureReaction getReaction()
- Returns:
- The reaction to adopt in case of error.
-
onError
protected final void onError(String message)
Function that either keeps silent, warns or raises an exception, depending on error reaction.- Parameters:
message
- The error message.- Throws:
DataException
- When error reaction isFailureReaction.FAIL
.
-
onError
protected final <T> T onError(String message, T def)
Function that either silently returns a value, warns and returns a value or raises an exception, depending on error reaction.- Type Parameters:
T
- The return type.- Parameters:
message
- The error message.def
- The default return value.- Returns:
def
if error reaction isFailureReaction.DEFAULT
orFailureReaction.WARN
.- Throws:
DataException
- When error reaction isFailureReaction.FAIL
.
-
onResult
protected final <T> T onResult(T result, String message, T def)
Function that returns a computed value if it is notnull
, or silently returns a default value, warns and returns a default value or throws an exception.- Type Parameters:
T
- The return type.- Parameters:
result
- The computed result.message
- The error message.def
- The default return value.- Returns:
result
if it is notnull
, ordef
if error reaction isFailureReaction.DEFAULT
orFailureReaction.WARN
.- Throws:
DataException
- Whenresult
isnull
and error reaction isFailureReaction.FAIL
.
-
getLogger
protected final org.apache.logging.log4j.Logger getLogger()
-
trace
protected void trace(String message)
-
trace
protected void trace()
-
parse
protected abstract R parse() throws XMLStreamException
Base method called to parse the stream.- Returns:
- The parsed result.
- Throws:
XMLStreamException
- When there is an error parsing the underlying XML source.
-
error
protected final InvalidDataException error(String message)
Throws an InvalidDataExeption.- Parameters:
message
- The exception message.- Returns:
- A new instance of InvalidDataException.
-
unexpectedEvent
protected final InvalidDataException unexpectedEvent()
- Returns:
- A new instance of InvalidDataException.
-
next
protected int next() throws XMLStreamException
Gets the next parsing event.It is a simple wrapper of
XMLStreamReader.next()
.- Returns:
- The integer code corresponding to the current parse event.
- Throws:
XMLStreamException
- When there is an error parsing the underlying XML source.
-
nextTag
protected int nextTag() throws XMLStreamException
Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.It is a simple wrapper of
XMLStreamReader.nextTag()
.If other than white space characters, COMMENT, PROCESSING_INSTRUCTION, START_ELEMENT, END_ELEMENT are encountered, an exception is thrown. This method should be used when processing element-only content separeted by white space.
- Returns:
- The event type of the element read (START_ELEMENT or END_ELEMENT).
- Throws:
XMLStreamException
- When the current event is not white space, PROCESSING_INSTRUCTION, START_ELEMENT or END_ELEMENT.
-
ignoreElement
protected void ignoreElement() throws XMLStreamException
Must be called after a START_ELEMENT has been found.Skips all events till the corresponding END_ELEMENT is reached.
- Throws:
XMLStreamException
- If there is a fatal error detecting the next states.
-
parseChildren
protected <C> void parseChildren(C context, String name, AbstractStAXParser.ContextElementParser<C> childParser) throws XMLStreamException
Utility used to parse an element and all its children.- Type Parameters:
C
- The context type.- Parameters:
context
- The context to pass tochildParser
.name
- The element name.childParser
- The child parser.- Throws:
XMLStreamException
- When there is an error parsing the underlying XML source.
-
parseChildren
protected void parseChildren(String name, AbstractStAXParser.ElementParser childParser) throws XMLStreamException
- Throws:
XMLStreamException
-
expect
protected void expect(String context, int eventType)
Checks that current event type is an expected one.If
false
, raises an exception.- Parameters:
context
- The call context.eventType
- The expected event type.- Throws:
InvalidDataException
- When current event type is not the expected one.
-
expectStartElement
protected void expectStartElement(String context, String name)
Checks that current event type is START_ELEMNT.If
false
, raises an exception.- Parameters:
context
- The call context.name
- The expected element name.- Throws:
InvalidDataException
- When current event type is not START_ELEMENT or name does not match.
-
expectEndElement
protected void expectEndElement(String context, String name)
Checks that current event type is END_ELEMNT.If
false
, raises an exception.- Parameters:
context
- The call context.name
- The expected element name.- Throws:
InvalidDataException
- When current event type is not END_ELEMENT or name does not match.
-
expectStartDocument
protected void expectStartDocument(String context)
Checks that current event type is START_DOCUMENT.If
false
, raises an exception.- Parameters:
context
- The call context.- Throws:
InvalidDataException
- When current event type is not START_DOCUMENT.
-
expectEndDocument
protected void expectEndDocument(String context)
Checks that current event type is END_DOCUMENT.If
false
, raises an exception.- Parameters:
context
- The call context.- Throws:
InvalidDataException
- When current event type is not END_DOCUMENT.
-
isStartElement
protected boolean isStartElement(String name)
Returnstrue
if reader is on a START_ELEMENT that has a given name.- Parameters:
name
- The element name.- Returns:
true
if reader is on a START_ELEMENT namedname
.
-
getAttributeValue
protected String getAttributeValue(String name, String def, FailureReaction missingReaction)
Returns an attribute value.- Parameters:
name
- The attribute name.def
- The default value.missingReaction
- The reaction to adopt when no attribute namedname
is found.- Returns:
- The attribute value, or
def
. - Throws:
NotFoundException
- WhenmissingReaction
is FAIL and no attribute is found.
-
getAttributeAsBoolean
protected boolean getAttributeAsBoolean(String name, boolean def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsBoolean
protected boolean getAttributeAsBoolean(String name, boolean def)
-
getAttributeAsOptionalBoolean
protected Boolean getAttributeAsOptionalBoolean(String name, Boolean def, FailureReaction errorReaction)
-
getAttributeAsOptionalBoolean
protected Boolean getAttributeAsOptionalBoolean(String name, Boolean def)
-
getAttributeAsLong
protected long getAttributeAsLong(String name, long def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsLong
protected long getAttributeAsLong(String name, long def)
-
getAttributeAsOptionalLong
protected Long getAttributeAsOptionalLong(String name, Long def, FailureReaction errorReaction)
-
getAttributeAsInt
protected int getAttributeAsInt(String name, int def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsInt
protected int getAttributeAsInt(String name, int def)
-
getAttributeAsOptionalInt
protected Integer getAttributeAsOptionalInt(String name, Integer def, FailureReaction errorReaction)
-
getAttributeAsShort
protected short getAttributeAsShort(String name, short def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsShort
protected short getAttributeAsShort(String name, short def)
-
getAttributeAsOptionalShort
protected Short getAttributeAsOptionalShort(String name, Short def, FailureReaction errorReaction)
-
getAttributeAsByte
protected byte getAttributeAsByte(String name, byte def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsByte
protected byte getAttributeAsByte(String name, byte def)
-
getAttributeAsOptionalByte
protected Byte getAttributeAsOptionalByte(String name, Byte def, FailureReaction errorReaction)
-
getAttributeAsDouble
protected double getAttributeAsDouble(String name, double def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsDouble
protected double getAttributeAsDouble(String name, double def)
-
getAttributeAsOptionalDouble
protected Double getAttributeAsOptionalDouble(String name, Double def, FailureReaction errorReaction)
-
getAttributeAsOptionalDouble
protected Double getAttributeAsOptionalDouble(String name, Double def)
-
getAttributeAsFloat
protected float getAttributeAsFloat(String name, float def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsFloat
protected float getAttributeAsFloat(String name, float def)
-
getAttributeAsOptionalFloat
protected Float getAttributeAsOptionalFloat(String name, Float def, FailureReaction errorReaction)
-
getAttributeAsRawEnum
protected Enum<?> getAttributeAsRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsRawEnum
protected Enum<?> getAttributeAsRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def)
-
getAttributeAsOptionalRawEnum
protected Enum<?> getAttributeAsOptionalRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def, FailureReaction errorReaction)
-
getAttributeAsOptionalRawEnum
protected Enum<?> getAttributeAsOptionalRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def)
-
getAttributeAsEnum
protected <E extends Enum<E>> E getAttributeAsEnum(String name, Class<E> enumClass, E def, FailureReaction missingReaction, FailureReaction errorReaction)
-
getAttributeAsEnum
protected <E extends Enum<E>> E getAttributeAsEnum(String name, Class<E> enumClass, E def)
-
getAttributeAsOptionalEnum
protected <E extends Enum<E>> E getAttributeAsOptionalEnum(String name, Class<E> enumClass, E def, FailureReaction errorReaction)
-
-