Class TraxSource
- All Implemented Interfaces:
Source
JAXP TrAX Source that enables using XStream object serialization as
direct input for XSLT processors without resorting to an intermediate representation such as
text XML, DOM or DOM4J.
The following example shows how to apply an XSL Transformation
to a set of Java objects gathered into a List (source):
public static String transform(List source, String stylesheet) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(
new StreamSource(stylesheet));
TraxSource in = new TraxSource(source);
Writer out = new StringWriter();
transformer.transform(in, new StreamResult(out));
return out.toString();
} catch (TransformerException e) {
throw new RuntimeException("XSLT Transformation failed", e);
}
}
- Author:
- Laurent Bihanic
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringIfTransformerFactory.getFeature(String)returnstruewhen passed this value as an argument, the Transformer natively supports XStream. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a XStream TrAX source.TraxSource(Object source) Creates a XStream TrAX source, specifying the object to marshal.TraxSource(Object source, XStream xstream) Creates a XStream TrAX source, specifying the object to marshal and a configured (with aliases) XStream facade.TraxSource(List source) Creates a XStream TrAX source, setting the objects to marshal.TraxSource(List source, XStream xstream) Creates a XStream TrAX source, setting the objects to marshal and a configured (with aliases) XStream facade. -
Method Summary
Modifier and TypeMethodDescriptionReturns the XMLReader to be used for the Source.voidsetInputSource(InputSource inputSource) Sets the SAX InputSource to be used for the Source.voidSets the object to marshal.voidsetSourceAsList(List list) Sets the list of objects to marshal.voidsetXMLReader(XMLReader reader) Set the XMLReader to be used for the Source.voidsetXStream(XStream xstream) Sets the XStream facade to use when marshalling objects.Methods inherited from class SAXSource
getInputSource, getSystemId, isEmpty, setSystemId, sourceToInputSource
-
Field Details
-
XSTREAM_FEATURE
IfTransformerFactory.getFeature(String)returnstruewhen passed this value as an argument, the Transformer natively supports XStream.Note: This implementation does not override the
SAXSource.FEATUREvalue defined by its superclass to be considered as a SAXSource by Transformer implementations not natively supporting this XStream-specific source- See Also:
-
-
Constructor Details
-
TraxSource
public TraxSource()Creates a XStream TrAX source. -
TraxSource
Creates a XStream TrAX source, specifying the object to marshal.- Parameters:
source- the object to marshal.- Throws:
IllegalArgumentException- ifsourceisnull.- See Also:
-
TraxSource
Creates a XStream TrAX source, specifying the object to marshal and a configured (with aliases) XStream facade.- Parameters:
source- the object to marshal.xstream- a configured XStream facade.- Throws:
IllegalArgumentException- ifsourceorxstreamisnull.- See Also:
-
TraxSource
Creates a XStream TrAX source, setting the objects to marshal.- Parameters:
source- the list of objects to marshal.- Throws:
IllegalArgumentException- ifsourceisnullor empty.- See Also:
-
TraxSource
Creates a XStream TrAX source, setting the objects to marshal and a configured (with aliases) XStream facade.- Parameters:
source- the list of objects to marshal.xstream- a configured XStream facade.- Throws:
IllegalArgumentException- ifsourceorxstreamisnullorsourceis empty.- See Also:
-
-
Method Details
-
setInputSource
Sets the SAX InputSource to be used for the Source.As this implementation only supports object lists as data source, this method always throws an
UnsupportedOperationException.- Overrides:
setInputSourcein classSAXSource- Parameters:
inputSource- a valid InputSource reference.- Throws:
UnsupportedOperationException- always!
-
setXMLReader
Set the XMLReader to be used for the Source.As this implementation only supports object lists as data source, this method throws an
UnsupportedOperationExceptionif the provided reader object does not implement the SAXXMLFilterinterface. Otherwise, aSaxWriterinstance will be attached as parent of the filter chain.- Overrides:
setXMLReaderin classSAXSource- Parameters:
reader- a valid XMLReader or XMLFilter reference.- Throws:
UnsupportedOperationException- ifreaderis not a SAXXMLFilter.- See Also:
-
getXMLReader
Returns the XMLReader to be used for the Source.This implementation returns a specific XMLReader (
SaxWriter) generating the XML from a list of input objects.- Overrides:
getXMLReaderin classSAXSource- Returns:
- an XMLReader generating the XML from a list of input objects.
-
setXStream
Sets the XStream facade to use when marshalling objects.- Parameters:
xstream- a configured XStream facade.- Throws:
IllegalArgumentException- ifxstreamisnull.
-
setSource
Sets the object to marshal.- Parameters:
obj- the object to marshal.- Throws:
IllegalArgumentException- ifsourceisnull.
-
setSourceAsList
Sets the list of objects to marshal.When dealing with non-text input (such as SAX or DOM), XSLT processors support multiple root node children for the source tree (see section 3.1 of the "XSL Transformations (XSLT) Version 1.0" specification. Using a list of objects as source makes use of this feature and allows creating XML documents merging the XML serialization of several Java objects.
- Parameters:
list- the list of objects to marshal.- Throws:
IllegalArgumentException- ifsourceisnullor empty.
-