com.atlassian.confluence.content.render.xhtml
Class StaxUtils

java.lang.Object
  extended by com.atlassian.confluence.content.render.xhtml.StaxUtils

public final class StaxUtils
extends Object


Constructor Summary
StaxUtils()
           
 
Method Summary
static void closeQuietly(XMLEventReader xmlEventReader)
           
static void closeQuietly(XMLEventWriter xmlEventWriter)
           
static void closeQuietly(XMLStreamReader xmlStreamReader)
           
static void closeQuietly(XMLStreamWriter xmlStreamWriter)
           
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, 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 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 boolean hasAttributes(StartElement startElement, String... attributeNames)
          Returns true if start element has all the specified attributes.
static boolean hasClass(StartElement startElement, String className)
           
static String readCharactersAndEntities(XMLEventReader reader)
          Read all characters and entities from the supplied reader until a non-character or non-entity event is read.
static List<String> splitCData(String data)
           Take a String and split into multiple substrings where each substring is suitable to be written as CData.
static CharSequence stripIllegalControlChars(CharSequence unclean)
          Removes control characters from a CharSequence so that it can be parsed by a Stax parser.
static String toString(XMLEventReader reader)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaxUtils

public StaxUtils()
Method Detail

closeQuietly

public static void closeQuietly(XMLEventReader xmlEventReader)

closeQuietly

public static void closeQuietly(XMLStreamReader xmlStreamReader)

closeQuietly

public static void closeQuietly(XMLEventWriter xmlEventWriter)

closeQuietly

public static void closeQuietly(XMLStreamWriter xmlStreamWriter)

getAttributeValue

public static String getAttributeValue(StartElement startElement,
                                       String attributeName)

getAttributeValue

public static String getAttributeValue(StartElement startElement,
                                       QName attributeQName)

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.

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

toString

public static String toString(XMLEventReader reader)

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

    splitCData

    public static List<String> splitCData(String data)

    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

    1. <![CDATA[stuff]]
    2. >
    The final > would close the CDATA so is returned in a separate String. Both these returned Strings can be safely written to CDATA sections in a document.

    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

    public 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.

    Parameters:
    ex - the Woodstox exception to be converted to an XhtmlException.
    Returns:
    an XhtmlParsingException if the WstxLazyException is wrapping a WstxParsingException, otherwise an XhtmlException

    stripIllegalControlChars

    public static CharSequence stripIllegalControlChars(CharSequence unclean)
    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


    Copyright © 2003-2012 Atlassian. All Rights Reserved.