KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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.1.2.1 2006/09/19 01:07:39 jeffsuttor Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.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 import javax.xml.XMLConstants JavaDoc;
30
31 import javax.xml.transform.Templates JavaDoc;
32 import javax.xml.transform.Transformer JavaDoc;
33 import javax.xml.transform.TransformerConfigurationException JavaDoc;
34 import javax.xml.transform.URIResolver JavaDoc;
35
36 import com.sun.org.apache.xalan.internal.xsltc.DOM;
37 import com.sun.org.apache.xalan.internal.xsltc.Translet;
38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
39 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
40 import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
41
42 /**
43  * @author Morten Jorgensen
44  * @author G. Todd Millerj
45  * @author Jochen Cordes <Jochen.Cordes@t-online.de>
46  * @author Santiago Pericas-Geertsen
47  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

210     public synchronized void setURIResolver(URIResolver JavaDoc resolver) {
211     _uriResolver = resolver;
212     }
213
214     /**
215      * The TransformerFactory must pass us the translet bytecodes using this
216      * method before we can create any translet instances
217      *
218      * * Note: This method is private for security reasons. See
219      * CR 6537898. When merging with Apache, we must ensure
220      * that the privateness of this method is maintained (that
221      * is why it wasn't removed).
222      */

223     private synchronized void setTransletBytecodes(byte[][] bytecodes) {
224     _bytecodes = bytecodes;
225     }
226
227     /**
228      * Returns the translet bytecodes stored in this template
229      *
230      * Note: This method is private for security reasons. See
231      * CR 6537898. When merging with Apache, we must ensure
232      * that the privateness of this method is maintained (that
233      * is why it wasn't removed).
234      */

235     private synchronized byte[][] getTransletBytecodes() {
236     return _bytecodes;
237     }
238
239     /**
240      * Returns the translet bytecodes stored in this template
241      *
242      * Note: This method is private for security reasons. See
243      * CR 6537898. When merging with Apache, we must ensure
244      * that the privateness of this method is maintained (that
245      * is why it wasn't removed).
246      */

247     private synchronized Class JavaDoc[] getTransletClasses() {
248     try {
249         if (_class == null) defineTransletClasses();
250     }
251     catch (TransformerConfigurationException JavaDoc e) {
252         // Falls through
253
}
254     return _class;
255     }
256
257     /**
258      * Returns the index of the main class in array of bytecodes
259      */

260     public synchronized int getTransletIndex() {
261     try {
262         if (_class == null) defineTransletClasses();
263     }
264     catch (TransformerConfigurationException JavaDoc e) {
265         // Falls through
266
}
267     return _transletIndex;
268     }
269
270     /**
271      * The TransformerFactory should call this method to set the translet name
272      */

273     protected synchronized void setTransletName(String JavaDoc name) {
274     _name = name;
275     }
276
277     /**
278      * Returns the name of the main translet class stored in this template
279      */

280     protected synchronized String JavaDoc getTransletName() {
281     return _name;
282     }
283
284     /**
285      * Defines the translet class and auxiliary classes.
286      * Returns a reference to the Class object that defines the main class
287      */

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

344     private Translet getTransletInstance()
345     throws TransformerConfigurationException JavaDoc {
346     try {
347         if (_name == null) return null;
348
349         if (_class == null) defineTransletClasses();
350
351         // The translet needs to keep a reference to all its auxiliary
352
// class to prevent the GC from collecting them
353
AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance();
354             translet.postInitialization();
355         translet.setTemplates(this);
356         if (_auxClasses != null) {
357             translet.setAuxiliaryClasses(_auxClasses);
358         }
359         
360         return translet;
361     }
362     catch (InstantiationException JavaDoc e) {
363         ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
364         throw new TransformerConfigurationException JavaDoc(err.toString());
365     }
366     catch (IllegalAccessException JavaDoc e) {
367         ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
368         throw new TransformerConfigurationException JavaDoc(err.toString());
369     }
370     }
371
372     /**
373      * Implements JAXP's Templates.newTransformer()
374      *
375      * @throws TransformerConfigurationException
376      */

377     public synchronized Transformer JavaDoc newTransformer()
378     throws TransformerConfigurationException JavaDoc
379     {
380     TransformerImpl transformer;
381
382     transformer = new TransformerImpl(getTransletInstance(), _outputProperties,
383         _indentNumber, _tfactory);
384     
385     if (_uriResolver != null) {
386         transformer.setURIResolver(_uriResolver);
387     }
388         
389         if (_tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) {
390             transformer.setSecureProcessing(true);
391         }
392
393     return transformer;
394     }
395
396     /**
397      * Implements JAXP's Templates.getOutputProperties(). We need to
398      * instanciate a translet to get the output settings, so
399      * we might as well just instanciate a Transformer and use its
400      * implementation of this method.
401      */

402     public synchronized Properties JavaDoc getOutputProperties() {
403     try {
404         return newTransformer().getOutputProperties();
405     }
406     catch (TransformerConfigurationException JavaDoc e) {
407         return null;
408     }
409     }
410
411     /**
412      * Return the thread local copy of the stylesheet DOM.
413      */

414     public DOM getStylesheetDOM() {
415         return (DOM)_sdom.get();
416     }
417     
418     /**
419      * Set the thread local copy of the stylesheet DOM.
420      */

421     public void setStylesheetDOM(DOM sdom) {
422         _sdom.set(sdom);
423     }
424 }
425
Popular Tags