Top Description Fields Constructors Methods
com.thoughtworks.xstream.io.xml

public Class TraxSource

extends SAXSource
Class Inheritance
Imports
com.thoughtworks.xstream.XStream, org.xml.sax.InputSource, .SAXException, .XMLFilter, .XMLReader, javax.xml.transform.sax.SAXSource, java.util.ArrayList, .List

A 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

Modifier and TypeField and Description
private List<E>
source

The list of Java objects to be serialized.

private XMLReader
xmlReader

The XMLReader object associated to this source or null if no XMLReader has yet been requested.

private XStream
xstream

The configured XStream facade to use for serializing objects.

public static final String
XSTREAM_FEATURE

If javax.xml.transform.TransformerFactory#getFeature returns true when passed this value as an argument, the Transformer natively supports XStream.

Inherited from javax.xml.transform.sax.SAXSource:
FEATURE

Constructor Summary

AccessConstructor and Description
public
TraxSource()

Creates a XStream TrAX source.

public
TraxSource(Object
the object to marshal.
source
)

Creates a XStream TrAX source, specifying the object to marshal.

public
TraxSource(Object
the object to marshal.
source
,
XStream
a configured XStream facade.
xstream
)

Creates a XStream TrAX source, specifying the object to marshal and a configured (with aliases) XStream facade.

public
TraxSource(List<E>
the list of objects to marshal.
source
)

Creates a XStream TrAX source, setting the objects to marshal.

public
TraxSource(List<E>
the list of objects to marshal.
source
,
XStream
a configured XStream facade.
xstream
)

Creates a XStream TrAX source, setting the objects to marshal and a configured (with aliases) XStream facade.

Method Summary

Modifier and TypeMethod and Description
private void
private void
public XMLReader

Returns:

an XMLReader generating the XML from a list of input objects.
getXMLReader
()

Overrides javax.xml.transform.sax.SAXSource.getXMLReader.

Returns the XMLReader to be used for the Source.
public void
setInputSource(InputSource
a valid InputSource reference.
inputSource
)

Overrides javax.xml.transform.sax.SAXSource.setInputSource.

Sets the SAX InputSource to be used for the Source.
public void
setSource(Object
the object to marshal.
obj
)

Sets the object to marshal.

public void
setSourceAsList(List<E>
the list of objects to marshal.
list
)

Sets the list of objects to marshal.

public void
setXMLReader(XMLReader
a valid XMLReader or XMLFilter reference.
reader
)

Overrides javax.xml.transform.sax.SAXSource.setXMLReader.

Set the XMLReader to be used for the Source.
public void
setXStream(XStream
a configured XStream facade.
xstream
)

Sets the XStream facade to use when marshalling objects.

Inherited from javax.xml.transform.sax.SAXSource:
getInputSourcegetSystemIdisEmptysetSystemIdsourceToInputSource

Field Detail

sourceback to summary
private List<E> source

The list of Java objects to be serialized.

xmlReaderback to summary
private XMLReader xmlReader

The XMLReader object associated to this source or null if no XMLReader has yet been requested.

See Also
getXMLReader
xstreamback to summary
private XStream xstream

The configured XStream facade to use for serializing objects.

XSTREAM_FEATUREback to summary
public static final String XSTREAM_FEATURE

If javax.xml.transform.TransformerFactory#getFeature returns true when passed this value as an argument, the Transformer natively supports XStream.

Note

This implementation does not override the SAXSource#FEATURE value defined by its superclass to be considered as a SAXSource by Transformer implementations not natively supporting this XStream-specific source

Constructor Detail

TraxSourceback to summary
public TraxSource()

Creates a XStream TrAX source.

TraxSourceback to summary
public TraxSource(Object source)

Creates a XStream TrAX source, specifying the object to marshal.

Parameters
source:Object

the object to marshal.

Exceptions
IllegalArgumentException:
if source is null.
See Also
setSource(java.lang.Object)
TraxSourceback to summary
public TraxSource(Object source, XStream xstream)

Creates a XStream TrAX source, specifying the object to marshal and a configured (with aliases) XStream facade.

Parameters
source:Object

the object to marshal.

xstream:XStream

a configured XStream facade.

Exceptions
IllegalArgumentException:
if source or xstream is null.
See Also
setSource(java.lang.Object), setXStream(com.thoughtworks.xstream.XStream)
TraxSourceback to summary
public TraxSource(List<E> source)

Creates a XStream TrAX source, setting the objects to marshal.

Parameters
source:List<E>

the list of objects to marshal.

Exceptions
IllegalArgumentException:
if source is null or empty.
See Also
setSourceAsList(java.util.List)
TraxSourceback to summary
public TraxSource(List<E> source, XStream xstream)

Creates a XStream TrAX source, setting the objects to marshal and a configured (with aliases) XStream facade.

Parameters
source:List<E>

the list of objects to marshal.

xstream:XStream

a configured XStream facade.

Exceptions
IllegalArgumentException:
if source or xstream is null or source is empty.
See Also
setSourceAsList(java.util.List), setXStream(com.thoughtworks.xstream.XStream)

Method Detail

configureXMLReaderback to summary
private void configureXMLReader()
createXMLReaderback to summary
private void createXMLReader(XMLReader filterChain)
getXMLReaderback to summary
public XMLReader getXMLReader()

Overrides javax.xml.transform.sax.SAXSource.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.

Returns:XMLReader

an XMLReader generating the XML from a list of input objects.

setInputSourceback to summary
public void setInputSource(InputSource inputSource)

Overrides javax.xml.transform.sax.SAXSource.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.

Parameters
inputSource:InputSource

a valid InputSource reference.

Exceptions
UnsupportedOperationException:
always!
setSourceback to summary
public void setSource(Object obj)

Sets the object to marshal.

Parameters
obj:Object

the object to marshal.

Exceptions
IllegalArgumentException:
if source is null.
setSourceAsListback to summary
public void setSourceAsList(List<E> list)

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:List<E>

the list of objects to marshal.

Exceptions
IllegalArgumentException:
if source is null or empty.
setXMLReaderback to summary
public void setXMLReader(XMLReader reader)

Overrides javax.xml.transform.sax.SAXSource.setXMLReader.

Set the XMLReader to be used for the Source.

As this implementation only supports object lists as data source, this method throws an UnsupportedOperationException if the provided reader object does not implement the SAX XMLFilter interface. Otherwise, a SaxWriter instance will be attached as parent of the filter chain.

Parameters
reader:XMLReader

a valid XMLReader or XMLFilter reference.

Exceptions
UnsupportedOperationException:
if reader is not a SAX XMLFilter.
See Also
getXMLReader
setXStreamback to summary
public void setXStream(XStream xstream)

Sets the XStream facade to use when marshalling objects.

Parameters
xstream:XStream

a configured XStream facade.

Exceptions
IllegalArgumentException:
if xstream is null.