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