1 /*
2  * Copyright (c) 2003, 2015, 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 java.io.BufferedOutputStream;
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.transform.Transformer;
37 import javax.xml.transform.TransformerException;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.dom.DOMSource;
40 import javax.xml.transform.stream.StreamSource;
41 import javax.xml.transform.stream.StreamResult;
42 
43 import org.xml.sax.ErrorHandler;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.SAXParseException;
46 import org.w3c.dom.Document;
47 import org.w3c.dom.DOMException;
48 
49 public class jvmtiGen
50 {
51     private static final int EXIT_FAILURE_ERROR = 1;
52     private static final int EXIT_FAILURE_BADARGUMENTS = 2;
53 
54     private static boolean verbose = false;
55 
56     /**
57      * Write out usage and exit.
58      */
showUsage()59     private static void showUsage() {
60         System.err.println("usage:");
61         System.err.println("  java jvmtiGen " +
62                            "[-verbose] " +
63                            "-IN <input XML file name> " +
64                            "-XSL <XSL file> " +
65                            "-OUT <output file name> " +
66                            "[-PARAM <name> <expression> ...]");
67         System.exit(EXIT_FAILURE_BADARGUMENTS); // There is no returning from showUsage()
68     }
69 
main(String argv [])70     public static void main (String argv []) {
71         String inFileName = null;
72         String xslFileName = null;
73         String outFileName = null;
74         final List<String> params = new ArrayList<String>();
75         for (int ii = 0; ii < argv.length; ii++) {
76             if (argv[ii].equals("-verbose")) {
77                 verbose = true;
78             } else if (argv[ii].equals("-IN")) {
79                 inFileName = argv[++ii];
80             } else if (argv[ii].equals("-XSL")) {
81                 xslFileName = argv[++ii];
82             } else if (argv[ii].equals("-OUT")) {
83                 outFileName = argv[++ii];
84             } else if (argv[ii].equals("-PARAM")) {
85                 if (ii + 2 < argv.length) {
86                     final String name = argv[++ii];
87                     params.add(name);
88                     final String expression = argv[++ii];
89                     params.add(expression);
90                 } else {
91                     showUsage();
92                 }
93             } else {
94                 showUsage();
95             }
96         }
97         if (inFileName == null || xslFileName == null || outFileName == null) {
98             showUsage();
99         }
100 
101         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
102 
103         factory.setNamespaceAware(true);
104         factory.setValidating(true);
105         factory.setXIncludeAware(true);
106 
107         final File datafile   = new File(inFileName);
108         final File stylesheet = new File(xslFileName);
109 
110         try (
111             final OutputStream os = new BufferedOutputStream(new FileOutputStream(outFileName));
112         ) {
113             final StreamSource stylesource = new StreamSource(stylesheet);
114             // Use a Transformer for output
115             final Transformer transformer =
116                 TransformerFactory.newInstance().newTransformer(stylesource);
117             for (int ii = 0; ii < params.size(); ii += 2) {
118                 transformer.setParameter(params.get(ii), params.get(ii + 1));
119             }
120             final DocumentBuilder builder = factory.newDocumentBuilder();
121             builder.setErrorHandler(new ErrorHandler() {
122                     public void fatalError(SAXParseException exn) throws SAXException {
123                         throw new SAXException(exn);
124                     }
125                     public void error(SAXParseException exn) throws SAXException {
126                         fatalError(exn);
127                     }
128                     public void warning(SAXParseException exn) throws SAXException {
129                         if (verbose) {
130                             System.err.println("jvmtiGen warning: " + exn.getMessage());
131                         }
132                     }
133                 });
134             final Document document = builder.parse(datafile);
135             final DOMSource source = new DOMSource(document);
136             final StreamResult result = new StreamResult(os);
137             transformer.transform(source, result);
138         } catch (IOException
139             | ParserConfigurationException
140             | SAXException
141             | TransformerException exn) {
142             System.err.print("jvmtiGen error: " + exn.getMessage());
143             exn.printStackTrace(System.err);
144             System.exit(EXIT_FAILURE_ERROR);
145         }
146     } // main
147 }
148