Class MCRXSLTransformation

java.lang.Object
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 Details

    • MCRXSLTransformation

      public MCRXSLTransformation()
  • Method Details

    • 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.
      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.
    • 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
    • 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