All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdom2.transform.XSLTransformer Maven / Gradle / Ivy

Go to download

A complete, Java-based solution for accessing, manipulating, and outputting XML data

The newest version!
/*--

 Copyright (C) 2001-2007 Jason Hunter & Brett McLaughlin.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows
    these conditions in the documentation and/or other materials
    provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact .

 4. Products derived from this software may not be called "JDOM", nor
    may "JDOM" appear in their name, without prior written permission
    from the JDOM Project Management .

 In addition, we request (but do not require) that you include in the
 end-user documentation provided with the redistribution and/or in the
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by the
      JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many
 individuals on behalf of the JDOM Project and was originally
 created by Jason Hunter  and
 Brett McLaughlin .  For more information
 on the JDOM Project, please see .

 */

package org.jdom2.transform;

import java.util.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import org.jdom2.*;
import org.xml.sax.EntityResolver;

/**
 * A convenience class to handle simple transformations. The JAXP TrAX classes
 * have more bells and whistles and can be used with {@link JDOMSource} and
 * {@link JDOMResult} for advanced uses. This class handles the common case and
 * presents a simple interface.  XSLTransformer is thread safe and may be
 * used from multiple threads.
 *
 * 

 * XSLTransformer transformer = new XSLTransformer("file.xsl");
 *
 * Document x2 = transformer.transform(x);  // x is a Document
 * Document y2 = transformer.transform(y);  // y is a Document
 * 
* * JDOM relies on TrAX to perform the transformation. * The javax.xml.transform.TransformerFactory Java system property * determines which XSLT engine TrAX uses. Its value should be * the fully qualified name of the implementation of the abstract * javax.xml.transform.TransformerFactory class. * Values of this property for popular XSLT processors include: *

*
  • Saxon 6.x: com.icl.saxon.TransformerFactoryImpl
  • *
  • Saxon 7.x: net.sf.saxon.TransformerFactoryImpl
  • *
  • Xalan: org.apache.xalan.processor.TransformerFactoryImpl
  • *
  • jd.xslt: jd.xml.xslt.trax.TransformerFactoryImpl
  • *
  • Oracle: oracle.xml.jaxp.JXSAXTransformerFactory
  • *
*

* This property can be set in all the usual ways a Java system property * can be set. TrAX picks from them in this order:

*
    *
  1. Invoking System.setProperty( "javax.xml.transform.TransformerFactory", * "classname")
  2. *
  3. The value specified at the command line using the * -Djavax.xml.transform.TransformerFactory=classname * option to the java interpreter
  4. *
  5. The class named in the lib/jaxp.properties properties file * in the JRE directory, in a line like this one: *
    javax.xml.parsers.DocumentBuilderFactory=classname
  6. *
  7. The class named in the * META-INF/services/javax.xml.transform.TransformerFactory file * in the JAR archives available to the runtime
  8. *
  9. Finally, if all of the above options fail, * a default implementation is chosen. In Sun's JDK 1.4, this is * Xalan 2.2d10.
  10. *
* @author Jason Hunter * @author Elliotte Rusty Harold */ public class XSLTransformer { private Templates templates; /** * The custom JDOM factory to use when building the transformation * result or null to use the default JDOM classes. */ private JDOMFactory factory = null; // Internal constructor to support the other constructors private XSLTransformer(Source stylesheet) throws XSLTransformException { try { templates = TransformerFactory.newInstance() .newTemplates(stylesheet); } catch (TransformerException e) { throw new XSLTransformException("Could not construct XSLTransformer", e); } } /** * Creates a transformer for a given stylesheet system id. * * @param stylesheetSystemId source stylesheet as a Source object * @throws XSLTransformException if there's a problem in the TrAX back-end */ public XSLTransformer(String stylesheetSystemId) throws XSLTransformException { this(new StreamSource(stylesheetSystemId)); } /** *

* This will create a new XSLTransformer by * reading the stylesheet from the specified * InputStream. *

* * @param stylesheet InputStream from which the stylesheet is read. * @throws XSLTransformException when an IOException, format error, or * something else prevents the stylesheet from being compiled */ public XSLTransformer(InputStream stylesheet) throws XSLTransformException { this(new StreamSource(stylesheet)); } /** *

* This will create a new XSLTransformer by * reading the stylesheet from the specified * Reader. *

* * @param stylesheet Reader from which the stylesheet is read. * @throws XSLTransformException when an IOException, format error, or * something else prevents the stylesheet from being compiled */ public XSLTransformer(Reader stylesheet) throws XSLTransformException { this(new StreamSource(stylesheet)); } /** *

* This will create a new XSLTransformer by * reading the stylesheet from the specified * File. *

* * @param stylesheet File from which the stylesheet is read. * @throws XSLTransformException when an IOException, format error, or * something else prevents the stylesheet from being compiled */ public XSLTransformer(File stylesheet) throws XSLTransformException { this(new StreamSource(stylesheet)); } /** *

* This will create a new XSLTransformer by * reading the stylesheet from the specified * Document. *

* * @param stylesheet Document containing the stylesheet. * @throws XSLTransformException when the supplied Document * is not syntactically correct XSLT */ public XSLTransformer(Document stylesheet) throws XSLTransformException { this(new JDOMSource(stylesheet)); } /** * Transforms the given input nodes to a list of output nodes. * * @param inputNodes input nodes * @return transformed output nodes * @throws XSLTransformException if there's a problem in the transformation */ public List transform(List inputNodes) throws XSLTransformException { JDOMSource source = new JDOMSource(inputNodes); JDOMResult result = new JDOMResult(); result.setFactory(factory); // null ok try { templates.newTransformer().transform(source, result); return result.getResult(); } catch (TransformerException e) { throw new XSLTransformException("Could not perform transformation", e); } } /** * Transforms the given document to an output document. * * @param inputDoc input document * @return transformed output document * @throws XSLTransformException if there's a problem in the transformation */ public Document transform(Document inputDoc) throws XSLTransformException { return transform(inputDoc, null); } /** * Transforms the given document to an output document. * * @param inputDoc input document * @param resolver entity resolver for the input document * @return transformed output document * @throws XSLTransformException if there's a problem in the transformation */ public Document transform(Document inputDoc, EntityResolver resolver) throws XSLTransformException { JDOMSource source = new JDOMSource(inputDoc, resolver); JDOMResult result = new JDOMResult(); result.setFactory(factory); // null ok try { templates.newTransformer().transform(source, result); return result.getDocument(); } catch (TransformerException e) { throw new XSLTransformException("Could not perform transformation", e); } } /** * Sets a custom JDOMFactory to use when building the * transformation result. Use a custom factory to build the tree * with your own subclasses of the JDOM classes. * * @param factory the custom JDOMFactory to use or * null to use the default JDOM * classes. * * @see #getFactory */ public void setFactory(JDOMFactory factory) { this.factory = factory; } /** * Returns the custom JDOMFactory used to build the transformation * result. * * @return the custom JDOMFactory used to build the * transformation result or null if the * default JDOM classes are being used. * * @see #setFactory */ public JDOMFactory getFactory() { return this.factory; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy