org.mycore.common.xml
Class MCRXSLTransformation

java.lang.Object
  extended by org.mycore.common.xml.MCRXSLTransformation

public class MCRXSLTransformation
extends Object

This class implements XSLTransformation functions to be used in all other MyCoRe packages. The class is implemented as singleton and should be very easy to use. So here is an example:

 // Get an instance of the class 
 MCRXSLTransformation transformation = MCRXSLTransformation.getInstance(); 
 // Get the template: myStylesheet could be a String (i.e. a filename), 
 // a File or a StreamSource Templates
 templates = transformation.getStylesheet(myStylesheet); 
 // Next, you are in need of a TransformerHandler: 
 TransformerHandler th = transformation.getTransformerHandler(templates); 
 // Now you are able to set some properties (if you want!): 
 Properties parameters = new Properties(); ...
 transformation.setParameters(th, parameters); 
 // Finally, you need an OutputStream and might get at work: 
 OutputStream out = response.getOutputStream(); 
 transformation.transform(jdom, th, out); 
 // You might also want to transform into something different, perhaps a ZIP-File:
 OutputStream out = new ZipOutputStream(response.getOutputStream());
 ((ZipOutputStream) out).setLevel(Deflater.BEST_COMPRESSION); 
 ZipEntry ze = new ZipEntry("_index.htm"); 
 ((ZipOutputStream) out).putNextEntry(ze); ... 
 // After all this work is done, you could close the OutputStream: 
 out.close(); 
 // This is not done by transform, the later example 
 // should show, why. * 
 

Version:
$Revision: 1.0 $ $Date: 2003/02/03 14:57:25 $
Author:
Werner Gresshoff

Constructor Summary
MCRXSLTransformation()
           
 
Method Summary
static MCRXSLTransformation getInstance()
          Method getInstance.
 Templates getStylesheet(File stylesheet)
          Method getStylesheet.
 Templates getStylesheet(Source stylesheet)
          Method getStylesheet.
 Templates getStylesheet(String stylesheet)
          Method getStylesheet.
 TransformerHandler getTransformerHandler(Templates stylesheet)
          Method getTransformerHandler.
static void setParameters(TransformerHandler handler, Map parameters)
          Method setParameters.
static void setParameters(Transformer transformer, Map parameters)
          Method setParameters.
static Document transform(Document in, Source stylesheet, Map parameters)
          Method transform.
static Document transform(Document in, String stylesheet)
          Method transform.
static Document transform(Document in, String stylesheet, Map parameters)
          Method transform.
static Document transform(Document in, Transformer transformer)
          transforms a jdom Document via XSLT.
 void transform(Document in, TransformerHandler handler, OutputStream out)
          Method transform.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MCRXSLTransformation

public MCRXSLTransformation()
Method Detail

getInstance

public static MCRXSLTransformation getInstance()
Method getInstance. Creates an instance of MCRXSLTransformation, when called the first time.

Returns:
MCRXSLTransformation

getStylesheet

public Templates getStylesheet(String stylesheet)
Method getStylesheet. Returns a precompiled stylesheet.

Parameters:
stylesheet - Full path to the stylesheet
Returns:
Templates The precompiled Stylesheet

getStylesheet

public Templates getStylesheet(File stylesheet)
Method getStylesheet. Returns a precompiled stylesheet.

Parameters:
stylesheet - A File with the stylesheet code
Returns:
Templates The precompiled Stylesheet

getStylesheet

public Templates getStylesheet(Source stylesheet)
Method getStylesheet. Returns a precompiled stylesheet.

Parameters:
stylesheet - A StreamSource
Returns:
Templates The precompiled Stylesheet

getTransformerHandler

public TransformerHandler getTransformerHandler(Templates stylesheet)
Method getTransformerHandler. Returns a TransformerHandler for the given Template.

Parameters:
stylesheet -
Returns:
TransformerHandler

setParameters

public static void setParameters(TransformerHandler handler,
                                 Map parameters)
Method setParameters. Set some parameters which can be used by the Stylesheet for the transformation.

Parameters:
handler -
parameters -

setParameters

public static void setParameters(Transformer transformer,
                                 Map parameters)
Method setParameters. Set some parameters which can be used by the Stylesheet for the transformation.


transform

public void transform(Document in,
                      TransformerHandler handler,
                      OutputStream out)
Method transform. Transforms a JDOM-Document to the given OutputStream

Parameters:
in -
handler -
out -

transform

public static Document transform(Document in,
                                 String stylesheet)
Method transform. Transforms a JDOM-Document in with a given stylesheet to a new document.

Parameters:
in - A JDOM-Document.
stylesheet - The Filename with complete path (this is not a servlet!) of the stylesheet.
Returns:
Document The new document or null, if an exception was thrown.

transform

public static Document transform(Document in,
                                 String stylesheet,
                                 Map parameters)
Method transform. Transforms a JDOM-Document in with a given stylesheet to a new document.

Parameters:
in - A JDOM-Document.
stylesheet - The Filename with complete path (this is not a servlet!) of the stylesheet.
parameters - parameters used by the stylesheet for transformation
Returns:
Document The new document or null, if an exception was thrown.

transform

public static Document transform(Document in,
                                 Source stylesheet,
                                 Map parameters)
Method transform. Transforms a JDOM-Document in with a given stylesheet to a new document.

Parameters:
in - A JDOM-Document.
stylesheet - The Filename with complete path (this is not a servlet!) of the stylesheet.
parameters - parameters used by the stylesheet for transformation
Returns:
Document The new document or null, if an exception was thrown.

transform

public static Document transform(Document in,
                                 Transformer transformer)
                          throws TransformerException
transforms a jdom Document via XSLT.

Parameters:
in - Document input
transformer - Transformer handling the transformation process
Returns:
the transformation result as jdom Document
Throws:
TransformerException - if transformation fails