KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
18  * $Id: TransformerFactoryImpl.java,v 1.1.2.1 2006/09/19 01:07:40 jeffsuttor Exp $
19  */

20
21 package com.sun.org.apache.xalan.internal.xsltc.trax;
22
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FilenameFilter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.zip.ZipEntry JavaDoc;
36 import java.util.zip.ZipFile JavaDoc;
37
38 import javax.xml.XMLConstants JavaDoc;
39
40 import javax.xml.parsers.SAXParserFactory JavaDoc;
41 import javax.xml.parsers.SAXParser JavaDoc;
42 import javax.xml.parsers.ParserConfigurationException JavaDoc;
43
44 import javax.xml.transform.ErrorListener JavaDoc;
45 import javax.xml.transform.Source JavaDoc;
46 import javax.xml.transform.Templates JavaDoc;
47 import javax.xml.transform.Transformer JavaDoc;
48 import javax.xml.transform.TransformerConfigurationException JavaDoc;
49 import javax.xml.transform.TransformerException JavaDoc;
50 import javax.xml.transform.URIResolver JavaDoc;
51 import javax.xml.transform.dom.DOMResult JavaDoc;
52 import javax.xml.transform.dom.DOMSource JavaDoc;
53 import javax.xml.transform.sax.SAXResult JavaDoc;
54 import javax.xml.transform.sax.SAXSource JavaDoc;
55 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
56 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
57 import javax.xml.transform.sax.TransformerHandler JavaDoc;
58 import javax.xml.transform.stream.StreamResult JavaDoc;
59 import javax.xml.transform.stream.StreamSource JavaDoc;
60
61 import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler;
62 import com.sun.org.apache.xml.internal.utils.StopParseException;
63
64 import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
65 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
66 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
67 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
68
69 import org.xml.sax.InputSource JavaDoc;
70 import org.xml.sax.XMLFilter JavaDoc;
71 import org.xml.sax.XMLReader JavaDoc;
72 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
73
74 /**
75  * Implementation of a JAXP1.1 TransformerFactory for Translets.
76  */

77 public class TransformerFactoryImpl
78     extends SAXTransformerFactory JavaDoc implements SourceLoader, ErrorListener JavaDoc
79 {
80     /**
81      * <p>Name of class as a constant to use for debugging.</p>
82      */

83     private static final String JavaDoc CLASS_NAME = "TransformerFactoryImpl";
84
85     // Public constants for attributes supported by the XSLTC TransformerFactory.
86
public final static String JavaDoc TRANSLET_NAME = "translet-name";
87     public final static String JavaDoc DESTINATION_DIRECTORY = "destination-directory";
88     public final static String JavaDoc PACKAGE_NAME = "package-name";
89     public final static String JavaDoc JAR_NAME = "jar-name";
90     public final static String JavaDoc GENERATE_TRANSLET = "generate-translet";
91     public final static String JavaDoc AUTO_TRANSLET = "auto-translet";
92     public final static String JavaDoc USE_CLASSPATH = "use-classpath";
93     public final static String JavaDoc DEBUG = "debug";
94     public final static String JavaDoc ENABLE_INLINING = "enable-inlining";
95     public final static String JavaDoc INDENT_NUMBER = "indent-number";
96         
97     /**
98      * This error listener is used only for this factory and is not passed to
99      * the Templates or Transformer objects that we create.
100      */

101     private ErrorListener JavaDoc _errorListener = this;
102
103     /**
104      * This URIResolver is passed to all created Templates and Transformers
105      */

106     private URIResolver JavaDoc _uriResolver = null;
107
108     /**
109      * As Gregor Samsa awoke one morning from uneasy dreams he found himself
110      * transformed in his bed into a gigantic insect. He was lying on his hard,
111      * as it were armour plated, back, and if he lifted his head a little he
112      * could see his big, brown belly divided into stiff, arched segments, on
113      * top of which the bed quilt could hardly keep in position and was about
114      * to slide off completely. His numerous legs, which were pitifully thin
115      * compared to the rest of his bulk, waved helplessly before his eyes.
116      * "What has happened to me?", he thought. It was no dream....
117      */

118     protected static String JavaDoc DEFAULT_TRANSLET_NAME = "GregorSamsa";
119     
120     /**
121      * The class name of the translet
122      */

123     private String JavaDoc _transletName = DEFAULT_TRANSLET_NAME;
124     
125     /**
126      * The destination directory for the translet
127      */

128     private String JavaDoc _destinationDirectory = null;
129     
130     /**
131      * The package name prefix for all generated translet classes
132      */

133     private String JavaDoc _packageName = null;
134     
135     /**
136      * The jar file name which the translet classes are packaged into
137      */

138     private String JavaDoc _jarFileName = null;
139
140     /**
141      * This Hashtable is used to store parameters for locating
142      * <?xml-stylesheet ...?> processing instructions in XML docs.
143      */

144     private Hashtable JavaDoc _piParams = null;
145
146
147     /**
148      * Use a thread local variable to store a copy of an XML Reader.
149      */

150     static ThreadLocal JavaDoc _xmlReader = new ThreadLocal JavaDoc();
151
152     /**
153      * The above hashtable stores objects of this class.
154      */

155     private static class PIParamWrapper {
156     public String JavaDoc _media = null;
157     public String JavaDoc _title = null;
158     public String JavaDoc _charset = null;
159     
160     public PIParamWrapper(String JavaDoc media, String JavaDoc title, String JavaDoc charset) {
161         _media = media;
162         _title = title;
163         _charset = charset;
164     }
165     }
166
167     /**
168      * Set to <code>true</code> when debugging is enabled.
169      */

170     private boolean _debug = false;
171
172     /**
173      * Set to <code>true</code> when templates are inlined.
174      */

175     private boolean _enableInlining = false;
176     
177     /**
178      * Set to <code>true</code> when we want to generate
179      * translet classes from the stylesheet.
180      */

181     private boolean _generateTranslet = false;
182     
183     /**
184      * If this is set to <code>true</code>, we attempt to use translet classes
185      * for transformation if possible without compiling the stylesheet. The
186      * translet class is only used if its timestamp is newer than the timestamp
187      * of the stylesheet.
188      */

189     private boolean _autoTranslet = false;
190     
191     /**
192      * If this is set to <code>true</code>, we attempt to load the translet
193      * from the CLASSPATH.
194      */

195     private boolean _useClasspath = false;
196
197     /**
198      * Number of indent spaces when indentation is turned on.
199      */

200     private int _indentNumber = -1;
201
202     /**
203      * The provider of the XSLTC DTM Manager service. This is fixed for any
204      * instance of this class. In order to change service providers, a new
205      * XSLTC <code>TransformerFactory</code> must be instantiated.
206      * @see XSLTCDTMManager#getDTMManagerClass()
207      */

208     private Class JavaDoc m_DTMManagerClass;
209
210     /**
211      * <p>State of secure processing feature.</p>
212      */

213     private boolean _isSecureProcessing = false;
214
215     /**
216      * javax.xml.transform.sax.TransformerFactory implementation.
217      */

218     public TransformerFactoryImpl() {
219         m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass();
220     }
221
222     /**
223      * javax.xml.transform.sax.TransformerFactory implementation.
224      * Set the error event listener for the TransformerFactory, which is used
225      * for the processing of transformation instructions, and not for the
226      * transformation itself.
227      *
228      * @param listener The error listener to use with the TransformerFactory
229      * @throws IllegalArgumentException
230      */

231     public void setErrorListener(ErrorListener JavaDoc listener)
232     throws IllegalArgumentException JavaDoc
233     {
234     if (listener == null) {
235         ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR,
236                     "TransformerFactory");
237             throw new IllegalArgumentException JavaDoc(err.toString());
238     }
239     _errorListener = listener;
240     }
241
242     /**
243      * javax.xml.transform.sax.TransformerFactory implementation.
244      * Get the error event handler for the TransformerFactory.
245      *
246      * @return The error listener used with the TransformerFactory
247      */

248     public ErrorListener JavaDoc getErrorListener() {
249     return _errorListener;
250     }
251
252     /**
253      * javax.xml.transform.sax.TransformerFactory implementation.
254      * Returns the value set for a TransformerFactory attribute
255      *
256      * @param name The attribute name
257      * @return An object representing the attribute value
258      * @throws IllegalArgumentException
259      */

260     public Object JavaDoc getAttribute(String JavaDoc name)
261     throws IllegalArgumentException JavaDoc
262     {
263     // Return value for attribute 'translet-name'
264
if (name.equals(TRANSLET_NAME)) {
265         return _transletName;
266     }
267     else if (name.equals(GENERATE_TRANSLET)) {
268         return new Boolean JavaDoc(_generateTranslet);
269     }
270     else if (name.equals(AUTO_TRANSLET)) {
271         return new Boolean JavaDoc(_autoTranslet);
272     }
273
274     // Throw an exception for all other attributes
275
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
276     throw new IllegalArgumentException JavaDoc(err.toString());
277     }
278
279     /**
280      * javax.xml.transform.sax.TransformerFactory implementation.
281      * Sets the value for a TransformerFactory attribute.
282      *
283      * @param name The attribute name
284      * @param value An object representing the attribute value
285      * @throws IllegalArgumentException
286      */

287     public void setAttribute(String JavaDoc name, Object JavaDoc value)
288     throws IllegalArgumentException JavaDoc
289     {
290     // Set the default translet name (ie. class name), which will be used
291
// for translets that cannot be given a name from their system-id.
292
if (name.equals(TRANSLET_NAME) && value instanceof String JavaDoc) {
293         _transletName = (String JavaDoc) value;
294         return;
295     }
296     else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String JavaDoc) {
297         _destinationDirectory = (String JavaDoc) value;
298         return;
299     }
300     else if (name.equals(PACKAGE_NAME) && value instanceof String JavaDoc) {
301         _packageName = (String JavaDoc) value;
302         return;
303     }
304     else if (name.equals(JAR_NAME) && value instanceof String JavaDoc) {
305         _jarFileName = (String JavaDoc) value;
306         return;
307     }
308     else if (name.equals(GENERATE_TRANSLET)) {
309         if (value instanceof Boolean JavaDoc) {
310         _generateTranslet = ((Boolean JavaDoc) value).booleanValue();
311         return;
312         }
313         else if (value instanceof String JavaDoc) {
314         _generateTranslet = ((String JavaDoc) value).equalsIgnoreCase("true");
315         return;
316         }
317     }
318     else if (name.equals(AUTO_TRANSLET)) {
319         if (value instanceof Boolean JavaDoc) {
320         _autoTranslet = ((Boolean JavaDoc) value).booleanValue();
321         return;
322         }
323         else if (value instanceof String JavaDoc) {
324         _autoTranslet = ((String JavaDoc) value).equalsIgnoreCase("true");
325         return;
326         }
327     }
328     else if (name.equals(USE_CLASSPATH)) {
329         if (value instanceof Boolean JavaDoc) {
330         _useClasspath = ((Boolean JavaDoc) value).booleanValue();
331         return;
332         }
333         else if (value instanceof String JavaDoc) {
334         _useClasspath = ((String JavaDoc) value).equalsIgnoreCase("true");
335         return;
336         }
337     }
338     else if (name.equals(DEBUG)) {
339         if (value instanceof Boolean JavaDoc) {
340         _debug = ((Boolean JavaDoc) value).booleanValue();
341         return;
342         }
343         else if (value instanceof String JavaDoc) {
344         _debug = ((String JavaDoc) value).equalsIgnoreCase("true");
345         return;
346         }
347     }
348     else if (name.equals(ENABLE_INLINING)) {
349         if (value instanceof Boolean JavaDoc) {
350         _enableInlining = ((Boolean JavaDoc) value).booleanValue();
351         return;
352         }
353         else if (value instanceof String JavaDoc) {
354         _enableInlining = ((String JavaDoc) value).equalsIgnoreCase("true");
355         return;
356         }
357     }
358     else if (name.equals(INDENT_NUMBER)) {
359         if (value instanceof String JavaDoc) {
360         try {
361             _indentNumber = Integer.parseInt((String JavaDoc) value);
362             return;
363         }
364         catch (NumberFormatException JavaDoc e) {
365             // Falls through
366
}
367         }
368         else if (value instanceof Integer JavaDoc) {
369         _indentNumber = ((Integer JavaDoc) value).intValue();
370         return;
371         }
372     }
373
374     // Throw an exception for all other attributes
375
final ErrorMsg err
376         = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
377     throw new IllegalArgumentException JavaDoc(err.toString());
378     }
379
380     /**
381      * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
382      * or <code>Template</code>s created by this factory.</p>
383      *
384      * <p>
385      * Feature names are fully qualified {@link java.net.URI}s.
386      * Implementations may define their own features.
387      * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
388      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
389      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
390      * </p>
391      *
392      * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p>
393      *
394      * @param name Feature name.
395      * @param value Is feature state <code>true</code> or <code>false</code>.
396      *
397      * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
398      * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
399      * @throws NullPointerException If the <code>name</code> parameter is null.
400      */

401     public void setFeature(String JavaDoc name, boolean value)
402         throws TransformerConfigurationException JavaDoc {
403
404     // feature name cannot be null
405
if (name == null) {
406             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
407             throw new NullPointerException JavaDoc(err.toString());
408     }
409     // secure processing?
410
else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
411         _isSecureProcessing = value;
412         // all done processing feature
413
return;
414     }
415     else {
416         // unknown feature
417
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
418             throw new TransformerConfigurationException JavaDoc(err.toString());
419         }
420     }
421
422     /**
423      * javax.xml.transform.sax.TransformerFactory implementation.
424      * Look up the value of a feature (to see if it is supported).
425      * This method must be updated as the various methods and features of this
426      * class are implemented.
427      *
428      * @param name The feature name
429      * @return 'true' if feature is supported, 'false' if not
430      */

431     public boolean getFeature(String JavaDoc name) {
432     // All supported features should be listed here
433
String JavaDoc[] features = {
434         DOMSource.FEATURE,
435         DOMResult.FEATURE,
436         SAXSource.FEATURE,
437         SAXResult.FEATURE,
438         StreamSource.FEATURE,
439         StreamResult.FEATURE,
440         SAXTransformerFactory.FEATURE,
441         SAXTransformerFactory.FEATURE_XMLFILTER
442     };
443
444     // feature name cannot be null
445
if (name == null) {
446             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
447             throw new NullPointerException JavaDoc(err.toString());
448     }
449
450     // Inefficient, but array is small
451
for (int i =0; i < features.length; i++) {
452         if (name.equals(features[i])) {
453         return true;
454         }
455     }
456     // secure processing?
457
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
458         return _isSecureProcessing;
459     }
460
461     // Feature not supported
462
return false;
463     }
464
465     /**
466      * javax.xml.transform.sax.TransformerFactory implementation.
467      * Get the object that is used by default during the transformation to
468      * resolve URIs used in document(), xsl:import, or xsl:include.
469      *
470      * @return The URLResolver used for this TransformerFactory and all
471      * Templates and Transformer objects created using this factory
472      */

473     public URIResolver JavaDoc getURIResolver() {
474     return _uriResolver;
475     }
476
477     /**
478      * javax.xml.transform.sax.TransformerFactory implementation.
479      * Set the object that is used by default during the transformation to
480      * resolve URIs used in document(), xsl:import, or xsl:include. Note that
481      * this does not affect Templates and Transformers that are already
482      * created with this factory.
483      *
484      * @param resolver The URLResolver used for this TransformerFactory and all
485      * Templates and Transformer objects created using this factory
486      */

487     public void setURIResolver(URIResolver JavaDoc resolver) {
488     _uriResolver = resolver;
489     }
490
491     /**
492      * javax.xml.transform.sax.TransformerFactory implementation.
493      * Get the stylesheet specification(s) associated via the xml-stylesheet
494      * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
495      * the document document specified in the source parameter, and that match
496      * the given criteria.
497      *
498      * @param source The XML source document.
499      * @param media The media attribute to be matched. May be null, in which
500      * case the prefered templates will be used (i.e. alternate = no).
501      * @param title The value of the title attribute to match. May be null.
502      * @param charset The value of the charset attribute to match. May be null.
503      * @return A Source object suitable for passing to the TransformerFactory.
504      * @throws TransformerConfigurationException
505      */

506     public Source JavaDoc getAssociatedStylesheet(Source JavaDoc source, String JavaDoc media,
507                       String JavaDoc title, String JavaDoc charset)
508     throws TransformerConfigurationException JavaDoc {
509
510         String JavaDoc baseId;
511         XMLReader JavaDoc reader = null;
512         InputSource JavaDoc isource = null;
513
514
515         /**
516          * Fix for bugzilla bug 24187
517          */

518         StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);
519
520         try {
521   
522             if (source instanceof DOMSource JavaDoc ) {
523                 final DOMSource JavaDoc domsrc = (DOMSource JavaDoc) source;
524                 baseId = domsrc.getSystemId();
525                 final org.w3c.dom.Node JavaDoc node = domsrc.getNode();
526                 final DOM2SAX dom2sax = new DOM2SAX(node);
527
528                 _stylesheetPIHandler.setBaseId(baseId);
529
530                 dom2sax.setContentHandler( _stylesheetPIHandler);
531                 dom2sax.parse();
532             } else {
533                 isource = SAXSource.sourceToInputSource(source);
534                 baseId = isource.getSystemId();
535
536                 SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
537                 factory.setNamespaceAware(true);
538                 
539                 if (_isSecureProcessing) {
540                     try {
541                         factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
542                     }
543                     catch (org.xml.sax.SAXException JavaDoc e) {}
544                 }
545
546                 SAXParser JavaDoc jaxpParser = factory.newSAXParser();
547
548                 reader = jaxpParser.getXMLReader();
549                 if (reader == null) {
550                     reader = XMLReaderFactory.createXMLReader();
551                 }
552
553                 _stylesheetPIHandler.setBaseId(baseId);
554                 reader.setContentHandler(_stylesheetPIHandler);
555                 reader.parse(isource);
556
557             }
558
559             if (_uriResolver != null ) {
560                 _stylesheetPIHandler.setURIResolver(_uriResolver);
561             }
562
563         } catch (StopParseException e ) {
564           // startElement encountered so do not parse further
565

566         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
567
568              throw new TransformerConfigurationException JavaDoc(
569              "getAssociatedStylesheets failed", e);
570
571         } catch (org.xml.sax.SAXException JavaDoc se) {
572
573              throw new TransformerConfigurationException JavaDoc(
574              "getAssociatedStylesheets failed", se);
575
576
577         } catch (IOException JavaDoc ioe ) {
578            throw new TransformerConfigurationException JavaDoc(
579            "getAssociatedStylesheets failed", ioe);
580
581         }
582
583          return _stylesheetPIHandler.getAssociatedStylesheet();
584
585     }
586
587     /**
588      * javax.xml.transform.sax.TransformerFactory implementation.
589      * Create a Transformer object that copies the input document to the result.
590      *
591      * @return A Transformer object that simply copies the source to the result.
592      * @throws TransformerConfigurationException
593      */

594     public Transformer JavaDoc newTransformer()
595     throws TransformerConfigurationException JavaDoc
596     {
597     TransformerImpl result = new TransformerImpl(new Properties JavaDoc(),
598         _indentNumber, this);
599     if (_uriResolver != null) {
600         result.setURIResolver(_uriResolver);
601     }
602         
603         if (_isSecureProcessing) {
604             result.setSecureProcessing(true);
605         }
606
607     return result;
608     }
609
610     /**
611      * javax.xml.transform.sax.TransformerFactory implementation.
612      * Process the Source into a Templates object, which is a a compiled
613      * representation of the source. Note that this method should not be
614      * used with XSLTC, as the time-consuming compilation is done for each
615      * and every transformation.
616      *
617      * @return A Templates object that can be used to create Transformers.
618      * @throws TransformerConfigurationException
619      */

620     public Transformer JavaDoc newTransformer(Source JavaDoc source) throws
621     TransformerConfigurationException JavaDoc
622     {
623     final Templates JavaDoc templates = newTemplates(source);
624     final Transformer JavaDoc transformer = templates.newTransformer();
625     if (_uriResolver != null) {
626         transformer.setURIResolver(_uriResolver);
627     }
628     return(transformer);
629     }
630
631     /**
632      * Pass warning messages from the compiler to the error listener
633      */

634     private void passWarningsToListener(Vector JavaDoc messages)
635     throws TransformerException JavaDoc
636     {
637     if (_errorListener == null || messages == null) {
638         return;
639     }
640     // Pass messages to listener, one by one
641
final int count = messages.size();
642     for (int pos = 0; pos < count; pos++) {
643         String JavaDoc message = messages.elementAt(pos).toString();
644         _errorListener.error(
645         new TransformerConfigurationException JavaDoc(message));
646     }
647     }
648
649     /**
650      * Pass error messages from the compiler to the error listener
651      */

652     private void passErrorsToListener(Vector JavaDoc messages) {
653     try {
654         if (_errorListener == null || messages == null) {
655         return;
656         }
657         // Pass messages to listener, one by one
658
final int count = messages.size();
659         for (int pos = 0; pos < count; pos++) {
660         String JavaDoc message = messages.elementAt(pos).toString();
661         _errorListener.error(new TransformerException JavaDoc(message));
662         }
663     }
664     catch (TransformerException JavaDoc e) {
665         // nada
666
}
667     }
668
669     /**
670      * javax.xml.transform.sax.TransformerFactory implementation.
671      * Process the Source into a Templates object, which is a a compiled
672      * representation of the source.
673      *
674      * @param stylesheet The input stylesheet - DOMSource not supported!!!
675      * @return A Templates object that can be used to create Transformers.
676      * @throws TransformerConfigurationException
677      */

678     public Templates JavaDoc newTemplates(Source JavaDoc source)
679     throws TransformerConfigurationException JavaDoc
680     {
681     // If the _useClasspath attribute is true, try to load the translet from
682
// the CLASSPATH and create a template object using the loaded
683
// translet.
684
if (_useClasspath) {
685         String JavaDoc transletName = getTransletBaseName(source);
686                 
687         if (_packageName != null)
688             transletName = _packageName + "." + transletName;
689             
690         try {
691                 final Class JavaDoc clazz = ObjectFactory.findProviderClass(
692                     transletName, ObjectFactory.findClassLoader(), true);
693             resetTransientAttributes();
694                 
695             return new TemplatesImpl(new Class JavaDoc[]{clazz}, transletName, null, _indentNumber, this);
696         }
697         catch (ClassNotFoundException JavaDoc cnfe) {
698             ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName);
699             throw new TransformerConfigurationException JavaDoc(err.toString());
700         }
701         catch (Exception JavaDoc e) {
702             ErrorMsg err = new ErrorMsg(
703                                      new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)
704                                      + e.getMessage());
705             throw new TransformerConfigurationException JavaDoc(err.toString());
706         }
707     }
708     
709     // If _autoTranslet is true, we will try to load the bytecodes
710
// from the translet classes without compiling the stylesheet.
711
if (_autoTranslet) {
712         byte[][] bytecodes = null;
713         String JavaDoc transletClassName = getTransletBaseName(source);
714         
715         if (_packageName != null)
716             transletClassName = _packageName + "." + transletClassName;
717         
718         if (_jarFileName != null)
719             bytecodes = getBytecodesFromJar(source, transletClassName);
720         else
721             bytecodes = getBytecodesFromClasses(source, transletClassName);
722       
723         if (bytecodes != null) {
724             if (_debug) {
725                 if (_jarFileName != null)
726                 System.err.println(new ErrorMsg(
727                         ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName));
728                 else
729                     System.err.println(new ErrorMsg(
730                         ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
731             }
732
733             // Reset the per-session attributes to their default values
734
// after each newTemplates() call.
735
resetTransientAttributes();
736         
737             return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);
738         }
739     }
740     
741     // Create and initialize a stylesheet compiler
742
final XSLTC xsltc = new XSLTC();
743     if (_debug) xsltc.setDebug(true);
744     if (_enableInlining) xsltc.setTemplateInlining(true);
745         if (_isSecureProcessing) xsltc.setSecureProcessing(true);
746     xsltc.init();
747
748     // Set a document loader (for xsl:include/import) if defined
749
if (_uriResolver != null) {
750         xsltc.setSourceLoader(this);
751     }
752
753     // Pass parameters to the Parser to make sure it locates the correct
754
// <?xml-stylesheet ...?> PI in an XML input document
755
if ((_piParams != null) && (_piParams.get(source) != null)) {
756         // Get the parameters for this Source object
757
PIParamWrapper p = (PIParamWrapper)_piParams.get(source);
758         // Pass them on to the compiler (which will pass then to the parser)
759
if (p != null) {
760         xsltc.setPIParameters(p._media, p._title, p._charset);
761         }
762     }
763
764     // Set the attributes for translet generation
765
int outputType = XSLTC.BYTEARRAY_OUTPUT;
766     if (_generateTranslet || _autoTranslet) {
767         // Set the translet name
768
xsltc.setClassName(getTransletBaseName(source));
769       
770         if (_destinationDirectory != null)
771             xsltc.setDestDirectory(_destinationDirectory);
772         else {
773             String JavaDoc xslName = getStylesheetFileName(source);
774             if (xslName != null) {
775                 File JavaDoc xslFile = new File JavaDoc(xslName);
776                 String JavaDoc xslDir = xslFile.getParent();
777         
778                 if (xslDir != null)
779                     xsltc.setDestDirectory(xslDir);
780             }
781         }
782       
783         if (_packageName != null)
784             xsltc.setPackageName(_packageName);
785     
786         if (_jarFileName != null) {
787             xsltc.setJarFileName(_jarFileName);
788             outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT;
789         }
790         else
791             outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT;
792     }
793
794     // Compile the stylesheet
795
final InputSource JavaDoc input = Util.getInputSource(xsltc, source);
796     byte[][] bytecodes = xsltc.compile(null, input, outputType);
797     final String JavaDoc transletName = xsltc.getClassName();
798
799     // Output to the jar file if the jar file name is set.
800
if ((_generateTranslet || _autoTranslet)
801         && bytecodes != null && _jarFileName != null) {
802         try {
803             xsltc.outputToJar();
804         }
805         catch (java.io.IOException JavaDoc e) { }
806     }
807
808     // Reset the per-session attributes to their default values
809
// after each newTemplates() call.
810
resetTransientAttributes();
811
812     // Pass compiler warnings to the error listener
813
if (_errorListener != this) {
814         try {
815         passWarningsToListener(xsltc.getWarnings());
816         }
817         catch (TransformerException JavaDoc e) {
818         throw new TransformerConfigurationException JavaDoc(e);
819         }
820     }
821     else {
822         xsltc.printWarnings();
823     }
824
825     // Check that the transformation went well before returning
826
if (bytecodes == null) {
827         
828         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
829         TransformerConfigurationException JavaDoc exc = new TransformerConfigurationException JavaDoc(err.toString());
830         
831         // Pass compiler errors to the error listener
832
if (_errorListener != null) {
833             passErrorsToListener(xsltc.getErrors());
834             
835             // As required by TCK 1.2, send a fatalError to the
836
// error listener because compilation of the stylesheet
837
// failed and no further processing will be possible.
838
try {
839                 _errorListener.fatalError(exc);
840             } catch (TransformerException JavaDoc te) {
841                 // well, we tried.
842
}
843         }
844         else {
845             xsltc.printErrors();
846         }
847         throw exc;
848     }
849
850     return new TemplatesImpl(bytecodes, transletName,
851         xsltc.getOutputProperties(), _indentNumber, this);
852     }
853
854     /**
855      * javax.xml.transform.sax.SAXTransformerFactory implementation.
856      * Get a TemplatesHandler object that can process SAX ContentHandler
857      * events into a Templates object.
858      *
859      * @return A TemplatesHandler object that can handle SAX events
860      * @throws TransformerConfigurationException
861      */

862     public TemplatesHandler JavaDoc newTemplatesHandler()
863     throws TransformerConfigurationException JavaDoc
864     {
865     final TemplatesHandlerImpl handler =
866         new TemplatesHandlerImpl(_indentNumber, this);
867     if (_uriResolver != null) {
868         handler.setURIResolver(_uriResolver);
869     }
870     return handler;
871     }
872
873     /**
874      * javax.xml.transform.sax.SAXTransformerFactory implementation.
875      * Get a TransformerHandler object that can process SAX ContentHandler
876      * events into a Result. This method will return a pure copy transformer.
877      *
878      * @return A TransformerHandler object that can handle SAX events
879      * @throws TransformerConfigurationException
880      */

881     public TransformerHandler JavaDoc newTransformerHandler()
882     throws TransformerConfigurationException JavaDoc
883     {
884     final Transformer JavaDoc transformer = newTransformer();
885     if (_uriResolver != null) {
886         transformer.setURIResolver(_uriResolver);
887     }
888     return new TransformerHandlerImpl((TransformerImpl) transformer);
889     }
890
891     /**
892      * javax.xml.transform.sax.SAXTransformerFactory implementation.
893      * Get a TransformerHandler object that can process SAX ContentHandler
894      * events into a Result, based on the transformation instructions
895      * specified by the argument.
896      *
897      * @param src The source of the transformation instructions.
898      * @return A TransformerHandler object that can handle SAX events
899      * @throws TransformerConfigurationException
900      */

901     public TransformerHandler JavaDoc newTransformerHandler(Source JavaDoc src)
902     throws TransformerConfigurationException JavaDoc
903     {
904     final Transformer JavaDoc transformer = newTransformer(src);
905     if (_uriResolver != null) {
906         transformer.setURIResolver(_uriResolver);
907     }
908     return new TransformerHandlerImpl((TransformerImpl) transformer);
909     }
910
911     /**
912      * javax.xml.transform.sax.SAXTransformerFactory implementation.
913      * Get a TransformerHandler object that can process SAX ContentHandler
914      * events into a Result, based on the transformation instructions
915      * specified by the argument.
916      *
917      * @param templates Represents a pre-processed stylesheet
918      * @return A TransformerHandler object that can handle SAX events
919      * @throws TransformerConfigurationException
920      */

921     public TransformerHandler JavaDoc newTransformerHandler(Templates JavaDoc templates)
922     throws TransformerConfigurationException JavaDoc
923     {
924     final Transformer JavaDoc transformer = templates.newTransformer();
925     final TransformerImpl internal = (TransformerImpl)transformer;
926     return new TransformerHandlerImpl(internal);
927     }
928
929     /**
930      * javax.xml.transform.sax.SAXTransformerFactory implementation.
931      * Create an XMLFilter that uses the given source as the
932      * transformation instructions.
933      *
934      * @param src The source of the transformation instructions.
935      * @return An XMLFilter object, or null if this feature is not supported.
936      * @throws TransformerConfigurationException
937      */

938     public XMLFilter JavaDoc newXMLFilter(Source JavaDoc src)
939     throws TransformerConfigurationException JavaDoc
940     {
941     Templates JavaDoc templates = newTemplates(src);
942     if (templates == null) return null;
943     return newXMLFilter(templates);
944     }
945
946     /**
947      * javax.xml.transform.sax.SAXTransformerFactory implementation.
948      * Create an XMLFilter that uses the given source as the
949      * transformation instructions.
950      *
951      * @param src The source of the transformation instructions.
952      * @return An XMLFilter object, or null if this feature is not supported.
953      * @throws TransformerConfigurationException
954      */

955     public XMLFilter JavaDoc newXMLFilter(Templates JavaDoc templates)
956     throws TransformerConfigurationException JavaDoc
957     {
958     try {
959             return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
960         }
961     catch (TransformerConfigurationException JavaDoc e1) {
962             if (_errorListener != null) {
963                 try {
964                 _errorListener.fatalError(e1);
965                 return null;
966             }
967         catch (TransformerException JavaDoc e2) {
968                 new TransformerConfigurationException JavaDoc(e2);
969             }
970             }
971             throw e1;
972         }
973     }
974
975     /**
976      * Receive notification of a recoverable error.
977      * The transformer must continue to provide normal parsing events after
978      * invoking this method. It should still be possible for the application
979      * to process the document through to the end.
980      *
981      * @param exception The warning information encapsulated in a transformer
982      * exception.
983      * @throws TransformerException if the application chooses to discontinue
984      * the transformation (always does in our case).
985      */

986     public void error(TransformerException JavaDoc e)
987     throws TransformerException JavaDoc
988     {
989     Throwable JavaDoc wrapped = e.getException();
990         if (wrapped != null) {
991             System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG,
992                                             e.getMessageAndLocation(),
993                                             wrapped.getMessage()));
994         } else {
995             System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG,
996                                             e.getMessageAndLocation()));
997     }
998     throw e;
999     }
1000
1001    /**
1002     * Receive notification of a non-recoverable error.
1003     * The application must assume that the transformation cannot continue
1004     * after the Transformer has invoked this method, and should continue
1005     * (if at all) only to collect addition error messages. In fact,
1006     * Transformers are free to stop reporting events once this method has
1007     * been invoked.
1008     *
1009     * @param exception The warning information encapsulated in a transformer
1010     * exception.
1011     * @throws TransformerException if the application chooses to discontinue
1012     * the transformation (always does in our case).
1013     */

1014    public void fatalError(TransformerException JavaDoc e)
1015    throws TransformerException JavaDoc
1016    {
1017    Throwable JavaDoc wrapped = e.getException();
1018        if (wrapped != null) {
1019            System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG,
1020                                            e.getMessageAndLocation(),
1021                                            wrapped.getMessage()));
1022        } else {
1023            System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG,
1024                                            e.getMessageAndLocation()));
1025        }
1026    throw e;
1027    }
1028
1029    /**
1030     * Receive notification of a warning.
1031     * Transformers can use this method to report conditions that are not
1032     * errors or fatal errors. The default behaviour is to take no action.
1033     * After invoking this method, the Transformer must continue with the
1034     * transformation. It should still be possible for the application to
1035     * process the document through to the end.
1036     *
1037     * @param exception The warning information encapsulated in a transformer
1038     * exception.
1039     * @throws TransformerException if the application chooses to discontinue
1040     * the transformation (never does in our case).
1041     */

1042    public void warning(TransformerException JavaDoc e)
1043    throws TransformerException JavaDoc
1044    {
1045    Throwable JavaDoc wrapped = e.getException();
1046    if (wrapped != null) {
1047            System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG,
1048                                            e.getMessageAndLocation(),
1049                                            wrapped.getMessage()));
1050    } else {
1051            System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG,
1052                                            e.getMessageAndLocation()));
1053        }
1054    }
1055
1056    /**
1057     * This method implements XSLTC's SourceLoader interface. It is used to
1058     * glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes.
1059     *
1060     * @param href The URI of the document to load
1061     * @param context The URI of the currently loaded document
1062     * @param xsltc The compiler that resuests the document
1063     * @return An InputSource with the loaded document
1064     */

1065    public InputSource JavaDoc loadSource(String JavaDoc href, String JavaDoc context, XSLTC xsltc) {
1066    try {
1067        if (_uriResolver != null) {
1068        final Source JavaDoc source = _uriResolver.resolve(href, context);
1069        if (source != null) {
1070            return Util.getInputSource(xsltc, source);
1071        }
1072        }
1073    }
1074    catch (TransformerException JavaDoc e) {
1075        // Falls through
1076
}
1077    return null;
1078    }
1079
1080    /**
1081     * Reset the per-session attributes to their default values
1082     */

1083    private void resetTransientAttributes() {
1084    _transletName = DEFAULT_TRANSLET_NAME;
1085    _destinationDirectory = null;
1086    _packageName = null;
1087    _jarFileName = null;
1088    }
1089        
1090    /**
1091     * Load the translet classes from local .class files and return
1092     * the bytecode array.
1093     *
1094     * @param source The xsl source
1095     * @param fullClassName The full name of the translet
1096     * @return The bytecode array
1097     */

1098    private byte[][] getBytecodesFromClasses(Source JavaDoc source, String JavaDoc fullClassName)
1099    {
1100        if (fullClassName == null)
1101            return null;
1102          
1103        String JavaDoc xslFileName = getStylesheetFileName(source);
1104        File JavaDoc xslFile = null;
1105        if (xslFileName != null)
1106            xslFile = new File JavaDoc(xslFileName);
1107        
1108        // Find the base name of the translet
1109
final String JavaDoc transletName;
1110        int lastDotIndex = fullClassName.lastIndexOf('.');
1111        if (lastDotIndex > 0)
1112            transletName = fullClassName.substring(lastDotIndex+1);
1113        else
1114            transletName = fullClassName;
1115                
1116        // Construct the path name for the translet class file
1117
String JavaDoc transletPath = fullClassName.replace('.', '/');
1118        if (_destinationDirectory != null) {
1119            transletPath = _destinationDirectory + "/" + transletPath + ".class";
1120        }
1121        else {
1122            if (xslFile != null && xslFile.getParent() != null)
1123                transletPath = xslFile.getParent() + "/" + transletPath + ".class";
1124            else
1125                transletPath = transletPath + ".class";
1126        }
1127                        
1128        // Return null if the translet class file does not exist.
1129
File JavaDoc transletFile = new File JavaDoc(transletPath);
1130        if (!transletFile.exists())
1131            return null;
1132                  
1133        // Compare the timestamps of the translet and the xsl file.
1134
// If the translet is older than the xsl file, return null
1135
// so that the xsl file is used for the transformation and
1136
// the translet is regenerated.
1137
if (xslFile != null && xslFile.exists()) {
1138            long xslTimestamp = xslFile.lastModified();
1139            long transletTimestamp = transletFile.lastModified();
1140            if (transletTimestamp < xslTimestamp)
1141                return null;
1142        }
1143        
1144        // Load the translet into a bytecode array.
1145
Vector JavaDoc bytecodes = new Vector JavaDoc();
1146        int fileLength = (int)transletFile.length();
1147        if (fileLength > 0) {
1148            FileInputStream JavaDoc input = null;
1149            try {
1150                input = new FileInputStream JavaDoc(transletFile);
1151            }
1152            catch (FileNotFoundException JavaDoc e) {
1153                return null;
1154            }
1155          
1156            byte[] bytes = new byte[fileLength];
1157            try {
1158            readFromInputStream(bytes, input, fileLength);
1159            input.close();
1160        }
1161        catch (IOException JavaDoc e) {
1162                return null;
1163            }
1164          
1165            bytecodes.addElement(bytes);
1166        }
1167        else
1168            return null;
1169        
1170        // Find the parent directory of the translet.
1171
String JavaDoc transletParentDir = transletFile.getParent();
1172        if (transletParentDir == null)
1173            transletParentDir = System.getProperty("user.dir");
1174          
1175        File JavaDoc transletParentFile = new File JavaDoc(transletParentDir);
1176        
1177        // Find all the auxiliary files which have a name pattern of "transletClass$nnn.class".
1178
final String JavaDoc transletAuxPrefix = transletName + "$";
1179        File JavaDoc[] auxfiles = transletParentFile.listFiles(new FilenameFilter JavaDoc() {
1180            public boolean accept(File JavaDoc dir, String JavaDoc name)
1181            {
1182                return (name.endsWith(".class") && name.startsWith(transletAuxPrefix));
1183            }
1184              });
1185        
1186        // Load the auxiliary class files and add them to the bytecode array.
1187
for (int i = 0; i < auxfiles.length; i++)
1188        {
1189            File JavaDoc auxfile = auxfiles[i];
1190            int auxlength = (int)auxfile.length();
1191            if (auxlength > 0) {
1192                FileInputStream JavaDoc auxinput = null;
1193                try {
1194                    auxinput = new FileInputStream JavaDoc(auxfile);
1195                }
1196                catch (FileNotFoundException JavaDoc e) {
1197                    continue;
1198                }
1199          
1200                byte[] bytes = new byte[auxlength];
1201            
1202                try {
1203                    readFromInputStream(bytes, auxinput, auxlength);
1204                    auxinput.close();
1205                }
1206                catch (IOException JavaDoc e) {
1207                    continue;
1208                }
1209            
1210                bytecodes.addElement(bytes);
1211            }
1212        }
1213        
1214        // Convert the Vector of byte[] to byte[][].
1215
final int count = bytecodes.size();
1216        if ( count > 0) {
1217            final byte[][] result = new byte[count][1];
1218            for (int i = 0; i < count; i++) {
1219                result[i] = (byte[])bytecodes.elementAt(i);
1220            }
1221          
1222            return result;
1223        }
1224        else
1225            return null;
1226    }
1227    
1228    /**
1229     * Load the translet classes from the jar file and return the bytecode.
1230     *
1231     * @param source The xsl source
1232     * @param fullClassName The full name of the translet
1233     * @return The bytecode array
1234     */

1235    private byte[][] getBytecodesFromJar(Source JavaDoc source, String JavaDoc fullClassName)
1236    {
1237        String JavaDoc xslFileName = getStylesheetFileName(source);
1238        File JavaDoc xslFile = null;
1239        if (xslFileName != null)
1240            xslFile = new File JavaDoc(xslFileName);
1241      
1242        // Construct the path for the jar file
1243
String JavaDoc jarPath = null;
1244        if (_destinationDirectory != null)
1245            jarPath = _destinationDirectory + "/" + _jarFileName;
1246        else {
1247            if (xslFile != null && xslFile.getParent() != null)
1248                jarPath = xslFile.getParent() + "/" + _jarFileName;
1249            else
1250                jarPath = _jarFileName;
1251        }
1252            
1253        // Return null if the jar file does not exist.
1254
File JavaDoc file = new File JavaDoc(jarPath);
1255        if (!file.exists())
1256            return null;
1257
1258        // Compare the timestamps of the jar file and the xsl file. Return null
1259
// if the xsl file is newer than the jar file.
1260
if (xslFile != null && xslFile.exists()) {
1261            long xslTimestamp = xslFile.lastModified();
1262            long transletTimestamp = file.lastModified();
1263            if (transletTimestamp < xslTimestamp)
1264                return null;
1265        }
1266      
1267        // Create a ZipFile object for the jar file
1268
ZipFile JavaDoc jarFile = null;
1269        try {
1270            jarFile = new ZipFile JavaDoc(file);
1271        }
1272        catch (IOException JavaDoc e) {
1273            return null;
1274        }
1275      
1276        String JavaDoc transletPath = fullClassName.replace('.', '/');
1277        String JavaDoc transletAuxPrefix = transletPath + "$";
1278        String JavaDoc transletFullName = transletPath + ".class";
1279      
1280        Vector JavaDoc bytecodes = new Vector JavaDoc();
1281      
1282        // Iterate through all entries in the jar file to find the
1283
// translet and auxiliary classes.
1284
Enumeration JavaDoc entries = jarFile.entries();
1285        while (entries.hasMoreElements())
1286        {
1287            ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
1288            String JavaDoc entryName = entry.getName();
1289            if (entry.getSize() > 0 &&
1290                  (entryName.equals(transletFullName) ||
1291                  (entryName.endsWith(".class") &&
1292                      entryName.startsWith(transletAuxPrefix))))
1293            {
1294                try {
1295                    InputStream JavaDoc input = jarFile.getInputStream(entry);
1296                    int size = (int)entry.getSize();
1297                    byte[] bytes = new byte[size];
1298                    readFromInputStream(bytes, input, size);
1299                    input.close();
1300                    bytecodes.addElement(bytes);
1301                }
1302                catch (IOException JavaDoc e) {
1303                    return null;
1304                }
1305            }
1306        }
1307      
1308        // Convert the Vector of byte[] to byte[][].
1309
final int count = bytecodes.size();
1310        if (count > 0) {
1311            final byte[][] result = new byte[count][1];
1312            for (int i = 0; i < count; i++) {
1313                result[i] = (byte[])bytecodes.elementAt(i);
1314            }
1315          
1316            return result;
1317        }
1318        else
1319            return null;
1320    }
1321    
1322    /**
1323     * Read a given number of bytes from the InputStream into a byte array.
1324     *
1325     * @param bytes The byte array to store the input content.
1326     * @param input The input stream.
1327     * @param size The number of bytes to read.
1328     */

1329    private void readFromInputStream(byte[] bytes, InputStream JavaDoc input, int size)
1330        throws IOException JavaDoc
1331    {
1332      int n = 0;
1333      int offset = 0;
1334      int length = size;
1335      while (length > 0 && (n = input.read(bytes, offset, length)) > 0) {
1336          offset = offset + n;
1337          length = length - n;
1338      }
1339    }
1340
1341    /**
1342     * Return the base class name of the translet.
1343     * The translet name is resolved using the following rules:
1344     * 1. if the _transletName attribute is set and its value is not "GregorSamsa",
1345     * then _transletName is returned.
1346     * 2. otherwise get the translet name from the base name of the system ID
1347     * 3. return "GregorSamsa" if the result from step 2 is null.
1348     *
1349     * @param source The input Source
1350     * @return The name of the translet class
1351     */

1352    private String JavaDoc getTransletBaseName(Source JavaDoc source)
1353    {
1354        String JavaDoc transletBaseName = null;
1355        if (!_transletName.equals(DEFAULT_TRANSLET_NAME))
1356            return _transletName;
1357        else {
1358            String JavaDoc systemId = source.getSystemId();
1359            if (systemId != null) {
1360            String JavaDoc baseName = Util.baseName(systemId);
1361        if (baseName != null) {
1362            baseName = Util.noExtName(baseName);
1363            transletBaseName = Util.toJavaName(baseName);
1364        }
1365            }
1366        }
1367      
1368        return (transletBaseName != null) ? transletBaseName : DEFAULT_TRANSLET_NAME;
1369    }
1370        
1371    /**
1372     * Return the local file name from the systemId of the Source object
1373     *
1374     * @param source The Source
1375     * @return The file name in the local filesystem, or null if the
1376     * systemId does not represent a local file.
1377     */

1378    private String JavaDoc getStylesheetFileName(Source JavaDoc source)
1379    {
1380        String JavaDoc systemId = source.getSystemId();
1381        if (systemId != null) {
1382            File JavaDoc file = new File JavaDoc(systemId);
1383            if (file.exists())
1384                return systemId;
1385            else {
1386                URL JavaDoc url = null;
1387            try {
1388                    url = new URL JavaDoc(systemId);
1389            }
1390            catch (MalformedURLException JavaDoc e) {
1391                    return null;
1392            }
1393          
1394            if ("file".equals(url.getProtocol()))
1395                    return url.getFile();
1396            else
1397                    return null;
1398            }
1399        }
1400        else
1401            return null;
1402    }
1403
1404    /**
1405     * Returns the Class object the provides the XSLTC DTM Manager service.
1406     */

1407    protected Class JavaDoc getDTMManagerClass() {
1408        return m_DTMManagerClass;
1409    }
1410}
1411
Popular Tags