1 /* 2 * Copyright (c) 2003, 2005, 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 javax.xml.parsers.DocumentBuilder; 26 import javax.xml.parsers.DocumentBuilderFactory; 27 import javax.xml.parsers.FactoryConfigurationError; 28 import javax.xml.parsers.ParserConfigurationException; 29 30 import org.xml.sax.SAXException; 31 import org.xml.sax.SAXParseException; 32 import org.w3c.dom.Document; 33 import org.w3c.dom.DOMException; 34 35 // For write operation 36 import javax.xml.transform.Transformer; 37 import javax.xml.transform.TransformerException; 38 import javax.xml.transform.TransformerFactory; 39 import javax.xml.transform.TransformerConfigurationException; 40 import javax.xml.transform.dom.DOMSource; 41 import javax.xml.transform.stream.StreamSource; 42 import javax.xml.transform.stream.StreamResult; 43 44 import java.io.*; 45 46 public class jvmtiGen 47 { 48 /** 49 * Write out usage and exit. 50 */ 51 private static void showUsage() { 52 System.err.println("usage:"); 53 System.err.println(" java jvmtiGen " + 54 "-IN <input XML file name> " + 55 "-XSL <XSL file> " + 56 "-OUT <output file name> " + 57 "[-PARAM <name> <expression> ...]"); 58 System.exit(0); // There is no returning from showUsage() 59 } 60 61 // Global value so it can be ref'd by the tree-adapter 62 static Document document; 63 64 public static void main (String argv []) 65 { 66 String inFileName=null; 67 String xslFileName=null; 68 String outFileName=null; 69 java.util.Vector<String> params=new java.util.Vector<String>(); 70 for (int ii = 0; ii < argv.length; ii++) { 71 if (argv[ii].equals("-IN")) { 72 inFileName = argv[++ii]; 73 } else if (argv[ii].equals("-XSL")) { 74 xslFileName = argv[++ii]; 75 } else if (argv[ii].equals("-OUT")) { 76 outFileName = argv[++ii]; 77 } else if (argv[ii].equals("-PARAM")) { 78 if (ii + 2 < argv.length) { 79 String name = argv[++ii]; 80 params.addElement(name); 81 String expression = argv[++ii]; 82 params.addElement(expression); 83 } else { 84 showUsage(); 85 } 86 } else { 87 showUsage(); 88 } 89 } 90 if (inFileName==null || xslFileName==null || outFileName==null){ 91 showUsage(); 92 } 93 94 /* 95 * Due to JAXP breakage in some intermediate Tiger builds, the 96 * com.sun.org.apache..... parser may throw an exception: 97 * com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 98 * org.apache.xalan.serialize.SerializerToText 99 * 100 * To work around the problem, this program uses the 101 * org.apache.xalan.... version if it is available. It is 102 * available in J2SE 1.4.x and early builds of 1.5 (Tiger). 103 * It was removed at the same time the thrown exception issue 104 * above was fixed, so if the class is not found we can proceed 105 * and use the default parser. 106 */ 107 final String parserProperty = 108 "javax.xml.transform.TransformerFactory"; 109 final String workaroundParser = 110 "org.apache.xalan.processor.TransformerFactoryImpl"; 111 112 try { 113 java.lang.Class cls = java.lang.Class.forName(workaroundParser); 114 /* 115 * If we get here, we found the class. Use it. 116 */ 117 System.setProperty(parserProperty, workaroundParser); 118 System.out.println("Info: jvmtiGen using " + parserProperty + 119 " = " + workaroundParser); 120 } catch (ClassNotFoundException cnfex) { 121 /* 122 * We didn't find workaroundParser. Ignore the 123 * exception and proceed with default settings. 124 */ 125 } 126 127 DocumentBuilderFactory factory = 128 DocumentBuilderFactory.newInstance(); 129 130 factory.setNamespaceAware(true); 131 factory.setValidating(true); 132 133 try { 134 File datafile = new File(inFileName); 135 File stylesheet = new File(xslFileName); 136 137 DocumentBuilder builder = factory.newDocumentBuilder(); 138 document = builder.parse(datafile); 139 140 // Use a Transformer for output 141 TransformerFactory tFactory = 142 TransformerFactory.newInstance(); 143 StreamSource stylesource = new StreamSource(stylesheet); 144 Transformer transformer = tFactory.newTransformer(stylesource); 145 for (int ii = 0; ii < params.size(); ii += 2){ 146 transformer.setParameter((String) params.elementAt(ii), 147 (String) params.elementAt(ii + 1)); 148 } 149 DOMSource source = new DOMSource(document); 150 151 PrintStream ps = new PrintStream( new FileOutputStream(outFileName)); 152 StreamResult result = new StreamResult(ps); 153 transformer.transform(source, result); 154 155 } catch (TransformerConfigurationException tce) { 156 // Error generated by the parser 157 System.out.println ("\n** Transformer Factory error"); 158 System.out.println(" " + tce.getMessage() ); 159 160 // Use the contained exception, if any 161 Throwable x = tce; 162 if (tce.getException() != null) 163 x = tce.getException(); 164 x.printStackTrace(); 165 166 } catch (TransformerException te) { 167 // Error generated by the parser 168 System.out.println ("\n** Transformation error"); 169 System.out.println(" " + te.getMessage() ); 170 171 // Use the contained exception, if any 172 Throwable x = te; 173 if (te.getException() != null) 174 x = te.getException(); 175 x.printStackTrace(); 176 177 } catch (SAXException sxe) { 178 // Error generated by this application 179 // (or a parser-initialization error) 180 Exception x = sxe; 181 if (sxe.getException() != null) 182 x = sxe.getException(); 183 x.printStackTrace(); 184 185 } catch (ParserConfigurationException pce) { 186 // Parser with specified options can't be built 187 pce.printStackTrace(); 188 189 } catch (IOException ioe) { 190 // I/O error 191 ioe.printStackTrace(); 192 } 193 } // main 194 }