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