+++ /dev/null
-/*
- * $Id: TraxTransform.java,v 1.5 2003/03/07 10:09:31 jeremias Exp $
- * ============================================================================
- * The Apache Software License, Version 1.1
- * ============================================================================
- *
- * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modifica-
- * tion, 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 following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. The end-user documentation included with the redistribution, if any, must
- * include the following acknowledgment: "This product includes software
- * developed by the Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself, if
- * and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "FOP" and "Apache Software Foundation" must not be used to
- * endorse or promote products derived from this software without prior
- * written permission. For written permission, please contact
- * apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache", nor may
- * "Apache" appear in their name, without prior written permission of the
- * Apache Software Foundation.
- *
- * 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
- * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
- * DING, 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 Apache Software Foundation and was originally created by
- * James Tauber <jtauber@jtauber.com>. For more information on the Apache
- * Software Foundation, please see <http://www.apache.org/>.
- */
-package org.apache.fop.tools.xslt;
-
-import java.io.InputStream;
-import java.io.Writer;
-import java.util.Map;
-
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Templates;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-
-import org.w3c.dom.Document;
-
-/**
- * Handles xslt tranformations via Trax (xalan2)
- */
-public class TraxTransform {
-
- /**
- * Cache of compiled stylesheets (filename, StylesheetRoot)
- */
- private static Map stylesheetCache = new java.util.Hashtable();
-
- /**
- * Gets a Trax transformer
- * @param xsltFilename Filename of the XSLT file
- * @param cache True, if caching of the stylesheet is allowed
- * @return Transformer the Trax transformer
- */
- public static Transformer getTransformer(String xsltFilename,
- boolean cache) {
- try {
- if (cache && stylesheetCache.containsKey(xsltFilename)) {
- Templates cachedStylesheet =
- (Templates)stylesheetCache.get(xsltFilename);
- return cachedStylesheet.newTransformer();
- }
-
- Source xslSheet =
- new javax.xml.transform.stream.StreamSource(xsltFilename);
-
-
- /*
- * System.out.println("****************************");
- * System.out.println("trax compile \nin: " + xsltFilename);
- * System.out.println("****************************");
- */
- TransformerFactory factory = TransformerFactory.newInstance();
-
- Templates compiledSheet = factory.newTemplates(xslSheet);
- if (cache) {
- stylesheetCache.put(xsltFilename, compiledSheet);
- }
- return compiledSheet.newTransformer();
- } catch (TransformerConfigurationException ex) {
- ex.printStackTrace();
- }
- return null;
-
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Filename of the source XML file
- * @param xslURL Filename of the XSLT filename
- * @param outputFile Target filename
- */
- public static void transform(String xmlSource, String xslURL,
- String outputFile) {
- transform(new javax.xml.transform.stream.StreamSource(xmlSource),
- new javax.xml.transform.stream.StreamSource(xslURL),
- new javax.xml.transform.stream.StreamResult(outputFile));
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Source DOM Document
- * @param xslURL Filename of the XSLT filename
- * @param outputFile Target filename
- */
- public static void transform(Document xmlSource, String xslURL,
- String outputFile) {
-
- transform(new javax.xml.transform.dom.DOMSource(xmlSource),
- new javax.xml.transform.stream.StreamSource(xslURL),
- new javax.xml.transform.stream.StreamResult(outputFile));
-
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Filename of the source XML file
- * @param xslURL Filename of the XSLT filename
- * @param output Target Writer instance
- */
- public static void transform(String xmlSource, String xslURL,
- Writer output) {
- transform(new javax.xml.transform.stream.StreamSource(xmlSource),
- new javax.xml.transform.stream.StreamSource(xslURL),
- new javax.xml.transform.stream.StreamResult(output));
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Source DOM Document
- * @param xsl Filename of the XSLT filename
- * @param outputDoc Target DOM document
- */
- public static void transform(Document xmlSource, InputStream xsl,
- Document outputDoc) {
- transform(new javax.xml.transform.dom.DOMSource(xmlSource),
- new javax.xml.transform.stream.StreamSource(xsl),
- new javax.xml.transform.dom.DOMResult(outputDoc));
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource XML Source
- * @param xslSource XSLT Source
- * @param result Target Result
- */
- public static void transform(Source xmlSource, Source xslSource,
- Result result) {
- try {
- Transformer transformer;
- if (xslSource.getSystemId() == null) {
- TransformerFactory factory = TransformerFactory.newInstance();
- transformer = factory.newTransformer(xslSource);
- } else {
- transformer = getTransformer(xslSource.getSystemId(), true);
- }
- transformer.transform(xmlSource, result);
- } catch (TransformerConfigurationException ex) {
- ex.printStackTrace();
- } catch (TransformerException ex) {
- ex.printStackTrace();
- }
-
- }
-
-}
+++ /dev/null
-/*
- * $Id: XSLTransform.java,v 1.5 2003/03/07 10:09:31 jeremias Exp $
- * ============================================================================
- * The Apache Software License, Version 1.1
- * ============================================================================
- *
- * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modifica-
- * tion, 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 following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. The end-user documentation included with the redistribution, if any, must
- * include the following acknowledgment: "This product includes software
- * developed by the Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself, if
- * and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "FOP" and "Apache Software Foundation" must not be used to
- * endorse or promote products derived from this software without prior
- * written permission. For written permission, please contact
- * apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache", nor may
- * "Apache" appear in their name, without prior written permission of the
- * Apache Software Foundation.
- *
- * 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
- * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
- * DING, 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 Apache Software Foundation and was originally created by
- * James Tauber <jtauber@jtauber.com>. For more information on the Apache
- * Software Foundation, please see <http://www.apache.org/>.
- */
-package org.apache.fop.tools.xslt;
-
-import java.io.InputStream;
-import java.io.Writer;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-/**
- * Class for transforming XML using XSLT. Wraps either Trax (JAXP) or Xalan 1.x.
- */
-public class XSLTransform {
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Filename of the source XML file
- * @param xslURL Filename of the XSLT filename
- * @param outputFile Target filename
- * @throws Exception If the conversion fails
- */
- public static void transform(String xmlSource, String xslURL,
- String outputFile) throws Exception {
- Class[] argTypes = {
- String.class, String.class, String.class
- };
- Object[] params = {
- xmlSource, xslURL, outputFile
- };
- transform(params, argTypes);
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Source DOM Document
- * @param xslURL Filename of the XSLT filename
- * @param outputFile Target filename
- * @throws Exception If the conversion fails
- */
- public static void transform(org.w3c.dom.Document xmlSource,
- String xslURL,
- String outputFile) throws Exception {
- Class[] argTypes = {
- org.w3c.dom.Document.class, String.class, String.class
- };
-
- Object[] params = {
- xmlSource, xslURL, outputFile
- };
- transform(params, argTypes);
-
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Filename of the source XML file
- * @param xslURL Filename of the XSLT filename
- * @param outputWriter Target Writer instance
- * @throws Exception If the conversion fails
- */
- public static void transform(String xmlSource, String xslURL,
- Writer outputWriter) throws Exception {
- Class[] argTypes = {
- String.class, String.class, Writer.class
- };
- Object[] params = {
- xmlSource, xslURL, outputWriter
- };
- transform(params, argTypes);
-
- }
-
- /**
- * Transforms an XML file using XSLT.
- * @param xmlSource Source DOM Document
- * @param xsl Filename of the XSLT filename
- * @param outputDoc Target DOM document
- * @throws Exception If the conversion fails
- */
- public static void transform(org.w3c.dom.Document xmlSource,
- InputStream xsl,
- org.w3c.dom.Document outputDoc) throws Exception {
- Class[] argTypes = {
- org.w3c.dom.Document.class, InputStream.class,
- org.w3c.dom.Document.class
- };
- Object[] params = {
- xmlSource, xsl, outputDoc
- };
- transform(params, argTypes);
-
- }
-
-
- private static void transform(Object[] args,
- Class[] argTypes) throws Exception {
- Class transformer = getTransformClass();
- if (transformer != null) {
- Method transformMethod = getTransformMethod(transformer,
- argTypes);
- if (transformMethod != null) {
- try {
- transformMethod.invoke(null, args);
- } catch (InvocationTargetException ex) {
- ex.printStackTrace();
- }
- } else {
- throw new Exception("transform method not found");
- }
- } else {
- throw new Exception("no transformer class found");
- }
-
- }
-
-
- private static Class getTransformClass() {
- try {
- // try trax first
- Class transformer =
- Class.forName("javax.xml.transform.Transformer");
- // ok, make sure we have a liaison to trax
- transformer =
- Class.forName("org.apache.fop.tools.xslt.TraxTransform");
- return transformer;
-
- } catch (ClassNotFoundException ex) {
- //nop
- }
- // otherwise, try regular xalan1
- try {
- Class transformer =
- Class.forName("org.apache.xalan.xslt.XSLTProcessor");
- // get the liaison
- transformer =
- Class.forName("org.apache.fop.tools.xslt.Xalan1Transform");
- return transformer;
- } catch (ClassNotFoundException ex) {
- //nop
- }
- return null;
-
- }
-
-
- private static Method getTransformMethod(Class c, Class[] argTypes) {
- // System.out.println("transformer class = "+c);
-
- try {
- // Class[] argTypes = new Class[args.length];
- for (int i = 0; i < argTypes.length; i++) {
- // argTypes[i] = args[i].getClass();
- // System.out.println("arg["+i+"] type = "+argTypes[i]);
-
- }
-
- Method transformer = c.getMethod("transform", argTypes);
- return transformer;
-
- } catch (NoSuchMethodException ex) {
- ex.printStackTrace();
-
- }
- return null;
- }
-
-}