duke@435: /* sla@5237: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: import javax.xml.parsers.DocumentBuilder; duke@435: import javax.xml.parsers.DocumentBuilderFactory; duke@435: import javax.xml.parsers.FactoryConfigurationError; duke@435: import javax.xml.parsers.ParserConfigurationException; duke@435: duke@435: import org.xml.sax.SAXException; duke@435: import org.xml.sax.SAXParseException; duke@435: import org.w3c.dom.Document; duke@435: import org.w3c.dom.DOMException; duke@435: // For write operation duke@435: import javax.xml.transform.Transformer; duke@435: import javax.xml.transform.TransformerException; duke@435: import javax.xml.transform.TransformerFactory; duke@435: import javax.xml.transform.TransformerConfigurationException; duke@435: import javax.xml.transform.dom.DOMSource; duke@435: import javax.xml.transform.stream.StreamSource; duke@435: import javax.xml.transform.stream.StreamResult; duke@435: duke@435: import java.io.*; duke@435: duke@435: public class jvmtiGen duke@435: { duke@435: /** duke@435: * Write out usage and exit. duke@435: */ duke@435: private static void showUsage() { duke@435: System.err.println("usage:"); duke@435: System.err.println(" java jvmtiGen " + duke@435: "-IN " + duke@435: "-XSL " + duke@435: "-OUT " + duke@435: "[-PARAM ...]"); duke@435: System.exit(0); // There is no returning from showUsage() duke@435: } duke@435: duke@435: // Global value so it can be ref'd by the tree-adapter duke@435: static Document document; duke@435: duke@435: public static void main (String argv []) duke@435: { duke@435: String inFileName=null; duke@435: String xslFileName=null; duke@435: String outFileName=null; duke@435: java.util.Vector params=new java.util.Vector(); duke@435: for (int ii = 0; ii < argv.length; ii++) { duke@435: if (argv[ii].equals("-IN")) { duke@435: inFileName = argv[++ii]; duke@435: } else if (argv[ii].equals("-XSL")) { duke@435: xslFileName = argv[++ii]; duke@435: } else if (argv[ii].equals("-OUT")) { duke@435: outFileName = argv[++ii]; duke@435: } else if (argv[ii].equals("-PARAM")) { duke@435: if (ii + 2 < argv.length) { duke@435: String name = argv[++ii]; duke@435: params.addElement(name); duke@435: String expression = argv[++ii]; duke@435: params.addElement(expression); duke@435: } else { duke@435: showUsage(); duke@435: } duke@435: } else { duke@435: showUsage(); duke@435: } duke@435: } duke@435: if (inFileName==null || xslFileName==null || outFileName==null){ duke@435: showUsage(); duke@435: } duke@435: duke@435: /* duke@435: * Due to JAXP breakage in some intermediate Tiger builds, the duke@435: * com.sun.org.apache..... parser may throw an exception: duke@435: * com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: duke@435: * org.apache.xalan.serialize.SerializerToText duke@435: * duke@435: * To work around the problem, this program uses the duke@435: * org.apache.xalan.... version if it is available. It is duke@435: * available in J2SE 1.4.x and early builds of 1.5 (Tiger). duke@435: * It was removed at the same time the thrown exception issue duke@435: * above was fixed, so if the class is not found we can proceed duke@435: * and use the default parser. duke@435: */ duke@435: final String parserProperty = duke@435: "javax.xml.transform.TransformerFactory"; duke@435: final String workaroundParser = duke@435: "org.apache.xalan.processor.TransformerFactoryImpl"; duke@435: duke@435: try { duke@435: java.lang.Class cls = java.lang.Class.forName(workaroundParser); duke@435: /* duke@435: * If we get here, we found the class. Use it. duke@435: */ duke@435: System.setProperty(parserProperty, workaroundParser); duke@435: System.out.println("Info: jvmtiGen using " + parserProperty + duke@435: " = " + workaroundParser); duke@435: } catch (ClassNotFoundException cnfex) { duke@435: /* duke@435: * We didn't find workaroundParser. Ignore the duke@435: * exception and proceed with default settings. duke@435: */ duke@435: } duke@435: duke@435: DocumentBuilderFactory factory = duke@435: DocumentBuilderFactory.newInstance(); duke@435: duke@435: factory.setNamespaceAware(true); duke@435: factory.setValidating(true); sla@5237: factory.setXIncludeAware(true); duke@435: duke@435: try { duke@435: File datafile = new File(inFileName); duke@435: File stylesheet = new File(xslFileName); duke@435: duke@435: DocumentBuilder builder = factory.newDocumentBuilder(); duke@435: document = builder.parse(datafile); duke@435: duke@435: // Use a Transformer for output duke@435: TransformerFactory tFactory = duke@435: TransformerFactory.newInstance(); duke@435: StreamSource stylesource = new StreamSource(stylesheet); duke@435: Transformer transformer = tFactory.newTransformer(stylesource); duke@435: for (int ii = 0; ii < params.size(); ii += 2){ duke@435: transformer.setParameter((String) params.elementAt(ii), duke@435: (String) params.elementAt(ii + 1)); duke@435: } duke@435: DOMSource source = new DOMSource(document); duke@435: duke@435: PrintStream ps = new PrintStream( new FileOutputStream(outFileName)); duke@435: StreamResult result = new StreamResult(ps); duke@435: transformer.transform(source, result); duke@435: duke@435: } catch (TransformerConfigurationException tce) { duke@435: // Error generated by the parser duke@435: System.out.println ("\n** Transformer Factory error"); duke@435: System.out.println(" " + tce.getMessage() ); duke@435: duke@435: // Use the contained exception, if any duke@435: Throwable x = tce; duke@435: if (tce.getException() != null) duke@435: x = tce.getException(); duke@435: x.printStackTrace(); duke@435: duke@435: } catch (TransformerException te) { duke@435: // Error generated by the parser duke@435: System.out.println ("\n** Transformation error"); duke@435: System.out.println(" " + te.getMessage() ); duke@435: duke@435: // Use the contained exception, if any duke@435: Throwable x = te; duke@435: if (te.getException() != null) duke@435: x = te.getException(); duke@435: x.printStackTrace(); duke@435: duke@435: } catch (SAXException sxe) { duke@435: // Error generated by this application duke@435: // (or a parser-initialization error) duke@435: Exception x = sxe; duke@435: if (sxe.getException() != null) duke@435: x = sxe.getException(); duke@435: x.printStackTrace(); duke@435: duke@435: } catch (ParserConfigurationException pce) { duke@435: // Parser with specified options can't be built duke@435: pce.printStackTrace(); duke@435: duke@435: } catch (IOException ioe) { duke@435: // I/O error duke@435: ioe.printStackTrace(); duke@435: } duke@435: } // main duke@435: }