KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TemplatesImpl.java,v 1.34 2004/02/23 10:29:36 aruny Exp $
18  */

19
20 package org.apache.xalan.xsltc.trax;
21
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29
30 import javax.xml.transform.Templates JavaDoc;
31 import javax.xml.transform.Transformer JavaDoc;
32 import javax.xml.transform.TransformerConfigurationException JavaDoc;
33 import javax.xml.transform.URIResolver JavaDoc;
34
35 import org.apache.xalan.xsltc.DOM;
36 import org.apache.xalan.xsltc.Translet;
37 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
38 import org.apache.xalan.xsltc.runtime.AbstractTranslet;
39 import org.apache.xalan.xsltc.runtime.Hashtable;
40
41 /**
42  * @author Morten Jorgensen
43  * @author G. Todd Millerj
44  * @author Jochen Cordes <Jochen.Cordes@t-online.de>
45  * @author Santiago Pericas-Geertsen
46  */

47 public final class TemplatesImpl implements Templates JavaDoc, Serializable JavaDoc {
48
49     /**
50      * Name of the superclass of all translets. This is needed to
51      * determine which, among all classes comprising a translet,
52      * is the main one.
53      */

54     private static String JavaDoc ABSTRACT_TRANSLET
55     = "org.apache.xalan.xsltc.runtime.AbstractTranslet";
56
57     /**
58      * Name of the main class or default name if unknown.
59      */

60     private String JavaDoc _name = null;
61
62     /**
63      * Contains the actual class definition for the translet class and
64      * any auxiliary classes.
65      */

66     private byte[][] _bytecodes = null;
67     
68     /**
69      * Contains the translet class definition(s). These are created when
70      * this Templates is created or when it is read back from disk.
71      */

72     private Class JavaDoc[] _class = null;
73
74     /**
75      * The index of the main translet class in the arrays _class[] and
76      * _bytecodes.
77      */

78     private int _transletIndex = -1;
79     
80     /**
81      * Contains the list of auxiliary class definitions.
82      */

83     private Hashtable _auxClasses = null;
84     
85     /**
86      * Output properties of this translet.
87      */

88     private Properties JavaDoc _outputProperties;
89
90     /**
91      * Number of spaces to add for output indentation.
92      */

93     private int _indentNumber;
94
95     /**
96      * This URIResolver is passed to all Transformers.
97      * Declaring it transient to fix bug 22438
98      */

99     private transient URIResolver JavaDoc _uriResolver = null;
100
101     /**
102      * Cache the DTM for the stylesheet in a thread local variable,
103      * which is used by the document('') function.
104      * Use ThreadLocal because a DTM cannot be shared between
105      * multiple threads.
106      * Declaring it transient to fix bug 22438
107      */

108     private transient ThreadLocal JavaDoc _sdom = new ThreadLocal JavaDoc();
109     
110     /**
111      * A reference to the transformer factory that this templates
112      * object belongs to.
113      */

114     private transient TransformerFactoryImpl _tfactory = null;
115
116     static final class TransletClassLoader extends ClassLoader JavaDoc {
117     TransletClassLoader(ClassLoader JavaDoc parent) {
118         super(parent);
119     }
120
121         /**
122          * Access to final protected superclass member from outer class.
123          */

124     Class JavaDoc defineClass(final byte[] b) {
125             return defineClass(null, b, 0, b.length);
126     }
127     }
128
129
130     /**
131      * Create an XSLTC template object from the bytecodes.
132      * The bytecodes for the translet and auxiliary classes, plus the name of
133      * the main translet class, must be supplied.
134      */

135     protected TemplatesImpl(byte[][] bytecodes, String JavaDoc transletName,
136     Properties JavaDoc outputProperties, int indentNumber,
137     TransformerFactoryImpl tfactory)
138     {
139     _bytecodes = bytecodes;
140     _name = transletName;
141     _outputProperties = outputProperties;
142     _indentNumber = indentNumber;
143     _tfactory = tfactory;
144     }
145     
146     /**
147      * Create an XSLTC template object from the translet class definition(s).
148      */

149     protected TemplatesImpl(Class JavaDoc[] transletClasses, String JavaDoc transletName,
150     Properties JavaDoc outputProperties, int indentNumber,
151     TransformerFactoryImpl tfactory)
152     {
153     _class = transletClasses;
154     _name = transletName;
155     _transletIndex = 0;
156     _outputProperties = outputProperties;
157     _indentNumber = indentNumber;
158     _tfactory = tfactory;
159     }
160     
161
162     /**
163      * Need for de-serialization, see readObject().
164      */

165     public TemplatesImpl() { }
166
167     /**
168      * Overrides the default readObject implementation since we decided
169      * it would be cleaner not to serialize the entire tranformer
170      * factory. [ ref bugzilla 12317 ]
171      * We need to check if the user defined class for URIResolver also
172      * implemented Serializable
173      * if yes then we need to deserialize the URIResolver
174      * Fix for bugzilla bug 22438
175      */

176     private void readObject(ObjectInputStream JavaDoc is)
177       throws IOException JavaDoc, ClassNotFoundException JavaDoc
178     {
179     is.defaultReadObject();
180         if (is.readBoolean()) {
181             _uriResolver = (URIResolver JavaDoc) is.readObject();
182         }
183
184     _tfactory = new TransformerFactoryImpl();
185     }
186
187
188     /**
189      * This is to fix bugzilla bug 22438
190      * If the user defined class implements URIResolver and Serializable
191      * then we want it to get serialized
192      */

193     private void writeObject(ObjectOutputStream JavaDoc os)
194         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
195         os.defaultWriteObject();
196         if (_uriResolver instanceof Serializable JavaDoc) {
197             os.writeBoolean(true);
198             os.writeObject((Serializable JavaDoc) _uriResolver);
199         }
200         else {
201             os.writeBoolean(false);
202         }
203     }
204
205
206      /**
207      * Store URIResolver needed for Transformers.
208      */

209     public synchronized void setURIResolver(URIResolver JavaDoc resolver) {
210     _uriResolver = resolver;
211     }
212
213     /**
214      * The TransformerFactory must pass us the translet bytecodes using this
215      * method before we can create any translet instances
216      */

217     protected synchronized void setTransletBytecodes(byte[][] bytecodes) {
218     _bytecodes = bytecodes;
219     }
220
221     /**
222      * Returns the translet bytecodes stored in this template
223      */

224     public synchronized byte[][] getTransletBytecodes() {
225     return _bytecodes;
226     }
227
228     /**
229      * Returns the translet bytecodes stored in this template
230      */

231     public synchronized Class JavaDoc[] getTransletClasses() {
232     try {
233         if (_class == null) defineTransletClasses();
234     }
235     catch (TransformerConfigurationException JavaDoc e) {
236         // Falls through
237
}
238     return _class;
239     }
240
241     /**
242      * Returns the index of the main class in array of bytecodes
243      */

244     public synchronized int getTransletIndex() {
245     try {
246         if (_class == null) defineTransletClasses();
247     }
248     catch (TransformerConfigurationException JavaDoc e) {
249         // Falls through
250
}
251     return _transletIndex;
252     }
253
254     /**
255      * The TransformerFactory should call this method to set the translet name
256      */

257     protected synchronized void setTransletName(String JavaDoc name) {
258     _name = name;
259     }
260
261     /**
262      * Returns the name of the main translet class stored in this template
263      */

264     protected synchronized String JavaDoc getTransletName() {
265     return _name;
266     }
267
268     /**
269      * Defines the translet class and auxiliary classes.
270      * Returns a reference to the Class object that defines the main class
271      */

272     private void defineTransletClasses()
273     throws TransformerConfigurationException JavaDoc {
274
275     if (_bytecodes == null) {
276         ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR);
277         throw new TransformerConfigurationException JavaDoc(err.toString());
278     }
279
280         TransletClassLoader loader = (TransletClassLoader)
281             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
282                 public Object JavaDoc run() {
283                     return new TransletClassLoader(ObjectFactory.findClassLoader());
284                 }
285             });
286
287     try {
288         final int classCount = _bytecodes.length;
289         _class = new Class JavaDoc[classCount];
290
291         if (classCount > 1) {
292             _auxClasses = new Hashtable();
293         }
294
295         for (int i = 0; i < classCount; i++) {
296         _class[i] = loader.defineClass(_bytecodes[i]);
297         final Class JavaDoc superClass = _class[i].getSuperclass();
298
299         // Check if this is the main class
300
if (superClass.getName().equals(ABSTRACT_TRANSLET)) {
301             _transletIndex = i;
302         }
303         else {
304             _auxClasses.put(_class[i].getName(), _class[i]);
305         }
306         }
307
308         if (_transletIndex < 0) {
309         ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name);
310         throw new TransformerConfigurationException JavaDoc(err.toString());
311         }
312     }
313     catch (ClassFormatError JavaDoc e) {
314         ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name);
315         throw new TransformerConfigurationException JavaDoc(err.toString());
316     }
317     catch (LinkageError JavaDoc e) {
318         ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
319         throw new TransformerConfigurationException JavaDoc(err.toString());
320     }
321     }
322
323     /**
324      * This method generates an instance of the translet class that is
325      * wrapped inside this Template. The translet instance will later
326      * be wrapped inside a Transformer object.
327      */

328     private Translet getTransletInstance()
329     throws TransformerConfigurationException JavaDoc {
330     try {
331         if (_name == null) return null;
332
333         if (_class == null) defineTransletClasses();
334
335         // The translet needs to keep a reference to all its auxiliary
336
// class to prevent the GC from collecting them
337
AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance();
338             translet.postInitialization();
339         translet.setTemplates(this);
340         if (_auxClasses != null) {
341             translet.setAuxiliaryClasses(_auxClasses);
342         }
343         
344         return translet;
345     }
346     catch (InstantiationException JavaDoc e) {
347         ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
348         throw new TransformerConfigurationException JavaDoc(err.toString());
349     }
350     catch (IllegalAccessException JavaDoc e) {
351         ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
352         throw new TransformerConfigurationException JavaDoc(err.toString());
353     }
354     }
355
356     /**
357      * Implements JAXP's Templates.newTransformer()
358      *
359      * @throws TransformerConfigurationException
360      */

361     public synchronized Transformer JavaDoc newTransformer()
362     throws TransformerConfigurationException JavaDoc
363     {
364     TransformerImpl transformer;
365
366     transformer = new TransformerImpl(getTransletInstance(), _outputProperties,
367         _indentNumber, _tfactory);
368     
369     if (_uriResolver != null) {
370         transformer.setURIResolver(_uriResolver);
371     }
372     return transformer;
373     }
374
375     /**
376      * Implements JAXP's Templates.getOutputProperties(). We need to
377      * instanciate a translet to get the output settings, so
378      * we might as well just instanciate a Transformer and use its
379      * implementation of this method.
380      */

381     public synchronized Properties JavaDoc getOutputProperties() {
382     try {
383         return newTransformer().getOutputProperties();
384     }
385     catch (TransformerConfigurationException JavaDoc e) {
386         return null;
387     }
388     }
389
390     /**
391      * Return the thread local copy of the stylesheet DOM.
392      */

393     public DOM getStylesheetDOM() {
394         return (DOM)_sdom.get();
395     }
396     
397     /**
398      * Set the thread local copy of the stylesheet DOM.
399      */

400     public void setStylesheetDOM(DOM sdom) {
401         _sdom.set(sdom);
402     }
403 }
404
Popular Tags