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