Package cdc.util.xml

Class 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
    • Field Detail

      • logger

        protected final org.apache.logging.log4j.Logger logger
    • 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 is FailureReaction.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 is FailureReaction.DEFAULT or FailureReaction.WARN.
        Throws:
        DataException - When error reaction is FailureReaction.FAIL.
      • onResult

        protected final <T> T onResult​(T result,
                                       String message,
                                       T def)
        Function that returns a computed value if it is not null, 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 not null, or def if error reaction is FailureReaction.DEFAULT or FailureReaction.WARN.
        Throws:
        DataException - When result is null and error reaction is FailureReaction.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.
      • 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 to childParser.
        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.
      • expectStartElement

        protected void expectStartElement​(String context,
                                          String... names)
      • 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.
      • expectEndElement

        protected void expectEndElement​(String context,
                                        String... names)
      • 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)
        Returns true 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 named name.
      • 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 named name is found.
        Returns:
        The attribute value, or def.
        Throws:
        NotFoundException - When missingReaction is FAIL and no attribute is found.
      • getAttributeAsBoolean

        protected boolean getAttributeAsBoolean​(String name,
                                                boolean def)
      • getAttributeAsOptionalBoolean

        protected Boolean getAttributeAsOptionalBoolean​(String name,
                                                        Boolean def)
      • getAttributeAsLong

        protected long getAttributeAsLong​(String name,
                                          long def)
      • getAttributeAsOptionalLong

        protected Long getAttributeAsOptionalLong​(String name,
                                                  Long def)
      • getAttributeAsInt

        protected int getAttributeAsInt​(String name,
                                        int def)
      • getAttributeAsOptionalInt

        protected Integer getAttributeAsOptionalInt​(String name,
                                                    Integer def)
      • getAttributeAsShort

        protected short getAttributeAsShort​(String name,
                                            short def)
      • getAttributeAsOptionalShort

        protected Short getAttributeAsOptionalShort​(String name,
                                                    Short def)
      • getAttributeAsByte

        protected byte getAttributeAsByte​(String name,
                                          byte def)
      • getAttributeAsOptionalByte

        protected Byte getAttributeAsOptionalByte​(String name,
                                                  Byte def)
      • getAttributeAsDouble

        protected double getAttributeAsDouble​(String name,
                                              double def)
      • getAttributeAsOptionalDouble

        protected Double getAttributeAsOptionalDouble​(String name,
                                                      Double def)
      • getAttributeAsFloat

        protected float getAttributeAsFloat​(String name,
                                            float def)
      • getAttributeAsOptionalFloat

        protected Float getAttributeAsOptionalFloat​(String name,
                                                    Float def)
      • getAttributeAsRawEnum

        protected Enum<?> getAttributeAsRawEnum​(String name,
                                                Class<? extends Enum<?>> enumClass,
                                                Enum<?> def)
      • 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)
      • getAttributeAsOptionalEnum

        protected <E extends Enum<E>> E getAttributeAsOptionalEnum​(String name,
                                                                   Class<E> enumClass,
                                                                   E def,
                                                                   FailureReaction errorReaction)
      • getAttributeAsOptionalEnum

        protected <E extends Enum<E>> E getAttributeAsOptionalEnum​(String name,
                                                                   Class<E> enumClass,
                                                                   E def)