001: /*--
002:
003: $Id: XSLTransformer.java,v 1.1 2005/04/27 09:32:43 wittek Exp $
004:
005: Copyright (C) 2001-2004 Jason Hunter & Brett McLaughlin.
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions
010: are met:
011:
012: 1. Redistributions of source code must retain the above copyright
013: notice, this list of conditions, and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright
016: notice, this list of conditions, and the disclaimer that follows
017: these conditions in the documentation and/or other materials
018: provided with the distribution.
019:
020: 3. The name "JDOM" must not be used to endorse or promote products
021: derived from this software without prior written permission. For
022: written permission, please contact <request_AT_jdom_DOT_org>.
023:
024: 4. Products derived from this software may not be called "JDOM", nor
025: may "JDOM" appear in their name, without prior written permission
026: from the JDOM Project Management <request_AT_jdom_DOT_org>.
027:
028: In addition, we request (but do not require) that you include in the
029: end-user documentation provided with the redistribution and/or in the
030: software itself an acknowledgement equivalent to the following:
031: "This product includes software developed by the
032: JDOM Project (http://www.jdom.org/)."
033: Alternatively, the acknowledgment may be graphical using the logos
034: available at http://www.jdom.org/images/logos.
035:
036: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
040: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: SUCH DAMAGE.
048:
049: This software consists of voluntary contributions made by many
050: individuals on behalf of the JDOM Project and was originally
051: created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
052: Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
053: on the JDOM Project, please see <http://www.jdom.org/>.
054:
055: */
056:
057: package org.jdom.transform;
058:
059: import java.util.*;
060: import java.io.*;
061: import javax.xml.transform.*;
062: import javax.xml.transform.stream.StreamSource;
063: import org.jdom.*;
064:
065: /**
066: * A convenience class to handle simple transformations. The JAXP TrAX classes
067: * have more bells and whistles and can be used with {@link JDOMSource} and
068: * {@link JDOMResult} for advanced uses. This class handles the common case and
069: * presents a simple interface. XSLTransformer is thread safe and may be
070: * used from multiple threads.
071: *
072: * <pre><code>
073: * XSLTransformer transformer = new XSLTransformer("file.xsl");
074: *
075: * Document x2 = transformer.transform(x); // x is a Document
076: * Document y2 = transformer.transform(y); // y is a Document
077: * </code></pre>
078: *
079: * JDOM relies on TrAX to perform the transformation.
080: * The <code>javax.xml.transform.TransformerFactory</code> Java system property
081: * determines which XSLT engine TrAX uses. Its value should be
082: * the fully qualified name of the implementation of the abstract
083: * <code>javax.xml.transform.TransformerFactory</code> class.
084: * Values of this property for popular XSLT processors include:
085: * </p>
086: * <ul><li>Saxon 6.x: <code>com.icl.saxon.TransformerFactoryImpl</code></li>
087: * <li>Saxon 7.x: <code>net.sf.saxon.TransformerFactoryImpl</code></li>
088: * <li>Xalan: <code>org.apache.xalan.processor.TransformerFactoryImpl</code></li>
089: * <li>jd.xslt: <code>jd.xml.xslt.trax.TransformerFactoryImpl</code></li>
090: * <li>Oracle: <code>oracle.xml.jaxp.JXSAXTransformerFactory</code></li>
091: * </ul>
092: * <p>
093: * This property can be set in all the usual ways a Java system property
094: * can be set. TrAX picks from them in this order:</p>
095: * <ol>
096: * <li> Invoking <code>System.setProperty( "javax.xml.transform.TransformerFactory",
097: * "<i><code>classname</code></i>")</code></li>
098: * <li>The value specified at the command line using the
099: * <tt>-Djavax.xml.transform.TransformerFactory=<i><code>classname</code></i></tt>
100: * option to the <b>java</b> interpreter</li>
101: * <li>The class named in the <code>lib/jaxp.properties</code> properties file
102: * in the JRE directory, in a line like this one:
103: * <pre>javax.xml.parsers.DocumentBuilderFactory=<i><code>classname</code></i></pre></li>
104: * <li>The class named in the
105: * <code>META-INF/services/javax.xml.transform.TransformerFactory</code> file
106: * in the JAR archives available to the runtime</li>
107: * <li>Finally, if all of the above options fail,
108: * a default implementation is chosen. In Sun's JDK 1.4, this is
109: * Xalan 2.2d10. </li>
110: * </ol>
111:
112: * @version $Revision: 1.1 $, $Date: 2005/04/27 09:32:43 $
113: * @author Jason Hunter
114: * @author Elliotte Rusty Harold
115: */
116: public class XSLTransformer {
117:
118: private static final String CVS_ID = "@(#) $RCSfile: XSLTransformer.java,v $ $Revision: 1.1 $ $Date: 2005/04/27 09:32:43 $ $Name: $";
119:
120: private Templates templates;
121:
122: // Internal constructor to support the other constructors
123: private XSLTransformer(Source stylesheet)
124: throws XSLTransformException {
125: try {
126: templates = TransformerFactory.newInstance().newTemplates(
127: stylesheet);
128: } catch (TransformerException e) {
129: throw new XSLTransformException(
130: "Could not construct XSLTransformer", e);
131: }
132: }
133:
134: /**
135: * Creates a transformer for a given stylesheet system id.
136: *
137: * @param stylesheetSystemId source stylesheet as a Source object
138: * @throws XSLTransformException if there's a problem in the TrAX back-end
139: */
140: public XSLTransformer(String stylesheetSystemId)
141: throws XSLTransformException {
142: this (new StreamSource(stylesheetSystemId));
143: }
144:
145: /**
146: * <p>
147: * This will create a new <code>XSLTransformer</code> by
148: * reading the stylesheet from the specified
149: * <code>InputStream</code>.
150: * </p>
151: *
152: * @param stylesheet <code>InputStream</code> from which the stylesheet is read.
153: * @throws XSLTransformException when an IOException, format error, or
154: * something else prevents the stylesheet from being compiled
155: */
156: public XSLTransformer(InputStream stylesheet)
157: throws XSLTransformException {
158: this (new StreamSource(stylesheet));
159: }
160:
161: /**
162: * <p>
163: * This will create a new <code>XSLTransformer</code> by
164: * reading the stylesheet from the specified
165: * <code>Reader</code>.
166: * </p>
167: *
168: * @param stylesheet <code>Reader</code> from which the stylesheet is read.
169: * @throws XSLTransformException when an IOException, format error, or
170: * something else prevents the stylesheet from being compiled
171: */
172: public XSLTransformer(Reader stylesheet)
173: throws XSLTransformException {
174: this (new StreamSource(stylesheet));
175: }
176:
177: /**
178: * <p>
179: * This will create a new <code>XSLTransformer</code> by
180: * reading the stylesheet from the specified
181: * <code>File</code>.
182: * </p>
183: *
184: * @param stylesheet <code>File</code> from which the stylesheet is read.
185: * @throws XSLTransformException when an IOException, format error, or
186: * something else prevents the stylesheet from being compiled
187: */
188: public XSLTransformer(File stylesheet) throws XSLTransformException {
189: this (new StreamSource(stylesheet));
190: }
191:
192: /**
193: * <p>
194: * This will create a new <code>XSLTransformer</code> by
195: * reading the stylesheet from the specified
196: * <code>Document</code>.
197: * </p>
198: *
199: * @param stylesheet <code>Document</code> containing the stylesheet.
200: * @throws XSLTransformException when the supplied <code>Document</code>
201: * is not syntactically correct XSLT
202: */
203: public XSLTransformer(Document stylesheet)
204: throws XSLTransformException {
205: this (new JDOMSource(stylesheet));
206: }
207:
208: /**
209: * Transforms the given input nodes to a list of output nodes.
210: *
211: * @param inputNodes input nodes
212: * @return transformed output nodes
213: * @throws XSLTransformException if there's a problem in the transformation
214: */
215: public List transform(List inputNodes) throws XSLTransformException {
216: JDOMSource source = new JDOMSource(inputNodes);
217: JDOMResult result = new JDOMResult();
218: try {
219: templates.newTransformer().transform(source, result);
220: return result.getResult();
221: } catch (TransformerException e) {
222: throw new XSLTransformException(
223: "Could not perform transformation", e);
224: }
225: }
226:
227: /**
228: * Transforms the given document to an output document.
229: *
230: * @param inputDoc input document
231: * @return transformed output document
232: * @throws XSLTransformException if there's a problem in the transformation
233: */
234: public Document transform(Document inputDoc)
235: throws XSLTransformException {
236: JDOMSource source = new JDOMSource(inputDoc);
237: JDOMResult result = new JDOMResult();
238: try {
239: templates.newTransformer().transform(source, result);
240: return result.getDocument();
241: } catch (TransformerException e) {
242: throw new XSLTransformException(
243: "Could not perform transformation", e);
244: }
245: }
246: }
|