KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > trax > SmartTransformerFactoryImpl


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: SmartTransformerFactoryImpl.java,v 1.11 2004/02/23 10:29:36 aruny Exp $
18  */

19
20
21 package org.apache.xalan.xsltc.trax;
22
23 import javax.xml.transform.ErrorListener JavaDoc;
24 import javax.xml.transform.Source JavaDoc;
25 import javax.xml.transform.Templates JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.URIResolver JavaDoc;
30 import javax.xml.transform.dom.DOMResult JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.sax.SAXResult JavaDoc;
33 import javax.xml.transform.sax.SAXSource JavaDoc;
34 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
35 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
36 import javax.xml.transform.sax.TransformerHandler JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
39
40 import org.xml.sax.XMLFilter JavaDoc;
41
42 /**
43  * Implementation of a transformer factory that uses an XSLTC
44  * transformer factory for the creation of Templates objects
45  * and uses the Xalan processor transformer factory for the
46  * creation of Transformer objects.
47  * @author G. Todd Miller
48  */

49 public class SmartTransformerFactoryImpl extends SAXTransformerFactory JavaDoc
50 {
51
52     private SAXTransformerFactory JavaDoc _xsltcFactory = null;
53     private SAXTransformerFactory JavaDoc _xalanFactory = null;
54     private SAXTransformerFactory JavaDoc _currFactory = null;
55     private ErrorListener JavaDoc _errorlistener = null;
56     private URIResolver JavaDoc _uriresolver = null;
57
58     /**
59      * implementation of the SmartTransformerFactory. This factory
60      * uses org.apache.xalan.xsltc.trax.TransformerFactory
61      * to return Templates objects; and uses
62      * org.apache.xalan.processor.TransformerFactory
63      * to return Transformer objects.
64      */

65     public SmartTransformerFactoryImpl() { }
66
67     private void createXSLTCTransformerFactory() {
68     _xsltcFactory = new TransformerFactoryImpl();
69     _currFactory = _xsltcFactory;
70     }
71
72     private void createXalanTransformerFactory() {
73     final String JavaDoc xalanMessage =
74         "org.apache.xalan.xsltc.trax.SmartTransformerFactoryImpl "+
75         "could not create an "+
76         "org.apache.xalan.processor.TransformerFactoryImpl.";
77     // try to create instance of Xalan factory...
78
try {
79             Class JavaDoc xalanFactClass = ObjectFactory.findProviderClass(
80                 "org.apache.xalan.processor.TransformerFactoryImpl",
81                 ObjectFactory.findClassLoader(), true);
82         _xalanFactory = (SAXTransformerFactory JavaDoc)
83         xalanFactClass.newInstance();
84     }
85     catch (ClassNotFoundException JavaDoc e) {
86         System.err.println(xalanMessage);
87         }
88     catch (InstantiationException JavaDoc e) {
89         System.err.println(xalanMessage);
90     }
91     catch (IllegalAccessException JavaDoc e) {
92         System.err.println(xalanMessage);
93     }
94     _currFactory = _xalanFactory;
95     }
96
97     public void setErrorListener(ErrorListener JavaDoc listener)
98     throws IllegalArgumentException JavaDoc
99     {
100     _errorlistener = listener;
101     }
102
103     public ErrorListener JavaDoc getErrorListener() {
104     return _errorlistener;
105     }
106
107     public Object JavaDoc getAttribute(String JavaDoc name)
108     throws IllegalArgumentException JavaDoc
109     {
110     // GTM: NB: 'debug' should change to something more unique...
111
if ((name.equals("translet-name")) || (name.equals("debug"))) {
112         if (_xsltcFactory == null) {
113                 createXSLTCTransformerFactory();
114             }
115             return _xsltcFactory.getAttribute(name);
116         }
117         else {
118         if (_xalanFactory == null) {
119             createXalanTransformerFactory();
120         }
121         return _xalanFactory.getAttribute(name);
122         }
123     }
124
125     public void setAttribute(String JavaDoc name, Object JavaDoc value)
126     throws IllegalArgumentException JavaDoc {
127     // GTM: NB: 'debug' should change to something more unique...
128
if ((name.equals("translet-name")) || (name.equals("debug"))) {
129         if (_xsltcFactory == null) {
130                 createXSLTCTransformerFactory();
131             }
132             _xsltcFactory.setAttribute(name, value);
133         }
134         else {
135         if (_xalanFactory == null) {
136             createXalanTransformerFactory();
137         }
138         _xalanFactory.setAttribute(name, value);
139         }
140     }
141
142     /**
143      * javax.xml.transform.sax.TransformerFactory implementation.
144      * Look up the value of a feature (to see if it is supported).
145      * This method must be updated as the various methods and features of this
146      * class are implemented.
147      *
148      * @param name The feature name
149      * @return 'true' if feature is supported, 'false' if not
150      */

151     public boolean getFeature(String JavaDoc name) {
152     // All supported features should be listed here
153
String JavaDoc[] features = {
154             DOMSource.FEATURE,
155             DOMResult.FEATURE,
156             SAXSource.FEATURE,
157             SAXResult.FEATURE,
158             StreamSource.FEATURE,
159             StreamResult.FEATURE
160         };
161
162         // Inefficient, but it really does not matter in a function like this
163
for (int i=0; i<features.length; i++) {
164             if (name.equals(features[i])) return true;
165     }
166
167         // Feature not supported
168
return false;
169     }
170
171     public URIResolver JavaDoc getURIResolver() {
172     return _uriresolver;
173     }
174
175     public void setURIResolver(URIResolver JavaDoc resolver) {
176     _uriresolver = resolver;
177     }
178
179     public Source JavaDoc getAssociatedStylesheet(Source JavaDoc source, String JavaDoc media,
180                       String JavaDoc title, String JavaDoc charset)
181     throws TransformerConfigurationException JavaDoc
182     {
183     if (_currFactory == null) {
184             createXSLTCTransformerFactory();
185         }
186     return _currFactory.getAssociatedStylesheet(source, media,
187         title, charset);
188     }
189
190     /**
191      * Create a Transformer object that copies the input document to the
192      * result. Uses the org.apache.xalan.processor.TransformerFactory.
193      * @return A Transformer object.
194      */

195     public Transformer JavaDoc newTransformer()
196     throws TransformerConfigurationException JavaDoc
197     {
198     if (_xalanFactory == null) {
199             createXalanTransformerFactory();
200         }
201     if (_errorlistener != null) {
202         _xalanFactory.setErrorListener(_errorlistener);
203     }
204     if (_uriresolver != null) {
205         _xalanFactory.setURIResolver(_uriresolver);
206     }
207     _currFactory = _xalanFactory;
208     return _currFactory.newTransformer();
209     }
210
211     /**
212      * Create a Transformer object that from the input stylesheet
213      * Uses the org.apache.xalan.processor.TransformerFactory.
214      * @param source the stylesheet.
215      * @return A Transformer object.
216      */

217     public Transformer JavaDoc newTransformer(Source JavaDoc source) throws
218     TransformerConfigurationException JavaDoc
219     {
220         if (_xalanFactory == null) {
221             createXalanTransformerFactory();
222         }
223     if (_errorlistener != null) {
224         _xalanFactory.setErrorListener(_errorlistener);
225     }
226     if (_uriresolver != null) {
227         _xalanFactory.setURIResolver(_uriresolver);
228     }
229     _currFactory = _xalanFactory;
230     return _currFactory.newTransformer(source);
231     }
232
233     /**
234      * Create a Templates object that from the input stylesheet
235      * Uses the org.apache.xalan.xsltc.trax.TransformerFactory.
236      * @param source the stylesheet.
237      * @return A Templates object.
238      */

239     public Templates JavaDoc newTemplates(Source JavaDoc source)
240     throws TransformerConfigurationException JavaDoc
241     {
242         if (_xsltcFactory == null) {
243             createXSLTCTransformerFactory();
244         }
245     if (_errorlistener != null) {
246         _xsltcFactory.setErrorListener(_errorlistener);
247     }
248     if (_uriresolver != null) {
249         _xsltcFactory.setURIResolver(_uriresolver);
250     }
251     _currFactory = _xsltcFactory;
252     return _currFactory.newTemplates(source);
253     }
254
255     /**
256      * Get a TemplatesHandler object that can process SAX ContentHandler
257      * events into a Templates object. Uses the
258      * org.apache.xalan.xsltc.trax.TransformerFactory.
259      */

260     public TemplatesHandler JavaDoc newTemplatesHandler()
261     throws TransformerConfigurationException JavaDoc
262     {
263         if (_xsltcFactory == null) {
264             createXSLTCTransformerFactory();
265         }
266     if (_errorlistener != null) {
267         _xsltcFactory.setErrorListener(_errorlistener);
268     }
269     if (_uriresolver != null) {
270         _xsltcFactory.setURIResolver(_uriresolver);
271     }
272     return _xsltcFactory.newTemplatesHandler();
273     }
274
275     /**
276      * Get a TransformerHandler object that can process SAX ContentHandler
277      * events based on a copy transformer.
278      * Uses org.apache.xalan.processor.TransformerFactory.
279      */

280     public TransformerHandler JavaDoc newTransformerHandler()
281     throws TransformerConfigurationException JavaDoc
282     {
283         if (_xalanFactory == null) {
284             createXalanTransformerFactory();
285         }
286     if (_errorlistener != null) {
287         _xalanFactory.setErrorListener(_errorlistener);
288     }
289     if (_uriresolver != null) {
290         _xalanFactory.setURIResolver(_uriresolver);
291     }
292     return _xalanFactory.newTransformerHandler();
293     }
294
295     /**
296      * Get a TransformerHandler object that can process SAX ContentHandler
297      * events based on a transformer specified by the stylesheet Source.
298      * Uses org.apache.xalan.processor.TransformerFactory.
299      */

300     public TransformerHandler JavaDoc newTransformerHandler(Source JavaDoc src)
301     throws TransformerConfigurationException JavaDoc
302     {
303         if (_xalanFactory == null) {
304             createXalanTransformerFactory();
305         }
306     if (_errorlistener != null) {
307         _xalanFactory.setErrorListener(_errorlistener);
308     }
309     if (_uriresolver != null) {
310         _xalanFactory.setURIResolver(_uriresolver);
311     }
312     return _xalanFactory.newTransformerHandler(src);
313     }
314
315
316     /**
317      * Get a TransformerHandler object that can process SAX ContentHandler
318      * events based on a transformer specified by the stylesheet Source.
319      * Uses org.apache.xalan.xsltc.trax.TransformerFactory.
320      */

321     public TransformerHandler JavaDoc newTransformerHandler(Templates JavaDoc templates)
322     throws TransformerConfigurationException JavaDoc
323     {
324         if (_xsltcFactory == null) {
325             createXSLTCTransformerFactory();
326         }
327     if (_errorlistener != null) {
328         _xsltcFactory.setErrorListener(_errorlistener);
329     }
330     if (_uriresolver != null) {
331         _xsltcFactory.setURIResolver(_uriresolver);
332     }
333         return _xsltcFactory.newTransformerHandler(templates);
334     }
335
336
337     /**
338      * Create an XMLFilter that uses the given source as the
339      * transformation instructions. Uses
340      * org.apache.xalan.xsltc.trax.TransformerFactory.
341      */

342     public XMLFilter JavaDoc newXMLFilter(Source JavaDoc src)
343     throws TransformerConfigurationException JavaDoc {
344         if (_xsltcFactory == null) {
345             createXSLTCTransformerFactory();
346         }
347     if (_errorlistener != null) {
348         _xsltcFactory.setErrorListener(_errorlistener);
349     }
350     if (_uriresolver != null) {
351         _xsltcFactory.setURIResolver(_uriresolver);
352     }
353     Templates JavaDoc templates = _xsltcFactory.newTemplates(src);
354     if (templates == null ) return null;
355     return newXMLFilter(templates);
356     }
357
358     /*
359      * Create an XMLFilter that uses the given source as the
360      * transformation instructions. Uses
361      * org.apache.xalan.xsltc.trax.TransformerFactory.
362      */

363     public XMLFilter JavaDoc newXMLFilter(Templates JavaDoc templates)
364     throws TransformerConfigurationException JavaDoc {
365     try {
366             return new org.apache.xalan.xsltc.trax.TrAXFilter(templates);
367         }
368         catch(TransformerConfigurationException JavaDoc e1) {
369             if (_xsltcFactory == null) {
370                 createXSLTCTransformerFactory();
371             }
372         ErrorListener JavaDoc errorListener = _xsltcFactory.getErrorListener();
373             if(errorListener != null) {
374                 try {
375                     errorListener.fatalError(e1);
376                     return null;
377                 }
378                 catch( TransformerException JavaDoc e2) {
379                     new TransformerConfigurationException JavaDoc(e2);
380                 }
381             }
382             throw e1;
383         }
384     }
385 }
386
Popular Tags