286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: Process.java,v 1.2.4.2 2005/09/15 18:21:57 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xalan.internal.xslt;
286N/A
286N/Aimport java.io.FileOutputStream;
286N/Aimport java.io.FileWriter;
286N/Aimport java.io.PrintWriter;
286N/Aimport java.io.StringReader;
286N/Aimport java.util.Properties;
286N/Aimport java.util.ResourceBundle;
286N/Aimport java.util.Vector;
286N/A
286N/Aimport javax.xml.XMLConstants;
286N/Aimport javax.xml.parsers.DocumentBuilder;
286N/Aimport javax.xml.parsers.DocumentBuilderFactory;
286N/Aimport javax.xml.parsers.ParserConfigurationException;
286N/Aimport javax.xml.transform.OutputKeys;
286N/Aimport javax.xml.transform.Source;
286N/Aimport javax.xml.transform.Templates;
286N/Aimport javax.xml.transform.Transformer;
286N/Aimport javax.xml.transform.TransformerConfigurationException;
286N/Aimport javax.xml.transform.TransformerException;
286N/Aimport javax.xml.transform.TransformerFactory;
286N/Aimport javax.xml.transform.TransformerFactoryConfigurationError;
286N/Aimport javax.xml.transform.URIResolver;
286N/Aimport javax.xml.transform.dom.DOMResult;
286N/Aimport javax.xml.transform.dom.DOMSource;
286N/Aimport javax.xml.transform.sax.SAXResult;
286N/Aimport javax.xml.transform.sax.SAXSource;
286N/Aimport javax.xml.transform.sax.SAXTransformerFactory;
286N/Aimport javax.xml.transform.sax.TransformerHandler;
286N/Aimport javax.xml.transform.stream.StreamResult;
286N/Aimport javax.xml.transform.stream.StreamSource;
286N/A
286N/Aimport com.sun.org.apache.xalan.internal.Version;
286N/Aimport com.sun.org.apache.xalan.internal.res.XSLMessages;
286N/Aimport com.sun.org.apache.xalan.internal.res.XSLTErrorResources;
286N/Aimport com.sun.org.apache.xalan.internal.utils.ObjectFactory;
286N/Aimport com.sun.org.apache.xalan.internal.utils.ConfigurationError;
524N/Aimport com.sun.org.apache.xalan.internal.utils.SecuritySupport;
286N/A
286N/A//J2SE does not support Xalan interpretive
286N/A/*
286N/Aimport com.sun.org.apache.xalan.internal.trace.PrintTraceListener;
286N/Aimport com.sun.org.apache.xalan.internal.trace.TraceManager;
286N/Aimport com.sun.org.apache.xalan.internal.transformer.XalanProperties;
286N/A*/
286N/A
286N/Aimport com.sun.org.apache.xml.internal.utils.DefaultErrorHandler;
286N/A
286N/Aimport org.w3c.dom.Document;
286N/Aimport org.w3c.dom.Node;
286N/A
286N/Aimport org.xml.sax.ContentHandler;
286N/Aimport org.xml.sax.EntityResolver;
286N/Aimport org.xml.sax.InputSource;
286N/Aimport org.xml.sax.XMLReader;
286N/Aimport org.xml.sax.helpers.XMLReaderFactory;
286N/A
286N/A/**
286N/A * The main() method handles the Xalan command-line interface.
286N/A * @xsl.usage general
286N/A */
286N/Apublic class Process
286N/A{
286N/A /**
286N/A * Prints argument options.
286N/A *
286N/A * @param resbundle Resource bundle
286N/A */
286N/A protected static void printArgOptions(ResourceBundle resbundle)
286N/A {
286N/A System.out.println(resbundle.getString("xslProc_option")); //"xslproc options: ");
286N/A System.out.println("\n\t\t\t" + resbundle.getString("xslProc_common_options") + "\n");
286N/A System.out.println(resbundle.getString("optionXSLTC")); //" [-XSLTC (use XSLTC for transformation)]
286N/A System.out.println(resbundle.getString("optionIN")); //" [-IN inputXMLURL]");
286N/A System.out.println(resbundle.getString("optionXSL")); //" [-XSL XSLTransformationURL]");
286N/A System.out.println(resbundle.getString("optionOUT")); //" [-OUT outputFileName]");
286N/A
286N/A // System.out.println(resbundle.getString("optionE")); //" [-E (Do not expand entity refs)]");
286N/A System.out.println(resbundle.getString("optionV")); //" [-V (Version info)]");
286N/A
286N/A // System.out.println(resbundle.getString("optionVALIDATE")); //" [-VALIDATE (Set whether validation occurs. Validation is off by default.)]");
286N/A System.out.println(resbundle.getString("optionEDUMP")); //" [-EDUMP {optional filename} (Do stackdump on error.)]");
286N/A System.out.println(resbundle.getString("optionXML")); //" [-XML (Use XML formatter and add XML header.)]");
286N/A System.out.println(resbundle.getString("optionTEXT")); //" [-TEXT (Use simple Text formatter.)]");
286N/A System.out.println(resbundle.getString("optionHTML")); //" [-HTML (Use HTML formatter.)]");
286N/A System.out.println(resbundle.getString("optionPARAM")); //" [-PARAM name expression (Set a stylesheet parameter)]");
286N/A
286N/A System.out.println(resbundle.getString("optionMEDIA"));
286N/A System.out.println(resbundle.getString("optionFLAVOR"));
286N/A System.out.println(resbundle.getString("optionDIAG"));
286N/A System.out.println(resbundle.getString("optionURIRESOLVER")); //" [-URIRESOLVER full class name (URIResolver to be used to resolve URIs)]");
286N/A System.out.println(resbundle.getString("optionENTITYRESOLVER")); //" [-ENTITYRESOLVER full class name (EntityResolver to be used to resolve entities)]");
286N/A waitForReturnKey(resbundle);
286N/A System.out.println(resbundle.getString("optionCONTENTHANDLER")); //" [-CONTENTHANDLER full class name (ContentHandler to be used to serialize output)]");
286N/A System.out.println(resbundle.getString("optionSECUREPROCESSING")); //" [-SECURE (set the secure processing feature to true)]");
286N/A
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xalan_options") + "\n");
286N/A
286N/A System.out.println(resbundle.getString("optionQC")); //" [-QC (Quiet Pattern Conflicts Warnings)]");
286N/A
286N/A // System.out.println(resbundle.getString("optionQ")); //" [-Q (Quiet Mode)]"); // sc 28-Feb-01 commented out
286N/A System.out.println(resbundle.getString("optionTT")); //" [-TT (Trace the templates as they are being called.)]");
286N/A System.out.println(resbundle.getString("optionTG")); //" [-TG (Trace each generation event.)]");
286N/A System.out.println(resbundle.getString("optionTS")); //" [-TS (Trace each selection event.)]");
286N/A System.out.println(resbundle.getString("optionTTC")); //" [-TTC (Trace the template children as they are being processed.)]");
286N/A System.out.println(resbundle.getString("optionTCLASS")); //" [-TCLASS (TraceListener class for trace extensions.)]");
286N/A System.out.println(resbundle.getString("optionLINENUMBERS")); //" [-L use line numbers]"
286N/A System.out.println(resbundle.getString("optionINCREMENTAL"));
286N/A System.out.println(resbundle.getString("optionNOOPTIMIMIZE"));
286N/A System.out.println(resbundle.getString("optionRL"));
286N/A */
286N/A
286N/A System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xsltc_options") + "\n");
286N/A System.out.println(resbundle.getString("optionXO"));
286N/A waitForReturnKey(resbundle);
286N/A System.out.println(resbundle.getString("optionXD"));
286N/A System.out.println(resbundle.getString("optionXJ"));
286N/A System.out.println(resbundle.getString("optionXP"));
286N/A System.out.println(resbundle.getString("optionXN"));
286N/A System.out.println(resbundle.getString("optionXX"));
286N/A System.out.println(resbundle.getString("optionXT"));
286N/A }
286N/A
286N/A /**
286N/A * Command line interface to transform an XML document according to
286N/A * the instructions found in an XSL stylesheet.
286N/A * <p>The Process class provides basic functionality for
286N/A * performing transformations from the command line. To see a
286N/A * list of arguments supported, call with zero arguments.</p>
286N/A * <p>To set stylesheet parameters from the command line, use
286N/A * <code>-PARAM name expression</code>. If you want to set the
286N/A * parameter to a string value, simply pass the string value
286N/A * as-is, and it will be interpreted as a string. (Note: if
286N/A * the value has spaces in it, you may need to quote it depending
286N/A * on your shell environment).</p>
286N/A *
286N/A * @param argv Input parameters from command line
286N/A */
286N/A // J2SE does not support Xalan interpretive
286N/A // main -> _main
286N/A public static void _main(String argv[])
286N/A {
286N/A
286N/A // Runtime.getRuntime().traceMethodCalls(false); // turns Java tracing off
286N/A boolean doStackDumpOnError = false;
286N/A boolean setQuietMode = false;
286N/A boolean doDiag = false;
286N/A String msg = null;
286N/A boolean isSecureProcessing = false;
286N/A
286N/A // Runtime.getRuntime().traceMethodCalls(false);
286N/A // Runtime.getRuntime().traceInstructions(false);
286N/A
286N/A /**
286N/A * The default diagnostic writer...
286N/A */
286N/A java.io.PrintWriter diagnosticsWriter = new PrintWriter(System.err, true);
286N/A java.io.PrintWriter dumpWriter = diagnosticsWriter;
286N/A ResourceBundle resbundle =
524N/A (SecuritySupport.getResourceBundle(
286N/A com.sun.org.apache.xml.internal.utils.res.XResourceBundle.ERROR_RESOURCES));
286N/A String flavor = "s2s";
286N/A
286N/A if (argv.length < 1)
286N/A {
286N/A printArgOptions(resbundle);
286N/A }
286N/A else
286N/A {
286N/A // J2SE does not support Xalan interpretive
286N/A // false -> true
286N/A boolean useXSLTC = true;
286N/A for (int i = 0; i < argv.length; i++)
286N/A {
286N/A if ("-XSLTC".equalsIgnoreCase(argv[i]))
286N/A {
286N/A useXSLTC = true;
286N/A }
286N/A }
286N/A
286N/A TransformerFactory tfactory;
286N/A if (useXSLTC)
286N/A {
286N/A String key = "javax.xml.transform.TransformerFactory";
286N/A String value = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
286N/A Properties props = System.getProperties();
286N/A props.put(key, value);
286N/A System.setProperties(props);
286N/A }
286N/A
286N/A try
286N/A {
286N/A tfactory = TransformerFactory.newInstance();
286N/A tfactory.setErrorListener(new DefaultErrorHandler());
286N/A }
286N/A catch (TransformerFactoryConfigurationError pfe)
286N/A {
286N/A pfe.printStackTrace(dumpWriter);
286N/A// "XSL Process was not successful.");
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_NOT_SUCCESSFUL, null);
286N/A diagnosticsWriter.println(msg);
286N/A
286N/A tfactory = null; // shut up compiler
286N/A
286N/A doExit(msg);
286N/A }
286N/A
286N/A boolean formatOutput = false;
286N/A boolean useSourceLocation = false;
286N/A String inFileName = null;
286N/A String outFileName = null;
286N/A String dumpFileName = null;
286N/A String xslFileName = null;
286N/A String treedumpFileName = null;
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A PrintTraceListener tracer = null;
286N/A */
286N/A String outputType = null;
286N/A String media = null;
286N/A Vector params = new Vector();
286N/A boolean quietConflictWarnings = false;
286N/A URIResolver uriResolver = null;
286N/A EntityResolver entityResolver = null;
286N/A ContentHandler contentHandler = null;
286N/A int recursionLimit=-1;
286N/A
286N/A for (int i = 0; i < argv.length; i++)
286N/A {
286N/A if ("-XSLTC".equalsIgnoreCase(argv[i]))
286N/A {
286N/A // The -XSLTC option has been processed.
286N/A }
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A else if ("-TT".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A {
286N/A if (null == tracer)
286N/A tracer = new PrintTraceListener(diagnosticsWriter);
286N/A
286N/A tracer.m_traceTemplates = true;
286N/A }
286N/A else
286N/A printInvalidXSLTCOption("-TT");
286N/A
286N/A // tfactory.setTraceTemplates(true);
286N/A }
286N/A else if ("-TG".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A {
286N/A if (null == tracer)
286N/A tracer = new PrintTraceListener(diagnosticsWriter);
286N/A
286N/A tracer.m_traceGeneration = true;
286N/A }
286N/A else
286N/A printInvalidXSLTCOption("-TG");
286N/A
286N/A // tfactory.setTraceSelect(true);
286N/A }
286N/A else if ("-TS".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A {
286N/A if (null == tracer)
286N/A tracer = new PrintTraceListener(diagnosticsWriter);
286N/A
286N/A tracer.m_traceSelection = true;
286N/A }
286N/A else
286N/A printInvalidXSLTCOption("-TS");
286N/A
286N/A // tfactory.setTraceTemplates(true);
286N/A }
286N/A else if ("-TTC".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A {
286N/A if (null == tracer)
286N/A tracer = new PrintTraceListener(diagnosticsWriter);
286N/A
286N/A tracer.m_traceElements = true;
286N/A }
286N/A else
286N/A printInvalidXSLTCOption("-TTC");
286N/A
286N/A // tfactory.setTraceTemplateChildren(true);
286N/A }
286N/A */
286N/A else if ("-INDENT".equalsIgnoreCase(argv[i]))
286N/A {
286N/A int indentAmount;
286N/A
286N/A if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))
286N/A {
286N/A indentAmount = Integer.parseInt(argv[++i]);
286N/A }
286N/A else
286N/A {
286N/A indentAmount = 0;
286N/A }
286N/A
286N/A // TBD:
286N/A // xmlProcessorLiaison.setIndent(indentAmount);
286N/A }
286N/A else if ("-IN".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A inFileName = argv[++i];
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-IN" })); //"Missing argument for);
286N/A }
286N/A else if ("-MEDIA".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length)
286N/A media = argv[++i];
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-MEDIA" })); //"Missing argument for);
286N/A }
286N/A else if ("-OUT".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A outFileName = argv[++i];
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-OUT" })); //"Missing argument for);
286N/A }
286N/A else if ("-XSL".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A xslFileName = argv[++i];
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-XSL" })); //"Missing argument for);
286N/A }
286N/A else if ("-FLAVOR".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length)
286N/A {
286N/A flavor = argv[++i];
286N/A }
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-FLAVOR" })); //"Missing argument for);
286N/A }
286N/A else if ("-PARAM".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 2 < argv.length)
286N/A {
286N/A String name = argv[++i];
286N/A
286N/A params.addElement(name);
286N/A
286N/A String expression = argv[++i];
286N/A
286N/A params.addElement(expression);
286N/A }
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-PARAM" })); //"Missing argument for);
286N/A }
286N/A else if ("-E".equalsIgnoreCase(argv[i]))
286N/A {
286N/A
286N/A // TBD:
286N/A // xmlProcessorLiaison.setShouldExpandEntityRefs(false);
286N/A }
286N/A else if ("-V".equalsIgnoreCase(argv[i]))
286N/A {
286N/A diagnosticsWriter.println(resbundle.getString("version") //">>>>>>> Xalan Version "
286N/A + Version.getVersion() + ", " +
286N/A
286N/A /* xmlProcessorLiaison.getParserDescription()+ */
286N/A resbundle.getString("version2")); // "<<<<<<<");
286N/A }
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A else if ("-QC".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A quietConflictWarnings = true;
286N/A else
286N/A printInvalidXSLTCOption("-QC");
286N/A }
286N/A */
286N/A else if ("-Q".equalsIgnoreCase(argv[i]))
286N/A {
286N/A setQuietMode = true;
286N/A }
286N/A else if ("-DIAG".equalsIgnoreCase(argv[i]))
286N/A {
286N/A doDiag = true;
286N/A }
286N/A else if ("-XML".equalsIgnoreCase(argv[i]))
286N/A {
286N/A outputType = "xml";
286N/A }
286N/A else if ("-TEXT".equalsIgnoreCase(argv[i]))
286N/A {
286N/A outputType = "text";
286N/A }
286N/A else if ("-HTML".equalsIgnoreCase(argv[i]))
286N/A {
286N/A outputType = "html";
286N/A }
286N/A else if ("-EDUMP".equalsIgnoreCase(argv[i]))
286N/A {
286N/A doStackDumpOnError = true;
286N/A
286N/A if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))
286N/A {
286N/A dumpFileName = argv[++i];
286N/A }
286N/A }
286N/A else if ("-URIRESOLVER".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length)
286N/A {
286N/A try
286N/A {
286N/A uriResolver = (URIResolver) ObjectFactory.newInstance(argv[++i], true);
286N/A
286N/A tfactory.setURIResolver(uriResolver);
286N/A }
286N/A catch (ConfigurationError cnfe)
286N/A {
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
286N/A new Object[]{ "-URIResolver" });
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A }
286N/A else
286N/A {
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-URIResolver" }); //"Missing argument for);
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A }
286N/A else if ("-ENTITYRESOLVER".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length)
286N/A {
286N/A try
286N/A {
286N/A entityResolver = (EntityResolver) ObjectFactory.newInstance(argv[++i], true);
286N/A }
286N/A catch (ConfigurationError cnfe)
286N/A {
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
286N/A new Object[]{ "-EntityResolver" });
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A }
286N/A else
286N/A {
286N/A// "Missing argument for);
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-EntityResolver" });
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A }
286N/A else if ("-CONTENTHANDLER".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (i + 1 < argv.length)
286N/A {
286N/A try
286N/A {
286N/A contentHandler = (ContentHandler) ObjectFactory.newInstance(argv[++i], true);
286N/A }
286N/A catch (ConfigurationError cnfe)
286N/A {
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
286N/A new Object[]{ "-ContentHandler" });
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A }
286N/A else
286N/A {
286N/A// "Missing argument for);
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-ContentHandler" });
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A }
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A else if ("-L".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A tfactory.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE);
286N/A else
286N/A printInvalidXSLTCOption("-L");
286N/A }
286N/A else if ("-INCREMENTAL".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A tfactory.setAttribute
286N/A ("http://xml.apache.org/xalan/features/incremental",
286N/A java.lang.Boolean.TRUE);
286N/A else
286N/A printInvalidXSLTCOption("-INCREMENTAL");
286N/A }
286N/A else if ("-NOOPTIMIZE".equalsIgnoreCase(argv[i]))
286N/A {
286N/A // Default is true.
286N/A //
286N/A // %REVIEW% We should have a generalized syntax for negative
286N/A // switches... and probably should accept the inverse even
286N/A // if it is the default.
286N/A if (!useXSLTC)
286N/A tfactory.setAttribute
286N/A ("http://xml.apache.org/xalan/features/optimize",
286N/A java.lang.Boolean.FALSE);
286N/A else
286N/A printInvalidXSLTCOption("-NOOPTIMIZE");
286N/A }
286N/A else if ("-RL".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (!useXSLTC)
286N/A {
286N/A if (i + 1 < argv.length)
286N/A recursionLimit = Integer.parseInt(argv[++i]);
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-rl" })); //"Missing argument for);
286N/A }
286N/A else
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A i++;
286N/A
286N/A printInvalidXSLTCOption("-RL");
286N/A }
286N/A }
286N/A */
286N/A // Generate the translet class and optionally specify the name
286N/A // of the translet class.
286N/A else if ("-XO".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
286N/A {
286N/A tfactory.setAttribute("generate-translet", "true");
286N/A tfactory.setAttribute("translet-name", argv[++i]);
286N/A }
286N/A else
286N/A tfactory.setAttribute("generate-translet", "true");
286N/A }
286N/A else
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A i++;
286N/A printInvalidXalanOption("-XO");
286N/A }
286N/A }
286N/A // Specify the destination directory for the translet classes.
286N/A else if ("-XD".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
286N/A tfactory.setAttribute("destination-directory", argv[++i]);
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-XD" })); //"Missing argument for);
286N/A
286N/A }
286N/A else
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A i++;
286N/A
286N/A printInvalidXalanOption("-XD");
286N/A }
286N/A }
286N/A // Specify the jar file name which the translet classes are packaged into.
286N/A else if ("-XJ".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
286N/A {
286N/A tfactory.setAttribute("generate-translet", "true");
286N/A tfactory.setAttribute("jar-name", argv[++i]);
286N/A }
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-XJ" })); //"Missing argument for);
286N/A }
286N/A else
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A i++;
286N/A
286N/A printInvalidXalanOption("-XJ");
286N/A }
286N/A
286N/A }
286N/A // Specify the package name prefix for the generated translet classes.
286N/A else if ("-XP".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
286N/A tfactory.setAttribute("package-name", argv[++i]);
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
286N/A new Object[]{ "-XP" })); //"Missing argument for);
286N/A }
286N/A else
286N/A {
286N/A if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
286N/A i++;
286N/A
286N/A printInvalidXalanOption("-XP");
286N/A }
286N/A
286N/A }
286N/A // Enable template inlining.
286N/A else if ("-XN".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A tfactory.setAttribute("enable-inlining", "true");
286N/A }
286N/A else
286N/A printInvalidXalanOption("-XN");
286N/A }
286N/A // Turns on additional debugging message output
286N/A else if ("-XX".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A tfactory.setAttribute("debug", "true");
286N/A }
286N/A else
286N/A printInvalidXalanOption("-XX");
286N/A }
286N/A // Create the Transformer from the translet if the translet class is newer
286N/A // than the stylesheet.
286N/A else if ("-XT".equalsIgnoreCase(argv[i]))
286N/A {
286N/A if (useXSLTC)
286N/A {
286N/A tfactory.setAttribute("auto-translet", "true");
286N/A }
286N/A else
286N/A printInvalidXalanOption("-XT");
286N/A }
286N/A else if ("-SECURE".equalsIgnoreCase(argv[i]))
286N/A {
286N/A isSecureProcessing = true;
286N/A try
286N/A {
286N/A tfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
286N/A }
286N/A catch (TransformerConfigurationException e) {}
286N/A }
286N/A else
286N/A System.err.println(
286N/A XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_INVALID_OPTION, new Object[]{ argv[i] })); //"Invalid argument:);
286N/A }
286N/A
286N/A // Print usage instructions if no xml and xsl file is specified in the command line
286N/A if (inFileName == null && xslFileName == null)
286N/A {
286N/A msg = resbundle.getString("xslProc_no_input");
286N/A System.err.println(msg);
286N/A doExit(msg);
286N/A }
286N/A
286N/A // Note that there are usage cases for calling us without a -IN arg
286N/A // The main XSL transformation occurs here!
286N/A try
286N/A {
286N/A long start = System.currentTimeMillis();
286N/A
286N/A if (null != dumpFileName)
286N/A {
286N/A dumpWriter = new PrintWriter(new FileWriter(dumpFileName));
286N/A }
286N/A
286N/A Templates stylesheet = null;
286N/A
286N/A if (null != xslFileName)
286N/A {
286N/A if (flavor.equals("d2d"))
286N/A {
286N/A
286N/A // Parse in the xml data into a DOM
286N/A DocumentBuilderFactory dfactory =
286N/A DocumentBuilderFactory.newInstance();
286N/A
286N/A dfactory.setNamespaceAware(true);
286N/A
286N/A if (isSecureProcessing)
286N/A {
286N/A try
286N/A {
286N/A dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
286N/A }
286N/A catch (ParserConfigurationException pce) {}
286N/A }
286N/A
286N/A DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
286N/A Node xslDOM = docBuilder.parse(new InputSource(xslFileName));
286N/A
286N/A stylesheet = tfactory.newTemplates(new DOMSource(xslDOM,
286N/A xslFileName));
286N/A }
286N/A else
286N/A {
286N/A // System.out.println("Calling newTemplates: "+xslFileName);
286N/A stylesheet = tfactory.newTemplates(new StreamSource(xslFileName));
286N/A // System.out.println("Done calling newTemplates: "+xslFileName);
286N/A }
286N/A }
286N/A
286N/A PrintWriter resultWriter;
286N/A StreamResult strResult;
286N/A
286N/A if (null != outFileName)
286N/A {
286N/A strResult = new StreamResult(new FileOutputStream(outFileName));
286N/A // One possible improvement might be to ensure this is
286N/A // a valid URI before setting the systemId, but that
286N/A // might have subtle changes that pre-existing users
286N/A // might notice; we can think about that later -sc r1.46
286N/A strResult.setSystemId(outFileName);
286N/A }
286N/A else
286N/A {
286N/A strResult = new StreamResult(System.out);
286N/A // We used to default to incremental mode in this case.
286N/A // We've since decided that since the -INCREMENTAL switch is
286N/A // available, that default is probably not necessary nor
286N/A // necessarily a good idea.
286N/A }
286N/A
286N/A SAXTransformerFactory stf = (SAXTransformerFactory) tfactory;
286N/A
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A // This is currently controlled via TransformerFactoryImpl.
286N/A if (!useXSLTC && useSourceLocation)
286N/A stf.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE);
286N/A */
286N/A
286N/A // Did they pass in a stylesheet, or should we get it from the
286N/A // document?
286N/A if (null == stylesheet)
286N/A {
286N/A Source source =
286N/A stf.getAssociatedStylesheet(new StreamSource(inFileName), media,
286N/A null, null);
286N/A
286N/A if (null != source)
286N/A stylesheet = tfactory.newTemplates(source);
286N/A else
286N/A {
286N/A if (null != media)
286N/A throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_IN_MEDIA, new Object[]{inFileName, media})); //"No stylesheet found in: "
286N/A // + inFileName + ", media="
286N/A // + media);
286N/A else
286N/A throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_PI, new Object[]{inFileName})); //"No xml-stylesheet PI found in: "
286N/A //+ inFileName);
286N/A }
286N/A }
286N/A
286N/A if (null != stylesheet)
286N/A {
286N/A Transformer transformer = flavor.equals("th") ? null : stylesheet.newTransformer();
286N/A transformer.setErrorListener(new DefaultErrorHandler());
286N/A
286N/A // Override the output format?
286N/A if (null != outputType)
286N/A {
286N/A transformer.setOutputProperty(OutputKeys.METHOD, outputType);
286N/A }
286N/A
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A if (transformer instanceof com.sun.org.apache.xalan.internal.transformer.TransformerImpl)
286N/A {
286N/A com.sun.org.apache.xalan.internal.transformer.TransformerImpl impl = (com.sun.org.apache.xalan.internal.transformer.TransformerImpl)transformer;
286N/A TraceManager tm = impl.getTraceManager();
286N/A
286N/A if (null != tracer)
286N/A tm.addTraceListener(tracer);
286N/A
286N/A impl.setQuietConflictWarnings(quietConflictWarnings);
286N/A
286N/A // This is currently controlled via TransformerFactoryImpl.
286N/A if (useSourceLocation)
286N/A impl.setProperty(XalanProperties.SOURCE_LOCATION, Boolean.TRUE);
286N/A
286N/A if(recursionLimit>0)
286N/A impl.setRecursionLimit(recursionLimit);
286N/A
286N/A // sc 28-Feb-01 if we re-implement this, please uncomment helpmsg in printArgOptions
286N/A // impl.setDiagnosticsOutput( setQuietMode ? null : diagnosticsWriter );
286N/A }
286N/A */
286N/A
286N/A int nParams = params.size();
286N/A
286N/A for (int i = 0; i < nParams; i += 2)
286N/A {
286N/A transformer.setParameter((String) params.elementAt(i),
286N/A (String) params.elementAt(i + 1));
286N/A }
286N/A
286N/A if (uriResolver != null)
286N/A transformer.setURIResolver(uriResolver);
286N/A
286N/A if (null != inFileName)
286N/A {
286N/A if (flavor.equals("d2d"))
286N/A {
286N/A
286N/A // Parse in the xml data into a DOM
286N/A DocumentBuilderFactory dfactory =
286N/A DocumentBuilderFactory.newInstance();
286N/A
286N/A dfactory.setCoalescing(true);
286N/A dfactory.setNamespaceAware(true);
286N/A
286N/A if (isSecureProcessing)
286N/A {
286N/A try
286N/A {
286N/A dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
286N/A }
286N/A catch (ParserConfigurationException pce) {}
286N/A }
286N/A
286N/A DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
286N/A
286N/A if (entityResolver != null)
286N/A docBuilder.setEntityResolver(entityResolver);
286N/A
286N/A Node xmlDoc = docBuilder.parse(new InputSource(inFileName));
286N/A Document doc = docBuilder.newDocument();
286N/A org.w3c.dom.DocumentFragment outNode =
286N/A doc.createDocumentFragment();
286N/A
286N/A transformer.transform(new DOMSource(xmlDoc, inFileName),
286N/A new DOMResult(outNode));
286N/A
286N/A // Now serialize output to disk with identity transformer
286N/A Transformer serializer = stf.newTransformer();
286N/A serializer.setErrorListener(new DefaultErrorHandler());
286N/A
286N/A Properties serializationProps =
286N/A stylesheet.getOutputProperties();
286N/A
286N/A serializer.setOutputProperties(serializationProps);
286N/A
286N/A if (contentHandler != null)
286N/A {
286N/A SAXResult result = new SAXResult(contentHandler);
286N/A
286N/A serializer.transform(new DOMSource(outNode), result);
286N/A }
286N/A else
286N/A serializer.transform(new DOMSource(outNode), strResult);
286N/A }
286N/A else if (flavor.equals("th"))
286N/A {
286N/A for (int i = 0; i < 1; i++) // Loop for diagnosing bugs with inconsistent behavior
286N/A {
286N/A // System.out.println("Testing the TransformerHandler...");
286N/A
286N/A XMLReader reader = null;
286N/A
286N/A // Use JAXP1.1 ( if possible )
286N/A try
286N/A {
286N/A javax.xml.parsers.SAXParserFactory factory =
286N/A javax.xml.parsers.SAXParserFactory.newInstance();
286N/A
286N/A factory.setNamespaceAware(true);
286N/A
286N/A if (isSecureProcessing)
286N/A {
286N/A try
286N/A {
286N/A factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
286N/A }
286N/A catch (org.xml.sax.SAXException se) {}
286N/A }
286N/A
286N/A javax.xml.parsers.SAXParser jaxpParser =
286N/A factory.newSAXParser();
286N/A
286N/A reader = jaxpParser.getXMLReader();
286N/A }
286N/A catch (javax.xml.parsers.ParserConfigurationException ex)
286N/A {
286N/A throw new org.xml.sax.SAXException(ex);
286N/A }
286N/A catch (javax.xml.parsers.FactoryConfigurationError ex1)
286N/A {
286N/A throw new org.xml.sax.SAXException(ex1.toString());
286N/A }
286N/A catch (NoSuchMethodError ex2){}
286N/A catch (AbstractMethodError ame){}
286N/A
286N/A if (null == reader)
286N/A {
286N/A reader = XMLReaderFactory.createXMLReader();
286N/A }
286N/A
286N/A // J2SE does not support Xalan interpretive
286N/A /*
286N/A if (!useXSLTC)
286N/A stf.setAttribute(com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl.FEATURE_INCREMENTAL,
286N/A Boolean.TRUE);
286N/A */
286N/A
286N/A TransformerHandler th = stf.newTransformerHandler(stylesheet);
286N/A
286N/A reader.setContentHandler(th);
286N/A reader.setDTDHandler(th);
286N/A
286N/A if(th instanceof org.xml.sax.ErrorHandler)
286N/A reader.setErrorHandler((org.xml.sax.ErrorHandler)th);
286N/A
286N/A try
286N/A {
286N/A reader.setProperty(
286N/A "http://xml.org/sax/properties/lexical-handler", th);
286N/A }
286N/A catch (org.xml.sax.SAXNotRecognizedException e){}
286N/A catch (org.xml.sax.SAXNotSupportedException e){}
286N/A try
286N/A {
286N/A reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
286N/A true);
286N/A } catch (org.xml.sax.SAXException se) {}
286N/A
286N/A th.setResult(strResult);
286N/A
286N/A reader.parse(new InputSource(inFileName));
286N/A }
286N/A }
286N/A else
286N/A {
286N/A if (entityResolver != null)
286N/A {
286N/A XMLReader reader = null;
286N/A
286N/A // Use JAXP1.1 ( if possible )
286N/A try
286N/A {
286N/A javax.xml.parsers.SAXParserFactory factory =
286N/A javax.xml.parsers.SAXParserFactory.newInstance();
286N/A
286N/A factory.setNamespaceAware(true);
286N/A
286N/A if (isSecureProcessing)
286N/A {
286N/A try
286N/A {
286N/A factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
286N/A }
286N/A catch (org.xml.sax.SAXException se) {}
286N/A }
286N/A
286N/A javax.xml.parsers.SAXParser jaxpParser =
286N/A factory.newSAXParser();
286N/A
286N/A reader = jaxpParser.getXMLReader();
286N/A }
286N/A catch (javax.xml.parsers.ParserConfigurationException ex)
286N/A {
286N/A throw new org.xml.sax.SAXException(ex);
286N/A }
286N/A catch (javax.xml.parsers.FactoryConfigurationError ex1)
286N/A {
286N/A throw new org.xml.sax.SAXException(ex1.toString());
286N/A }
286N/A catch (NoSuchMethodError ex2){}
286N/A catch (AbstractMethodError ame){}
286N/A
286N/A if (null == reader)
286N/A {
286N/A reader = XMLReaderFactory.createXMLReader();
286N/A }
286N/A
286N/A reader.setEntityResolver(entityResolver);
286N/A
286N/A if (contentHandler != null)
286N/A {
286N/A SAXResult result = new SAXResult(contentHandler);
286N/A
286N/A transformer.transform(
286N/A new SAXSource(reader, new InputSource(inFileName)),
286N/A result);
286N/A }
286N/A else
286N/A {
286N/A transformer.transform(
286N/A new SAXSource(reader, new InputSource(inFileName)),
286N/A strResult);
286N/A }
286N/A }
286N/A else if (contentHandler != null)
286N/A {
286N/A SAXResult result = new SAXResult(contentHandler);
286N/A
286N/A transformer.transform(new StreamSource(inFileName), result);
286N/A }
286N/A else
286N/A {
286N/A // System.out.println("Starting transform");
286N/A transformer.transform(new StreamSource(inFileName),
286N/A strResult);
286N/A // System.out.println("Done with transform");
286N/A }
286N/A }
286N/A }
286N/A else
286N/A {
286N/A StringReader reader =
286N/A new StringReader("<?xml version=\"1.0\"?> <doc/>");
286N/A
286N/A transformer.transform(new StreamSource(reader), strResult);
286N/A }
286N/A }
286N/A else
286N/A {
286N/A// "XSL Process was not successful.");
286N/A msg = XSLMessages.createMessage(
286N/A XSLTErrorResources.ER_NOT_SUCCESSFUL, null);
286N/A diagnosticsWriter.println(msg);
286N/A doExit(msg);
286N/A }
286N/A
286N/A // close output streams
286N/A if (null != outFileName && strResult!=null)
286N/A {
286N/A java.io.OutputStream out = strResult.getOutputStream();
286N/A java.io.Writer writer = strResult.getWriter();
286N/A try
286N/A {
286N/A if (out != null) out.close();
286N/A if (writer != null) writer.close();
286N/A }
286N/A catch(java.io.IOException ie) {}
286N/A }
286N/A
286N/A long stop = System.currentTimeMillis();
286N/A long millisecondsDuration = stop - start;
286N/A
286N/A if (doDiag)
286N/A {
286N/A Object[] msgArgs = new Object[]{ inFileName, xslFileName, new Long(millisecondsDuration) };
286N/A msg = XSLMessages.createMessage("diagTiming", msgArgs);
286N/A diagnosticsWriter.println('\n');
286N/A diagnosticsWriter.println(msg);
286N/A }
286N/A
286N/A }
286N/A catch (Throwable throwable)
286N/A {
286N/A while (throwable
286N/A instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException)
286N/A {
286N/A throwable =
286N/A ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) throwable).getException();
286N/A }
286N/A
286N/A if ((throwable instanceof NullPointerException)
286N/A || (throwable instanceof ClassCastException))
286N/A doStackDumpOnError = true;
286N/A
286N/A diagnosticsWriter.println();
286N/A
286N/A if (doStackDumpOnError)
286N/A throwable.printStackTrace(dumpWriter);
286N/A else
286N/A {
286N/A DefaultErrorHandler.printLocation(diagnosticsWriter, throwable);
286N/A diagnosticsWriter.println(
286N/A XSLMessages.createMessage(XSLTErrorResources.ER_XSLT_ERROR, null)
286N/A + " (" + throwable.getClass().getName() + "): "
286N/A + throwable.getMessage());
286N/A }
286N/A
286N/A // diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); //"XSL Process was not successful.");
286N/A if (null != dumpFileName)
286N/A {
286N/A dumpWriter.close();
286N/A }
286N/A
286N/A doExit(throwable.getMessage());
286N/A }
286N/A
286N/A if (null != dumpFileName)
286N/A {
286N/A dumpWriter.close();
286N/A }
286N/A
286N/A if (null != diagnosticsWriter)
286N/A {
286N/A
286N/A // diagnosticsWriter.close();
286N/A }
286N/A
286N/A // if(!setQuietMode)
286N/A // diagnosticsWriter.println(resbundle.getString("xsldone")); //"Xalan: done");
286N/A // else
286N/A // diagnosticsWriter.println(""); //"Xalan: done");
286N/A }
286N/A }
286N/A
286N/A /** It is _much_ easier to debug under VJ++ if I can set a single breakpoint
286N/A * before this blows itself out of the water...
286N/A * (I keep checking this in, it keeps vanishing. Grr!)
286N/A * */
286N/A static void doExit(String msg)
286N/A {
286N/A throw new RuntimeException(msg);
286N/A }
286N/A
286N/A /**
286N/A * Wait for a return key to continue
286N/A *
286N/A * @param resbundle The resource bundle
286N/A */
286N/A private static void waitForReturnKey(ResourceBundle resbundle)
286N/A {
286N/A System.out.println(resbundle.getString("xslProc_return_to_continue"));
286N/A try
286N/A {
286N/A while (System.in.read() != '\n');
286N/A }
286N/A catch (java.io.IOException e) { }
286N/A }
286N/A
286N/A /**
286N/A * Print a message if an option cannot be used with -XSLTC.
286N/A *
286N/A * @param option The option String
286N/A */
286N/A private static void printInvalidXSLTCOption(String option)
286N/A {
286N/A System.err.println(XSLMessages.createMessage("xslProc_invalid_xsltc_option", new Object[]{option}));
286N/A }
286N/A
286N/A /**
286N/A * Print a message if an option can only be used with -XSLTC.
286N/A *
286N/A * @param option The option String
286N/A */
286N/A private static void printInvalidXalanOption(String option)
286N/A {
286N/A System.err.println(XSLMessages.createMessage("xslProc_invalid_xalan_option", new Object[]{option}));
286N/A }
286N/A}