Class TraxSource

  • All Implemented Interfaces:
    javax.xml.transform.Source

    public class TraxSource
    extends javax.xml.transform.sax.SAXSource
    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);
         }
     }
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List source
      The list of Java objects to be serialized.
      private org.xml.sax.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.
      static java.lang.String XSTREAM_FEATURE
      If TransformerFactory.getFeature(java.lang.String) returns true when passed this value as an argument, the Transformer natively supports XStream.
      • Fields inherited from class javax.xml.transform.sax.SAXSource

        FEATURE
    • Constructor Summary

      Constructors 
      Constructor Description
      TraxSource()
      Creates a XStream TrAX source.
      TraxSource​(java.lang.Object source)
      Creates a XStream TrAX source, specifying the object to marshal.
      TraxSource​(java.lang.Object source, XStream xstream)
      Creates a XStream TrAX source, specifying the object to marshal and a configured (with aliases) XStream facade.
      TraxSource​(java.util.List source)
      Creates a XStream TrAX source, setting the objects to marshal.
      TraxSource​(java.util.List source, XStream xstream)
      Creates a XStream TrAX source, setting the objects to marshal and a configured (with aliases) XStream facade.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void configureXMLReader()  
      private void createXMLReader​(org.xml.sax.XMLReader filterChain)  
      org.xml.sax.XMLReader getXMLReader()
      Returns the XMLReader to be used for the Source.
      void setInputSource​(org.xml.sax.InputSource inputSource)
      Sets the SAX InputSource to be used for the Source.
      void setSource​(java.lang.Object obj)
      Sets the object to marshal.
      void setSourceAsList​(java.util.List list)
      Sets the list of objects to marshal.
      void setXMLReader​(org.xml.sax.XMLReader reader)
      Set the XMLReader to be used for the Source.
      void setXStream​(XStream xstream)
      Sets the XStream facade to use when marshalling objects.
      • Methods inherited from class javax.xml.transform.sax.SAXSource

        getInputSource, getSystemId, isEmpty, setSystemId, sourceToInputSource
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • XSTREAM_FEATURE

        public static final java.lang.String XSTREAM_FEATURE
        If TransformerFactory.getFeature(java.lang.String) 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

        See Also:
        Constant Field Values
      • xmlReader

        private org.xml.sax.XMLReader xmlReader
        The XMLReader object associated to this source or null if no XMLReader has yet been requested.
        See Also:
        getXMLReader()
      • xstream

        private XStream xstream
        The configured XStream facade to use for serializing objects.
      • source

        private java.util.List source
        The list of Java objects to be serialized.
    • Constructor Detail

      • TraxSource

        public TraxSource()
        Creates a XStream TrAX source.
      • TraxSource

        public TraxSource​(java.lang.Object source)
        Creates a XStream TrAX source, specifying the object to marshal.
        Parameters:
        source - the object to marshal.
        Throws:
        java.lang.IllegalArgumentException - if source is null.
        See Also:
        setSource(java.lang.Object)
      • TraxSource

        public TraxSource​(java.lang.Object source,
                          XStream xstream)
        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:
        java.lang.IllegalArgumentException - if source or xstream is null.
        See Also:
        setSource(java.lang.Object), setXStream(com.thoughtworks.xstream.XStream)
      • TraxSource

        public TraxSource​(java.util.List source)
        Creates a XStream TrAX source, setting the objects to marshal.
        Parameters:
        source - the list of objects to marshal.
        Throws:
        java.lang.IllegalArgumentException - if source is null or empty.
        See Also:
        setSourceAsList(java.util.List)
      • TraxSource

        public TraxSource​(java.util.List source,
                          XStream xstream)
        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:
        java.lang.IllegalArgumentException - if source or xstream is null or source is empty.
        See Also:
        setSourceAsList(java.util.List), setXStream(com.thoughtworks.xstream.XStream)
    • Method Detail

      • setInputSource

        public void setInputSource​(org.xml.sax.InputSource inputSource)
        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:
        setInputSource in class javax.xml.transform.sax.SAXSource
        Parameters:
        inputSource - a valid InputSource reference.
        Throws:
        java.lang.UnsupportedOperationException - always!
      • setXMLReader

        public void setXMLReader​(org.xml.sax.XMLReader reader)
        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.

        Overrides:
        setXMLReader in class javax.xml.transform.sax.SAXSource
        Parameters:
        reader - a valid XMLReader or XMLFilter reference.
        Throws:
        java.lang.UnsupportedOperationException - if reader is not a SAX XMLFilter.
        See Also:
        getXMLReader()
      • getXMLReader

        public org.xml.sax.XMLReader 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:
        getXMLReader in class javax.xml.transform.sax.SAXSource
        Returns:
        an XMLReader generating the XML from a list of input objects.
      • setXStream

        public void setXStream​(XStream xstream)
        Sets the XStream facade to use when marshalling objects.
        Parameters:
        xstream - a configured XStream facade.
        Throws:
        java.lang.IllegalArgumentException - if xstream is null.
      • setSource

        public void setSource​(java.lang.Object obj)
        Sets the object to marshal.
        Parameters:
        obj - the object to marshal.
        Throws:
        java.lang.IllegalArgumentException - if source is null.
      • setSourceAsList

        public void setSourceAsList​(java.util.List 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 - the list of objects to marshal.
        Throws:
        java.lang.IllegalArgumentException - if source is null or empty.
      • createXMLReader

        private void createXMLReader​(org.xml.sax.XMLReader filterChain)
      • configureXMLReader

        private void configureXMLReader()