Package ptolemy.util

Class XSLTUtilities


  • public class XSLTUtilities
    extends java.lang.Object
    A collection of utilities for manipulating strings using XSLT. These utilities do not depend on any other ptolemy.* packages.

    This file uses Saxon, the XSLT and XQuery Processor http://saxon.sourceforge.net.

    Between Java 1.4.x and Java 1.5, Xalan was removed from the jar files that are shipped. Since Caltrop uses Saxon anyway, we now use Saxon here as well.

    Since:
    Ptolemy II 2.1
    Version:
    $Id$
    Author:
    Christopher Hylands, Haiyang Zheng
    Pt.AcceptedRating:
    Yellow (cxh)
    Pt.ProposedRating:
    Green (eal) Pending Java 1.5 changes
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void main​(java.lang.String[] args)
      Apply XSL transforms to an input file and generate an output file.
      static org.w3c.dom.Document parse​(java.lang.String filename)
      Parse an XML document using Saxon.
      static void setExportDTD​(boolean exportDTD)
      Set the flag indicating whether to export DTD specification when transforming XML files.
      static java.lang.String toString​(org.w3c.dom.Document document)
      Given a Document, generate a String.
      static void transform​(java.lang.String input, java.io.FileWriter fileWriter, java.util.List xslFileNames)
      Transform a file by applying a list of XSL transforms.
      static org.w3c.dom.Document transform​(org.w3c.dom.Document inputDocument, java.lang.String xslFileName)
      Transform a document.
      static org.w3c.dom.Document transform​(org.w3c.dom.Document inputDocument, java.util.List xslFileNames)
      Transform a document by applying a list of transforms.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        Apply XSL transforms to an input file and generate an output file.

        Example use:

          java -classpath $PTII ptolemy.util.XSLTUtilities
               $PTII/ptolemy/hsif/demo/SwimmingPool/SwimmingPool.xml \
               $PTII/ptolemy/hsif/xsl/GlobalVariablePreprocessor.xsl \
               exportMoMLDTD \
               /tmp/SwimmingPool_1.xml
         
        Parameters:
        args - At least three arguments:
        • The first argument is the input file name.
        • The second through n-1 arguments are the named xsl files.
        • The final argument is the output file name.
        Throws:
        java.lang.Exception - If there are problems with the transform.
      • parse

        public static org.w3c.dom.Document parse​(java.lang.String filename)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 java.io.IOException
        Parse an XML document using Saxon.
        Parameters:
        filename - The file name of the xml file to be read in The filename is passed to org.xml.sax.InputSource(String), so it may be a file name or a URL.
        Returns:
        the parsed document.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If there is a problem creating the DocumentBuilder.
        java.io.IOException - If the filename could not be parsed.
      • setExportDTD

        public static void setExportDTD​(boolean exportDTD)
        Set the flag indicating whether to export DTD specification when transforming XML files. By default, the transformer does not export DTD.
        Parameters:
        exportDTD - True for export DTD, false for not.
      • toString

        public static java.lang.String toString​(org.w3c.dom.Document document)
                                         throws javax.xml.transform.TransformerException,
                                                java.io.IOException
        Given a Document, generate a String.
        Parameters:
        document - The document to be converted to a string.
        Returns:
        A string representation of the Document.
        Throws:
        javax.xml.transform.TransformerException - If there is a a problem creating a new Transformer or parser.
        java.io.IOException - If there is a problem closing the output stream.
      • transform

        public static org.w3c.dom.Document transform​(org.w3c.dom.Document inputDocument,
                                                     java.lang.String xslFileName)
                                              throws javax.xml.transform.TransformerException,
                                                     java.io.IOException
        Transform a document.
        Parameters:
        inputDocument - The Document to be transformed
        xslFileName - The file name of the xsl file to be used. If the file cannot be found, then we look up the file in the classpath.
        Returns:
        a transformed document
        Throws:
        javax.xml.transform.TransformerException - If there is a problem with the transform.
        java.io.IOException - If there is a problem finding the transform file.
      • transform

        public static org.w3c.dom.Document transform​(org.w3c.dom.Document inputDocument,
                                                     java.util.List xslFileNames)
                                              throws javax.xml.transform.TransformerException,
                                                     java.io.IOException
        Transform a document by applying a list of transforms.
        Parameters:
        inputDocument - The Document to be transformed
        xslFileNames - A list of Strings naming the xsl files to be applied sequentially.
        Returns:
        a transformed document
        Throws:
        javax.xml.transform.TransformerException - If there is a a problem creating a new Transformer or parser.
        java.io.IOException - If there is a problem closing the output stream.
      • transform

        public static void transform​(java.lang.String input,
                                     java.io.FileWriter fileWriter,
                                     java.util.List xslFileNames)
                              throws javax.xml.parsers.ParserConfigurationException,
                                     javax.xml.transform.TransformerException,
                                     java.io.IOException
        Transform a file by applying a list of XSL transforms.
        Parameters:
        input - The XML to be transformed
        fileWriter - A FileWriter that will write to the MoML file. The caller of this method is responsible for closing the the FileWriter.
        xslFileNames - A list of Strings naming the xsl files to be applied sequentially.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If there is a problem creating the DocumentBuilder.
        javax.xml.transform.TransformerException - If there is a a problem with the transform.
        java.io.IOException - If there is a problem finding a transform file or applying a transform.