src/share/vm/prims/jvmtiGen.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5237
f2110083203d
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

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

mercurial