KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > processor > TransformerFactoryImpl


1 /*
2  * Copyright 1999-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: TransformerFactoryImpl.java,v 1.56 2004/02/11 18:15:50 minchau Exp $
18  */

19 package org.apache.xalan.processor;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.xml.transform.ErrorListener JavaDoc;
28 import javax.xml.transform.Source JavaDoc;
29 import javax.xml.transform.Templates JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.URIResolver JavaDoc;
34 import javax.xml.transform.dom.DOMResult JavaDoc;
35 import javax.xml.transform.dom.DOMSource JavaDoc;
36 import javax.xml.transform.sax.SAXResult JavaDoc;
37 import javax.xml.transform.sax.SAXSource JavaDoc;
38 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
39 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
40 import javax.xml.transform.sax.TransformerHandler JavaDoc;
41 import javax.xml.transform.stream.StreamResult JavaDoc;
42 import javax.xml.transform.stream.StreamSource JavaDoc;
43
44 import org.apache.xalan.res.XSLMessages;
45 import org.apache.xalan.res.XSLTErrorResources;
46 import org.apache.xalan.transformer.TrAXFilter;
47 import org.apache.xalan.transformer.TransformerIdentityImpl;
48 import org.apache.xalan.transformer.TransformerImpl;
49 import org.apache.xalan.transformer.XalanProperties;
50
51 import org.apache.xml.dtm.ref.sax2dtm.SAX2DTM;
52 import org.apache.xml.utils.DefaultErrorHandler;
53 import org.apache.xml.utils.SystemIDResolver;
54 import org.apache.xml.utils.TreeWalker;
55 import org.apache.xml.utils.StylesheetPIHandler;
56 import org.apache.xml.utils.StopParseException;
57
58 import org.w3c.dom.Node JavaDoc;
59
60 import org.xml.sax.InputSource JavaDoc;
61 import org.xml.sax.XMLFilter JavaDoc;
62 import org.xml.sax.XMLReader JavaDoc;
63 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
64
65 /**
66  * The TransformerFactoryImpl, which implements the TRaX TransformerFactory
67  * interface, processes XSLT stylesheets into a Templates object
68  * (a StylesheetRoot).
69  */

70 public class TransformerFactoryImpl extends SAXTransformerFactory JavaDoc
71 {
72
73   /**
74    * The path/filename of the property file: XSLTInfo.properties
75    * Maintenance note: see also
76    * <code>org.apache.xpath.functions.FuncSystemProperty.XSLT_PROPERTIES</code>
77    */

78   public static final String JavaDoc XSLT_PROPERTIES =
79     "org/apache/xalan/res/XSLTInfo.properties";
80
81   /**
82    * Constructor TransformerFactoryImpl
83    *
84    */

85   public TransformerFactoryImpl()
86   {
87   }
88
89   /** Static string to be used for incremental feature */
90   public static final String JavaDoc FEATURE_INCREMENTAL =
91                              "http://xml.apache.org/xalan/features/incremental";
92
93   /** Static string to be used for optimize feature */
94   public static final String JavaDoc FEATURE_OPTIMIZE =
95                              "http://xml.apache.org/xalan/features/optimize";
96
97   /** Static string to be used for source_location feature */
98   public static final String JavaDoc FEATURE_SOURCE_LOCATION =
99                              XalanProperties.SOURCE_LOCATION;
100
101   public javax.xml.transform.Templates JavaDoc processFromNode(Node JavaDoc node)
102           throws TransformerConfigurationException JavaDoc
103   {
104
105     try
106     {
107       TemplatesHandler JavaDoc builder = newTemplatesHandler();
108       TreeWalker walker = new TreeWalker(builder,
109                                          new org.apache.xml.utils.DOM2Helper(),
110                                          builder.getSystemId());
111
112       walker.traverse(node);
113
114       return builder.getTemplates();
115     }
116     catch (org.xml.sax.SAXException JavaDoc se)
117     {
118       if (m_errorListener != null)
119       {
120         try
121         {
122           m_errorListener.fatalError(new TransformerException JavaDoc(se));
123         }
124         catch (TransformerException JavaDoc ex)
125         {
126           throw new TransformerConfigurationException JavaDoc(ex);
127         }
128
129         return null;
130       }
131       else
132
133         // Should remove this later... but right now diagnostics from
134
// TransformerConfigurationException are not good.
135
// se.printStackTrace();
136
throw new TransformerConfigurationException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se); //"processFromNode failed",
137
//se);
138
}
139     catch (TransformerConfigurationException JavaDoc tce)
140     {
141       // Assume it's already been reported to the error listener.
142
throw tce;
143     }
144    /* catch (TransformerException tce)
145     {
146       // Assume it's already been reported to the error listener.
147       throw new TransformerConfigurationException(tce.getMessage(), tce);
148     }*/

149     catch (Exception JavaDoc e)
150     {
151       if (m_errorListener != null)
152       {
153         try
154         {
155           m_errorListener.fatalError(new TransformerException JavaDoc(e));
156         }
157         catch (TransformerException JavaDoc ex)
158         {
159           throw new TransformerConfigurationException JavaDoc(ex);
160         }
161
162         return null;
163       }
164       else
165
166         // Should remove this later... but right now diagnostics from
167
// TransformerConfigurationException are not good.
168
// se.printStackTrace();
169
throw new TransformerConfigurationException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e); //"processFromNode failed",
170
//e);
171
}
172   }
173
174   /**
175    * The systemID that was specified in
176    * processFromNode(Node node, String systemID).
177    */

178   private String JavaDoc m_DOMsystemID = null;
179
180   /**
181    * The systemID that was specified in
182    * processFromNode(Node node, String systemID).
183    *
184    * @return The systemID, or null.
185    */

186   String JavaDoc getDOMsystemID()
187   {
188     return m_DOMsystemID;
189   }
190
191   /**
192    * Process the stylesheet from a DOM tree, if the
193    * processor supports the "http://xml.org/trax/features/dom/input"
194    * feature.
195    *
196    * @param node A DOM tree which must contain
197    * valid transform instructions that this processor understands.
198    * @param systemID The systemID from where xsl:includes and xsl:imports
199    * should be resolved from.
200    *
201    * @return A Templates object capable of being used for transformation purposes.
202    *
203    * @throws TransformerConfigurationException
204    */

205   javax.xml.transform.Templates JavaDoc processFromNode(Node JavaDoc node, String JavaDoc systemID)
206           throws TransformerConfigurationException JavaDoc
207   {
208
209     m_DOMsystemID = systemID;
210
211     return processFromNode(node);
212   }
213
214   /**
215    * Get InputSource specification(s) that are associated with the
216    * given document specified in the source param,
217    * via the xml-stylesheet processing instruction
218    * (see http://www.w3.org/TR/xml-stylesheet/), and that matches
219    * the given criteria. Note that it is possible to return several stylesheets
220    * that match the criteria, in which case they are applied as if they were
221    * a list of imports or cascades.
222    *
223    * <p>Note that DOM2 has it's own mechanism for discovering stylesheets.
224    * Therefore, there isn't a DOM version of this method.</p>
225    *
226    *
227    * @param source The XML source that is to be searched.
228    * @param media The media attribute to be matched. May be null, in which
229    * case the prefered templates will be used (i.e. alternate = no).
230    * @param title The value of the title attribute to match. May be null.
231    * @param charset The value of the charset attribute to match. May be null.
232    *
233    * @return A Source object capable of being used to create a Templates object.
234    *
235    * @throws TransformerConfigurationException
236    */

237   public Source JavaDoc getAssociatedStylesheet(
238           Source JavaDoc source, String JavaDoc media, String JavaDoc title, String JavaDoc charset)
239             throws TransformerConfigurationException JavaDoc
240   {
241
242     String JavaDoc baseID;
243     InputSource JavaDoc isource = null;
244     Node JavaDoc node = null;
245     XMLReader JavaDoc reader = null;
246
247     if (source instanceof DOMSource JavaDoc)
248     {
249       DOMSource JavaDoc dsource = (DOMSource JavaDoc) source;
250
251       node = dsource.getNode();
252       baseID = dsource.getSystemId();
253     }
254     else
255     {
256       isource = SAXSource.sourceToInputSource(source);
257       baseID = isource.getSystemId();
258     }
259
260     // What I try to do here is parse until the first startElement
261
// is found, then throw a special exception in order to terminate
262
// the parse.
263
StylesheetPIHandler handler = new StylesheetPIHandler(baseID, media,
264                                     title, charset);
265     
266     // Use URIResolver. Patch from Dmitri Ilyin
267
if (m_uriResolver != null)
268     {
269       handler.setURIResolver(m_uriResolver);
270     }
271
272     try
273     {
274       if (null != node)
275       {
276         TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), baseID);
277
278         walker.traverse(node);
279       }
280       else
281       {
282
283         // Use JAXP1.1 ( if possible )
284
try
285         {
286           javax.xml.parsers.SAXParserFactory JavaDoc factory =
287             javax.xml.parsers.SAXParserFactory.newInstance();
288
289           factory.setNamespaceAware(true);
290
291           javax.xml.parsers.SAXParser JavaDoc jaxpParser = factory.newSAXParser();
292
293           reader = jaxpParser.getXMLReader();
294         }
295         catch (javax.xml.parsers.ParserConfigurationException JavaDoc ex)
296         {
297           throw new org.xml.sax.SAXException JavaDoc(ex);
298         }
299         catch (javax.xml.parsers.FactoryConfigurationError JavaDoc ex1)
300         {
301           throw new org.xml.sax.SAXException JavaDoc(ex1.toString());
302         }
303         catch (NoSuchMethodError JavaDoc ex2){}
304         catch (AbstractMethodError JavaDoc ame){}
305
306         if (null == reader)
307         {
308           reader = XMLReaderFactory.createXMLReader();
309         }
310
311         // Need to set options!
312
reader.setContentHandler(handler);
313         reader.parse(isource);
314       }
315     }
316     catch (StopParseException spe)
317     {
318
319       // OK, good.
320
}
321     catch (org.xml.sax.SAXException JavaDoc se)
322     {
323       throw new TransformerConfigurationException JavaDoc(
324         "getAssociatedStylesheets failed", se);
325     }
326     catch (IOException JavaDoc ioe)
327     {
328       throw new TransformerConfigurationException JavaDoc(
329         "getAssociatedStylesheets failed", ioe);
330     }
331
332     return handler.getAssociatedStylesheet();
333   }
334
335   /**
336    * Create a new Transformer object that performs a copy
337    * of the source to the result.
338    *
339    * @param source An object that holds a URI, input stream, etc.
340    *
341    * @return A Transformer object that may be used to perform a transformation
342    * in a single thread, never null.
343    *
344    * @throws TransformerConfigurationException May throw this during
345    * the parse when it is constructing the
346    * Templates object and fails.
347    */

348   public TemplatesHandler JavaDoc newTemplatesHandler()
349           throws TransformerConfigurationException JavaDoc
350   {
351     return new StylesheetHandler(this);
352   }
353
354   /**
355    * Look up the value of a feature.
356    *
357    * <p>The feature name is any fully-qualified URI. It is
358    * possible for an TransformerFactory to recognize a feature name but
359    * to be unable to return its value; this is especially true
360    * in the case of an adapter for a SAX1 Parser, which has
361    * no way of knowing whether the underlying parser is
362    * validating, for example.</p>
363    *
364    * @param name The feature name, which is a fully-qualified URI.
365    * @return The current state of the feature (true or false).
366    */

367   public boolean getFeature(String JavaDoc name)
368   {
369
370     // Try first with identity comparison, which
371
// will be faster.
372
if ((DOMResult.FEATURE == name) || (DOMSource.FEATURE == name)
373             || (SAXResult.FEATURE == name) || (SAXSource.FEATURE == name)
374             || (StreamResult.FEATURE == name)
375             || (StreamSource.FEATURE == name)
376             || (SAXTransformerFactory.FEATURE == name)
377             || (SAXTransformerFactory.FEATURE_XMLFILTER == name))
378       return true;
379     else if ((DOMResult.FEATURE.equals(name))
380              || (DOMSource.FEATURE.equals(name))
381              || (SAXResult.FEATURE.equals(name))
382              || (SAXSource.FEATURE.equals(name))
383              || (StreamResult.FEATURE.equals(name))
384              || (StreamSource.FEATURE.equals(name))
385              || (SAXTransformerFactory.FEATURE.equals(name))
386              || (SAXTransformerFactory.FEATURE_XMLFILTER.equals(name)))
387       return true;
388     else
389       return false;
390   }
391   
392   public static boolean m_optimize = true;
393   
394   /** Flag set by FEATURE_SOURCE_LOCATION.
395    * This feature specifies whether the transformation phase should
396    * keep track of line and column numbers for the input source
397    * document. Note that this works only when that
398    * information is available from the source -- in other words, if you
399    * pass in a DOM, there's little we can do for you.
400    *
401    * The default is false. Setting it true may significantly
402    * increase storage cost per node.
403    */

404   public static boolean m_source_location = false;
405   
406   /**
407    * Allows the user to set specific attributes on the underlying
408    * implementation.
409    *
410    * @param name The name of the attribute.
411    * @param value The value of the attribute; Boolean or String="true"|"false"
412    *
413    * @throws IllegalArgumentException thrown if the underlying
414    * implementation doesn't recognize the attribute.
415    */

416   public void setAttribute(String JavaDoc name, Object JavaDoc value)
417           throws IllegalArgumentException JavaDoc
418   {
419     if (name.equals(FEATURE_INCREMENTAL))
420     {
421       if(value instanceof Boolean JavaDoc)
422       {
423         // Accept a Boolean object..
424
org.apache.xml.dtm.DTMManager.setIncremental(((Boolean JavaDoc)value).booleanValue());
425       }
426       else if(value instanceof String JavaDoc)
427       {
428         // .. or a String object
429
org.apache.xml.dtm.DTMManager.setIncremental((new Boolean JavaDoc((String JavaDoc)value)).booleanValue());
430       }
431       else
432       {
433         // Give a more meaningful error message
434
throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object JavaDoc[]{name, value})); //name + " bad value " + value);
435
}
436     }
437     else if (name.equals(FEATURE_OPTIMIZE))
438     {
439       if(value instanceof Boolean JavaDoc)
440       {
441         // Accept a Boolean object..
442
m_optimize = ((Boolean JavaDoc)value).booleanValue();
443       }
444       else if(value instanceof String JavaDoc)
445       {
446         // .. or a String object
447
m_optimize = (new Boolean JavaDoc((String JavaDoc)value)).booleanValue();
448       }
449       else
450       {
451         // Give a more meaningful error message
452
throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object JavaDoc[]{name, value})); //name + " bad value " + value);
453
}
454     }
455     
456     // Custom Xalan feature: annotate DTM with SAX source locator fields.
457
// This gets used during SAX2DTM instantiation.
458
//
459
// %REVIEW% Should the name of this field really be in XalanProperties?
460
// %REVIEW% I hate that it's a global static, but didn't want to change APIs yet.
461
else if(name.equals(FEATURE_SOURCE_LOCATION))
462     {
463       if(value instanceof Boolean JavaDoc)
464       {
465         // Accept a Boolean object..
466
m_source_location = ((Boolean JavaDoc)value).booleanValue();
467         // Pass the source location property to SAX2DTM, where it actually gets used.
468
SAX2DTM.setUseSourceLocation(m_source_location);
469       }
470       else if(value instanceof String JavaDoc)
471       {
472         // .. or a String object
473
m_source_location = (new Boolean JavaDoc((String JavaDoc)value)).booleanValue();
474         SAX2DTM.setUseSourceLocation(m_source_location);
475       }
476       else
477       {
478         // Give a more meaningful error message
479
throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object JavaDoc[]{name, value})); //name + " bad value " + value);
480
}
481     }
482     
483     else
484     {
485       throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_NOT_SUPPORTED, new Object JavaDoc[]{name})); //name + "not supported");
486
}
487   }
488
489   /**
490    * Allows the user to retrieve specific attributes on the underlying
491    * implementation.
492    *
493    * @param name The name of the attribute.
494    * @return value The value of the attribute.
495    *
496    * @throws IllegalArgumentException thrown if the underlying
497    * implementation doesn't recognize the attribute.
498    */

499   public Object JavaDoc getAttribute(String JavaDoc name) throws IllegalArgumentException JavaDoc
500   {
501     if (name.equals(FEATURE_INCREMENTAL))
502     {
503       return new Boolean JavaDoc(org.apache.xml.dtm.DTMManager.getIncremental());
504     }
505     else if (name.equals(FEATURE_OPTIMIZE))
506     {
507       return new Boolean JavaDoc(m_optimize);
508     }
509     else if (name.equals(FEATURE_SOURCE_LOCATION))
510     {
511       return new Boolean JavaDoc(m_source_location);
512     }
513     else
514       throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_ATTRIB_VALUE_NOT_RECOGNIZED, new Object JavaDoc[]{name})); //name + " attribute not recognized");
515
}
516
517   /**
518    * Create an XMLFilter that uses the given source as the
519    * transformation instructions.
520    *
521    * @param src The source of the transformation instructions.
522    *
523    * @return An XMLFilter object, or null if this feature is not supported.
524    *
525    * @throws TransformerConfigurationException
526    */

527   public XMLFilter JavaDoc newXMLFilter(Source JavaDoc src)
528           throws TransformerConfigurationException JavaDoc
529   {
530
531     Templates JavaDoc templates = newTemplates(src);
532     if( templates==null ) return null;
533     
534     return newXMLFilter(templates);
535   }
536
537   /**
538    * Create an XMLFilter that uses the given source as the
539    * transformation instructions.
540    *
541    * @param src The source of the transformation instructions.
542    *
543    * @param templates non-null reference to Templates object.
544    *
545    * @return An XMLFilter object, or null if this feature is not supported.
546    *
547    * @throws TransformerConfigurationException
548    */

549   public XMLFilter JavaDoc newXMLFilter(Templates JavaDoc templates)
550           throws TransformerConfigurationException JavaDoc
551   {
552     try {
553       return new TrAXFilter(templates);
554     } catch( TransformerConfigurationException JavaDoc ex ) {
555       if( m_errorListener != null) {
556         try {
557           m_errorListener.fatalError( ex );
558           return null;
559         } catch( TransformerException JavaDoc ex1 ) {
560           new TransformerConfigurationException JavaDoc(ex1);
561         }
562       }
563       throw ex;
564     }
565   }
566
567   /**
568    * Get a TransformerHandler object that can process SAX
569    * ContentHandler events into a Result, based on the transformation
570    * instructions specified by the argument.
571    *
572    * @param src The source of the transformation instructions.
573    *
574    * @return TransformerHandler ready to transform SAX events.
575    *
576    * @throws TransformerConfigurationException
577    */

578   public TransformerHandler JavaDoc newTransformerHandler(Source JavaDoc src)
579           throws TransformerConfigurationException JavaDoc
580   {
581
582     Templates JavaDoc templates = newTemplates(src);
583     if( templates==null ) return null;
584     
585     return newTransformerHandler(templates);
586   }
587
588   /**
589    * Get a TransformerHandler object that can process SAX
590    * ContentHandler events into a Result, based on the Templates argument.
591    *
592    * @param templates The source of the transformation instructions.
593    *
594    * @return TransformerHandler ready to transform SAX events.
595    * @throws TransformerConfigurationException
596    */

597   public TransformerHandler JavaDoc newTransformerHandler(Templates JavaDoc templates)
598           throws TransformerConfigurationException JavaDoc
599   {
600     try {
601       TransformerImpl transformer =
602         (TransformerImpl) templates.newTransformer();
603       transformer.setURIResolver(m_uriResolver);
604       TransformerHandler JavaDoc th =
605         (TransformerHandler JavaDoc) transformer.getInputContentHandler(true);
606
607       return th;
608     } catch( TransformerConfigurationException JavaDoc ex ) {
609       if( m_errorListener != null ) {
610         try {
611           m_errorListener.fatalError( ex );
612           return null;
613         } catch (TransformerException JavaDoc ex1 ) {
614           ex=new TransformerConfigurationException JavaDoc(ex1);
615         }
616       }
617       throw ex;
618     }
619     
620   }
621
622 // /** The identity transform string, for support of newTransformerHandler()
623
// * and newTransformer(). */
624
// private static final String identityTransform =
625
// "<xsl:stylesheet " + "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' "
626
// + "version='1.0'>" + "<xsl:template match='/|node()'>"
627
// + "<xsl:copy-of select='.'/>" + "</xsl:template>" + "</xsl:stylesheet>";
628
//
629
// /** The identity transform Templates, built from identityTransform,
630
// * for support of newTransformerHandler() and newTransformer(). */
631
// private static Templates m_identityTemplate = null;
632

633   /**
634    * Get a TransformerHandler object that can process SAX
635    * ContentHandler events into a Result.
636    *
637    * @param src The source of the transformation instructions.
638    *
639    * @return TransformerHandler ready to transform SAX events.
640    *
641    * @throws TransformerConfigurationException
642    */

643   public TransformerHandler JavaDoc newTransformerHandler()
644           throws TransformerConfigurationException JavaDoc
645   {
646     return new TransformerIdentityImpl();
647   }
648
649   /**
650    * Process the source into a Transformer object. Care must
651    * be given to know that this object can not be used concurrently
652    * in multiple threads.
653    *
654    * @param source An object that holds a URL, input stream, etc.
655    *
656    * @return A Transformer object capable of
657    * being used for transformation purposes in a single thread.
658    *
659    * @throws TransformerConfigurationException May throw this during the parse when it
660    * is constructing the Templates object and fails.
661    */

662   public Transformer JavaDoc newTransformer(Source JavaDoc source)
663           throws TransformerConfigurationException JavaDoc
664   {
665     try {
666       Templates JavaDoc tmpl=newTemplates( source );
667       /* this can happen if an ErrorListener is present and it doesn't
668          throw any exception in fatalError.
669          The spec says: "a Transformer must use this interface
670          instead of throwing an exception" - the newTemplates() does
671          that, and returns null.
672       */

673       if( tmpl==null ) return null;
674       Transformer JavaDoc transformer = tmpl.newTransformer();
675       transformer.setURIResolver(m_uriResolver);
676       return transformer;
677     } catch( TransformerConfigurationException JavaDoc ex ) {
678       if( m_errorListener != null ) {
679         try {
680           m_errorListener.fatalError( ex );
681           return null;
682         } catch( TransformerException JavaDoc ex1 ) {
683           ex=new TransformerConfigurationException JavaDoc( ex1 );
684         }
685       }
686       throw ex;
687     }
688   }
689
690   /**
691    * Create a new Transformer object that performs a copy
692    * of the source to the result.
693    *
694    * @param source An object that holds a URL, input stream, etc.
695    *
696    * @return A Transformer object capable of
697    * being used for transformation purposes in a single thread.
698    *
699    * @throws TransformerConfigurationException May throw this during
700    * the parse when it is constructing the
701    * Templates object and it fails.
702    */

703   public Transformer JavaDoc newTransformer() throws TransformerConfigurationException JavaDoc
704   {
705       return new TransformerIdentityImpl();
706   }
707
708   /**
709    * Process the source into a Templates object, which is likely
710    * a compiled representation of the source. This Templates object
711    * may then be used concurrently across multiple threads. Creating
712    * a Templates object allows the TransformerFactory to do detailed
713    * performance optimization of transformation instructions, without
714    * penalizing runtime transformation.
715    *
716    * @param source An object that holds a URL, input stream, etc.
717    * @return A Templates object capable of being used for transformation purposes.
718    *
719    * @throws TransformerConfigurationException May throw this during the parse when it
720    * is constructing the Templates object and fails.
721    */

722   public Templates JavaDoc newTemplates(Source JavaDoc source)
723           throws TransformerConfigurationException JavaDoc
724   {
725
726     String JavaDoc baseID = source.getSystemId();
727
728     if (null != baseID) {
729        baseID = SystemIDResolver.getAbsoluteURI(baseID);
730     }
731
732
733     if (source instanceof DOMSource JavaDoc)
734     {
735       DOMSource JavaDoc dsource = (DOMSource JavaDoc) source;
736       Node JavaDoc node = dsource.getNode();
737
738       if (null != node)
739         return processFromNode(node, baseID);
740       else
741       {
742         String JavaDoc messageStr = XSLMessages.createMessage(
743           XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
744
745         throw new IllegalArgumentException JavaDoc(messageStr);
746       }
747     }
748
749     TemplatesHandler JavaDoc builder = newTemplatesHandler();
750     builder.setSystemId(baseID);
751     
752     try
753     {
754       InputSource JavaDoc isource = SAXSource.sourceToInputSource(source);
755       isource.setSystemId(baseID);
756       XMLReader JavaDoc reader = null;
757
758       if (source instanceof SAXSource JavaDoc)
759         reader = ((SAXSource JavaDoc) source).getXMLReader();
760         
761       if (null == reader)
762       {
763
764         // Use JAXP1.1 ( if possible )
765
try
766         {
767           javax.xml.parsers.SAXParserFactory JavaDoc factory =
768             javax.xml.parsers.SAXParserFactory.newInstance();
769
770           factory.setNamespaceAware(true);
771
772           javax.xml.parsers.SAXParser JavaDoc jaxpParser = factory.newSAXParser();
773
774           reader = jaxpParser.getXMLReader();
775         }
776         catch (javax.xml.parsers.ParserConfigurationException JavaDoc ex)
777         {
778           throw new org.xml.sax.SAXException JavaDoc(ex);
779         }
780         catch (javax.xml.parsers.FactoryConfigurationError JavaDoc ex1)
781         {
782           throw new org.xml.sax.SAXException JavaDoc(ex1.toString());
783         }
784         catch (NoSuchMethodError JavaDoc ex2){}
785         catch (AbstractMethodError JavaDoc ame){}
786       }
787
788       if (null == reader)
789         reader = XMLReaderFactory.createXMLReader();
790
791       // If you set the namespaces to true, we'll end up getting double
792
// xmlns attributes. Needs to be fixed. -sb
793
// reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
794
reader.setContentHandler(builder);
795       reader.parse(isource);
796     }
797     catch (org.xml.sax.SAXException JavaDoc se)
798     {
799       if (m_errorListener != null)
800       {
801         try
802         {
803           m_errorListener.fatalError(new TransformerException JavaDoc(se));
804         }
805         catch (TransformerException JavaDoc ex1)
806         {
807           throw new TransformerConfigurationException JavaDoc(ex1);
808         }
809       }
810       else
811         throw new TransformerConfigurationException JavaDoc(se.getMessage(), se);
812     }
813     catch (Exception JavaDoc e)
814     {
815       if (m_errorListener != null)
816       {
817         try
818         {
819           m_errorListener.fatalError(new TransformerException JavaDoc(e));
820
821           return null;
822         }
823         catch (TransformerException JavaDoc ex1)
824         {
825           throw new TransformerConfigurationException JavaDoc(ex1);
826         }
827       }
828       else
829         throw new TransformerConfigurationException JavaDoc(e.getMessage(), e);
830     }
831
832     return builder.getTemplates();
833   }
834
835   /**
836    * The object that implements the URIResolver interface,
837    * or null.
838    */

839   URIResolver JavaDoc m_uriResolver;
840
841   /**
842    * Set an object that will be used to resolve URIs used in
843    * xsl:import, etc. This will be used as the default for the
844    * transformation.
845    * @param resolver An object that implements the URIResolver interface,
846    * or null.
847    */

848   public void setURIResolver(URIResolver JavaDoc resolver)
849   {
850     m_uriResolver = resolver;
851   }
852
853   /**
854    * Get the object that will be used to resolve URIs used in
855    * xsl:import, etc. This will be used as the default for the
856    * transformation.
857    *
858    * @return The URIResolver that was set with setURIResolver.
859    */

860   public URIResolver JavaDoc getURIResolver()
861   {
862     return m_uriResolver;
863   }
864
865   /** The error listener. */
866   private ErrorListener JavaDoc m_errorListener = new DefaultErrorHandler();
867
868   /**
869    * Get the error listener in effect for the TransformerFactory.
870    *
871    * @return A non-null reference to an error listener.
872    */

873   public ErrorListener JavaDoc getErrorListener()
874   {
875     return m_errorListener;
876   }
877
878   /**
879    * Set an error listener for the TransformerFactory.
880    *
881    * @param listener Must be a non-null reference to an ErrorListener.
882    *
883    * @throws IllegalArgumentException if the listener argument is null.
884    */

885   public void setErrorListener(ErrorListener JavaDoc listener)
886           throws IllegalArgumentException JavaDoc
887   {
888
889     if (null == listener)
890       throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_ERRORLISTENER, null));
891       // "ErrorListener");
892

893     m_errorListener = listener;
894   }
895 }
896
Popular Tags