1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: Transform.java,v 1.2.4.1 2005/09/12 09:07:33 pvedula Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.cmdline;
  25 
  26 import java.io.FileNotFoundException;
  27 import java.net.MalformedURLException;
  28 import java.net.UnknownHostException;
  29 import java.util.Vector;
  30 
  31 import javax.xml.parsers.SAXParser;
  32 import javax.xml.parsers.SAXParserFactory;
  33 import javax.xml.transform.sax.SAXSource;
  34 
  35 import com.sun.org.apache.xalan.internal.xsltc.TransletException;
  36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  37 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
  38 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
  39 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
  40 import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;
  41 import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;
  42 import com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory;
  43 import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
  44 
  45 import org.xml.sax.InputSource;
  46 import org.xml.sax.SAXException;
  47 import org.xml.sax.XMLReader;
  48 
  49 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
  50 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
  51 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter;
  52 import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
  53 
  54 /**
  55  * @author Jacek Ambroziak
  56  * @author Santiago Pericas-Geertsen
  57  * @author G. Todd Miller
  58  * @author Morten Jorgensen
  59  */
  60 final public class Transform {
  61 
  62     private SerializationHandler _handler;
  63 
  64     private String  _fileName;
  65     private String  _className;
  66     private String  _jarFileSrc;
  67     private boolean _isJarFileSpecified = false;
  68     private Vector  _params = null;
  69     private boolean _uri, _debug;
  70     private int     _iterations;
  71 
  72     public Transform(String className, String fileName,
  73                      boolean uri, boolean debug, int iterations) {
  74         _fileName = fileName;
  75         _className = className;
  76         _uri = uri;
  77         _debug = debug;
  78         _iterations = iterations;
  79   }
  80 
  81    public String getFileName(){return _fileName;}
  82    public String getClassName(){return _className;}
  83 
  84     public void setParameters(Vector params) {
  85         _params = params;
  86     }
  87 
  88     private void setJarFileInputSrc(boolean flag,  String jarFile) {
  89         // TODO: at this time we do not do anything with this
  90         // information, attempts to add the jarfile to the CLASSPATH
  91         // were successful via System.setProperty, but the effects
  92         // were not visible to the running JVM. For now we add jarfile
  93         // to CLASSPATH in the wrapper script that calls this program.
  94         _isJarFileSpecified = flag;
  95         // TODO verify jarFile exists...
  96         _jarFileSrc = jarFile;
  97     }
  98 
  99     private void doTransform() {
 100         try {
 101             final Class clazz = ObjectFactory.findProviderClass(_className, true);
 102             final AbstractTranslet translet = (AbstractTranslet)clazz.newInstance();
 103             translet.postInitialization();
 104 
 105             // Create a SAX parser and get the XMLReader object it uses
 106             final SAXParserFactory factory = SAXParserFactory.newInstance();
 107             try {
 108                 factory.setFeature(Constants.NAMESPACE_FEATURE,true);
 109             }
 110             catch (Exception e) {
 111                 factory.setNamespaceAware(true);
 112             }
 113             final SAXParser parser = factory.newSAXParser();
 114             final XMLReader reader = parser.getXMLReader();
 115 
 116             // Set the DOM's DOM builder as the XMLReader's SAX2 content handler
 117             XSLTCDTMManager dtmManager =
 118                 (XSLTCDTMManager)XSLTCDTMManager.getDTMManagerClass()
 119                                                 .newInstance();
 120 
 121             DTMWSFilter wsfilter;
 122             if (translet != null && translet instanceof StripFilter) {
 123                 wsfilter = new DOMWSFilter(translet);
 124             } else {
 125                 wsfilter = null;
 126             }
 127 
 128             final DOMEnhancedForDTM dom =
 129                    (DOMEnhancedForDTM)dtmManager.getDTM(
 130                             new SAXSource(reader, new InputSource(_fileName)),
 131                             false, wsfilter, true, false, translet.hasIdCall());
 132 
 133             dom.setDocumentURI(_fileName);
 134             translet.prepassDocument(dom);
 135 
 136             // Pass global parameters
 137             int n = _params.size();
 138             for (int i = 0; i < n; i++) {
 139                 Parameter param = (Parameter) _params.elementAt(i);
 140                 translet.addParameter(param._name, param._value);
 141             }
 142 
 143             // Transform the document
 144             TransletOutputHandlerFactory tohFactory =
 145                 TransletOutputHandlerFactory.newInstance();
 146             tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM);
 147             tohFactory.setEncoding(translet._encoding);
 148             tohFactory.setOutputMethod(translet._method);
 149 
 150             if (_iterations == -1) {
 151                 translet.transform(dom, tohFactory.getSerializationHandler());
 152             }
 153             else if (_iterations > 0) {
 154                 long mm = System.currentTimeMillis();
 155                 for (int i = 0; i < _iterations; i++) {
 156                     translet.transform(dom,
 157                                        tohFactory.getSerializationHandler());
 158                 }
 159                 mm = System.currentTimeMillis() - mm;
 160 
 161                 System.err.println("\n<!--");
 162                 System.err.println("  transform  = "
 163                                    + (((double) mm) / ((double) _iterations))
 164                                    + " ms");
 165                 System.err.println("  throughput = "
 166                                    + (1000.0 / (((double) mm)
 167                                                  / ((double) _iterations)))
 168                                    + " tps");
 169                 System.err.println("-->");
 170             }
 171         }
 172         catch (TransletException e) {
 173             if (_debug) e.printStackTrace();
 174             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 175                    e.getMessage());
 176         }
 177         catch (RuntimeException e) {
 178             if (_debug) e.printStackTrace();
 179             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 180                                e.getMessage());
 181         }
 182         catch (FileNotFoundException e) {
 183             if (_debug) e.printStackTrace();
 184             ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, _fileName);
 185             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 186                                err.toString());
 187         }
 188         catch (MalformedURLException e) {
 189             if (_debug) e.printStackTrace();
 190             ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, _fileName);
 191             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 192                                err.toString());
 193         }
 194         catch (ClassNotFoundException e) {
 195             if (_debug) e.printStackTrace();
 196             ErrorMsg err= new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR,_className);
 197             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 198                                err.toString());
 199         }
 200         catch (UnknownHostException e) {
 201             if (_debug) e.printStackTrace();
 202             ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, _fileName);
 203             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 204                                err.toString());
 205         }
 206         catch (SAXException e) {
 207             Exception ex = e.getException();
 208             if (_debug) {
 209                 if (ex != null) ex.printStackTrace();
 210                 e.printStackTrace();
 211             }
 212             System.err.print(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY));
 213             if (ex != null)
 214                 System.err.println(ex.getMessage());
 215             else
 216                 System.err.println(e.getMessage());
 217         }
 218         catch (Exception e) {
 219             if (_debug) e.printStackTrace();
 220             System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
 221                                e.getMessage());
 222         }
 223     }
 224 
 225     public static void printUsage() {
 226         System.err.println(new ErrorMsg(ErrorMsg.TRANSFORM_USAGE_STR));
 227     }
 228 
 229     public static void main(String[] args) {
 230         try {
 231             if (args.length > 0) {
 232                 int i;
 233                 int iterations = -1;
 234                 boolean uri = false, debug = false;
 235                 boolean isJarFileSpecified = false;
 236                 String  jarFile = null;
 237 
 238                 // Parse options starting with '-'
 239                 for (i = 0; i < args.length && args[i].charAt(0) == '-'; i++) {
 240                     if (args[i].equals("-u")) {
 241                         uri = true;
 242                     }
 243                     else if (args[i].equals("-x")) {
 244                         debug = true;
 245                     }
 246                     else if (args[i].equals("-j")) {
 247                         isJarFileSpecified = true;
 248                         jarFile = args[++i];
 249                     }
 250                     else if (args[i].equals("-n")) {
 251                         try {
 252                             iterations = Integer.parseInt(args[++i]);
 253                         }
 254                         catch (NumberFormatException e) {
 255                             // ignore
 256                         }
 257                     }
 258                     else {
 259                         printUsage();
 260                     }
 261                 }
 262 
 263                 // Enough arguments left ?
 264                 if (args.length - i < 2) printUsage();
 265 
 266                 // Get document file and class name
 267                 Transform handler = new Transform(args[i+1], args[i], uri,
 268                     debug, iterations);
 269                 handler.setJarFileInputSrc(isJarFileSpecified,  jarFile);
 270 
 271                 // Parse stylesheet parameters
 272                 Vector params = new Vector();
 273                 for (i += 2; i < args.length; i++) {
 274                     final int equal = args[i].indexOf('=');
 275                     if (equal > 0) {
 276                         final String name  = args[i].substring(0, equal);
 277                         final String value = args[i].substring(equal+1);
 278                         params.addElement(new Parameter(name, value));
 279                     }
 280                     else {
 281                         printUsage();
 282                     }
 283                 }
 284 
 285                 if (i == args.length) {
 286                     handler.setParameters(params);
 287                     handler.doTransform();
 288                 }
 289             } else {
 290                 printUsage();
 291             }
 292         }
 293         catch (Exception e) {
 294             e.printStackTrace();
 295         }
 296     }
 297 }