hotspot/src/share/vm/prims/jvmtiGen.java
changeset 33773 caf1d935e1c7
parent 18025 b7bcf7497f93
equal deleted inserted replaced
33768:cd239519366a 33773:caf1d935e1c7
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
       
    25 import java.io.BufferedOutputStream;
       
    26 import java.io.File;
       
    27 import java.io.FileOutputStream;
       
    28 import java.io.IOException;
       
    29 import java.io.OutputStream;
       
    30 import java.util.ArrayList;
       
    31 import java.util.List;
       
    32 
    25 import javax.xml.parsers.DocumentBuilder;
    33 import javax.xml.parsers.DocumentBuilder;
    26 import javax.xml.parsers.DocumentBuilderFactory;
    34 import javax.xml.parsers.DocumentBuilderFactory;
    27 import javax.xml.parsers.FactoryConfigurationError;
       
    28 import javax.xml.parsers.ParserConfigurationException;
    35 import javax.xml.parsers.ParserConfigurationException;
       
    36 import javax.xml.transform.Transformer;
       
    37 import javax.xml.transform.TransformerException;
       
    38 import javax.xml.transform.TransformerFactory;
       
    39 import javax.xml.transform.dom.DOMSource;
       
    40 import javax.xml.transform.stream.StreamSource;
       
    41 import javax.xml.transform.stream.StreamResult;
    29 
    42 
       
    43 import org.xml.sax.ErrorHandler;
    30 import org.xml.sax.SAXException;
    44 import org.xml.sax.SAXException;
    31 import org.xml.sax.SAXParseException;
    45 import org.xml.sax.SAXParseException;
    32 import org.w3c.dom.Document;
    46 import org.w3c.dom.Document;
    33 import org.w3c.dom.DOMException;
    47 import org.w3c.dom.DOMException;
    34 // For write operation
       
    35 import javax.xml.transform.Transformer;
       
    36 import javax.xml.transform.TransformerException;
       
    37 import javax.xml.transform.TransformerFactory;
       
    38 import javax.xml.transform.TransformerConfigurationException;
       
    39 import javax.xml.transform.dom.DOMSource;
       
    40 import javax.xml.transform.stream.StreamSource;
       
    41 import javax.xml.transform.stream.StreamResult;
       
    42 
       
    43 import java.io.*;
       
    44 
    48 
    45 public class jvmtiGen
    49 public class jvmtiGen
    46 {
    50 {
       
    51     private static final int EXIT_FAILURE_ERROR = 1;
       
    52     private static final int EXIT_FAILURE_BADARGUMENTS = 2;
       
    53 
       
    54     private static boolean verbose = false;
       
    55 
    47     /**
    56     /**
    48      * Write out usage and exit.
    57      * Write out usage and exit.
    49      */
    58      */
    50     private static void showUsage() {
    59     private static void showUsage() {
    51         System.err.println("usage:");
    60         System.err.println("usage:");
    52         System.err.println("  java jvmtiGen " +
    61         System.err.println("  java jvmtiGen " +
       
    62                            "[-verbose] " +
    53                            "-IN <input XML file name> " +
    63                            "-IN <input XML file name> " +
    54                            "-XSL <XSL file> " +
    64                            "-XSL <XSL file> " +
    55                            "-OUT <output file name> " +
    65                            "-OUT <output file name> " +
    56                            "[-PARAM <name> <expression> ...]");
    66                            "[-PARAM <name> <expression> ...]");
    57         System.exit(0);         // There is no returning from showUsage()
    67         System.exit(EXIT_FAILURE_BADARGUMENTS); // There is no returning from showUsage()
    58     }
    68     }
    59 
    69 
    60     // Global value so it can be ref'd by the tree-adapter
    70     public static void main (String argv []) {
    61     static Document document;
    71         String inFileName = null;
    62 
    72         String xslFileName = null;
    63     public static void main (String argv [])
    73         String outFileName = null;
    64     {
    74         final List<String> params = new ArrayList<String>();
    65         String inFileName=null;
       
    66         String xslFileName=null;
       
    67         String outFileName=null;
       
    68         java.util.Vector<String> params=new java.util.Vector<String>();
       
    69         for (int ii = 0; ii < argv.length; ii++) {
    75         for (int ii = 0; ii < argv.length; ii++) {
    70             if (argv[ii].equals("-IN")) {
    76             if (argv[ii].equals("-verbose")) {
       
    77                 verbose = true;
       
    78             } else if (argv[ii].equals("-IN")) {
    71                 inFileName = argv[++ii];
    79                 inFileName = argv[++ii];
    72             } else if (argv[ii].equals("-XSL")) {
    80             } else if (argv[ii].equals("-XSL")) {
    73                 xslFileName = argv[++ii];
    81                 xslFileName = argv[++ii];
    74             } else if (argv[ii].equals("-OUT")) {
    82             } else if (argv[ii].equals("-OUT")) {
    75                 outFileName = argv[++ii];
    83                 outFileName = argv[++ii];
    76             } else if (argv[ii].equals("-PARAM")) {
    84             } else if (argv[ii].equals("-PARAM")) {
    77                 if (ii + 2 < argv.length) {
    85                 if (ii + 2 < argv.length) {
    78                     String name = argv[++ii];
    86                     final String name = argv[++ii];
    79                     params.addElement(name);
    87                     params.add(name);
    80                     String expression = argv[++ii];
    88                     final String expression = argv[++ii];
    81                     params.addElement(expression);
    89                     params.add(expression);
    82                 } else {
    90                 } else {
    83                     showUsage();
    91                     showUsage();
    84                 }
    92                 }
    85             } else {
    93             } else {
    86                 showUsage();
    94                 showUsage();
    87             }
    95             }
    88         }
    96         }
    89         if (inFileName==null || xslFileName==null || outFileName==null){
    97         if (inFileName == null || xslFileName == null || outFileName == null) {
    90             showUsage();
    98             showUsage();
    91         }
    99         }
    92 
   100 
    93         /*
   101         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    94          * Due to JAXP breakage in some intermediate Tiger builds, the
       
    95          * com.sun.org.apache..... parser may throw an exception:
       
    96          *   com.sun.org.apache.xml.internal.utils.WrappedRuntimeException:
       
    97          *     org.apache.xalan.serialize.SerializerToText
       
    98          *
       
    99          * To work around the problem, this program uses the
       
   100          * org.apache.xalan....  version if it is available.  It is
       
   101          * available in J2SE 1.4.x and early builds of 1.5 (Tiger).
       
   102          * It was removed at the same time the thrown exception issue
       
   103          * above was fixed, so if the class is not found we can proceed
       
   104          * and use the default parser.
       
   105          */
       
   106         final String parserProperty =
       
   107             "javax.xml.transform.TransformerFactory";
       
   108         final String workaroundParser =
       
   109             "org.apache.xalan.processor.TransformerFactoryImpl";
       
   110 
       
   111         try {
       
   112             java.lang.Class cls = java.lang.Class.forName(workaroundParser);
       
   113             /*
       
   114              * If we get here, we found the class.  Use it.
       
   115              */
       
   116             System.setProperty(parserProperty, workaroundParser);
       
   117             System.out.println("Info: jvmtiGen using " + parserProperty +
       
   118                                " = " + workaroundParser);
       
   119         } catch (ClassNotFoundException cnfex) {
       
   120             /*
       
   121              * We didn't find workaroundParser.  Ignore the
       
   122              * exception and proceed with default settings.
       
   123              */
       
   124         }
       
   125 
       
   126         DocumentBuilderFactory factory =
       
   127             DocumentBuilderFactory.newInstance();
       
   128 
   102 
   129         factory.setNamespaceAware(true);
   103         factory.setNamespaceAware(true);
   130         factory.setValidating(true);
   104         factory.setValidating(true);
   131         factory.setXIncludeAware(true);
   105         factory.setXIncludeAware(true);
   132 
   106 
   133         try {
   107         final File datafile   = new File(inFileName);
   134             File datafile   = new File(inFileName);
   108         final File stylesheet = new File(xslFileName);
   135             File stylesheet = new File(xslFileName);
       
   136 
   109 
   137             DocumentBuilder builder = factory.newDocumentBuilder();
   110         try (
   138             document = builder.parse(datafile);
   111             final OutputStream os = new BufferedOutputStream(new FileOutputStream(outFileName));
   139 
   112         ) {
       
   113             final StreamSource stylesource = new StreamSource(stylesheet);
   140             // Use a Transformer for output
   114             // Use a Transformer for output
   141             TransformerFactory tFactory =
   115             final Transformer transformer =
   142                 TransformerFactory.newInstance();
   116                 TransformerFactory.newInstance().newTransformer(stylesource);
   143             StreamSource stylesource = new StreamSource(stylesheet);
   117             for (int ii = 0; ii < params.size(); ii += 2) {
   144             Transformer transformer = tFactory.newTransformer(stylesource);
   118                 transformer.setParameter(params.get(ii), params.get(ii + 1));
   145             for (int ii = 0; ii < params.size(); ii += 2){
       
   146                 transformer.setParameter((String) params.elementAt(ii),
       
   147                                          (String) params.elementAt(ii + 1));
       
   148             }
   119             }
   149             DOMSource source = new DOMSource(document);
   120             final DocumentBuilder builder = factory.newDocumentBuilder();
   150 
   121             builder.setErrorHandler(new ErrorHandler() {
   151             PrintStream ps = new PrintStream( new FileOutputStream(outFileName));
   122                     public void fatalError(SAXParseException exn) throws SAXException {
   152             StreamResult result = new StreamResult(ps);
   123                         throw new SAXException(exn);
       
   124                     }
       
   125                     public void error(SAXParseException exn) throws SAXException {
       
   126                         fatalError(exn);
       
   127                     }
       
   128                     public void warning(SAXParseException exn) throws SAXException {
       
   129                         if (verbose) {
       
   130                             System.err.println("jvmtiGen warning: " + exn.getMessage());
       
   131                         }
       
   132                     }
       
   133                 });
       
   134             final Document document = builder.parse(datafile);
       
   135             final DOMSource source = new DOMSource(document);
       
   136             final StreamResult result = new StreamResult(os);
   153             transformer.transform(source, result);
   137             transformer.transform(source, result);
   154 
   138         } catch (IOException
   155         } catch (TransformerConfigurationException tce) {
   139             | ParserConfigurationException
   156            // Error generated by the parser
   140             | SAXException
   157            System.out.println ("\n** Transformer Factory error");
   141             | TransformerException exn) {
   158            System.out.println("   " + tce.getMessage() );
   142             System.err.print("jvmtiGen error: " + exn.getMessage());
   159 
   143             exn.printStackTrace(System.err);
   160            // Use the contained exception, if any
   144             System.exit(EXIT_FAILURE_ERROR);
   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         }
   145         }
   193     } // main
   146     } // main
   194 }
   147 }