process.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,185 行 · 第 1/3 页

JAVA
1,185
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation.  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 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 "Xalan" 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 (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 Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xalan.xslt;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.PrintWriter;import java.io.StringReader;import java.util.Properties;import java.util.ResourceBundle;import java.util.Vector;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Source;import javax.xml.transform.Templates;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerFactoryConfigurationError;import javax.xml.transform.URIResolver;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.TransformerHandler;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;import org.apache.xalan.processor.TransformerFactoryImpl;import org.apache.xalan.processor.XSLProcessorVersion;import org.apache.xalan.res.XSLMessages;import org.apache.xalan.res.XSLTErrorResources;import org.apache.xalan.trace.PrintTraceListener;import org.apache.xalan.trace.TraceManager;import org.apache.xalan.transformer.TransformerImpl;import org.apache.xalan.transformer.XalanProperties;import org.apache.xml.utils.DefaultErrorHandler;import org.apache.xml.utils.WrappedRuntimeException;import org.apache.xml.utils.res.XResourceBundle;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Node;import org.xml.sax.ContentHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;/** * <meta name="usage" content="general"/> * The main() method handles the Xalan command-line interface. */public class Process{  /**   * Prints argument options.   *   * @param resbundle Resource bundle   */  protected static void printArgOptions(ResourceBundle resbundle)  {    System.out.println(resbundle.getString("xslProc_option"));  //"xslproc options: ");    System.out.println("\n\t\t\t" + resbundle.getString("xslProc_common_options") + "\n");    System.out.println(resbundle.getString("optionXSLTC"));  //"    [-XSLTC (use XSLTC for transformation)]    System.out.println(resbundle.getString("optionIN"));  //"    [-IN inputXMLURL]");    System.out.println(resbundle.getString("optionXSL"));  //"   [-XSL XSLTransformationURL]");    System.out.println(resbundle.getString("optionOUT"));  //"   [-OUT outputFileName]");    // System.out.println(resbundle.getString("optionE")); //"   [-E (Do not expand entity refs)]");    System.out.println(resbundle.getString("optionV"));  //"   [-V (Version info)]");    // System.out.println(resbundle.getString("optionVALIDATE")); //"   [-VALIDATE (Set whether validation occurs.  Validation is off by default.)]");    System.out.println(resbundle.getString("optionEDUMP"));  //"   [-EDUMP {optional filename} (Do stackdump on error.)]");    System.out.println(resbundle.getString("optionXML"));  //"   [-XML (Use XML formatter and add XML header.)]");    System.out.println(resbundle.getString("optionTEXT"));  //"   [-TEXT (Use simple Text formatter.)]");    System.out.println(resbundle.getString("optionHTML"));  //"   [-HTML (Use HTML formatter.)]");    System.out.println(resbundle.getString("optionPARAM"));  //"   [-PARAM name expression (Set a stylesheet parameter)]");        System.out.println(resbundle.getString("optionMEDIA"));    System.out.println(resbundle.getString("optionFLAVOR"));    System.out.println(resbundle.getString("optionDIAG"));    System.out.println(resbundle.getString("optionURIRESOLVER"));  //"   [-URIRESOLVER full class name (URIResolver to be used to resolve URIs)]");        System.out.println(resbundle.getString("optionENTITYRESOLVER"));  //"   [-ENTITYRESOLVER full class name (EntityResolver to be used to resolve entities)]");    waitForReturnKey(resbundle);    System.out.println(resbundle.getString("optionCONTENTHANDLER"));  //"   [-CONTENTHANDLER full class name (ContentHandler to be used to serialize output)]");        System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xalan_options") + "\n");        System.out.println(resbundle.getString("optionQC"));  //"   [-QC (Quiet Pattern Conflicts Warnings)]");    // System.out.println(resbundle.getString("optionQ"));  //"   [-Q  (Quiet Mode)]"); // sc 28-Feb-01 commented out    System.out.println(resbundle.getString("optionTT"));  //"   [-TT (Trace the templates as they are being called.)]");    System.out.println(resbundle.getString("optionTG"));  //"   [-TG (Trace each generation event.)]");    System.out.println(resbundle.getString("optionTS"));  //"   [-TS (Trace each selection event.)]");    System.out.println(resbundle.getString("optionTTC"));  //"   [-TTC (Trace the template children as they are being processed.)]");    System.out.println(resbundle.getString("optionTCLASS"));  //"   [-TCLASS (TraceListener class for trace extensions.)]");    System.out.println(resbundle.getString("optionLINENUMBERS")); //"   [-L use line numbers]"    System.out.println(resbundle.getString("optionINCREMENTAL"));    System.out.println(resbundle.getString("optionNOOPTIMIMIZE"));    System.out.println(resbundle.getString("optionRL"));            System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xsltc_options") + "\n");    System.out.println(resbundle.getString("optionXO"));    System.out.println(resbundle.getString("optionXD"));    waitForReturnKey(resbundle);        System.out.println(resbundle.getString("optionXJ"));    System.out.println(resbundle.getString("optionXP"));    System.out.println(resbundle.getString("optionXN"));    System.out.println(resbundle.getString("optionXX"));    System.out.println(resbundle.getString("optionXT"));  }    /**   * Command line interface to transform an XML document according to   * the instructions found in an XSL stylesheet.     * <p>The Process class provides basic functionality for    * performing transformations from the command line.  To see a    * list of arguments supported, call with zero arguments.</p>   * <p>To set stylesheet parameters from the command line, use    * <code>-PARAM name expression</code>. If you want to set the    * parameter to a string value, simply pass the string value    * as-is, and it will be interpreted as a string.  (Note: if    * the value has spaces in it, you may need to quote it depending    * on your shell environment).</p>   *   * @param argv Input parameters from command line   */  public static void main(String argv[])  {        // Runtime.getRuntime().traceMethodCalls(false); // turns Java tracing off    boolean doStackDumpOnError = false;    boolean setQuietMode = false;    boolean doDiag = false;    // Runtime.getRuntime().traceMethodCalls(false);    // Runtime.getRuntime().traceInstructions(false);    /**     * The default diagnostic writer...     */    java.io.PrintWriter diagnosticsWriter = new PrintWriter(System.err, true);    java.io.PrintWriter dumpWriter = diagnosticsWriter;    ResourceBundle resbundle =      (XSLMessages.loadResourceBundle(        org.apache.xml.utils.res.XResourceBundle.ERROR_RESOURCES));    String flavor = "s2s";    if (argv.length < 1)    {      printArgOptions(resbundle);    }    else    {      boolean useXSLTC = false;      for (int i = 0; i < argv.length; i++)      {        if ("-XSLTC".equalsIgnoreCase(argv[i]))        {          useXSLTC = true;        }      }              TransformerFactory tfactory;      if (useXSLTC)      {	 String key = "javax.xml.transform.TransformerFactory";	 String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";	 Properties props = System.getProperties();	 props.put(key, value);	 System.setProperties(props);            }            try      {        tfactory = TransformerFactory.newInstance();      }      catch (TransformerFactoryConfigurationError pfe)      {        pfe.printStackTrace(dumpWriter);        diagnosticsWriter.println(          XSLMessages.createMessage(            XSLTErrorResources.ER_NOT_SUCCESSFUL, null));  //"XSL Process was not successful.");        tfactory = null;  // shut up compiler        doExit(-1);      }      boolean formatOutput = false;      boolean useSourceLocation = false;      String inFileName = null;      String outFileName = null;      String dumpFileName = null;      String xslFileName = null;      String treedumpFileName = null;      PrintTraceListener tracer = null;      String outputType = null;      String media = null;      Vector params = new Vector();      boolean quietConflictWarnings = false;      URIResolver uriResolver = null;      EntityResolver entityResolver = null;      ContentHandler contentHandler = null;      int recursionLimit=-1;      for (int i = 0; i < argv.length; i++)      {        if ("-XSLTC".equalsIgnoreCase(argv[i]))        {          // The -XSLTC option has been processed.        }        else if ("-TT".equalsIgnoreCase(argv[i]))        {          if (!useXSLTC)          {            if (null == tracer)              tracer = new PrintTraceListener(diagnosticsWriter);            tracer.m_traceTemplates = true;          }          else            printInvalidXSLTCOption("-TT");          // tfactory.setTraceTemplates(true);        }        else if ("-TG".equalsIgnoreCase(argv[i]))        {          if (!useXSLTC)          {            if (null == tracer)              tracer = new PrintTraceListener(diagnosticsWriter);            tracer.m_traceGeneration = true;          }          else            printInvalidXSLTCOption("-TG");          // tfactory.setTraceSelect(true);        }        else if ("-TS".equalsIgnoreCase(argv[i]))        {          if (!useXSLTC)          {            if (null == tracer)              tracer = new PrintTraceListener(diagnosticsWriter);            tracer.m_traceSelection = true;          }          else            printInvalidXSLTCOption("-TS");          // tfactory.setTraceTemplates(true);        }        else if ("-TTC".equalsIgnoreCase(argv[i]))        {          if (!useXSLTC)          {            if (null == tracer)              tracer = new PrintTraceListener(diagnosticsWriter);            tracer.m_traceElements = true;          }          else            printInvalidXSLTCOption("-TTC");          // tfactory.setTraceTemplateChildren(true);        }        else if ("-INDENT".equalsIgnoreCase(argv[i]))        {          int indentAmount;          if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))          {            indentAmount = Integer.parseInt(argv[++i]);          }          else          {            indentAmount = 0;          }          // TBD:          // xmlProcessorLiaison.setIndent(indentAmount);        }        else if ("-IN".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')            inFileName = argv[++i];          else            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-IN" }));  //"Missing argument for);        }        else if ("-MEDIA".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length)            media = argv[++i];          else            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-MEDIA" }));  //"Missing argument for);        }        else if ("-OUT".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')            outFileName = argv[++i];          else            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-OUT" }));  //"Missing argument for);        }        else if ("-XSL".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')            xslFileName = argv[++i];          else            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-XSL" }));  //"Missing argument for);        }        else if ("-FLAVOR".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length)          {            flavor = argv[++i];          }          else

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?