View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/devgui/XSLTransformer.java,v 1.11 2004/08/18 12:25:57 hkollmann Exp $
3    * $Revision: 1.11 $
4    * $Date: 2004/08/18 12:25:57 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.devgui;
25  
26  import org.dbforms.util.Util;
27  
28  import java.io.File;
29  import java.io.FileNotFoundException;
30  
31  // Imported java classes
32  import java.io.FileOutputStream;
33  import java.io.IOException;
34  
35  import javax.xml.transform.OutputKeys;
36  import javax.xml.transform.Transformer;
37  import javax.xml.transform.TransformerConfigurationException;
38  import javax.xml.transform.TransformerException;
39  
40  // Imported TraX classes
41  import javax.xml.transform.TransformerFactory;
42  import javax.xml.transform.stream.StreamResult;
43  import javax.xml.transform.stream.StreamSource;
44  
45  
46  
47  /***
48   * Simple sample code to show how to run the XSL processor from the API.
49   */
50  public class XSLTransformer {
51     /***
52      * DOCUMENT ME!
53      *
54      * @param xmlFile DOCUMENT ME!
55      * @param xslFile DOCUMENT ME!
56      * @param destinationFile DOCUMENT ME!
57      * @param useJsCalendar DOCUMENT ME!
58      * @param xsltEncoding DOCUMENT ME!
59      *
60      * @throws TransformerException DOCUMENT ME!
61      * @throws TransformerConfigurationException DOCUMENT ME!
62      * @throws FileNotFoundException DOCUMENT ME!
63      * @throws IOException DOCUMENT ME!
64      */
65     public static void transform(File    xmlFile,
66                                  File    xslFile,
67                                  File    destinationFile,
68                                  boolean useJsCalendar,
69                                  String  xsltEncoding)
70                           throws TransformerException, 
71                                  TransformerConfigurationException, 
72                                  FileNotFoundException, IOException {
73        // Use the static TransformerFactory.newInstance() method to instantiate 
74        // a TransformerFactory. The javax.xml.transform.TransformerFactory 
75        // system property setting determines the actual class to instantiate --
76        // org.apache.xalan.transformer.TransformerImpl.
77        TransformerFactory tFactory = TransformerFactory.newInstance();
78  
79        // Use the TransformerFactory to instantiate a Transformer that will work with  
80        // the stylesheet you specify. This method call also processes the stylesheet
81        // into a compiled Templates object.
82        Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));
83  
84        // Setting the XSLT encoding configured in the XSL Transformation Panel
85        if (!Util.isNull(xsltEncoding)) {
86           transformer.setOutputProperty(OutputKeys.ENCODING, xsltEncoding);
87        }
88  
89        // If user has checked checkbox to use JavaScript Calendar for editing of
90        // date fields, we have to pass a corresponding parameter to transformer
91        if (useJsCalendar) {
92           transformer.setParameter("useCalendar", "true");
93        }
94  
95        // Use the Transformer to apply the associated Templates object to an XML document
96        // (foo.xml) and write the output to a file (foo.out).
97        transformer.transform(new StreamSource(xmlFile),
98                              new StreamResult(new FileOutputStream(destinationFile)));
99     }
100 }