Class StaxUtils
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
closeQuietly
(@Nullable XMLEventReader xmlEventReader) static void
closeQuietly
(@Nullable XMLEventWriter xmlEventWriter) static void
closeQuietly
(XMLStreamReader xmlStreamReader) static void
closeQuietly
(XMLStreamWriter xmlStreamWriter) static StringBuilder
collectWhitespace
(StringBuilder builder, XMLEventReader reader) Collect whitespace from the given reader, into the given builder.static String
collectWhitespace
(XMLEventReader reader) Collect whitespace from the given reader.static XhtmlException
convertToXhtmlException
(com.ctc.wstx.exc.WstxLazyException ex) Take a WstxLazyException as a parameter and return the appropriate XhtmlException for the Woodstox exception supplied.static void
flushEventWriter
(XMLEventWriter eventWriter) Ensure all xml events that have been added to the event buffer of the writer are flushed.static String
getAttributeValue
(StartElement startElement, String attributeName) static String
getAttributeValue
(StartElement startElement, String attributeName, String defaultValue) Retrieves the value of the specified attribute name on the specified start element.static String
getAttributeValue
(StartElement startElement, QName attributeQName) static String
getAttributeValue
(StartElement startElement, QName attributeQName, String defaultValue) Retrieves the value of the specified attribute name on the specified start element.static boolean
hasAttributes
(StartElement startElement, String... attributeNames) Returns true if start element has all the specified attributes.static boolean
hasClass
(StartElement startElement, String className) static boolean
isHTML5RawTextlement
(QName name) static boolean
isHTML5VoidElement
(QName name) static boolean
Check if the given RuntimeException is a wrapper for any of Wstx* exceptionsstatic <E extends XhtmlException>
EprocessWrappedWstxExceptionOrTrowMapped
(RuntimeException ex, Function<RuntimeException, E> transformer) New version of the woodstox parser sometimes throws Wstx* exceptions wrapped into the RuntimeException.static String
Read all characters and entities from the supplied reader until a non-character or non-entity event is read.static void
skipWhitespace
(XMLEventReader reader) Skip whitespace from the given reader.splitCData
(String data) Take a String and split into multiple substrings where each substring is suitable to be written as CData.static @NonNull CharSequence
stripIllegalControlChars
(@NonNull CharSequence unclean) Removes control characters from a CharSequence so that it can be parsed by a Stax parser.static String
toString
(XMLEventReader reader) Deprecated.since 5.2.static String
toXmlString
(XMLEventReader reader, XmlOutputFactory xmlOutputFactory) static String
toXmlStringWithoutTag
(XMLEventReader reader, XmlOutputFactory xmlOutputFactory, Set<QName> ignoredElements) Reads an XML stream and turns it into a string while removing any tags matched in ignoredElementsstatic void
writeAttribute
(XMLStreamWriter writer, QName attribute, String value) Write an attribute and its value on the provided XMLStreamWriterstatic void
writeRawXML
(XMLStreamWriter writer, Writer out, Streamable streamable) Write raw XML properly on the provided Writer, which is wrapped by a XMLStreamWriterstatic void
writeStartElement
(XMLStreamWriter writer, QName element) Write the element start on the provided XMLStreamWriter
-
Constructor Details
-
StaxUtils
public StaxUtils()
-
-
Method Details
-
isHTML5VoidElement
-
isHTML5RawTextlement
-
writeStartElement
public static void writeStartElement(XMLStreamWriter writer, QName element) throws XMLStreamException Write the element start on the provided XMLStreamWriter- Throws:
XMLStreamException
- Since:
- 5.6
-
writeAttribute
public static void writeAttribute(XMLStreamWriter writer, QName attribute, String value) throws XMLStreamException Write an attribute and its value on the provided XMLStreamWriter- Throws:
XMLStreamException
- Since:
- 5.6
-
writeRawXML
public static void writeRawXML(XMLStreamWriter writer, Writer out, Streamable streamable) throws IOException, XMLStreamException Write raw XML properly on the provided Writer, which is wrapped by a XMLStreamWriter- Throws:
IOException
XMLStreamException
- Since:
- 5.6
-
closeQuietly
-
closeQuietly
-
closeQuietly
-
closeQuietly
-
getAttributeValue
-
getAttributeValue
-
getAttributeValue
public static String getAttributeValue(StartElement startElement, String attributeName, String defaultValue) Retrieves the value of the specified attribute name on the specified start element.- Parameters:
startElement
- start elementattributeName
- attribute name- Returns:
- attribute value or default value if attribute does not exist
-
getAttributeValue
public static String getAttributeValue(StartElement startElement, QName attributeQName, String defaultValue) Retrieves the value of the specified attribute name on the specified start element.- Parameters:
startElement
- start elementattributeQName
- attribute QName- Returns:
- attribute value or default value if attribute does not exist
-
hasAttributes
Returns true if start element has all the specified attributes.- Parameters:
startElement
- start elementattributeNames
- attribute names- Returns:
- true if start element has all the specified attributes, false otherwise.
-
hasClass
- Parameters:
startElement
-className
-- Returns:
- true if the supplied startElement has an HTML class attribute containing the supplied class name.
-
collectWhitespace
Collect whitespace from the given reader. Useful for conditional inclusion in parsed results.- Parameters:
reader
- the reader- Returns:
- the whitespace
- Throws:
XMLStreamException
- if there are problems reading from the supplied reader
-
collectWhitespace
public static StringBuilder collectWhitespace(StringBuilder builder, XMLEventReader reader) throws XMLStreamException Collect whitespace from the given reader, into the given builder. Useful for conditional inclusion in parsed results.- Parameters:
builder
- the builderreader
- the reader- Returns:
- the given builder
- Throws:
XMLStreamException
- if there are problems reading from the supplied reader
-
skipWhitespace
Skip whitespace from the given reader. Useful for unconditionally ignoring whitespace in parsed results.- Parameters:
reader
- the reader- Throws:
XMLStreamException
- if there are problems reading from the supplied reader
-
readCharactersAndEntities
Read all characters and entities from the supplied reader until a non-character or non-entity event is read. At this point the stream will be left with the next event being this terminating event.- Parameters:
reader
- the reader to read from, with the current event being either a character or an entity- Returns:
- a String including all characters and entities read.
- Throws:
XMLStreamException
- if there are problems reading from the supplied reader
-
toString
Deprecated. -
toXmlString
-
toXmlStringWithoutTag
public static String toXmlStringWithoutTag(XMLEventReader reader, XmlOutputFactory xmlOutputFactory, Set<QName> ignoredElements) Reads an XML stream and turns it into a string while removing any tags matched in ignoredElements- Parameters:
reader
- The XML event readerxmlOutputFactory
- The XML output factoryignoredElements
- A set of tags that should be stripped from the output- Returns:
- A string representation of the XML document with the specified tags removed.
-
flushEventWriter
Ensure all xml events that have been added to the event buffer of the writer are flushed.You want to be using this instead of
XMLEventWriter.flush()
when:- you have just written the start element to the event stream
- plan to then write the body of the element directly to the underlying writer
If you want to do this, you need this method to append the closing angle bracket of the start element.
You want to do in preparation for writing to the underlying writer directly.
- Parameters:
eventWriter
- the event writer- Throws:
XMLStreamException
-
splitCData
Take a String and split into multiple substrings where each substring is suitable to be written as CData.
You would use this method when you want to write CDATA to an XML document and you need to be sure that the content you are writing doesn't contain a CDATA closing triad (]]>) which would cause a premature closing of the CDATA.
So as an example if you supply a String like <![CDATA[stuff]]> then the returned Strings would be
- <![CDATA[stuff]]
- >
If the supplied String is already safe for writing to a CDATA section then a single item List will be returned containing the supplied String.
- Parameters:
data
- the data to be made safe for writing to a CDATA section.- Returns:
- a list of one or more items where each item is safe to be written to a separate CDATA sections.
-
convertToXhtmlException
Take a WstxLazyException as a parameter and return the appropriate XhtmlException for the Woodstox exception supplied.- Parameters:
ex
- the Woodstox exception to be converted to an XhtmlException.- Returns:
- an XhtmlParsingException if the WstxLazyException is wrapping a WstxParsingException, otherwise an XhtmlException
-
processWrappedWstxExceptionOrTrowMapped
public static <E extends XhtmlException> E processWrappedWstxExceptionOrTrowMapped(RuntimeException ex, Function<RuntimeException, E> transformer) New version of the woodstox parser sometimes throws Wstx* exceptions wrapped into the RuntimeException. This method tries to unwrap such exceptions and handle them in the same way asconvertToXhtmlException(WstxLazyException)
does. If it turns out that given RuntimeException is not a wrapper of any of Wstx* exceptions, method uses provided transformer to convert given runtime exception into a XhtmlException- Parameters:
ex
- runtime exceptiontransformer
- function to transform given runtime exception into the instance of XhtmlException, if it doesn't wrap any of Wstx* exceptions- Returns:
- istance of XhtmlException
-
isWrappedAnyWstxException
Check if the given RuntimeException is a wrapper for any of Wstx* exceptions- Parameters:
ex
- RuntimeException to test- Returns:
- true if given RuntimeException is a wrapper for any of Wstx* exceptions, false otherwise
-
stripIllegalControlChars
Removes control characters from a CharSequence so that it can be parsed by a Stax parser. The xml spec defines what characters an XML processor must accept:Legal characters are tab, carriage return, line feed, and the legal characters of Unicode and ISO/IEC 10646
XML Charsets.This method only removes illegal control chars, and doesn't attempt to remove illegal Unicode characters.
- Parameters:
unclean
-- Returns:
- the supplied CharSequence with illegal control characters stripped
-