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 interfaceAbstractStAXParser.ContextElementParser<C>static interfaceAbstractStAXParser.ElementParser
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.logging.log4j.Loggerloggerprotected FailureReactionreactionprotected XMLStreamReaderreader
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractStAXParser(XMLStreamReader reader, FailureReaction reaction)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected InvalidDataExceptionerror(String message)Throws an InvalidDataExeption.protected voidexpect(String context, int eventType)Checks that current event type is an expected one.protected voidexpectEndDocument(String context)Checks that current event type is END_DOCUMENT.protected voidexpectEndElement(String context, String name)Checks that current event type is END_ELEMNT.protected voidexpectEndElement(String context, String... names)protected voidexpectStartDocument(String context)Checks that current event type is START_DOCUMENT.protected voidexpectStartElement(String context, String name)Checks that current event type is START_ELEMNT.protected voidexpectStartElement(String context, String... names)protected booleangetAttributeAsBoolean(String name, boolean def)protected booleangetAttributeAsBoolean(String name, boolean def, FailureReaction missingReaction, FailureReaction errorReaction)protected bytegetAttributeAsByte(String name, byte def)protected bytegetAttributeAsByte(String name, byte def, FailureReaction missingReaction, FailureReaction errorReaction)protected doublegetAttributeAsDouble(String name, double def)protected doublegetAttributeAsDouble(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 floatgetAttributeAsFloat(String name, float def)protected floatgetAttributeAsFloat(String name, float def, FailureReaction missingReaction, FailureReaction errorReaction)protected intgetAttributeAsInt(String name, int def)protected intgetAttributeAsInt(String name, int def, FailureReaction missingReaction, FailureReaction errorReaction)protected longgetAttributeAsLong(String name, long def)protected longgetAttributeAsLong(String name, long def, FailureReaction missingReaction, FailureReaction errorReaction)protected BooleangetAttributeAsOptionalBoolean(String name, Boolean def)protected BooleangetAttributeAsOptionalBoolean(String name, Boolean def, FailureReaction errorReaction)protected BytegetAttributeAsOptionalByte(String name, Byte def)protected BytegetAttributeAsOptionalByte(String name, Byte def, FailureReaction errorReaction)protected DoublegetAttributeAsOptionalDouble(String name, Double def)protected DoublegetAttributeAsOptionalDouble(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 FloatgetAttributeAsOptionalFloat(String name, Float def)protected FloatgetAttributeAsOptionalFloat(String name, Float def, FailureReaction errorReaction)protected IntegergetAttributeAsOptionalInt(String name, Integer def)protected IntegergetAttributeAsOptionalInt(String name, Integer def, FailureReaction errorReaction)protected LonggetAttributeAsOptionalLong(String name, Long def)protected LonggetAttributeAsOptionalLong(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 ShortgetAttributeAsOptionalShort(String name, Short def)protected ShortgetAttributeAsOptionalShort(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 shortgetAttributeAsShort(String name, short def)protected shortgetAttributeAsShort(String name, short def, FailureReaction missingReaction, FailureReaction errorReaction)protected StringgetAttributeValue(String name, String def)protected StringgetAttributeValue(String name, String def, FailureReaction missingReaction)Returns an attribute value.protected org.apache.logging.log4j.LoggergetLogger()FailureReactiongetReaction()protected voidignoreElement()Must be called after a START_ELEMENT has been found.protected booleanisStartElement(String name)Returnstrueif reader is on a START_ELEMENT that has a given name.protected intnext()Gets the next parsing event.protected intnextTag()Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.protected voidonError(String message)Function that either keeps silent, warns or raises an exception, depending on error reaction.protected <T> TonError(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> TonResult(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 Rparse()Base method called to parse the stream.protected <C> voidparseChildren(C context, String name, AbstractStAXParser.ContextElementParser<C> childParser)Utility used to parse an element and all its children.protected voidparseChildren(String name, AbstractStAXParser.ElementParser childParser)protected voidtrace()protected voidtrace(String message)protected InvalidDataExceptionunexpectedEvent()
-
-
-
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:
defif error reaction isFailureReaction.DEFAULTorFailureReaction.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:
resultif it is notnull, ordefif error reaction isFailureReaction.DEFAULTorFailureReaction.WARN.- Throws:
DataException- Whenresultisnulland 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 XMLStreamExceptionGets 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 XMLStreamExceptionSkips 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 XMLStreamExceptionMust 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 XMLStreamExceptionUtility 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)
Returnstrueif reader is on a START_ELEMENT that has a given name.- Parameters:
name- The element name.- Returns:
trueif 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 namednameis found.- Returns:
- The attribute value, or
def. - Throws:
NotFoundException- WhenmissingReactionis 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)
-
-