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.ElementParser<C>
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.logging.log4j.Loggerloggerprotected XMLStreamReaderreader
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractStAXParser(XMLStreamReader reader)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voiderror(String message)Throws an InvcaliudDataExeption.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 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 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()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 abstract Rparse()Base method called to parse the stream.protected <C> voidparseChildren(C context, String name, AbstractStAXParser.ElementParser<C> childParser)Utility used to parse an element and all its children.protected voidunexpectedEvent()Throws an InvalidDataException to indcate an event is unexpected.
-
-
-
Field Detail
-
logger
protected final org.apache.logging.log4j.Logger logger
-
reader
protected final XMLStreamReader reader
-
-
Constructor Detail
-
AbstractStAXParser
protected AbstractStAXParser(XMLStreamReader reader)
-
-
Method Detail
-
getLogger
protected final org.apache.logging.log4j.Logger getLogger()
-
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 void error(String message)
Throws an InvcaliudDataExeption.- Parameters:
message- The exception message.- Throws:
InvalidDataException- In all cases.
-
unexpectedEvent
protected final void unexpectedEvent()
Throws an InvalidDataException to indcate an event is unexpected.- Throws:
InvalidDataException- In all cases.
-
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.ElementParser<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 tochildPareser.name- The element name.childParser- The child parser.- Throws:
XMLStreamException- When there is an error parsing the underlying XML source.
-
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)
-
-