12005
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2001-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: TemplatesImpl.java,v 1.8 2007/03/26 20:12:27 spericas Exp $
|
|
22 |
*/
|
|
23 |
|
|
24 |
package com.sun.org.apache.xalan.internal.xsltc.trax;
|
|
25 |
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.io.ObjectInputStream;
|
|
28 |
import java.io.ObjectOutputStream;
|
|
29 |
import java.io.Serializable;
|
|
30 |
import java.util.Properties;
|
|
31 |
import java.security.AccessController;
|
|
32 |
import java.security.PrivilegedAction;
|
|
33 |
|
|
34 |
import javax.xml.XMLConstants;
|
|
35 |
import javax.xml.transform.Templates;
|
|
36 |
import javax.xml.transform.Transformer;
|
|
37 |
import javax.xml.transform.TransformerConfigurationException;
|
|
38 |
import javax.xml.transform.URIResolver;
|
|
39 |
|
|
40 |
import com.sun.org.apache.xalan.internal.xsltc.DOM;
|
|
41 |
import com.sun.org.apache.xalan.internal.xsltc.Translet;
|
|
42 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
|
|
43 |
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
|
|
44 |
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
|
12458
|
45 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
|
16953
|
46 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
|
12005
|
47 |
|
|
48 |
/**
|
|
49 |
* @author Morten Jorgensen
|
|
50 |
* @author G. Todd Millerj
|
|
51 |
* @author Jochen Cordes <Jochen.Cordes@t-online.de>
|
|
52 |
* @author Santiago Pericas-Geertsen
|
|
53 |
*/
|
|
54 |
public final class TemplatesImpl implements Templates, Serializable {
|
|
55 |
static final long serialVersionUID = 673094361519270707L;
|
16953
|
56 |
public final static String DESERIALIZE_TRANSLET = "jdk.xml.enableTemplatesImplDeserialization";
|
|
57 |
|
12005
|
58 |
/**
|
|
59 |
* Name of the superclass of all translets. This is needed to
|
|
60 |
* determine which, among all classes comprising a translet,
|
|
61 |
* is the main one.
|
|
62 |
*/
|
|
63 |
private static String ABSTRACT_TRANSLET
|
|
64 |
= "com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet";
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Name of the main class or default name if unknown.
|
|
68 |
*/
|
|
69 |
private String _name = null;
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Contains the actual class definition for the translet class and
|
|
73 |
* any auxiliary classes.
|
|
74 |
*/
|
|
75 |
private byte[][] _bytecodes = null;
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Contains the translet class definition(s). These are created when
|
|
79 |
* this Templates is created or when it is read back from disk.
|
|
80 |
*/
|
|
81 |
private Class[] _class = null;
|
|
82 |
|
|
83 |
/**
|
|
84 |
* The index of the main translet class in the arrays _class[] and
|
|
85 |
* _bytecodes.
|
|
86 |
*/
|
|
87 |
private int _transletIndex = -1;
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Contains the list of auxiliary class definitions.
|
|
91 |
*/
|
|
92 |
private Hashtable _auxClasses = null;
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Output properties of this translet.
|
|
96 |
*/
|
|
97 |
private Properties _outputProperties;
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Number of spaces to add for output indentation.
|
|
101 |
*/
|
|
102 |
private int _indentNumber;
|
|
103 |
|
|
104 |
/**
|
|
105 |
* This URIResolver is passed to all Transformers.
|
|
106 |
* Declaring it transient to fix bug 22438
|
|
107 |
*/
|
|
108 |
private transient URIResolver _uriResolver = null;
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Cache the DTM for the stylesheet in a thread local variable,
|
|
112 |
* which is used by the document('') function.
|
|
113 |
* Use ThreadLocal because a DTM cannot be shared between
|
|
114 |
* multiple threads.
|
|
115 |
* Declaring it transient to fix bug 22438
|
|
116 |
*/
|
|
117 |
private transient ThreadLocal _sdom = new ThreadLocal();
|
|
118 |
|
|
119 |
/**
|
|
120 |
* A reference to the transformer factory that this templates
|
|
121 |
* object belongs to.
|
|
122 |
*/
|
|
123 |
private transient TransformerFactoryImpl _tfactory = null;
|
|
124 |
|
12458
|
125 |
private boolean _useServicesMechanism;
|
|
126 |
|
12005
|
127 |
static final class TransletClassLoader extends ClassLoader {
|
|
128 |
TransletClassLoader(ClassLoader parent) {
|
|
129 |
super(parent);
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Access to final protected superclass member from outer class.
|
|
134 |
*/
|
|
135 |
Class defineClass(final byte[] b) {
|
|
136 |
return defineClass(null, b, 0, b.length);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Create an XSLTC template object from the bytecodes.
|
|
143 |
* The bytecodes for the translet and auxiliary classes, plus the name of
|
|
144 |
* the main translet class, must be supplied.
|
|
145 |
*/
|
|
146 |
protected TemplatesImpl(byte[][] bytecodes, String transletName,
|
|
147 |
Properties outputProperties, int indentNumber,
|
|
148 |
TransformerFactoryImpl tfactory)
|
|
149 |
{
|
|
150 |
_bytecodes = bytecodes;
|
12458
|
151 |
init(transletName, outputProperties, indentNumber, tfactory);
|
12005
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Create an XSLTC template object from the translet class definition(s).
|
|
156 |
*/
|
|
157 |
protected TemplatesImpl(Class[] transletClasses, String transletName,
|
|
158 |
Properties outputProperties, int indentNumber,
|
|
159 |
TransformerFactoryImpl tfactory)
|
|
160 |
{
|
|
161 |
_class = transletClasses;
|
12458
|
162 |
_transletIndex = 0;
|
|
163 |
init(transletName, outputProperties, indentNumber, tfactory);
|
|
164 |
}
|
|
165 |
|
|
166 |
private void init(String transletName,
|
|
167 |
Properties outputProperties, int indentNumber,
|
|
168 |
TransformerFactoryImpl tfactory) {
|
12005
|
169 |
_name = transletName;
|
|
170 |
_outputProperties = outputProperties;
|
|
171 |
_indentNumber = indentNumber;
|
|
172 |
_tfactory = tfactory;
|
12458
|
173 |
_useServicesMechanism = tfactory.useServicesMechnism();
|
12005
|
174 |
}
|
|
175 |
/**
|
|
176 |
* Need for de-serialization, see readObject().
|
|
177 |
*/
|
|
178 |
public TemplatesImpl() { }
|
|
179 |
|
|
180 |
/**
|
|
181 |
* Overrides the default readObject implementation since we decided
|
|
182 |
* it would be cleaner not to serialize the entire tranformer
|
|
183 |
* factory. [ ref bugzilla 12317 ]
|
|
184 |
* We need to check if the user defined class for URIResolver also
|
|
185 |
* implemented Serializable
|
|
186 |
* if yes then we need to deserialize the URIResolver
|
|
187 |
* Fix for bugzilla bug 22438
|
|
188 |
*/
|
|
189 |
private void readObject(ObjectInputStream is)
|
|
190 |
throws IOException, ClassNotFoundException
|
|
191 |
{
|
16953
|
192 |
SecurityManager security = System.getSecurityManager();
|
|
193 |
if (security != null){
|
|
194 |
String temp = SecuritySupport.getSystemProperty(DESERIALIZE_TRANSLET);
|
|
195 |
if (temp == null || !(temp.length()==0 || temp.equalsIgnoreCase("true"))) {
|
|
196 |
ErrorMsg err = new ErrorMsg(ErrorMsg.DESERIALIZE_TRANSLET_ERR);
|
|
197 |
throw new UnsupportedOperationException(err.toString());
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
12005
|
201 |
is.defaultReadObject();
|
|
202 |
if (is.readBoolean()) {
|
|
203 |
_uriResolver = (URIResolver) is.readObject();
|
|
204 |
}
|
|
205 |
|
|
206 |
_tfactory = new TransformerFactoryImpl();
|
|
207 |
}
|
|
208 |
|
|
209 |
|
|
210 |
/**
|
|
211 |
* This is to fix bugzilla bug 22438
|
|
212 |
* If the user defined class implements URIResolver and Serializable
|
|
213 |
* then we want it to get serialized
|
|
214 |
*/
|
|
215 |
private void writeObject(ObjectOutputStream os)
|
|
216 |
throws IOException, ClassNotFoundException {
|
|
217 |
os.defaultWriteObject();
|
|
218 |
if (_uriResolver instanceof Serializable) {
|
|
219 |
os.writeBoolean(true);
|
|
220 |
os.writeObject((Serializable) _uriResolver);
|
|
221 |
}
|
|
222 |
else {
|
|
223 |
os.writeBoolean(false);
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
12458
|
227 |
/**
|
|
228 |
* Return the state of the services mechanism feature.
|
|
229 |
*/
|
|
230 |
public boolean useServicesMechnism() {
|
|
231 |
return _useServicesMechanism;
|
|
232 |
}
|
12005
|
233 |
|
|
234 |
/**
|
|
235 |
* Store URIResolver needed for Transformers.
|
|
236 |
*/
|
|
237 |
public synchronized void setURIResolver(URIResolver resolver) {
|
|
238 |
_uriResolver = resolver;
|
|
239 |
}
|
|
240 |
|
|
241 |
/**
|
|
242 |
* The TransformerFactory must pass us the translet bytecodes using this
|
|
243 |
* method before we can create any translet instances
|
|
244 |
*
|
|
245 |
* Note: This method is private for security reasons. See
|
|
246 |
* CR 6537898. When merging with Apache, we must ensure
|
|
247 |
* that the privateness of this method is maintained (that
|
|
248 |
* is why it wasn't removed).
|
|
249 |
*/
|
|
250 |
private synchronized void setTransletBytecodes(byte[][] bytecodes) {
|
|
251 |
_bytecodes = bytecodes;
|
|
252 |
}
|
|
253 |
|
|
254 |
/**
|
|
255 |
* Returns the translet bytecodes stored in this template
|
|
256 |
*
|
|
257 |
* Note: This method is private for security reasons. See
|
|
258 |
* CR 6537898. When merging with Apache, we must ensure
|
|
259 |
* that the privateness of this method is maintained (that
|
|
260 |
* is why it wasn't removed).
|
|
261 |
*/
|
|
262 |
private synchronized byte[][] getTransletBytecodes() {
|
|
263 |
return _bytecodes;
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Returns the translet bytecodes stored in this template
|
|
268 |
*
|
|
269 |
* Note: This method is private for security reasons. See
|
|
270 |
* CR 6537898. When merging with Apache, we must ensure
|
|
271 |
* that the privateness of this method is maintained (that
|
|
272 |
* is why it wasn't removed).
|
|
273 |
*/
|
|
274 |
private synchronized Class[] getTransletClasses() {
|
|
275 |
try {
|
|
276 |
if (_class == null) defineTransletClasses();
|
|
277 |
}
|
|
278 |
catch (TransformerConfigurationException e) {
|
|
279 |
// Falls through
|
|
280 |
}
|
|
281 |
return _class;
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Returns the index of the main class in array of bytecodes
|
|
286 |
*/
|
|
287 |
public synchronized int getTransletIndex() {
|
|
288 |
try {
|
|
289 |
if (_class == null) defineTransletClasses();
|
|
290 |
}
|
|
291 |
catch (TransformerConfigurationException e) {
|
|
292 |
// Falls through
|
|
293 |
}
|
|
294 |
return _transletIndex;
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* The TransformerFactory should call this method to set the translet name
|
|
299 |
*/
|
|
300 |
protected synchronized void setTransletName(String name) {
|
|
301 |
_name = name;
|
|
302 |
}
|
|
303 |
|
|
304 |
/**
|
|
305 |
* Returns the name of the main translet class stored in this template
|
|
306 |
*/
|
|
307 |
protected synchronized String getTransletName() {
|
|
308 |
return _name;
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Defines the translet class and auxiliary classes.
|
|
313 |
* Returns a reference to the Class object that defines the main class
|
|
314 |
*/
|
|
315 |
private void defineTransletClasses()
|
|
316 |
throws TransformerConfigurationException {
|
|
317 |
|
|
318 |
if (_bytecodes == null) {
|
|
319 |
ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR);
|
|
320 |
throw new TransformerConfigurationException(err.toString());
|
|
321 |
}
|
|
322 |
|
|
323 |
TransletClassLoader loader = (TransletClassLoader)
|
|
324 |
AccessController.doPrivileged(new PrivilegedAction() {
|
|
325 |
public Object run() {
|
|
326 |
return new TransletClassLoader(ObjectFactory.findClassLoader());
|
|
327 |
}
|
|
328 |
});
|
|
329 |
|
|
330 |
try {
|
|
331 |
final int classCount = _bytecodes.length;
|
|
332 |
_class = new Class[classCount];
|
|
333 |
|
|
334 |
if (classCount > 1) {
|
|
335 |
_auxClasses = new Hashtable();
|
|
336 |
}
|
|
337 |
|
|
338 |
for (int i = 0; i < classCount; i++) {
|
|
339 |
_class[i] = loader.defineClass(_bytecodes[i]);
|
|
340 |
final Class superClass = _class[i].getSuperclass();
|
|
341 |
|
|
342 |
// Check if this is the main class
|
|
343 |
if (superClass.getName().equals(ABSTRACT_TRANSLET)) {
|
|
344 |
_transletIndex = i;
|
|
345 |
}
|
|
346 |
else {
|
|
347 |
_auxClasses.put(_class[i].getName(), _class[i]);
|
|
348 |
}
|
|
349 |
}
|
|
350 |
|
|
351 |
if (_transletIndex < 0) {
|
|
352 |
ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name);
|
|
353 |
throw new TransformerConfigurationException(err.toString());
|
|
354 |
}
|
|
355 |
}
|
|
356 |
catch (ClassFormatError e) {
|
|
357 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name);
|
|
358 |
throw new TransformerConfigurationException(err.toString());
|
|
359 |
}
|
|
360 |
catch (LinkageError e) {
|
|
361 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
|
|
362 |
throw new TransformerConfigurationException(err.toString());
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
/**
|
|
367 |
* This method generates an instance of the translet class that is
|
|
368 |
* wrapped inside this Template. The translet instance will later
|
|
369 |
* be wrapped inside a Transformer object.
|
|
370 |
*/
|
|
371 |
private Translet getTransletInstance()
|
|
372 |
throws TransformerConfigurationException {
|
|
373 |
try {
|
|
374 |
if (_name == null) return null;
|
|
375 |
|
|
376 |
if (_class == null) defineTransletClasses();
|
|
377 |
|
|
378 |
// The translet needs to keep a reference to all its auxiliary
|
|
379 |
// class to prevent the GC from collecting them
|
|
380 |
AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance();
|
|
381 |
translet.postInitialization();
|
|
382 |
translet.setTemplates(this);
|
12458
|
383 |
translet.setServicesMechnism(_useServicesMechanism);
|
12005
|
384 |
if (_auxClasses != null) {
|
|
385 |
translet.setAuxiliaryClasses(_auxClasses);
|
|
386 |
}
|
|
387 |
|
|
388 |
return translet;
|
|
389 |
}
|
|
390 |
catch (InstantiationException e) {
|
|
391 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
|
|
392 |
throw new TransformerConfigurationException(err.toString());
|
|
393 |
}
|
|
394 |
catch (IllegalAccessException e) {
|
|
395 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
|
|
396 |
throw new TransformerConfigurationException(err.toString());
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Implements JAXP's Templates.newTransformer()
|
|
402 |
*
|
|
403 |
* @throws TransformerConfigurationException
|
|
404 |
*/
|
|
405 |
public synchronized Transformer newTransformer()
|
|
406 |
throws TransformerConfigurationException
|
|
407 |
{
|
|
408 |
TransformerImpl transformer;
|
|
409 |
|
|
410 |
transformer = new TransformerImpl(getTransletInstance(), _outputProperties,
|
|
411 |
_indentNumber, _tfactory);
|
|
412 |
|
|
413 |
if (_uriResolver != null) {
|
|
414 |
transformer.setURIResolver(_uriResolver);
|
|
415 |
}
|
|
416 |
|
|
417 |
if (_tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) {
|
|
418 |
transformer.setSecureProcessing(true);
|
|
419 |
}
|
|
420 |
return transformer;
|
|
421 |
}
|
|
422 |
|
|
423 |
/**
|
|
424 |
* Implements JAXP's Templates.getOutputProperties(). We need to
|
|
425 |
* instanciate a translet to get the output settings, so
|
|
426 |
* we might as well just instanciate a Transformer and use its
|
|
427 |
* implementation of this method.
|
|
428 |
*/
|
|
429 |
public synchronized Properties getOutputProperties() {
|
|
430 |
try {
|
|
431 |
return newTransformer().getOutputProperties();
|
|
432 |
}
|
|
433 |
catch (TransformerConfigurationException e) {
|
|
434 |
return null;
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
/**
|
|
439 |
* Return the thread local copy of the stylesheet DOM.
|
|
440 |
*/
|
|
441 |
public DOM getStylesheetDOM() {
|
|
442 |
return (DOM)_sdom.get();
|
|
443 |
}
|
|
444 |
|
|
445 |
/**
|
|
446 |
* Set the thread local copy of the stylesheet DOM.
|
|
447 |
*/
|
|
448 |
public void setStylesheetDOM(DOM sdom) {
|
|
449 |
_sdom.set(sdom);
|
|
450 |
}
|
|
451 |
}
|