Class TransformRepresentation


  • public class TransformRepresentation
    extends WriterRepresentation
    Representation able to apply an XSLT transformation. The internal JAXP transformer is created when the getTransformer() method is first called. So, if you need to specify a custom URI resolver, do it before actually using the representation for a transformation.

    This representation should be viewed as a wrapper representation that applies a transform sheet on a source representation when it is read or written out. Therefore, it isn't intended to be reused on different sources. For this use case, you should instead use the org.restlet.routing.Transformer filter.
    • Field Detail

      • errorListener

        private volatile javax.xml.transform.ErrorListener errorListener
        The transformer's error listener.
      • outputProperties

        private volatile java.util.Map<java.lang.String,​java.lang.String> outputProperties
        The JAXP transformer output properties.
      • parameters

        private volatile java.util.Map<java.lang.String,​java.lang.Object> parameters
        The JAXP transformer parameters.
      • sourceRepresentation

        private volatile Representation sourceRepresentation
        The source representation to transform.
      • templates

        private volatile javax.xml.transform.Templates templates
        The template to be used and reused.
      • transformSheet

        private volatile Representation transformSheet
        The XSLT transform sheet to apply to message entities.
      • uriResolver

        private volatile javax.xml.transform.URIResolver uriResolver
        The URI resolver.
    • Constructor Detail

      • TransformRepresentation

        public TransformRepresentation​(Context context,
                                       Representation source,
                                       Representation transformSheet)
        Constructor. Note that a default URI resolver will be created based on the given context.
        Parameters:
        context - The parent context.
        source - The source representation to transform.
        transformSheet - The XSLT transform sheet to apply.
      • TransformRepresentation

        public TransformRepresentation​(Representation source,
                                       Representation transformSheet)
        Default constructor.
        Parameters:
        source - The source representation to transform.
        transformSheet - The XSLT transform sheet to apply.
      • TransformRepresentation

        public TransformRepresentation​(javax.xml.transform.URIResolver uriResolver,
                                       Representation source,
                                       Representation transformSheet)
        Constructor. Note that a default URI resolver will be created based on the given context.
        Parameters:
        uriResolver - The JAXP URI resolver.
        source - The source representation to transform.
        transformSheet - The XSLT transform sheet to apply.
      • TransformRepresentation

        private TransformRepresentation​(javax.xml.transform.URIResolver uriResolver,
                                        Representation source,
                                        Representation transformSheet,
                                        javax.xml.transform.Templates templates)
        Constructor.
        Parameters:
        uriResolver - The optional JAXP URI resolver.
        source - The source representation to transform.
        templates - The precompiled JAXP template.
      • TransformRepresentation

        public TransformRepresentation​(javax.xml.transform.URIResolver uriResolver,
                                       Representation source,
                                       javax.xml.transform.Templates templates)
        Constructor.
        Parameters:
        uriResolver - The optional JAXP URI resolver.
        source - The source representation to transform.
        templates - The precompiled JAXP template.
    • Method Detail

      • toSaxSource

        public static javax.xml.transform.sax.SAXSource toSaxSource​(Representation representation)
                                                             throws java.io.IOException
        Wraps a source representation into a SAXSource. This method can detect other XmlRepresentation instances to use their XmlRepresentation.getSaxSource() method as well as other TransformRepresentation instances to support transformation chaining.
        Parameters:
        representation - The source representation.
        Returns:
        The SAX source.
        Throws:
        java.io.IOException
      • getErrorListener

        public javax.xml.transform.ErrorListener getErrorListener()
        Returns the transformer's error listener. Default value is null, leaving the original listener intact.
        Returns:
        The transformer's error listener.
      • getOutputProperties

        public java.util.Map<java.lang.String,​java.lang.String> getOutputProperties()
        Returns the modifiable map of JAXP transformer output properties.
        Returns:
        The JAXP transformer output properties.
      • getParameters

        public java.util.Map<java.lang.String,​java.lang.Object> getParameters()
        Returns the modifiable map of JAXP transformer parameters.
        Returns:
        The JAXP transformer parameters.
      • getSaxSource

        public javax.xml.transform.sax.SAXSource getSaxSource()
                                                       throws java.io.IOException
        Returns the SAX source associated to the source representation.
        Returns:
        The SAX source associated to the source representation.
        Throws:
        java.io.IOException
      • getSaxTransformerFactory

        private javax.xml.transform.sax.SAXTransformerFactory getSaxTransformerFactory()
        Returns the default SAX transformer factory.
        Returns:
        The default SAX transformer factory.
      • getSourceRepresentation

        public Representation getSourceRepresentation()
        Returns the source representation to transform.
        Returns:
        The source representation to transform.
      • getTemplates

        public javax.xml.transform.Templates getTemplates()
                                                   throws java.io.IOException
        Returns the templates to be used and reused. If no one exists, it creates a new one based on the transformSheet representation and on the URI resolver.
        Returns:
        The templates to be used and reused.
        Throws:
        java.io.IOException
      • getTransformer

        public javax.xml.transform.Transformer getTransformer()
                                                       throws java.io.IOException
        Returns a new transformer to be used. Creation is based on the getTemplates().newTransformer() method.
        Returns:
        The new transformer to be used.
        Throws:
        java.io.IOException
      • getTransformerHandler

        public javax.xml.transform.sax.TransformerHandler getTransformerHandler()
                                                                         throws java.io.IOException
        Returns the SAX transformer handler associated to the transform sheet.
        Returns:
        The SAX transformer handler.
        Throws:
        java.io.IOException
      • getTransformSheet

        public Representation getTransformSheet()
        Returns the XSLT transform sheet to apply to the source representation.
        Returns:
        The XSLT transform sheet to apply.
      • getUriResolver

        public javax.xml.transform.URIResolver getUriResolver()
        Returns the URI resolver.
        Returns:
        The URI resolver.
      • getXmlFilter

        public org.xml.sax.XMLFilter getXmlFilter()
                                           throws java.io.IOException
        Returns the SAX XML filter applying the transform sheet to its input.
        Returns:
        The SAX XML filter.
        Throws:
        java.io.IOException
      • release

        public void release()
        Releases the source and transform sheet representations, the transformer and the URI resolver.
        Overrides:
        release in class Representation
      • setErrorListener

        public void setErrorListener​(javax.xml.transform.ErrorListener errorListener)
        Sets the transformer's error listener.
        Parameters:
        errorListener - The transformer's error listener.
      • setOutputProperties

        public void setOutputProperties​(java.util.Map<java.lang.String,​java.lang.String> outputProperties)
        Sets the modifiable map of JAXP transformer output properties.
        Parameters:
        outputProperties - The JAXP transformer output properties.
      • setParameters

        public void setParameters​(java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Sets the JAXP transformer parameters.
        Parameters:
        parameters - The JAXP transformer parameters.
      • setSourceRepresentation

        public void setSourceRepresentation​(Representation source)
        Sets the source representation to transform.
        Parameters:
        source - The source representation to transform.
      • setTemplates

        public void setTemplates​(javax.xml.transform.Templates templates)
        Sets the templates to be used and reused.
        Parameters:
        templates - The templates to be used and reused.
      • setTransformSheet

        public void setTransformSheet​(Representation transformSheet)
        Sets the XSLT transform sheet to apply to message entities.
        Parameters:
        transformSheet - The XSLT transform sheet to apply to message entities.
      • setUriResolver

        public void setUriResolver​(javax.xml.transform.URIResolver uriResolver)
        Sets the URI resolver.
        Parameters:
        uriResolver - The URI resolver.
      • transform

        public void transform​(javax.xml.transform.Source source,
                              javax.xml.transform.Result result)
                       throws java.io.IOException
        Transforms the given JAXP source into the given result.
        Parameters:
        source - The JAXP source object.
        result - The JAXP result object.
        Throws:
        java.io.IOException
      • write

        public void write​(javax.xml.transform.Result result)
                   throws java.io.IOException
        Writes the transformed source into the given JAXP result. The source is retrieved using the getSaxSource() method.
        Parameters:
        result - The JAXP result object.
        Throws:
        java.io.IOException
      • write

        public void write​(java.io.Writer writer)
                   throws java.io.IOException
        Writes the transformed source into the given output stream. By default, it leverages the write(Result) method using a StreamResult object.
        Specified by:
        write in class Representation
        Parameters:
        writer - The characters writer.
        Throws:
        java.io.IOException