1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiGen.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,194 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +import javax.xml.parsers.DocumentBuilder; 1.29 +import javax.xml.parsers.DocumentBuilderFactory; 1.30 +import javax.xml.parsers.FactoryConfigurationError; 1.31 +import javax.xml.parsers.ParserConfigurationException; 1.32 + 1.33 +import org.xml.sax.SAXException; 1.34 +import org.xml.sax.SAXParseException; 1.35 +import org.w3c.dom.Document; 1.36 +import org.w3c.dom.DOMException; 1.37 +// For write operation 1.38 +import javax.xml.transform.Transformer; 1.39 +import javax.xml.transform.TransformerException; 1.40 +import javax.xml.transform.TransformerFactory; 1.41 +import javax.xml.transform.TransformerConfigurationException; 1.42 +import javax.xml.transform.dom.DOMSource; 1.43 +import javax.xml.transform.stream.StreamSource; 1.44 +import javax.xml.transform.stream.StreamResult; 1.45 + 1.46 +import java.io.*; 1.47 + 1.48 +public class jvmtiGen 1.49 +{ 1.50 + /** 1.51 + * Write out usage and exit. 1.52 + */ 1.53 + private static void showUsage() { 1.54 + System.err.println("usage:"); 1.55 + System.err.println(" java jvmtiGen " + 1.56 + "-IN <input XML file name> " + 1.57 + "-XSL <XSL file> " + 1.58 + "-OUT <output file name> " + 1.59 + "[-PARAM <name> <expression> ...]"); 1.60 + System.exit(0); // There is no returning from showUsage() 1.61 + } 1.62 + 1.63 + // Global value so it can be ref'd by the tree-adapter 1.64 + static Document document; 1.65 + 1.66 + public static void main (String argv []) 1.67 + { 1.68 + String inFileName=null; 1.69 + String xslFileName=null; 1.70 + String outFileName=null; 1.71 + java.util.Vector<String> params=new java.util.Vector<String>(); 1.72 + for (int ii = 0; ii < argv.length; ii++) { 1.73 + if (argv[ii].equals("-IN")) { 1.74 + inFileName = argv[++ii]; 1.75 + } else if (argv[ii].equals("-XSL")) { 1.76 + xslFileName = argv[++ii]; 1.77 + } else if (argv[ii].equals("-OUT")) { 1.78 + outFileName = argv[++ii]; 1.79 + } else if (argv[ii].equals("-PARAM")) { 1.80 + if (ii + 2 < argv.length) { 1.81 + String name = argv[++ii]; 1.82 + params.addElement(name); 1.83 + String expression = argv[++ii]; 1.84 + params.addElement(expression); 1.85 + } else { 1.86 + showUsage(); 1.87 + } 1.88 + } else { 1.89 + showUsage(); 1.90 + } 1.91 + } 1.92 + if (inFileName==null || xslFileName==null || outFileName==null){ 1.93 + showUsage(); 1.94 + } 1.95 + 1.96 + /* 1.97 + * Due to JAXP breakage in some intermediate Tiger builds, the 1.98 + * com.sun.org.apache..... parser may throw an exception: 1.99 + * com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 1.100 + * org.apache.xalan.serialize.SerializerToText 1.101 + * 1.102 + * To work around the problem, this program uses the 1.103 + * org.apache.xalan.... version if it is available. It is 1.104 + * available in J2SE 1.4.x and early builds of 1.5 (Tiger). 1.105 + * It was removed at the same time the thrown exception issue 1.106 + * above was fixed, so if the class is not found we can proceed 1.107 + * and use the default parser. 1.108 + */ 1.109 + final String parserProperty = 1.110 + "javax.xml.transform.TransformerFactory"; 1.111 + final String workaroundParser = 1.112 + "org.apache.xalan.processor.TransformerFactoryImpl"; 1.113 + 1.114 + try { 1.115 + java.lang.Class cls = java.lang.Class.forName(workaroundParser); 1.116 + /* 1.117 + * If we get here, we found the class. Use it. 1.118 + */ 1.119 + System.setProperty(parserProperty, workaroundParser); 1.120 + System.out.println("Info: jvmtiGen using " + parserProperty + 1.121 + " = " + workaroundParser); 1.122 + } catch (ClassNotFoundException cnfex) { 1.123 + /* 1.124 + * We didn't find workaroundParser. Ignore the 1.125 + * exception and proceed with default settings. 1.126 + */ 1.127 + } 1.128 + 1.129 + DocumentBuilderFactory factory = 1.130 + DocumentBuilderFactory.newInstance(); 1.131 + 1.132 + factory.setNamespaceAware(true); 1.133 + factory.setValidating(true); 1.134 + factory.setXIncludeAware(true); 1.135 + 1.136 + try { 1.137 + File datafile = new File(inFileName); 1.138 + File stylesheet = new File(xslFileName); 1.139 + 1.140 + DocumentBuilder builder = factory.newDocumentBuilder(); 1.141 + document = builder.parse(datafile); 1.142 + 1.143 + // Use a Transformer for output 1.144 + TransformerFactory tFactory = 1.145 + TransformerFactory.newInstance(); 1.146 + StreamSource stylesource = new StreamSource(stylesheet); 1.147 + Transformer transformer = tFactory.newTransformer(stylesource); 1.148 + for (int ii = 0; ii < params.size(); ii += 2){ 1.149 + transformer.setParameter((String) params.elementAt(ii), 1.150 + (String) params.elementAt(ii + 1)); 1.151 + } 1.152 + DOMSource source = new DOMSource(document); 1.153 + 1.154 + PrintStream ps = new PrintStream( new FileOutputStream(outFileName)); 1.155 + StreamResult result = new StreamResult(ps); 1.156 + transformer.transform(source, result); 1.157 + 1.158 + } catch (TransformerConfigurationException tce) { 1.159 + // Error generated by the parser 1.160 + System.out.println ("\n** Transformer Factory error"); 1.161 + System.out.println(" " + tce.getMessage() ); 1.162 + 1.163 + // Use the contained exception, if any 1.164 + Throwable x = tce; 1.165 + if (tce.getException() != null) 1.166 + x = tce.getException(); 1.167 + x.printStackTrace(); 1.168 + 1.169 + } catch (TransformerException te) { 1.170 + // Error generated by the parser 1.171 + System.out.println ("\n** Transformation error"); 1.172 + System.out.println(" " + te.getMessage() ); 1.173 + 1.174 + // Use the contained exception, if any 1.175 + Throwable x = te; 1.176 + if (te.getException() != null) 1.177 + x = te.getException(); 1.178 + x.printStackTrace(); 1.179 + 1.180 + } catch (SAXException sxe) { 1.181 + // Error generated by this application 1.182 + // (or a parser-initialization error) 1.183 + Exception x = sxe; 1.184 + if (sxe.getException() != null) 1.185 + x = sxe.getException(); 1.186 + x.printStackTrace(); 1.187 + 1.188 + } catch (ParserConfigurationException pce) { 1.189 + // Parser with specified options can't be built 1.190 + pce.printStackTrace(); 1.191 + 1.192 + } catch (IOException ioe) { 1.193 + // I/O error 1.194 + ioe.printStackTrace(); 1.195 + } 1.196 + } // main 1.197 +}