Nux 1.6

nux.xom.pool
Class XSLTransformFactory

java.lang.Object
  extended by nux.xom.pool.XSLTransformFactory

public class XSLTransformFactory
extends Object

Creates and returns new XSLTransform objects using flexible parametrization (thread-safe). TRAX factories, attributes and URIResolver of the underlying TRAX TransformerFactory can be specified by overriding the protected getPreferredTransformerFactories and initFactory methods.

For anything but simple/basic use cases, this API is more robust, configurable and convenient than the underlying XOM XSLTransform constructor API.

This implementation is thread-safe.

Example usage:

   // without custom factories:
   XSLTransform trans = new XSLTransformFactory().createTransform(new File("/tmp/test.xsl"));
   Document doc = BuilderPool.GLOBAL_POOL.getBuilder(false).build(new File("/tmp/test.xml"));
   Nodes nodes = trans.transform(doc);
   for (int i=0; i < nodes.size(); i++) {
      System.out.println("node "+i+": "+nodes.get(i).toXML());
   }
 
   // with custom factories:
   XSLTransformFactory factory = new XSLTransformFactory() {
     protected String[] getPreferredTransformerFactories() {
       return new String[] {
           "net.sf.saxon.TransformerFactoryImpl",
           "org.apache.xalan.processor.TransformerFactoryImpl"
       };
     }
   };
   XSLTransform trans = factory.createTransform(new File("/tmp/test.xsl"));
   Document doc = BuilderPool.GLOBAL_POOL.getBuilder(false).build(new File("/tmp/test.xml"));
   Nodes nodes = trans.transform(doc);
 

Author:
whoschek.AT.lbl.DOT.gov, $Author: hoschek3 $

Constructor Summary
XSLTransformFactory()
          Creates a factory instance.
 
Method Summary
 XSLTransform createTransform(Document stylesheet)
          Creates and returns a new XSLTransform for the given stylesheet.
 XSLTransform createTransform(File stylesheet)
          Creates and returns a new XSLTransform for the given stylesheet.
 XSLTransform createTransform(InputStream stylesheet, URI baseURI)
          Creates and returns a new XSLTransform for the given stylesheet.
protected  String[] getPreferredTransformerFactories()
          Callback that returns a search list of fully qualified class names of TRAX TransformerFactory implementations, given in order of preference from left to right.
protected  void initFactory(TransformerFactory factory)
          Callback that initializes the supplied TransformerFactory with application-specific attributes and a URIResolver, if so desired.
protected  XSLTransform newTransform(Document stylesheet, TransformerFactory transformerFactory)
          Callback that creates and returns a new XSLTransform for the given stylesheet and TransformerFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XSLTransformFactory

public XSLTransformFactory()
Creates a factory instance.

Method Detail

createTransform

public XSLTransform createTransform(Document stylesheet)
                             throws XSLException
Creates and returns a new XSLTransform for the given stylesheet.

Parameters:
stylesheet - the stylesheet to compile
Returns:
an XSL transform
Throws:
XSLException - if the XSLTransform can't be created, e.g. because of an XSL syntax error.

createTransform

public XSLTransform createTransform(File stylesheet)
                             throws XSLException,
                                    ParsingException,
                                    IOException
Creates and returns a new XSLTransform for the given stylesheet.

Parameters:
stylesheet - the stylesheet to compile
Returns:
an XSL transform
Throws:
IOException - if an I/O error occurs while reading from the stream
ParsingException - if the stylesheet is not well-formed XML
XSLException - if the XSLTransform can't be created, e.g. because of an XSL syntax error.

createTransform

public XSLTransform createTransform(InputStream stylesheet,
                                    URI baseURI)
                             throws XSLException,
                                    ParsingException,
                                    IOException
Creates and returns a new XSLTransform for the given stylesheet.

Parameters:
stylesheet - the stylesheet to compile
baseURI - the (absolute) baseURI of the stylesheet (may be null) Need not be the stream's actual URI.
Returns:
an XSL transform
Throws:
IOException - if an I/O error occurs while reading from the stream
ParsingException - if the stylesheet is not well-formed XML
XSLException - if the XSLTransform can't be created, e.g. because of an XSL syntax error.

newTransform

protected XSLTransform newTransform(Document stylesheet,
                                    TransformerFactory transformerFactory)
                             throws XSLException
Callback that creates and returns a new XSLTransform for the given stylesheet and TransformerFactory.

Override this method if you want to create custom subclasses of XSLTransform.

Parameters:
stylesheet - the stylesheet to compile
transformerFactory - the TransformerFactory
Returns:
an XSL transform
Throws:
XSLException - if no XSLTransform can be obtained for the given stylesheet; in particular when the supplied stylesheet is not syntactically correct XSLT

getPreferredTransformerFactories

protected String[] getPreferredTransformerFactories()
Callback that returns a search list of fully qualified class names of TRAX TransformerFactory implementations, given in order of preference from left to right. May return null.

Override this method for custom behaviour. This default implementation returns a search list for the most popular TRAX implementations.

Returns:
a search list

initFactory

protected void initFactory(TransformerFactory factory)
Callback that initializes the supplied TransformerFactory with application-specific attributes and a URIResolver, if so desired.

Override this method if you need custom attributes/resolvers. This default implementation does nothing.

Note: Attributes and resolver are not part of the constructor because they may well be stateful and mutable, hence it may well be unsafe to share them among multiple XSLTransforms in a multi-threaded context. By providing this method, an application can create attributes/resolvers as needed via straightforward subclassing/overriding of this method.

Parameters:
factory - the factory to initialize

Nux 1.6