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.ElementParser<C>
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.logging.log4j.Logger
logger
protected XMLStreamReader
reader
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractStAXParser(XMLStreamReader reader)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
error(String message)
Throws an InvcaliudDataExeption.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
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 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()
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 abstract R
parse()
Base method called to parse the stream.protected <C> void
parseChildren(C context, String name, AbstractStAXParser.ElementParser<C> childParser)
Utility used to parse an element and all its children.protected void
unexpectedEvent()
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 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.ElementParser<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 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)
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)
-
-