Class StaxUtils


  • public final class StaxUtils
    extends Object
    • Constructor Detail

      • StaxUtils

        public StaxUtils()
    • Method Detail

      • isHTML5VoidElement

        public static boolean isHTML5VoidElement​(QName name)
      • isHTML5RawTextlement

        public static boolean isHTML5RawTextlement​(QName name)
      • closeQuietly

        public static void closeQuietly​(@Nullable XMLEventReader xmlEventReader)
      • closeQuietly

        public static void closeQuietly​(XMLStreamReader xmlStreamReader)
      • closeQuietly

        public static void closeQuietly​(@Nullable XMLEventWriter xmlEventWriter)
      • closeQuietly

        public static void closeQuietly​(XMLStreamWriter xmlStreamWriter)
      • 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 element
        attributeName - 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 element
        attributeQName - attribute QName
        Returns:
        attribute value or default value if attribute does not exist
      • hasAttributes

        public static boolean hasAttributes​(StartElement startElement,
                                            String... attributeNames)
        Returns true if start element has all the specified attributes.
        Parameters:
        startElement - start element
        attributeNames - attribute names
        Returns:
        true if start element has all the specified attributes, false otherwise.
      • hasClass

        public static boolean hasClass​(StartElement startElement,
                                       String className)
        Parameters:
        startElement -
        className -
        Returns:
        true if the supplied startElement has an HTML class attribute containing the supplied class name.
      • collectWhitespace

        public static String collectWhitespace​(XMLEventReader reader)
                                        throws XMLStreamException
        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 builder
        reader - the reader
        Returns:
        the given builder
        Throws:
        XMLStreamException - if there are problems reading from the supplied reader
      • skipWhitespace

        public static void skipWhitespace​(XMLEventReader reader)
                                   throws XMLStreamException
        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

        public static String readCharactersAndEntities​(XMLEventReader reader)
                                                throws XMLStreamException
        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
      • 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 reader
        xmlOutputFactory - The XML output factory
        ignoredElements - 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

        public static void flushEventWriter​(XMLEventWriter eventWriter)
                                     throws XMLStreamException
        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