}
  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;    XSLTErrorResources resbundle =      (XSLTErrorResources) (XSLMessages.loadResourceBundle(        org.apache.xml.utils.res.XResourceBundle.ERROR_RESOURCES));    String flavor = "s2s";    if (argv.length < 1)    {      printArgOptions(resbundle);    }    else    {      TransformerFactory tfactory;      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;      for (int i = 0; i < argv.length; i++)      {        if ("-TT".equalsIgnoreCase(argv[i]))        {          if (null == tracer)            tracer = new PrintTraceListener(diagnosticsWriter);          tracer.m_traceTemplates = true;          // tfactory.setTraceTemplates(true);        }        else if ("-TG".equalsIgnoreCase(argv[i]))        {          if (null == tracer)            tracer = new PrintTraceListener(diagnosticsWriter);          tracer.m_traceGeneration = true;          // tfactory.setTraceSelect(true);        }        else if ("-TS".equalsIgnoreCase(argv[i]))        {          if (null == tracer)            tracer = new PrintTraceListener(diagnosticsWriter);          tracer.m_traceSelection = true;          // tfactory.setTraceTemplates(true);        }        else if ("-TTC".equalsIgnoreCase(argv[i]))        {          if (null == tracer)            tracer = new PrintTraceListener(diagnosticsWriter);          tracer.m_traceElements = true;          // 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)            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)            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)            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            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-FLAVOR" }));  //"Missing argument for);        }        else if ("-PARAM".equalsIgnoreCase(argv[i]))        {          if (i + 2 < argv.length)          {            String name = argv[++i];            params.addElement(name);            String expression = argv[++i];            params.addElement(expression);          }          else            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-PARAM" }));  //"Missing argument for);        }        else if ("-treedump".equalsIgnoreCase(argv[i]))  // sc 28-Feb-01 appears to be unused; can we remove?        {          if (i + 1 < argv.length)            treedumpFileName = argv[++i];          else            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-treedump" }));  //"Missing argument for);        }        else if ("-F".equalsIgnoreCase(argv[i]))  // sc 28-Feb-01 appears to be unused; can we remove?        {          formatOutput = true;        }        else if ("-E".equalsIgnoreCase(argv[i]))        {          // TBD:          // xmlProcessorLiaison.setShouldExpandEntityRefs(false);        }        else if ("-V".equalsIgnoreCase(argv[i]))        {          diagnosticsWriter.println(resbundle.getString("version")  //">>>>>>> Xalan Version "                                    + XSLProcessorVersion.S_VERSION + ", " +          /* xmlProcessorLiaison.getParserDescription()+ */          resbundle.getString("version2"));  // "<<<<<<<");        }        else if ("-QC".equalsIgnoreCase(argv[i]))        {          quietConflictWarnings = true;        }        else if ("-Q".equalsIgnoreCase(argv[i]))        {          setQuietMode = true;        }        /*        else if("-VALIDATE".equalsIgnoreCase(argv[i]))        {          String shouldValidate;          if(((i+1) < argv.length) && (argv[i+1].charAt(0) != '-'))          {            shouldValidate = argv[++i];          }          else          {            shouldValidate = "yes";          }          // xmlProcessorLiaison.setUseValidation(shouldValidate.equalsIgnoreCase("yes"));        }        */        else if ("-DIAG".equalsIgnoreCase(argv[i]))        {          doDiag = true;        }        else if ("-XML".equalsIgnoreCase(argv[i]))        {          outputType = "xml";        }        else if ("-TEXT".equalsIgnoreCase(argv[i]))        {          outputType = "text";        }        else if ("-HTML".equalsIgnoreCase(argv[i]))        {          outputType = "html";        }        else if ("-EDUMP".equalsIgnoreCase(argv[i]))        {          doStackDumpOnError = true;          if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))          {            dumpFileName = argv[++i];          }        }        else if ("-URIRESOLVER".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length)          {            try            {              uriResolver =                (URIResolver) Class.forName(argv[++i]).newInstance();              tfactory.setURIResolver(uriResolver);            }            catch (Exception cnfe)            {              System.err.println(                XSLMessages.createMessage(                  XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,                  new Object[]{ "-URIResolver" }));              doExit(-1);            }          }          else          {            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-URIResolver" }));  //"Missing argument for);            doExit(-1);          }        }        else if ("-ENTITYRESOLVER".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length)          {            try            {              entityResolver =                (EntityResolver) Class.forName(argv[++i]).newInstance();            }            catch (Exception cnfe)            {              System.err.println(                XSLMessages.createMessage(                  XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,                  new Object[]{ "-EntityResolver" }));              doExit(-1);            }          }          else          {            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-EntityResolver" }));  //"Missing argument for);            doExit(-1);          }        }        else if ("-CONTENTHANDLER".equalsIgnoreCase(argv[i]))        {          if (i + 1 < argv.length)          {            try            {              contentHandler =                (ContentHandler) Class.forName(argv[++i]).newInstance();            }            catch (Exception cnfe)            {              System.err.println(                XSLMessages.createMessage(                  XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,                  new Object[]{ "-ContentHandler" }));              doExit(-1);            }          }          else          {            System.err.println(              XSLMessages.createMessage(                XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,                new Object[]{ "-ContentHandler" }));  //"Missing argument for);            doExit(-1);          }        }        else if ("-L".equalsIgnoreCase(argv[i]))          useSourceLocation = true;        else          System.err.println(            XSLMessages.createMessage(              XSLTErrorResources.ER_INVALID_OPTION, new Object[]{ argv[i] }));  //"Invalid argument:);      }      // Note that there are usage cases for calling us without a -IN arg      // The main XSL transformation occurs here!      try      {        long start = System.currentTimeMillis();        if (null != dumpFileName)        {          dumpWriter = new PrintWriter(new FileWriter(dumpFileName));        }        Templates stylesheet = null;        if (null != xslFileName)        {          if (flavor.equals("d2d"))          {            // Parse in the xml data into a DOM            DocumentBuilderFactory dfactory =              DocumentBuilderFactory.newInstance();            dfactory.setNamespaceAware(true);            DocumentBuilder docBuilder = dfactory.newDocumentBuilder();            Node xslDOM = docBuilder.parse(new InputSource(xslFileName));            stylesheet = tfactory.newTemplates(new DOMSource(xslDOM,                    xslFileName));          }          else          {            // System.out.println("Calling newTemplates: "+xslFileName);            stylesheet = tfactory.newTemplates(new StreamSource(xslFileName));            // System.out.println("Done calling newTemplates: "+xslFileName);          }        }        PrintWriter resultWriter;        StreamResult strResult;        if (null != outFileName)        {          strResult = new StreamResult(new FileOutputStream(outFileName));        }        else        {          strResult = new StreamResult(System.out);          tfactory.setAttribute(           org.apache.xalan.processor.TransformerFactoryImpl.FEATURE_INCREMENTAL           , Boolean.TRUE);        }        SAXTransformerFactory stf = (SAXTransformerFactory) tfactory;        // Did they pass in a stylesheet, or should we get it from the         // document?        if (null == stylesheet)        {          Source source =            stf.getAssociatedStylesheet(new StreamSource(inFileName), media,                                        null, null);          if (null != source)            stylesheet = tfactory.newTemplates(source);          else          {            if (null != media)              throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_IN_MEDIA, new Object[]{inFileName, media})); //"No stylesheet found in: "                                            // + inFileName + ", media="                                            // + media);            else              throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_PI, new Object[]{inFileName})); //"No xml-stylesheet PI found in: "                                             //+ inFileName);          }        }        if (null != stylesheet)        {          Transformer transformer = flavor.equals("th") ? null : stylesheet.newTransformer();          // Override the output format?          if (null != outputType)          {            transformer.setOutputProperty(OutputKeys.METHOD, outputType);          }          if (transformer instanceof TransformerImpl)          {            TransformerImpl impl = ((TransformerImpl) transformer);            TraceManager tm = impl.getTraceManager();            if (null != tracer)              tm.addTraceListener(tracer);            impl.setQuietConflictWarnings(quietConflictWarnings);            if (useSourceLocation)              impl.setProperty(XalanProperties.SOURCE_LOCATION, Boolean.TRUE);            // sc 28-Feb-01 if we re-implement this, please uncomment helpmsg in printArgOptions            // impl.setDiagnosticsOutput( setQuietMode ? null : diagnosticsWriter );          }          int nParams = params.size();          for (int i = 0; i < nParams; i += 2)          {            transformer.setParameter((String) params.elementAt(i),                                     (String) params.elementAt(i + 1));          }          if (uriResolver != null)            transformer.setURIResolver(uriResolver);          if (null != inFileName)          {            if (flavor.equals("d2d"))            {              // Parse in the xml data into a DOM              DocumentBuilderFactory dfactory =                DocumentBuilderFactory.newInstance();              dfactory.setCoalescing(true);              dfactory.setNamespaceAware(true);              DocumentBuilder docBuilder = dfactory.newDocumentBuilder();              if (entityResolver != null)                docBuilder.setEntityResolver(entityResolver);              Node xmlDoc = docBuilder.parse(new InputSource(inFileName));              Document doc = docBuilder.newDocument();              org.w3c.dom.DocumentFragment outNode =                doc.createDocumentFragment();              transformer.transform(new DOMSource(xmlDoc, inFileName),                                    new DOMResult(outNode));              // Now serialize output to disk with identity transformer              Transformer serializer = stf.newTransformer();              Properties serializationProps =                stylesheet.getOutputProperties();              serializer.setOutputProperties(serializationProps);              if (contentHandler != null)              {                SAXResult result = new SAXResult(contentHandler);                serializer.transform(new DOMSource(outNode), result);              }              else                serializer.transform(new DOMSource(outNode), strResult);            }            else if (flavor.equals("th"))            {              for (int i = 0; i < 1; i++) // Loop for diagnosing bugs with inconsistent behavior              {              // System.out.println("Testing the TransformerHandler...");              // ===============              XMLReader reader = null;              // Use JAXP1.1 ( if possible )                    try              {                javax.xml.parsers.SAXParserFactory factory =                  javax.xml.parsers.SAXParserFactory.newInstance();                factory.setNamespaceAware(true);                javax.xml.parsers.SAXParser jaxpParser =                  factory.newSAXParser();                reader = jaxpParser.getXMLReader();              }              catch (javax.xml.parsers.ParserConfigurationException ex)              {                throw new org.xml.sax.SAXException(ex);              }              catch (javax.xml.parsers.FactoryConfigurationError ex1)              {                throw new org.xml.sax.SAXException(ex1.toString());              }              catch (NoSuchMethodError ex2){}              catch (AbstractMethodError ame){}              if (null == reader)              {                reader = XMLReaderFactory.createXMLReader();              }                            stf.setAttribute(org.apache.xalan.processor.TransformerFactoryImpl.FEATURE_INCREMENTAL,                  Boolean.TRUE);                               TransformerHandler th = stf.newTransformerHandler(stylesheet);                            reader.setContentHandler(th);              reader.setDTDHandler(th);                            if(th instanceof org.xml.sax.ErrorHandler)                reader.setErrorHandler((org.xml.sax.ErrorHandler)th);                            try              {                reader.setProperty(                  "http://xml.org/sax/properties/lexical-handler", th);              }              catch (org.xml.sax.SAXNotRecognizedException e){}              catch (org.xml.sax.SAXNotSupportedException e){}              try              {                reader.setFeature("http://xml.org/sax/features/namespace-prefixes",                                  true);              } catch (org.xml.sax.SAXException se) {}                      try              {                reader.setFeature("http://apache.org/xml/features/validation/dynamic",                                  true);              } catch (org.xml.sax.SAXException se) {}                            th.setResult(strResult);                            // System.out.println("sending parse events to the handler...");              // for (int i = 0; i < 50; i++)               {// System.out.print(".");// if((i % 50) == 0)//   System.out.println("");                reader.parse(new InputSource(inFileName));                // Transformer t = ((org.apache.xalan.transformer.TransformerHandlerImpl)th).getTransformer();                // System.err.println("Calling reset");                // ((TransformerImpl)t).reset();              }                            //              if (contentHandler != null)//              {//                SAXResult result = new SAXResult(contentHandler);////                transformer.transform(//                  new SAXSource(reader, new InputSource(inFileName)), result);//              }//              else//              {//                transformer.transform(//                  new SAXSource(reader, new InputSource(inFileName)),//                  strResult);//              }              // ===============              }            }            else            {              if (entityResolver != null)              {                XMLReader reader = null;                // Use JAXP1.1 ( if possible )                      try                {                  javax.xml.parsers.SAXParserFactory factory =                    javax.xml.parsers.SAXParserFactory.newInstance();                  factory.setNamespaceAware(true);                  javax.xml.parsers.SAXParser jaxpParser =                    factory.newSAXParser();                  reader = jaxpParser.getXMLReader();                }                catch (javax.xml.parsers.ParserConfigurationException ex)                {                  throw new org.xml.sax.SAXException(ex);                }                catch (javax.xml.parsers.FactoryConfigurationError ex1)                {                  throw new org.xml.sax.SAXException(ex1.toString());                }                catch (NoSuchMethodError ex2){}                catch (AbstractMethodError ame){}                if (null == reader)                {                  reader = XMLReaderFactory.createXMLReader();                }                reader.setEntityResolver(entityResolver);                if (contentHandler != null)                {                  SAXResult result = new SAXResult(contentHandler);                  transformer.transform(                    new SAXSource(reader, new InputSource(inFileName)),                    result);                }                else                {                  transformer.transform(                    new SAXSource(reader, new InputSource(inFileName)),                    strResult);                }              }              else if (contentHandler != null)              {                SAXResult result = new SAXResult(contentHandler);                transformer.transform(new StreamSource(inFileName), result);              }              else              {                // System.out.println("Starting transform");                transformer.transform(new StreamSource(inFileName),                                      strResult);                // System.out.println("Done with transform");              }            }          }          else          {            StringReader reader =              new StringReader("<?xml version=\"1.0\"?> <doc/>");            transformer.transform(new StreamSource(reader), strResult);          }        }        else        {          diagnosticsWriter.println(            XSLMessages.createMessage(              XSLTErrorResources.ER_NOT_SUCCESSFUL, null));  //"XSL Process was not successful.");          doExit(-1);        }        long stop = System.currentTimeMillis();        long millisecondsDuration = stop - start;        if (doDiag)          diagnosticsWriter.println("\n\n========\nTransform of "                                    + inFileName + " via " + xslFileName                                    + " took " + millisecondsDuration                                    + " ms");      }      catch (Throwable throwable)      {        while (throwable               instanceof org.apache.xml.utils.WrappedRuntimeException)        {          throwable =            ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();        }        if ((throwable instanceof NullPointerException)                || (throwable instanceof ClassCastException))          doStackDumpOnError = true;        diagnosticsWriter.println();        if (doStackDumpOnError)          throwable.printStackTrace(dumpWriter);        else          diagnosticsWriter.println(            XSLMessages.createMessage(XSLTErrorResources.ER_XSLT_ERROR, null)            + " (" + throwable.getClass().getName() + "): "            + throwable.getMessage());        // diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); //"XSL Process was not successful.");        if (null != dumpFileName)        {          dumpWriter.close();        }        doExit(-1);      }      if (null != dumpFileName)      {        dumpWriter.close();      }      if (null != diagnosticsWriter)      {        // diagnosticsWriter.close();      }      // if(!setQuietMode)      //  diagnosticsWriter.println(resbundle.getString("xsldone")); //"Xalan: done");      // else      diagnosticsWriter.println("");  //"Xalan: done");    }  }