Class XmlEventUtil

java.lang.Object
dev.metaschema.core.model.util.XmlEventUtil

public final class XmlEventUtil extends Object
Provides utility functions to support reading and writing XML stream events, and for producing error and warning messages.
  • Method Summary

    Modifier and Type
    Method
    Description
    static XMLEvent
    advanceTo(org.codehaus.stax2.XMLEventReader2 reader, int eventType)
    Advance through XMLEvents until the event type identified by eventType is reached or the end of stream is found.
    static XMLEvent
    assertNext(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType)
    Ensure that the next event from reader is of the type identified by presumedEventType.
    static XMLEvent
    assertNext(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType, QName presumedName)
    Ensure that the next event from reader is of the type identified by presumedEventType and has the name identified by presumedName.
    static XMLEvent
    consumeAndAssert(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType)
    Consume the next event from reader and assert that this event is of the type identified by presumedEventType.
    static XMLEvent
    consumeAndAssert(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType, QName presumedName)
    Consume the next event from reader and assert that this event is of the type identified by presumedEventType and has the name identified by presumedName.
    generateExpectedMessage(XMLEvent event, URI resource, int presumedEventType, QName presumedName)
    Generate a message intended for error reporting based on a presumed event.
    Generate a location string for the current location in the XML event stream.
    generateLocationMessage(Location location, URI resource)
    Generate a location string for the current location in the XML event stream.
    static boolean
    Determine if the event is an end of document event.
    static boolean
    isEventEndElement(XMLEvent event, QName expectedQName)
    Determine if the event is an end element whose name matches the provided expectedQName.
    static boolean
    isEventStartElement(XMLEvent event, QName expectedQName)
    Determine if the event is a start element whose name matches the provided expectedQName.
    static EndElement
    requireEndElement(org.codehaus.stax2.XMLEventReader2 reader, URI resource, QName presumedName)
    Ensure that the next event is an XML start element that matches the presumed name.
    requireStartElement(org.codehaus.stax2.XMLEventReader2 reader, URI resource, QName presumedName)
    Ensure that the next event is an XML start element that matches the presumed name.
    static XMLEvent
    skipElement(org.codehaus.stax2.XMLEventReader2 reader)
    Skip over the next element in the event stream.
    static XMLEvent
    skipEvents(org.codehaus.stax2.XMLEventReader2 reader, int... events)
    Skips events specified by events.
    static XMLEvent
    skipProcessingInstructions(org.codehaus.stax2.XMLEventReader2 reader)
    Skip over any processing instructions.
    static XMLEvent
    skipWhitespace(org.codehaus.stax2.XMLEventReader2 reader)
    Skip over any whitespace.
    static String
    toEventName(int eventType)
    Get the event name of the eventType, which is one of the types defined by XMLStreamConstants.
    static String
    Get the event name of the event.
    static Location
    Retrieve the resource location of event.
    static QName
    Retrieve the name of the node associated with event.
    toString(XMLEvent xmlEvent, URI resource)
    Generate a message suitable for logging that describes the provided XMLEvent.
    toString(Location location, URI resource)
    Generates a message for the provided Location.
    toString(org.codehaus.stax2.XMLStreamReader2 reader, URI resource)
    Generates a string containing the current event and location of the stream reader.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toString

      @NonNull public static CharSequence toString(@Nullable XMLEvent xmlEvent, @NonNull URI resource)
      Generate a message suitable for logging that describes the provided XMLEvent.
      Parameters:
      xmlEvent - the XML event to generate the message for
      resource - the resource being parsed
      Returns:
      the message
    • toString

      @NonNull public static CharSequence toString(@Nullable Location location, @NonNull URI resource)
      Generates a message for the provided Location.
      Parameters:
      location - the location to generate the message for
      resource - the resource being parsed
      Returns:
      the message
    • toString

      @NonNull public static CharSequence toString(@NonNull org.codehaus.stax2.XMLStreamReader2 reader, @NonNull URI resource)
      Generates a string containing the current event and location of the stream reader.
      Parameters:
      reader - the XML event stream reader
      resource - the resource being parsed
      Returns:
      the generated string
    • toLocation

      @Nullable public static Location toLocation(@NonNull XMLEvent event)
      Retrieve the resource location of event.
      Parameters:
      event - the XML event to identify the location for
      Returns:
      the location or null if the location is unknown
    • toQName

      @Nullable public static QName toQName(@NonNull XMLEvent event)
      Retrieve the name of the node associated with event.
      Parameters:
      event - the XML event to get the QName for
      Returns:
      the name of the node or null if the event is not a start or end element
    • toEventName

      @NonNull public static String toEventName(@NonNull XMLEvent event)
      Get the event name of the event.
      Parameters:
      event - the XML event to get the event name for
      Returns:
      the event name
    • toEventName

      @NonNull public static String toEventName(int eventType)
      Get the event name of the eventType, which is one of the types defined by XMLStreamConstants.
      Parameters:
      eventType - the event constant to get the event name for as defined by XMLStreamConstants
      Returns:
      the event name
    • advanceTo

      @Nullable public static XMLEvent advanceTo(@NonNull org.codehaus.stax2.XMLEventReader2 reader, int eventType) throws XMLStreamException
      Advance through XMLEvents until the event type identified by eventType is reached or the end of stream is found.
      Parameters:
      reader - the XML event reader to advance
      eventType - the event type to stop on as defined by XMLStreamConstants
      Returns:
      the next event of the specified type or null if the end of stream is reached
      Throws:
      XMLStreamException - if an error occurred while advancing the stream
    • skipElement

      public static XMLEvent skipElement(@NonNull org.codehaus.stax2.XMLEventReader2 reader) throws XMLStreamException
      Skip over the next element in the event stream.
      Parameters:
      reader - the XML event stream reader
      Returns:
      the next XML event
      Throws:
      XMLStreamException - if an error occurred while reading the event stream
    • skipProcessingInstructions

      @NonNull public static XMLEvent skipProcessingInstructions(@NonNull org.codehaus.stax2.XMLEventReader2 reader) throws XMLStreamException
      Skip over any processing instructions.
      Parameters:
      reader - the XML event reader to advance
      Returns:
      the last processing instruction event or the reader's next event if no processing instruction was found
      Throws:
      XMLStreamException - if an error occurred while advancing the stream
    • skipWhitespace

      @NonNull public static XMLEvent skipWhitespace(@NonNull org.codehaus.stax2.XMLEventReader2 reader) throws XMLStreamException
      Skip over any whitespace.
      Parameters:
      reader - the XML event reader to advance
      Returns:
      the last character event containing whitespace or the reader's next event if no character event was found
      Throws:
      XMLStreamException - if an error occurred while advancing the stream
    • isEventEndElement

      public static boolean isEventEndElement(@Nullable XMLEvent event, @NonNull QName expectedQName)
      Determine if the event is an end element whose name matches the provided expectedQName.
      Parameters:
      event - the XML event
      expectedQName - the expected element name
      Returns:
      true if the next event matches the expectedQName
    • isEventEndDocument

      public static boolean isEventEndDocument(@Nullable XMLEvent event)
      Determine if the event is an end of document event.
      Parameters:
      event - the XML event
      Returns:
      true if the next event is an end of document event
    • isEventStartElement

      public static boolean isEventStartElement(@Nullable XMLEvent event, @NonNull QName expectedQName) throws XMLStreamException
      Determine if the event is a start element whose name matches the provided expectedQName.
      Parameters:
      event - the event
      expectedQName - the expected element name
      Returns:
      true if the next event is a start element that matches the expectedQName
      Throws:
      XMLStreamException - if an error occurred while looking at the next event
    • consumeAndAssert

      public static XMLEvent consumeAndAssert(@NonNull org.codehaus.stax2.XMLEventReader2 reader, @NonNull URI resource, int presumedEventType) throws XMLStreamException
      Consume the next event from reader and assert that this event is of the type identified by presumedEventType.
      Parameters:
      reader - the XML event reader
      resource - the resource being parsed
      presumedEventType - the expected event type as defined by XMLStreamConstants
      Returns:
      the next event
      Throws:
      XMLStreamException - if an error occurred while looking at the next event
    • consumeAndAssert

      public static XMLEvent consumeAndAssert(@NonNull org.codehaus.stax2.XMLEventReader2 reader, @NonNull URI resource, int presumedEventType, @Nullable QName presumedName) throws XMLStreamException
      Consume the next event from reader and assert that this event is of the type identified by presumedEventType and has the name identified by presumedName.
      Parameters:
      reader - the XML event reader
      resource - the resource being parsed
      presumedEventType - the expected event type as defined by XMLStreamConstants
      presumedName - the expected name of the node associated with the event
      Returns:
      the next event
      Throws:
      XMLStreamException - if an error occurred while looking at the next event
    • requireStartElement

      @NonNull public static StartElement requireStartElement(@NonNull org.codehaus.stax2.XMLEventReader2 reader, @NonNull URI resource, @NonNull QName presumedName) throws IOException, XMLStreamException
      Ensure that the next event is an XML start element that matches the presumed name.
      Parameters:
      reader - the XML event reader
      resource - the resource being parsed
      presumedName - the qualified name of the expected next event
      Returns:
      the XML start element event
      Throws:
      IOException - if an error occurred while parsing the resource
      XMLStreamException - if an error occurred while parsing the XML event stream
    • requireEndElement

      @NonNull public static EndElement requireEndElement(@NonNull org.codehaus.stax2.XMLEventReader2 reader, @NonNull URI resource, @NonNull QName presumedName) throws IOException, XMLStreamException
      Ensure that the next event is an XML start element that matches the presumed name.
      Parameters:
      reader - the XML event reader
      resource - the resource being parsed
      presumedName - the qualified name of the expected next event
      Returns:
      the XML start element event
      Throws:
      IOException - if an error occurred while parsing the resource
      XMLStreamException - if an error occurred while parsing the XML event stream
    • assertNext

      public static XMLEvent assertNext(@NonNull org.codehaus.stax2.XMLEventReader2 reader, @NonNull URI resource, int presumedEventType) throws XMLStreamException
      Ensure that the next event from reader is of the type identified by presumedEventType.
      Parameters:
      reader - the event reader
      resource - the resource being parsed
      presumedEventType - the expected event type as defined by XMLStreamConstants
      Returns:
      the next event
      Throws:
      XMLStreamException - if an error occurred while looking at the next event
      AssertionError - if the next event does not match the presumed event
    • assertNext

      public static XMLEvent assertNext(@NonNull org.codehaus.stax2.XMLEventReader2 reader, @NonNull URI resource, int presumedEventType, @Nullable QName presumedName) throws XMLStreamException
      Ensure that the next event from reader is of the type identified by presumedEventType and has the name identified by presumedName.
      Parameters:
      reader - the event reader
      resource - the resource being parsed
      presumedEventType - the expected event type as defined by XMLStreamConstants
      presumedName - the expected name of the node associated with the event
      Returns:
      the next event
      Throws:
      XMLStreamException - if an error occurred while looking at the next event
      AssertionError - if the next event does not match the presumed event
    • generateLocationMessage

      public static CharSequence generateLocationMessage(@Nullable XMLEvent event, @NonNull URI resource)
      Generate a location string for the current location in the XML event stream.
      Parameters:
      event - an XML event
      resource - the resource being parsed
      Returns:
      the location string
    • generateLocationMessage

      public static CharSequence generateLocationMessage(@NonNull Location location, @NonNull URI resource)
      Generate a location string for the current location in the XML event stream.
      Parameters:
      location - an XML event stream location
      resource - the resource being parsed
      Returns:
      the location string
    • generateExpectedMessage

      public static CharSequence generateExpectedMessage(@Nullable XMLEvent event, @NonNull URI resource, int presumedEventType, @Nullable QName presumedName)
      Generate a message intended for error reporting based on a presumed event.
      Parameters:
      event - the current XML event
      resource - the resource being parsed
      presumedEventType - the expected event type (XMLEvent.getEventType())
      presumedName - the expected event qualified name or null if there is no expectation
      Returns:
      the message string
    • skipEvents

      public static XMLEvent skipEvents(org.codehaus.stax2.XMLEventReader2 reader, int... events) throws XMLStreamException
      Skips events specified by events.
      Parameters:
      reader - the event reader
      events - the events to skip
      Returns:
      the next non-mataching event returned by XMLEventReader.peek(), or null if there was no next event
      Throws:
      XMLStreamException - if an error occurred while reading