Path: blob/master/src/hotspot/share/prims/jvmtiGen.java
41144 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324import java.io.BufferedOutputStream;25import java.io.File;26import java.io.FileOutputStream;27import java.io.IOException;28import java.io.OutputStream;29import java.util.ArrayList;30import java.util.List;3132import javax.xml.parsers.DocumentBuilder;33import javax.xml.parsers.DocumentBuilderFactory;34import javax.xml.parsers.ParserConfigurationException;35import javax.xml.transform.Transformer;36import javax.xml.transform.TransformerException;37import javax.xml.transform.TransformerFactory;38import javax.xml.transform.dom.DOMSource;39import javax.xml.transform.stream.StreamSource;40import javax.xml.transform.stream.StreamResult;4142import org.xml.sax.ErrorHandler;43import org.xml.sax.SAXException;44import org.xml.sax.SAXParseException;45import org.w3c.dom.Document;46import org.w3c.dom.DOMException;4748public class jvmtiGen49{50private static final int EXIT_FAILURE_ERROR = 1;51private static final int EXIT_FAILURE_BADARGUMENTS = 2;5253private static boolean verbose = false;5455/**56* Write out usage and exit.57*/58private static void showUsage() {59System.err.println("usage:");60System.err.println(" java jvmtiGen " +61"[-verbose] " +62"-IN <input XML file name> " +63"-XSL <XSL file> " +64"-OUT <output file name> " +65"[-PARAM <name> <expression> ...]");66System.exit(EXIT_FAILURE_BADARGUMENTS); // There is no returning from showUsage()67}6869public static void main (String argv []) {70String inFileName = null;71String xslFileName = null;72String outFileName = null;73final List<String> params = new ArrayList<String>();74for (int ii = 0; ii < argv.length; ii++) {75if (argv[ii].equals("-verbose")) {76verbose = true;77} else if (argv[ii].equals("-IN")) {78inFileName = argv[++ii];79} else if (argv[ii].equals("-XSL")) {80xslFileName = argv[++ii];81} else if (argv[ii].equals("-OUT")) {82outFileName = argv[++ii];83} else if (argv[ii].equals("-PARAM")) {84if (ii + 2 < argv.length) {85final String name = argv[++ii];86params.add(name);87final String expression = argv[++ii];88params.add(expression);89} else {90showUsage();91}92} else {93showUsage();94}95}96if (inFileName == null || xslFileName == null || outFileName == null) {97showUsage();98}99100final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();101102factory.setNamespaceAware(true);103factory.setValidating(true);104factory.setXIncludeAware(true);105106final File datafile = new File(inFileName);107final File stylesheet = new File(xslFileName);108109try (110final OutputStream os = new BufferedOutputStream(new FileOutputStream(outFileName));111) {112final StreamSource stylesource = new StreamSource(stylesheet);113// Use a Transformer for output114final Transformer transformer =115TransformerFactory.newInstance().newTransformer(stylesource);116for (int ii = 0; ii < params.size(); ii += 2) {117transformer.setParameter(params.get(ii), params.get(ii + 1));118}119final DocumentBuilder builder = factory.newDocumentBuilder();120builder.setErrorHandler(new ErrorHandler() {121public void fatalError(SAXParseException exn) throws SAXException {122throw new SAXException(exn);123}124public void error(SAXParseException exn) throws SAXException {125fatalError(exn);126}127public void warning(SAXParseException exn) throws SAXException {128if (verbose) {129System.err.println("jvmtiGen warning: " + exn.getMessage());130}131}132});133final Document document = builder.parse(datafile);134final DOMSource source = new DOMSource(document);135final StreamResult result = new StreamResult(os);136transformer.transform(source, result);137} catch (IOException138| ParserConfigurationException139| SAXException140| TransformerException exn) {141System.err.print("jvmtiGen error: " + exn.getMessage());142exn.printStackTrace(System.err);143System.exit(EXIT_FAILURE_ERROR);144}145} // main146}147148149