author | dcubed |
Mon, 24 Jun 2019 22:38:17 -0400 | |
changeset 55479 | 80b27dc96ca3 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
25 |
import java.io.BufferedOutputStream; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
26 |
import java.io.File; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
27 |
import java.io.FileOutputStream; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
28 |
import java.io.IOException; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
29 |
import java.io.OutputStream; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
30 |
import java.util.ArrayList; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
31 |
import java.util.List; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
32 |
|
1 | 33 |
import javax.xml.parsers.DocumentBuilder; |
34 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
35 |
import javax.xml.parsers.ParserConfigurationException; |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
36 |
import javax.xml.transform.Transformer; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
37 |
import javax.xml.transform.TransformerException; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
38 |
import javax.xml.transform.TransformerFactory; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
39 |
import javax.xml.transform.dom.DOMSource; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
40 |
import javax.xml.transform.stream.StreamSource; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
41 |
import javax.xml.transform.stream.StreamResult; |
1 | 42 |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
43 |
import org.xml.sax.ErrorHandler; |
1 | 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 |
{ |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
51 |
private static final int EXIT_FAILURE_ERROR = 1; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
52 |
private static final int EXIT_FAILURE_BADARGUMENTS = 2; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
53 |
|
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
54 |
private static boolean verbose = false; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
55 |
|
1 | 56 |
/** |
57 |
* Write out usage and exit. |
|
58 |
*/ |
|
59 |
private static void showUsage() { |
|
60 |
System.err.println("usage:"); |
|
61 |
System.err.println(" java jvmtiGen " + |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
62 |
"[-verbose] " + |
1 | 63 |
"-IN <input XML file name> " + |
64 |
"-XSL <XSL file> " + |
|
65 |
"-OUT <output file name> " + |
|
66 |
"[-PARAM <name> <expression> ...]"); |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
67 |
System.exit(EXIT_FAILURE_BADARGUMENTS); // There is no returning from showUsage() |
1 | 68 |
} |
69 |
||
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
70 |
public static void main (String argv []) { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
71 |
String inFileName = null; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
72 |
String xslFileName = null; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
73 |
String outFileName = null; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
74 |
final List<String> params = new ArrayList<String>(); |
1 | 75 |
for (int ii = 0; ii < argv.length; ii++) { |
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
76 |
if (argv[ii].equals("-verbose")) { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
77 |
verbose = true; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
78 |
} else if (argv[ii].equals("-IN")) { |
1 | 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) { |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
86 |
final String name = argv[++ii]; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
87 |
params.add(name); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
88 |
final String expression = argv[++ii]; |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
89 |
params.add(expression); |
1 | 90 |
} else { |
91 |
showUsage(); |
|
92 |
} |
|
93 |
} else { |
|
94 |
showUsage(); |
|
95 |
} |
|
96 |
} |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
97 |
if (inFileName == null || xslFileName == null || outFileName == null) { |
1 | 98 |
showUsage(); |
99 |
} |
|
100 |
||
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
101 |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
1 | 102 |
|
103 |
factory.setNamespaceAware(true); |
|
104 |
factory.setValidating(true); |
|
18025 | 105 |
factory.setXIncludeAware(true); |
1 | 106 |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
107 |
final File datafile = new File(inFileName); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
108 |
final File stylesheet = new File(xslFileName); |
1 | 109 |
|
33773
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
110 |
try ( |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
111 |
final OutputStream os = new BufferedOutputStream(new FileOutputStream(outFileName)); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
112 |
) { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
113 |
final StreamSource stylesource = new StreamSource(stylesheet); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
114 |
// Use a Transformer for output |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
115 |
final Transformer transformer = |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
116 |
TransformerFactory.newInstance().newTransformer(stylesource); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
117 |
for (int ii = 0; ii < params.size(); ii += 2) { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
118 |
transformer.setParameter(params.get(ii), params.get(ii + 1)); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
119 |
} |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
120 |
final DocumentBuilder builder = factory.newDocumentBuilder(); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
121 |
builder.setErrorHandler(new ErrorHandler() { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
122 |
public void fatalError(SAXParseException exn) throws SAXException { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
123 |
throw new SAXException(exn); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
124 |
} |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
125 |
public void error(SAXParseException exn) throws SAXException { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
126 |
fatalError(exn); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
127 |
} |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
128 |
public void warning(SAXParseException exn) throws SAXException { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
129 |
if (verbose) { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
130 |
System.err.println("jvmtiGen warning: " + exn.getMessage()); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
131 |
} |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
132 |
} |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
133 |
}); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
134 |
final Document document = builder.parse(datafile); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
135 |
final DOMSource source = new DOMSource(document); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
136 |
final StreamResult result = new StreamResult(os); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
137 |
transformer.transform(source, result); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
138 |
} catch (IOException |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
139 |
| ParserConfigurationException |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
140 |
| SAXException |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
141 |
| TransformerException exn) { |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
142 |
System.err.print("jvmtiGen error: " + exn.getMessage()); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
143 |
exn.printStackTrace(System.err); |
caf1d935e1c7
8141035: Let jvmtiGen exit with a non-zero exit code upon failure
sspitsyn
parents:
18025
diff
changeset
|
144 |
System.exit(EXIT_FAILURE_ERROR); |
1 | 145 |
} |
146 |
} // main |
|
147 |
} |