KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > cmdline > Transform


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
20 package com.sun.org.apache.xalan.internal.xsltc.cmdline;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29 import javax.xml.transform.sax.SAXSource JavaDoc;
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 JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.XMLReader JavaDoc;
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 /**
50  * @author Jacek Ambroziak
51  * @author Santiago Pericas-Geertsen
52  * @author G. Todd Miller
53  * @author Morten Jorgensen
54  */

55 final public class Transform {
56
57     private SerializationHandler _handler;
58
59     private String JavaDoc _fileName;
60     private String JavaDoc _className;
61     private String JavaDoc _jarFileSrc;
62     private boolean _isJarFileSpecified = false;
63     private Vector JavaDoc _params = null;
64     private boolean _uri, _debug;
65     private int _iterations;
66
67     private static boolean _allowExit = true;
68
69     public Transform(String JavaDoc className, String JavaDoc 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 JavaDoc getFileName(){return _fileName;}
79    public String JavaDoc getClassName(){return _className;}
80
81     public void setParameters(Vector JavaDoc params) {
82     _params = params;
83     }
84
85     private void setJarFileInputSrc(boolean flag, String JavaDoc jarFile) {
86     // TODO: at this time we do not do anything with this
87
// information, attempts to add the jarfile to the CLASSPATH
88
// were successful via System.setProperty, but the effects
89
// were not visible to the running JVM. For now we add jarfile
90
// to CLASSPATH in the wrapper script that calls this program.
91
_isJarFileSpecified = flag;
92     // TODO verify jarFile exists...
93
_jarFileSrc = jarFile;
94     }
95
96     private void doTransform() {
97     try {
98             final Class JavaDoc clazz = ObjectFactory.findProviderClass(
99                 _className, ObjectFactory.findClassLoader(), true);
100         final AbstractTranslet translet = (AbstractTranslet)clazz.newInstance();
101             translet.postInitialization();
102
103         // Create a SAX parser and get the XMLReader object it uses
104
final SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
105         try {
106         factory.setFeature(Constants.NAMESPACE_FEATURE,true);
107         }
108         catch (Exception JavaDoc e) {
109         factory.setNamespaceAware(true);
110         }
111         final SAXParser JavaDoc parser = factory.newSAXParser();
112         final XMLReader JavaDoc reader = parser.getXMLReader();
113
114         // Set the DOM's DOM builder as the XMLReader's SAX2 content handler
115
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 JavaDoc(reader, new InputSource JavaDoc(_fileName)),
129                             false, wsfilter, true, false, translet.hasIdCall());
130
131         dom.setDocumentURI(_fileName);
132             translet.prepassDocument(dom);
133
134         // Pass global parameters
135
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         // Transform the document
142
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
211         Exception JavaDoc 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 JavaDoc 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 JavaDoc[] 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 JavaDoc jarFile = null;
244
245         // Parse options starting with '-'
246
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 JavaDoc e) {
265                 // ignore
266
}
267             }
268             else {
269             printUsage();
270             }
271         }
272
273         // Enough arguments left ?
274
if (args.length - i < 2) printUsage();
275
276         // Get document file and class name
277
Transform handler = new Transform(args[i+1], args[i], uri,
278             debug, iterations);
279         handler.setJarFileInputSrc(isJarFileSpecified, jarFile);
280
281         // Parse stylesheet parameters
282
Vector JavaDoc params = new Vector JavaDoc();
283         for (i += 2; i < args.length; i++) {
284             final int equal = args[i].indexOf('=');
285             if (equal > 0) {
286             final String JavaDoc name = args[i].substring(0, equal);
287             final String JavaDoc 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 JavaDoc e) {
305         e.printStackTrace();
306     }
307     }
308 }
309
Popular Tags