1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 import javax.xml.parsers.DocumentBuilder;
  26 import javax.xml.parsers.DocumentBuilderFactory;
  27 import javax.xml.parsers.FactoryConfigurationError;
  28 import javax.xml.parsers.ParserConfigurationException;
  29 
  30 import org.xml.sax.SAXException;
  31 import org.xml.sax.SAXParseException;
  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.DOMException;
  34 // For write operation
  35 import javax.xml.transform.Transformer;
  36 import javax.xml.transform.TransformerException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.transform.TransformerConfigurationException;
  39 import javax.xml.transform.dom.DOMSource;
  40 import javax.xml.transform.stream.StreamSource;
  41 import javax.xml.transform.stream.StreamResult;
  42 
  43 import java.io.*;
  44 
  45 public class jvmtiGen
  46 {
  47     /**
  48      * Write out usage and exit.
  49      */
  50     private static void showUsage() {
  51         System.err.println("usage:");
  52         System.err.println("  java jvmtiGen " +
  53                            "-IN <input XML file name> " +
  54                            "-XSL <XSL file> " +
  55                            "-OUT <output file name> " +
  56                            "[-PARAM <name> <expression> ...]");
  57         System.exit(0);         // There is no returning from showUsage()
  58     }
  59 
  60     // Global value so it can be ref'd by the tree-adapter
  61     static Document document;
  62 
  63     public static void main (String argv [])
  64     {
  65         String inFileName=null;
  66         String xslFileName=null;
  67         String outFileName=null;
  68         java.util.Vector<String> params=new java.util.Vector<String>();
  69         for (int ii = 0; ii < argv.length; ii++) {
  70             if (argv[ii].equals("-IN")) {
  71                 inFileName = argv[++ii];
  72             } else if (argv[ii].equals("-XSL")) {
  73                 xslFileName = argv[++ii];
  74             } else if (argv[ii].equals("-OUT")) {
  75                 outFileName = argv[++ii];
  76             } else if (argv[ii].equals("-PARAM")) {
  77                 if (ii + 2 < argv.length) {
  78                     String name = argv[++ii];
  79                     params.addElement(name);
  80                     String expression = argv[++ii];
  81                     params.addElement(expression);
  82                 } else {
  83                     showUsage();
  84                 }
  85             } else {
  86                 showUsage();
  87             }
  88         }
  89         if (inFileName==null || xslFileName==null || outFileName==null){
  90             showUsage();
  91         }
  92 
  93         /*
  94          * Due to JAXP breakage in some intermediate Tiger builds, the
  95          * com.sun.org.apache..... parser may throw an exception:
  96          *   com.sun.org.apache.xml.internal.utils.WrappedRuntimeException:
  97          *     org.apache.xalan.serialize.SerializerToText
  98          *
  99          * To work around the problem, this program uses the
 100          * org.apache.xalan....  version if it is available.  It is
 101          * available in J2SE 1.4.x and early builds of 1.5 (Tiger).
 102          * It was removed at the same time the thrown exception issue
 103          * above was fixed, so if the class is not found we can proceed
 104          * and use the default parser.
 105          */
 106         final String parserProperty =
 107             "javax.xml.transform.TransformerFactory";
 108         final String workaroundParser =
 109             "org.apache.xalan.processor.TransformerFactoryImpl";
 110 
 111         try {
 112             java.lang.Class cls = java.lang.Class.forName(workaroundParser);
 113             /*
 114              * If we get here, we found the class.  Use it.
 115              */
 116             System.setProperty(parserProperty, workaroundParser);
 117             System.out.println("Info: jvmtiGen using " + parserProperty +
 118                                " = " + workaroundParser);
 119         } catch (ClassNotFoundException cnfex) {
 120             /*
 121              * We didn't find workaroundParser.  Ignore the
 122              * exception and proceed with default settings.
 123              */
 124         }
 125 
 126         DocumentBuilderFactory factory =
 127             DocumentBuilderFactory.newInstance();
 128 
 129         factory.setNamespaceAware(true);
 130         factory.setValidating(true);
 131         factory.setXIncludeAware(true);
 132 
 133         try {
 134             File datafile   = new File(inFileName);
 135             File stylesheet = new File(xslFileName);
 136 
 137             DocumentBuilder builder = factory.newDocumentBuilder();
 138             document = builder.parse(datafile);
 139 
 140             // Use a Transformer for output
 141             TransformerFactory tFactory =
 142                 TransformerFactory.newInstance();
 143             StreamSource stylesource = new StreamSource(stylesheet);
 144             Transformer transformer = tFactory.newTransformer(stylesource);
 145             for (int ii = 0; ii < params.size(); ii += 2){
 146                 transformer.setParameter((String) params.elementAt(ii),
 147                                          (String) params.elementAt(ii + 1));
 148             }
 149             DOMSource source = new DOMSource(document);
 150 
 151             PrintStream ps = new PrintStream( new FileOutputStream(outFileName));
 152             StreamResult result = new StreamResult(ps);
 153             transformer.transform(source, result);
 154 
 155         } catch (TransformerConfigurationException tce) {
 156            // Error generated by the parser
 157            System.out.println ("\n** Transformer Factory error");
 158            System.out.println("   " + tce.getMessage() );
 159 
 160            // Use the contained exception, if any
 161            Throwable x = tce;
 162            if (tce.getException() != null)
 163                x = tce.getException();
 164            x.printStackTrace();
 165 
 166         } catch (TransformerException te) {
 167            // Error generated by the parser
 168            System.out.println ("\n** Transformation error");
 169            System.out.println("   " + te.getMessage() );
 170 
 171            // Use the contained exception, if any
 172            Throwable x = te;
 173            if (te.getException() != null)
 174                x = te.getException();
 175            x.printStackTrace();
 176 
 177          } catch (SAXException sxe) {
 178            // Error generated by this application
 179            // (or a parser-initialization error)
 180            Exception  x = sxe;
 181            if (sxe.getException() != null)
 182                x = sxe.getException();
 183            x.printStackTrace();
 184 
 185         } catch (ParserConfigurationException pce) {
 186             // Parser with specified options can't be built
 187             pce.printStackTrace();
 188 
 189         } catch (IOException ioe) {
 190            // I/O error
 191            ioe.printStackTrace();
 192         }
 193     } // main
 194 }