java.lang.Object
dev.metaschema.core.model.util.XmlEventUtil
Provides utility functions to support reading and writing XML stream events,
and for producing error and warning messages.
-
Method Summary
Modifier and TypeMethodDescriptionstatic XMLEventadvanceTo(org.codehaus.stax2.XMLEventReader2 reader, int eventType) Advance through XMLEvents until the event type identified byeventTypeis reached or the end of stream is found.static XMLEventassertNext(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType) Ensure that the next event fromreaderis of the type identified bypresumedEventType.static XMLEventassertNext(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType, QName presumedName) Ensure that the next event fromreaderis of the type identified bypresumedEventTypeand has the name identified bypresumedName.static XMLEventconsumeAndAssert(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType) Consume the next event fromreaderand assert that this event is of the type identified bypresumedEventType.static XMLEventconsumeAndAssert(org.codehaus.stax2.XMLEventReader2 reader, URI resource, int presumedEventType, QName presumedName) Consume the next event fromreaderand assert that this event is of the type identified bypresumedEventTypeand has the name identified bypresumedName.static CharSequencegenerateExpectedMessage(XMLEvent event, URI resource, int presumedEventType, QName presumedName) Generate a message intended for error reporting based on a presumed event.static CharSequencegenerateLocationMessage(XMLEvent event, URI resource) Generate a location string for the current location in the XML event stream.static CharSequencegenerateLocationMessage(Location location, URI resource) Generate a location string for the current location in the XML event stream.static booleanisEventEndDocument(XMLEvent event) Determine if theeventis an end of document event.static booleanisEventEndElement(XMLEvent event, QName expectedQName) Determine if theeventis an end element whose name matches the providedexpectedQName.static booleanisEventStartElement(XMLEvent event, QName expectedQName) Determine if theeventis a start element whose name matches the providedexpectedQName.static EndElementrequireEndElement(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 StartElementrequireStartElement(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 XMLEventskipElement(org.codehaus.stax2.XMLEventReader2 reader) Skip over the next element in the event stream.static XMLEventskipEvents(org.codehaus.stax2.XMLEventReader2 reader, int... events) Skips events specified byevents.static XMLEventskipProcessingInstructions(org.codehaus.stax2.XMLEventReader2 reader) Skip over any processing instructions.static XMLEventskipWhitespace(org.codehaus.stax2.XMLEventReader2 reader) Skip over any whitespace.static StringtoEventName(int eventType) Get the event name of theeventType, which is one of the types defined byXMLStreamConstants.static StringtoEventName(XMLEvent event) Get the event name of theevent.static LocationtoLocation(XMLEvent event) Retrieve the resource location ofevent.static QNameRetrieve the name of the node associated withevent.static CharSequenceGenerate a message suitable for logging that describes the providedXMLEvent.static CharSequenceGenerates a message for the providedLocation.static CharSequenceGenerates a string containing the current event and location of the stream reader.
-
Method Details
-
toString
Generate a message suitable for logging that describes the providedXMLEvent.- Parameters:
xmlEvent- the XML event to generate the message forresource- the resource being parsed- Returns:
- the message
-
toString
Generates a message for the providedLocation.- Parameters:
location- the location to generate the message forresource- 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 readerresource- the resource being parsed- Returns:
- the generated string
-
toLocation
Retrieve the resource location ofevent.- Parameters:
event- the XML event to identify the location for- Returns:
- the location or
nullif the location is unknown
-
toQName
Retrieve the name of the node associated withevent.- Parameters:
event- the XML event to get theQNamefor- Returns:
- the name of the node or
nullif the event is not a start or end element
-
toEventName
Get the event name of theevent.- Parameters:
event- the XML event to get the event name for- Returns:
- the event name
-
toEventName
Get the event name of theeventType, which is one of the types defined byXMLStreamConstants.- Parameters:
eventType- the event constant to get the event name for as defined byXMLStreamConstants- 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 byeventTypeis reached or the end of stream is found.- Parameters:
reader- the XML event reader to advanceeventType- the event type to stop on as defined byXMLStreamConstants- Returns:
- the next event of the specified type or
nullif 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
Determine if theeventis an end element whose name matches the providedexpectedQName.- Parameters:
event- the XML eventexpectedQName- the expected element name- Returns:
trueif the next event matches theexpectedQName
-
isEventEndDocument
Determine if theeventis an end of document event.- Parameters:
event- the XML event- Returns:
trueif the next event is an end of document event
-
isEventStartElement
public static boolean isEventStartElement(@Nullable XMLEvent event, @NonNull QName expectedQName) throws XMLStreamException Determine if theeventis a start element whose name matches the providedexpectedQName.- Parameters:
event- the eventexpectedQName- the expected element name- Returns:
trueif the next event is a start element that matches theexpectedQName- 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 fromreaderand assert that this event is of the type identified bypresumedEventType.- Parameters:
reader- the XML event readerresource- the resource being parsedpresumedEventType- the expected event type as defined byXMLStreamConstants- 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 fromreaderand assert that this event is of the type identified bypresumedEventTypeand has the name identified bypresumedName.- Parameters:
reader- the XML event readerresource- the resource being parsedpresumedEventType- the expected event type as defined byXMLStreamConstantspresumedName- 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 readerresource- the resource being parsedpresumedName- the qualified name of the expected next event- Returns:
- the XML start element event
- Throws:
IOException- if an error occurred while parsing the resourceXMLStreamException- 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 readerresource- the resource being parsedpresumedName- the qualified name of the expected next event- Returns:
- the XML start element event
- Throws:
IOException- if an error occurred while parsing the resourceXMLStreamException- 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 fromreaderis of the type identified bypresumedEventType.- Parameters:
reader- the event readerresource- the resource being parsedpresumedEventType- the expected event type as defined byXMLStreamConstants- Returns:
- the next event
- Throws:
XMLStreamException- if an error occurred while looking at the next eventAssertionError- 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 fromreaderis of the type identified bypresumedEventTypeand has the name identified bypresumedName.- Parameters:
reader- the event readerresource- the resource being parsedpresumedEventType- the expected event type as defined byXMLStreamConstantspresumedName- 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 eventAssertionError- if the next event does not match the presumed event
-
generateLocationMessage
Generate a location string for the current location in the XML event stream.- Parameters:
event- an XML eventresource- 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 locationresource- 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 eventresource- the resource being parsedpresumedEventType- the expected event type (XMLEvent.getEventType())presumedName- the expected event qualified name ornullif 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 byevents.- Parameters:
reader- the event readerevents- the events to skip- Returns:
- the next non-mataching event returned by
XMLEventReader.peek(), ornullif there was no next event - Throws:
XMLStreamException- if an error occurred while reading
-