KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > trax > SmartTransformerFactoryImpl


1 /*
2  * @(#)$Id: SmartTransformerFactoryImpl.java,v 1.1.2.1 2006/09/19 01:07:39 jeffsuttor Exp $
3  *
4  * The Apache Software License, Version 1.1
5  *
6  *
7  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Xalan" and "Apache Software Foundation" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * nor may "Apache" appear in their name, without prior written
36  * permission of the Apache Software Foundation.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation and was
54  * originally based on software copyright (c) 2001, Sun
55  * Microsystems., http://www.sun.com. For more
56  * information on the Apache Software Foundation, please see
57  * <http://www.apache.org/>.
58  *
59  * @author G. Todd Miller
60  *
61  */

62
63
64 package com.sun.org.apache.xalan.internal.xsltc.trax;
65
66 import javax.xml.transform.ErrorListener JavaDoc;
67 import javax.xml.transform.Source JavaDoc;
68 import javax.xml.transform.Templates JavaDoc;
69 import javax.xml.transform.Transformer JavaDoc;
70 import javax.xml.transform.TransformerConfigurationException JavaDoc;
71 import javax.xml.transform.TransformerException JavaDoc;
72 import javax.xml.transform.URIResolver JavaDoc;
73 import javax.xml.transform.dom.DOMResult JavaDoc;
74 import javax.xml.transform.dom.DOMSource JavaDoc;
75 import javax.xml.transform.sax.SAXResult JavaDoc;
76 import javax.xml.transform.sax.SAXSource JavaDoc;
77 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
78 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
79 import javax.xml.transform.sax.TransformerHandler JavaDoc;
80 import javax.xml.transform.stream.StreamResult JavaDoc;
81 import javax.xml.transform.stream.StreamSource JavaDoc;
82
83 import com.sun.org.apache.xml.internal.utils.ObjectFactory;
84 import org.xml.sax.XMLFilter JavaDoc;
85
86 /**
87  * Implementation of a transformer factory that uses an XSLTC
88  * transformer factory for the creation of Templates objects
89  * and uses the Xalan processor transformer factory for the
90  * creation of Transformer objects.
91  */

92 public class SmartTransformerFactoryImpl extends SAXTransformerFactory JavaDoc
93 {
94
95     private SAXTransformerFactory JavaDoc _xsltcFactory = null;
96     private SAXTransformerFactory JavaDoc _xalanFactory = null;
97     private SAXTransformerFactory JavaDoc _currFactory = null;
98     private ErrorListener JavaDoc _errorlistener = null;
99     private URIResolver JavaDoc _uriresolver = null;
100
101     /**
102      * <p>State of secure processing feature.</p>
103      */

104     private boolean featureSecureProcessing = false;
105
106     /**
107      * implementation of the SmartTransformerFactory. This factory
108      * uses com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory
109      * to return Templates objects; and uses
110      * com.sun.org.apache.xalan.internal.processor.TransformerFactory
111      * to return Transformer objects.
112      */

113     public SmartTransformerFactoryImpl() { }
114
115     private void createXSLTCTransformerFactory() {
116     _xsltcFactory = new TransformerFactoryImpl();
117     _currFactory = _xsltcFactory;
118     }
119
120     private void createXalanTransformerFactory() {
121     final String JavaDoc xalanMessage =
122         "com.sun.org.apache.xalan.internal.xsltc.trax.SmartTransformerFactoryImpl "+
123         "could not create an "+
124         "com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl.";
125     // try to create instance of Xalan factory...
126
try {
127             Class JavaDoc xalanFactClass = ObjectFactory.findProviderClass(
128                 "com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl",
129                 ObjectFactory.findClassLoader(), true);
130         _xalanFactory = (SAXTransformerFactory JavaDoc)
131         xalanFactClass.newInstance();
132     }
133     catch (ClassNotFoundException JavaDoc e) {
134         System.err.println(xalanMessage);
135         }
136     catch (InstantiationException JavaDoc e) {
137         System.err.println(xalanMessage);
138     }
139     catch (IllegalAccessException JavaDoc e) {
140         System.err.println(xalanMessage);
141     }
142     _currFactory = _xalanFactory;
143     }
144
145     public void setErrorListener(ErrorListener JavaDoc listener)
146     throws IllegalArgumentException JavaDoc
147     {
148     _errorlistener = listener;
149     }
150
151     public ErrorListener JavaDoc getErrorListener() {
152     return _errorlistener;
153     }
154
155     public Object JavaDoc getAttribute(String JavaDoc name)
156     throws IllegalArgumentException JavaDoc
157     {
158     // GTM: NB: 'debug' should change to something more unique...
159
if ((name.equals("translet-name")) || (name.equals("debug"))) {
160         if (_xsltcFactory == null) {
161                 createXSLTCTransformerFactory();
162             }
163             return _xsltcFactory.getAttribute(name);
164         }
165         else {
166         if (_xalanFactory == null) {
167             createXalanTransformerFactory();
168         }
169         return _xalanFactory.getAttribute(name);
170         }
171     }
172
173     public void setAttribute(String JavaDoc name, Object JavaDoc value)
174     throws IllegalArgumentException JavaDoc {
175     // GTM: NB: 'debug' should change to something more unique...
176
if ((name.equals("translet-name")) || (name.equals("debug"))) {
177         if (_xsltcFactory == null) {
178                 createXSLTCTransformerFactory();
179             }
180             _xsltcFactory.setAttribute(name, value);
181         }
182         else {
183         if (_xalanFactory == null) {
184             createXalanTransformerFactory();
185         }
186         _xalanFactory.setAttribute(name, value);
187         }
188     }
189
190     /**
191      * <p>Set a feature for this <code>SmartTransformerFactory</code> and <code>Transformer</code>s
192      * or <code>Template</code>s created by this factory.</p>
193      *
194      * <p>
195      * Feature names are fully qualified {@link java.net.URI}s.
196      * Implementations may define their own features.
197      * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
198      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
199      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
200      * </p>
201      *
202      * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p>
203      *
204      * @param name Feature name.
205      * @param value Is feature state <code>true</code> or <code>false</code>.
206      *
207      * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
208      * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
209      * @throws NullPointerException If the <code>name</code> parameter is null.
210      */

211     public void setFeature(String JavaDoc name, boolean value)
212         throws TransformerConfigurationException JavaDoc {
213
214     // feature name cannot be null
215
if (name == null) {
216             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
217             throw new NullPointerException JavaDoc(err.toString());
218     }
219     // secure processing?
220
else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
221         featureSecureProcessing = value;
222         // all done processing feature
223
return;
224     }
225     else {
226         // unknown feature
227
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
228             throw new TransformerConfigurationException JavaDoc(err.toString());
229         }
230     }
231
232     /**
233      * javax.xml.transform.sax.TransformerFactory implementation.
234      * Look up the value of a feature (to see if it is supported).
235      * This method must be updated as the various methods and features of this
236      * class are implemented.
237      *
238      * @param name The feature name
239      * @return 'true' if feature is supported, 'false' if not
240      */

241     public boolean getFeature(String JavaDoc name) {
242     // All supported features should be listed here
243
String JavaDoc[] features = {
244             DOMSource.FEATURE,
245             DOMResult.FEATURE,
246             SAXSource.FEATURE,
247             SAXResult.FEATURE,
248             StreamSource.FEATURE,
249             StreamResult.FEATURE
250         };
251
252         // Inefficient, but it really does not matter in a function like this
253
for (int i=0; i<features.length; i++) {
254             if (name.equals(features[i])) return true;
255     }
256
257     // secure processing?
258
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
259         return featureSecureProcessing;
260     }
261
262         // Feature not supported
263
return false;
264     }
265
266     public URIResolver JavaDoc getURIResolver() {
267     return _uriresolver;
268     }
269
270     public void setURIResolver(URIResolver JavaDoc resolver) {
271     _uriresolver = resolver;
272     }
273
274     public Source JavaDoc getAssociatedStylesheet(Source JavaDoc source, String JavaDoc media,
275                       String JavaDoc title, String JavaDoc charset)
276     throws TransformerConfigurationException JavaDoc
277     {
278     if (_currFactory == null) {
279             createXSLTCTransformerFactory();
280         }
281     return _currFactory.getAssociatedStylesheet(source, media,
282         title, charset);
283     }
284
285     /**
286      * Create a Transformer object that copies the input document to the
287      * result. Uses the com.sun.org.apache.xalan.internal.processor.TransformerFactory.
288      * @return A Transformer object.
289      */

290     public Transformer JavaDoc newTransformer()
291     throws TransformerConfigurationException JavaDoc
292     {
293     if (_xalanFactory == null) {
294             createXalanTransformerFactory();
295         }
296     if (_errorlistener != null) {
297         _xalanFactory.setErrorListener(_errorlistener);
298     }
299     if (_uriresolver != null) {
300         _xalanFactory.setURIResolver(_uriresolver);
301     }
302     _currFactory = _xalanFactory;
303     return _currFactory.newTransformer();
304     }
305
306     /**
307      * Create a Transformer object that from the input stylesheet
308      * Uses the com.sun.org.apache.xalan.internal.processor.TransformerFactory.
309      * @param source the stylesheet.
310      * @return A Transformer object.
311      */

312     public Transformer JavaDoc newTransformer(Source JavaDoc source) throws
313     TransformerConfigurationException JavaDoc
314     {
315         if (_xalanFactory == null) {
316             createXalanTransformerFactory();
317         }
318     if (_errorlistener != null) {
319         _xalanFactory.setErrorListener(_errorlistener);
320     }
321     if (_uriresolver != null) {
322         _xalanFactory.setURIResolver(_uriresolver);
323     }
324     _currFactory = _xalanFactory;
325     return _currFactory.newTransformer(source);
326     }
327
328     /**
329      * Create a Templates object that from the input stylesheet
330      * Uses the com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
331      * @param source the stylesheet.
332      * @return A Templates object.
333      */

334     public Templates JavaDoc newTemplates(Source JavaDoc source)
335     throws TransformerConfigurationException JavaDoc
336     {
337         if (_xsltcFactory == null) {
338             createXSLTCTransformerFactory();
339         }
340     if (_errorlistener != null) {
341         _xsltcFactory.setErrorListener(_errorlistener);
342     }
343     if (_uriresolver != null) {
344         _xsltcFactory.setURIResolver(_uriresolver);
345     }
346     _currFactory = _xsltcFactory;
347     return _currFactory.newTemplates(source);
348     }
349
350     /**
351      * Get a TemplatesHandler object that can process SAX ContentHandler
352      * events into a Templates object. Uses the
353      * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
354      */

355     public TemplatesHandler JavaDoc newTemplatesHandler()
356     throws TransformerConfigurationException JavaDoc
357     {
358         if (_xsltcFactory == null) {
359             createXSLTCTransformerFactory();
360         }
361     if (_errorlistener != null) {
362         _xsltcFactory.setErrorListener(_errorlistener);
363     }
364     if (_uriresolver != null) {
365         _xsltcFactory.setURIResolver(_uriresolver);
366     }
367     return _xsltcFactory.newTemplatesHandler();
368     }
369
370     /**
371      * Get a TransformerHandler object that can process SAX ContentHandler
372      * events based on a copy transformer.
373      * Uses com.sun.org.apache.xalan.internal.processor.TransformerFactory.
374      */

375     public TransformerHandler JavaDoc newTransformerHandler()
376     throws TransformerConfigurationException JavaDoc
377     {
378         if (_xalanFactory == null) {
379             createXalanTransformerFactory();
380         }
381     if (_errorlistener != null) {
382         _xalanFactory.setErrorListener(_errorlistener);
383     }
384     if (_uriresolver != null) {
385         _xalanFactory.setURIResolver(_uriresolver);
386     }
387     return _xalanFactory.newTransformerHandler();
388     }
389
390     /**
391      * Get a TransformerHandler object that can process SAX ContentHandler
392      * events based on a transformer specified by the stylesheet Source.
393      * Uses com.sun.org.apache.xalan.internal.processor.TransformerFactory.
394      */

395     public TransformerHandler JavaDoc newTransformerHandler(Source JavaDoc src)
396     throws TransformerConfigurationException JavaDoc
397     {
398         if (_xalanFactory == null) {
399             createXalanTransformerFactory();
400         }
401     if (_errorlistener != null) {
402         _xalanFactory.setErrorListener(_errorlistener);
403     }
404     if (_uriresolver != null) {
405         _xalanFactory.setURIResolver(_uriresolver);
406     }
407     return _xalanFactory.newTransformerHandler(src);
408     }
409
410
411     /**
412      * Get a TransformerHandler object that can process SAX ContentHandler
413      * events based on a transformer specified by the stylesheet Source.
414      * Uses com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
415      */

416     public TransformerHandler JavaDoc newTransformerHandler(Templates JavaDoc templates)
417     throws TransformerConfigurationException JavaDoc
418     {
419         if (_xsltcFactory == null) {
420             createXSLTCTransformerFactory();
421         }
422     if (_errorlistener != null) {
423         _xsltcFactory.setErrorListener(_errorlistener);
424     }
425     if (_uriresolver != null) {
426         _xsltcFactory.setURIResolver(_uriresolver);
427     }
428         return _xsltcFactory.newTransformerHandler(templates);
429     }
430
431
432     /**
433      * Create an XMLFilter that uses the given source as the
434      * transformation instructions. Uses
435      * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
436      */

437     public XMLFilter JavaDoc newXMLFilter(Source JavaDoc src)
438     throws TransformerConfigurationException JavaDoc {
439         if (_xsltcFactory == null) {
440             createXSLTCTransformerFactory();
441         }
442     if (_errorlistener != null) {
443         _xsltcFactory.setErrorListener(_errorlistener);
444     }
445     if (_uriresolver != null) {
446         _xsltcFactory.setURIResolver(_uriresolver);
447     }
448     Templates JavaDoc templates = _xsltcFactory.newTemplates(src);
449     if (templates == null ) return null;
450     return newXMLFilter(templates);
451     }
452
453     /*
454      * Create an XMLFilter that uses the given source as the
455      * transformation instructions. Uses
456      * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
457      */

458     public XMLFilter JavaDoc newXMLFilter(Templates JavaDoc templates)
459     throws TransformerConfigurationException JavaDoc {
460     try {
461             return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
462         }
463         catch(TransformerConfigurationException JavaDoc e1) {
464             if (_xsltcFactory == null) {
465                 createXSLTCTransformerFactory();
466             }
467         ErrorListener JavaDoc errorListener = _xsltcFactory.getErrorListener();
468             if(errorListener != null) {
469                 try {
470                     errorListener.fatalError(e1);
471                     return null;
472                 }
473                 catch( TransformerException JavaDoc e2) {
474                     new TransformerConfigurationException JavaDoc(e2);
475                 }
476             }
477             throw e1;
478         }
479     }
480 }
481
Popular Tags