1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.cmdline; 21 22 import java.io.FileNotFoundException ; 23 import java.net.MalformedURLException ; 24 import java.net.UnknownHostException ; 25 import java.util.Vector ; 26 27 import javax.xml.parsers.SAXParser ; 28 import javax.xml.parsers.SAXParserFactory ; 29 import javax.xml.transform.sax.SAXSource ; 30 31 import com.sun.org.apache.xalan.internal.xsltc.TransletException; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 33 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM; 34 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager; 35 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 36 import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants; 37 import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter; 38 import com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory; 39 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 40 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.XMLReader ; 44 45 import com.sun.org.apache.xalan.internal.xsltc.StripFilter; 46 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter; 47 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter; 48 49 55 final public class Transform { 56 57 private SerializationHandler _handler; 58 59 private String _fileName; 60 private String _className; 61 private String _jarFileSrc; 62 private boolean _isJarFileSpecified = false; 63 private Vector _params = null; 64 private boolean _uri, _debug; 65 private int _iterations; 66 67 private static boolean _allowExit = true; 68 69 public Transform(String className, String fileName, 70 boolean uri, boolean debug, int iterations) { 71 _fileName = fileName; 72 _className = className; 73 _uri = uri; 74 _debug = debug; 75 _iterations = iterations; 76 } 77 78 public String getFileName(){return _fileName;} 79 public String getClassName(){return _className;} 80 81 public void setParameters(Vector params) { 82 _params = params; 83 } 84 85 private void setJarFileInputSrc(boolean flag, String jarFile) { 86 _isJarFileSpecified = flag; 92 _jarFileSrc = jarFile; 94 } 95 96 private void doTransform() { 97 try { 98 final Class clazz = ObjectFactory.findProviderClass( 99 _className, ObjectFactory.findClassLoader(), true); 100 final AbstractTranslet translet = (AbstractTranslet)clazz.newInstance(); 101 translet.postInitialization(); 102 103 final SAXParserFactory factory = SAXParserFactory.newInstance(); 105 try { 106 factory.setFeature(Constants.NAMESPACE_FEATURE,true); 107 } 108 catch (Exception e) { 109 factory.setNamespaceAware(true); 110 } 111 final SAXParser parser = factory.newSAXParser(); 112 final XMLReader reader = parser.getXMLReader(); 113 114 XSLTCDTMManager dtmManager = 116 (XSLTCDTMManager)XSLTCDTMManager.getDTMManagerClass() 117 .newInstance(); 118 119 DTMWSFilter wsfilter; 120 if (translet != null && translet instanceof StripFilter) { 121 wsfilter = new DOMWSFilter(translet); 122 } else { 123 wsfilter = null; 124 } 125 126 final DOMEnhancedForDTM dom = 127 (DOMEnhancedForDTM)dtmManager.getDTM( 128 new SAXSource (reader, new InputSource (_fileName)), 129 false, wsfilter, true, false, translet.hasIdCall()); 130 131 dom.setDocumentURI(_fileName); 132 translet.prepassDocument(dom); 133 134 int n = _params.size(); 136 for (int i = 0; i < n; i++) { 137 Parameter param = (Parameter) _params.elementAt(i); 138 translet.addParameter(param._name, param._value); 139 } 140 141 TransletOutputHandlerFactory tohFactory = 143 TransletOutputHandlerFactory.newInstance(); 144 tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM); 145 tohFactory.setEncoding(translet._encoding); 146 tohFactory.setOutputMethod(translet._method); 147 148 if (_iterations == -1) { 149 translet.transform(dom, tohFactory.getSerializationHandler()); 150 } 151 else if (_iterations > 0) { 152 long mm = System.currentTimeMillis(); 153 for (int i = 0; i < _iterations; i++) { 154 translet.transform(dom, 155 tohFactory.getSerializationHandler()); 156 } 157 mm = System.currentTimeMillis() - mm; 158 159 System.err.println("\n<!--"); 160 System.err.println(" transform = " 161 + (((double) mm) / ((double) _iterations)) 162 + " ms"); 163 System.err.println(" throughput = " 164 + (1000.0 / (((double) mm) 165 / ((double) _iterations))) 166 + " tps"); 167 System.err.println("-->"); 168 } 169 } 170 catch (TransletException e) { 171 if (_debug) e.printStackTrace(); 172 System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+ 173 e.getMessage()); 174 if (_allowExit) System.exit(-1); 175 } 176 catch (RuntimeException e) { 177 if (_debug) e.printStackTrace(); 178 System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+ 179 e.getMessage()); 180 if (_allowExit) System.exit(-1); 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 if (_allowExit) System.exit(-1); 188 } 189 catch (MalformedURLException e) { 190 if (_debug) e.printStackTrace(); 191 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, _fileName); 192 System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+ 193 err.toString()); 194 if (_allowExit) System.exit(-1); 195 } 196 catch (ClassNotFoundException e) { 197 if (_debug) e.printStackTrace(); 198 ErrorMsg err= new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR,_className); 199 System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+ 200 err.toString()); 201 if (_allowExit) System.exit(-1); 202 } 203 catch (UnknownHostException e) { 204 if (_debug) e.printStackTrace(); 205 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, _fileName); 206 System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+ 207 err.toString()); 208 if (_allowExit) System.exit(-1); 209 } 210 catch (SAXException e) { 211 Exception ex = e.getException(); 212 if (_debug) { 213 if (ex != null) ex.printStackTrace(); 214 e.printStackTrace(); 215 } 216 System.err.print(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)); 217 if (ex != null) 218 System.err.println(ex.getMessage()); 219 else 220 System.err.println(e.getMessage()); 221 if (_allowExit) System.exit(-1); 222 } 223 catch (Exception e) { 224 if (_debug) e.printStackTrace(); 225 System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+ 226 e.getMessage()); 227 if (_allowExit) System.exit(-1); 228 } 229 } 230 231 public static void printUsage() { 232 System.err.println(new ErrorMsg(ErrorMsg.TRANSFORM_USAGE_STR)); 233 if (_allowExit) System.exit(-1); 234 } 235 236 public static void _main(String [] args) { 237 try { 238 if (args.length > 0) { 239 int i; 240 int iterations = -1; 241 boolean uri = false, debug = false; 242 boolean isJarFileSpecified = false; 243 String jarFile = null; 244 245 for (i = 0; i < args.length && args[i].charAt(0) == '-'; i++) { 247 if (args[i].equals("-u")) { 248 uri = true; 249 } 250 else if (args[i].equals("-x")) { 251 debug = true; 252 } 253 else if (args[i].equals("-s")) { 254 _allowExit = false; 255 } 256 else if (args[i].equals("-j")) { 257 isJarFileSpecified = true; 258 jarFile = args[++i]; 259 } 260 else if (args[i].equals("-n")) { 261 try { 262 iterations = Integer.parseInt(args[++i]); 263 } 264 catch (NumberFormatException e) { 265 } 267 } 268 else { 269 printUsage(); 270 } 271 } 272 273 if (args.length - i < 2) printUsage(); 275 276 Transform handler = new Transform(args[i+1], args[i], uri, 278 debug, iterations); 279 handler.setJarFileInputSrc(isJarFileSpecified, jarFile); 280 281 Vector params = new Vector (); 283 for (i += 2; i < args.length; i++) { 284 final int equal = args[i].indexOf('='); 285 if (equal > 0) { 286 final String name = args[i].substring(0, equal); 287 final String value = args[i].substring(equal+1); 288 params.addElement(new Parameter(name, value)); 289 } 290 else { 291 printUsage(); 292 } 293 } 294 295 if (i == args.length) { 296 handler.setParameters(params); 297 handler.doTransform(); 298 if (_allowExit) System.exit(0); 299 } 300 } else { 301 printUsage(); 302 } 303 } 304 catch (Exception e) { 305 e.printStackTrace(); 306 } 307 } 308 } 309 | Popular Tags |