org.mule.module.xml.transformer
Class XsltTransformer

java.lang.Object
  extended by org.mule.transformer.AbstractTransformer
      extended by org.mule.transformer.AbstractMessageTransformer
          extended by org.mule.module.xml.transformer.AbstractXmlTransformer
              extended by org.mule.module.xml.transformer.XsltTransformer
All Implemented Interfaces:
AnnotatedObject, MuleContextAware, Disposable, Initialisable, NameableObject, NamedObject, MessageProcessor, MessageTransformer, Transformer, EndpointAware
Direct Known Subclasses:
JsonXsltTransformer

public class XsltTransformer
extends AbstractXmlTransformer

XsltTransformer performs an XSLT transform on a DOM (or other XML-ish) object.

This transformer maintains a pool of Transformer objects to speed up processing of concurrent requests. The pool can be configured using setMaxIdleTransformers(int).

Parameter can also be set as part of the transformation context and these can be mapped to conent in the current message using property extractors or can be fixed values.

For example, the current event's message has a property named "myproperty", also you want to generate a uuid as a parameter. To do this you can define context properties that can provide an expression to be evaluated on the current message.

Example Configuration:

  <mxml:xslt-transformer name="MyXsltTransformer" xslFile="myXslFile.xsl"&gt
      <context-property name="myParameter" value="#[head:myproperty]"/&gt
      <context-property name="myParameter2" value="#[function:uuid]"/&gt
  </mxml:xslt-transformer&gt
 

The 'header' expression pulls a header from the current message and 'function' can execute a set of arbitrary functions. You can also pass in static values by ommitting the expression prefix '#['.

In addition to being able to pass in an XSLT file you can also define templates inline. For example -

  <mxml:xslt-transformer name="MyXsltTransformer"&gt
      <mxml:xslt-text&gt
          
 

</mxml:xslt-text&gt


Nested Class Summary
protected  class XsltTransformer.DefaultErrorListener
           
protected  class XsltTransformer.PooledXsltTransformerFactory
           
 
Nested classes/interfaces inherited from class org.mule.module.xml.transformer.AbstractXmlTransformer
AbstractXmlTransformer.ResultHolder
 
Field Summary
static String PREFERRED_TRANSFORMER_FACTORY
           
protected  org.apache.commons.pool.impl.GenericObjectPool transformerPool
           
 
Fields inherited from class org.mule.transformer.AbstractTransformer
encoding, endpoint, logger, mimeType, MULE_MESSAGE_DATA_TYPE, muleContext, name, returnType, sourceTypes
 
Fields inherited from interface org.mule.api.lifecycle.Initialisable
PHASE_NAME
 
Fields inherited from interface org.mule.api.lifecycle.Disposable
PHASE_NAME
 
Fields inherited from interface org.mule.api.AnnotatedObject
PROPERTY_NAME
 
Constructor Summary
XsltTransformer()
           
 
Method Summary
protected  void doTransform(MuleMessage message, String outputEncoding, Source sourceDoc, Result result)
           
protected  Object evaluateTransformParameter(String key, Object value, MuleMessage message)
          Returns the value to be set for the parameter.
 Map<String,Object> getContextProperties()
          Gets the parameters to be used when applying the transformation
protected  Object getDelayedResult(MuleMessage message, String outputEncoding, Source sourceDoc)
           
 int getMaxActiveTransformers()
           
 int getMaxIdleTransformers()
           
protected  StreamSource getStreamSource()
          Returns the StreamSource corresponding to xslt (which should have been loaded in initialise()).
 URIResolver getUriResolver()
           
 String getXslFile()
           
 String getXslt()
           
 String getXslTransformerFactory()
          Returns the name of the currently configured javax.xml.transform.Transformer factory class used to create XSLT Transformers.
 void initialise()
          Template method where deriving classes can do any initialisation after the properties have been set on this transformer
 void setContextProperties(Map<String,Object> contextProperties)
          Sets the parameters to be used when applying the transformation
 void setMaxActiveTransformers(int maxActiveTransformers)
          Sets the the current maximum number of active transformer objects allowed in the pool
 void setMaxIdleTransformers(int maxIdleTransformers)
          Sets the the current maximum number of idle transformer objects allowed in the pool
 void setUriResolver(URIResolver uriResolver)
           
 void setXslFile(String xslFile)
           
 void setXslt(String xslt)
           
 void setXslTransformerFactory(String xslTransformerFactory)
          Configures the javax.xml.transform.Transformer factory class
 Object transformMessage(MuleMessage message, String putputEncoding)
          Transform, using XSLT, a XML String to another String.
 
Methods inherited from class org.mule.module.xml.transformer.AbstractXmlTransformer
convertToBytes, convertToText, convertToText, getOutputEncoding, getResultHolder, getXMLInputFactory, getXMLOutputFactory, isUseStaxSource, setOutputEncoding, setUseStaxSource, setXMLInputFactory, setXMLOutputFactory, writeToStream
 
Methods inherited from class org.mule.transformer.AbstractMessageTransformer
checkReturnClass, doTransform, isSourceDataTypeSupported, transform, transform, transform
 
Methods inherited from class org.mule.transformer.AbstractTransformer
dispose, generateTransformerName, getAnnotation, getAnnotations, getEncoding, getEncoding, getEndpoint, getMimeType, getName, getReturnClass, getReturnDataType, getSourceDataTypes, getSourceTypes, isAcceptNull, isAllowNullReturn, isConsumed, isIgnoreBadInput, isSourceDataTypeSupported, isSourceTypeSupported, isSourceTypeSupported, process, registerSourceType, registerSourceType, setAllowNullReturn, setAnnotations, setEncoding, setEndpoint, setIgnoreBadInput, setMimeType, setMuleContext, setName, setReturnClass, setReturnDataType, toString, transform, unregisterSourceType, unregisterSourceType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.mule.api.transformer.Transformer
getEncoding, getEndpoint, getMimeType, getReturnClass, getReturnDataType, getSourceDataTypes, getSourceTypes, isAcceptNull, isIgnoreBadInput, isSourceDataTypeSupported, isSourceTypeSupported, setReturnClass, setReturnDataType, transform
 
Methods inherited from interface org.mule.api.processor.MessageProcessor
process
 
Methods inherited from interface org.mule.api.lifecycle.Disposable
dispose
 
Methods inherited from interface org.mule.api.NameableObject
setName
 
Methods inherited from interface org.mule.api.NamedObject
getName
 
Methods inherited from interface org.mule.api.context.MuleContextAware
setMuleContext
 
Methods inherited from interface org.mule.endpoint.EndpointAware
setEndpoint
 

Field Detail

PREFERRED_TRANSFORMER_FACTORY

public static final String PREFERRED_TRANSFORMER_FACTORY
See Also:
Constant Field Values

transformerPool

protected final org.apache.commons.pool.impl.GenericObjectPool transformerPool
Constructor Detail

XsltTransformer

public XsltTransformer()
Method Detail

initialise

public void initialise()
                throws InitialisationException
Description copied from class: AbstractTransformer
Template method where deriving classes can do any initialisation after the properties have been set on this transformer

Specified by:
initialise in interface Initialisable
Overrides:
initialise in class AbstractTransformer
Throws:
InitialisationException - if a fatal error occurs causing the Mule instance to shutdown
RecoverableException - if an error occurs that can be recovered from

transformMessage

public Object transformMessage(MuleMessage message,
                               String putputEncoding)
                        throws TransformerException
Transform, using XSLT, a XML String to another String.

Specified by:
transformMessage in class AbstractMessageTransformer
Returns:
The result in the type specified by the user
Throws:
TransformerException

getDelayedResult

protected Object getDelayedResult(MuleMessage message,
                                  String outputEncoding,
                                  Source sourceDoc)

doTransform

protected void doTransform(MuleMessage message,
                           String outputEncoding,
                           Source sourceDoc,
                           Result result)
                    throws Exception
Throws:
Exception

getXslTransformerFactory

public String getXslTransformerFactory()
Returns the name of the currently configured javax.xml.transform.Transformer factory class used to create XSLT Transformers.

Returns:
a TransformerFactory class name or null if none has been configured

setXslTransformerFactory

public void setXslTransformerFactory(String xslTransformerFactory)
Configures the javax.xml.transform.Transformer factory class

Parameters:
xslTransformerFactory - the name of the TransformerFactory class to use

getXslFile

public String getXslFile()

setXslFile

public void setXslFile(String xslFile)

getXslt

public String getXslt()

setXslt

public void setXslt(String xslt)

getUriResolver

public URIResolver getUriResolver()

setUriResolver

public void setUriResolver(URIResolver uriResolver)

getStreamSource

protected StreamSource getStreamSource()
                                throws InitialisationException
Returns the StreamSource corresponding to xslt (which should have been loaded in initialise()).

Returns:
The StreamSource
Throws:
InitialisationException

getMaxActiveTransformers

public int getMaxActiveTransformers()
Returns:
The current maximum number of allowable active transformer objects in the pool

setMaxActiveTransformers

public void setMaxActiveTransformers(int maxActiveTransformers)
Sets the the current maximum number of active transformer objects allowed in the pool

Parameters:
maxActiveTransformers - New maximum size to set

getMaxIdleTransformers

public int getMaxIdleTransformers()
Returns:
The current maximum number of allowable idle transformer objects in the pool

setMaxIdleTransformers

public void setMaxIdleTransformers(int maxIdleTransformers)
Sets the the current maximum number of idle transformer objects allowed in the pool

Parameters:
maxIdleTransformers - New maximum size to set

getContextProperties

public Map<String,Object> getContextProperties()
Gets the parameters to be used when applying the transformation

Returns:
a map of the parameter names and associated values
See Also:
Transformer.setParameter(java.lang.String, java.lang.Object)

setContextProperties

public void setContextProperties(Map<String,Object> contextProperties)
Sets the parameters to be used when applying the transformation

Parameters:
contextProperties - a map of the parameter names and associated values
See Also:
Transformer.setParameter(java.lang.String, java.lang.Object)

evaluateTransformParameter

protected Object evaluateTransformParameter(String key,
                                            Object value,
                                            MuleMessage message)
                                     throws TransformerException
Returns the value to be set for the parameter. This method is called for each parameter before it is set on the transformer. The purpose of this method is to allow dynamic parameters related to the event (usually message properties) to be used. Any attribute of the current MuleEvent can be accessed using Property Extractors such as JXpath, bean path or header retrieval.

Parameters:
key - the name of the parameter. The name isn't used for this implementation but is exposed as a param for classes that may need it.
value - the value of the paramter
Returns:
the object to be set as the parameter value
Throws:
TransformerException


Copyright © 2003-2014 MuleSoft, Inc.. All Rights Reserved.