Class XMLStreamReaderUtils
- java.lang.Object
-
- org.apache.axiom.util.stax.XMLStreamReaderUtils
-
public class XMLStreamReaderUtils extends Object
Contains utility methods to work withXMLStreamReaderobjects, including the extension defined byDataHandlerReader. In addition toDataHandlerReadersupport, this class also provides support for the legacy extension mechanism described below.Legacy XMLStreamReader extensions for optimized base64 handling
XMLStreamReaderinstances supporting the legacy extension must conform to the following requirements:XMLStreamReader.getProperty(String)must returnBoolean.TRUEfor the property identified byOMConstants.IS_DATA_HANDLERS_AWARE, regardless of the current event. The property is assumed to be immutable and its value must not change during the lifetime of theXMLStreamReaderimplementation.-
If the
XMLStreamReaderwishes to expose base64 encoded content using aDataHandlerobject, it must do so using a singleXMLStreamConstants.CHARACTERSevent.To maintain compatibility with consumers that are unaware of the extensions described here, the implementation should make sure that
XMLStreamReader.getText(),XMLStreamReader.getTextStart(),XMLStreamReader.getTextLength(),XMLStreamReader.getTextCharacters(),XMLStreamReader.getTextCharacters(int, char[], int, int)andXMLStreamReader.getElementText()behave as expected for this type of event, i.e. return the base64 representation of the binary content. XMLStreamReader.getProperty(String)must returnBoolean.TRUEfor the property identified byOMConstants.IS_BINARYif the current event is aXMLStreamConstants.CHARACTERSevent representing base64 encoded binary content and for which aDataHandleris available. For all other events, the returned value must beBoolean.FALSE.-
If for a given event, the implementation returned
Boolean.TRUEfor theOMConstants.IS_BINARYproperty, then a call toXMLStreamReader.getProperty(String)with argumentOMConstants.DATA_HANDLERmust return the correspondingDataHandlerobject.The
OMConstants.DATA_HANDLERproperty is undefined for any other type of event. This implies that the consumer of theXMLStreamReadermust check theOMConstants.IS_BINARYproperty before retrieving theOMConstants.DATA_HANDLERproperty.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DataHandlergetDataHandlerFromElement(XMLStreamReader reader)Get aDataHandlerfor the binary data encoded in an element.static DataHandlerReadergetDataHandlerReader(XMLStreamReader reader)Get theDataHandlerReaderextension for a givenXMLStreamReader, if available.static ReadergetElementTextAsStream(XMLStreamReader reader, boolean allowNonTextChildren)Get the text content of the current element as aReaderobject.static XMLStreamReadergetOriginalXMLStreamReader(XMLStreamReader parser)Searches the wrapper and delegate classes to find the originalXMLStreamReader.static ObjectprocessGetProperty(DataHandlerReader extension, String propertyName)Helper method to implementXMLStreamReader.getProperty(String).static voidwriteTextTo(XMLStreamReader reader, Writer writer)Get the character data for the current event from the given reader and write it to the given writer.
-
-
-
Method Detail
-
getDataHandlerReader
public static DataHandlerReader getDataHandlerReader(XMLStreamReader reader)
Get theDataHandlerReaderextension for a givenXMLStreamReader, if available. If theXMLStreamReaderonly supports the legacy extension (as described above), then this method will return a compatibility wrapper. Note that this wrapper doesn't support deferred loading of the binary content.- Parameters:
reader- the stream reader to get theDataHandlerReaderextension from- Returns:
- the implementation of the extension, or
nullif theXMLStreamReaderdoesn't expose base64 encoded binary content asDataHandlerobjects.
-
processGetProperty
public static Object processGetProperty(DataHandlerReader extension, String propertyName)
Helper method to implementXMLStreamReader.getProperty(String). This method processed the properties defined byDataHandlerReader.PROPERTYand the legacy extension mechanism (as described above). It can therefore be used to make aXMLStreamReaderimplementation compatible with code that expects it to implement this legacy extension.- Parameters:
extension- the reference to theDataHandlerReaderextension for theXMLStreamReaderimplementationpropertyName- the name of the property, as passed to theXMLStreamReader.getProperty(String)method- Returns:
- the property value as specified by the
DataHandlerReaderor legacy extension;nullif the property is not specified by any of these two extensions
-
getDataHandlerFromElement
public static DataHandler getDataHandlerFromElement(XMLStreamReader reader) throws XMLStreamException
Get aDataHandlerfor the binary data encoded in an element. The method supports base64 encoded character data as well as optimized binary data through theDataHandlerReaderextension.Precondition: the reader is on a
XMLStreamConstants.START_ELEMENTPostcondition: the reader is on the corresponding
XMLStreamConstants.END_ELEMENT- Parameters:
reader- the stream to read the data from- Returns:
- the binary data from the element
- Throws:
XMLStreamException
-
writeTextTo
public static void writeTextTo(XMLStreamReader reader, Writer writer) throws XMLStreamException, IOException
Get the character data for the current event from the given reader and write it to the given writer. The method will try to figure out the most efficient way to copy the data without unnecessary buffering or conversions between strings and character arrays.- Parameters:
reader- the reader to get the character data fromwriter- the writer to write the character data to- Throws:
XMLStreamException- if the underlying XML source is not well-formedIOException- if an I/O error occurs when writing the character dataIllegalStateException- if this state is not a valid text state.- See Also:
CharacterDataReader
-
getElementTextAsStream
public static Reader getElementTextAsStream(XMLStreamReader reader, boolean allowNonTextChildren)
Get the text content of the current element as aReaderobject.- Parameters:
reader- The XML stream reader to read the element text from. The reader must be positioned on aXMLStreamConstants.START_ELEMENTevent.allowNonTextChildren- If set totrue, non text child nodes are allowed and skipped. If set tofalseonly text nodes are allowed and the presence of any other type of child node will trigger an exception.- Returns:
- The reader from which the element text can be read. After the reader has reported the
end of the stream, the XML stream reader will be positioned on the
XMLStreamConstants.END_ELEMENTevent corresponding to the initialXMLStreamConstants.START_ELEMENTevent. CallingReader.close()on the returned reader has no effect. Any parser exception will be reported by the reader usingXMLStreamIOException. - Throws:
IllegalStateException- if the XML stream reader is not positioned on aXMLStreamConstants.START_ELEMENTevent
-
getOriginalXMLStreamReader
public static XMLStreamReader getOriginalXMLStreamReader(XMLStreamReader parser)
Searches the wrapper and delegate classes to find the originalXMLStreamReader. This method should only be used when a consumer of Axiom really needs to access the original stream reader.- Parameters:
parser- XMLStreamReader used by Axiom- Returns:
- original parser
-
-