author | alanb |
Thu, 17 Mar 2016 19:04:05 +0000 | |
changeset 36520 | cb52aa47ff0a |
parent 33350 | 97bf4aeb431d |
child 37838 | 45821736044e |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
33349 | 2 |
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. |
12005 | 3 |
*/ |
4 |
/* |
|
33349 | 5 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
6 |
* contributor license agreements. See the NOTICE file distributed with |
|
7 |
* this work for additional information regarding copyright ownership. |
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
9 |
* (the "License"); you may not use this file except in compliance with |
|
10 |
* the License. You may obtain a copy of the License at |
|
12005 | 11 |
* |
33349 | 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
12005 | 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 |
||
17534 | 26 |
import com.sun.org.apache.xalan.internal.XalanConstants; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
27 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
28 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
29 |
import com.sun.org.apache.xalan.internal.xsltc.DOM; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
30 |
import com.sun.org.apache.xalan.internal.xsltc.Translet; |
36520 | 31 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
32 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
33 |
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; |
12005 | 34 |
import java.io.IOException; |
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
35 |
import java.io.NotSerializableException; |
12005 | 36 |
import java.io.ObjectInputStream; |
37 |
import java.io.ObjectOutputStream; |
|
33349 | 38 |
import java.io.ObjectStreamField; |
12005 | 39 |
import java.io.Serializable; |
36520 | 40 |
import java.util.Arrays; |
41 |
import java.util.Collections; |
|
12005 | 42 |
import java.security.AccessController; |
43 |
import java.security.PrivilegedAction; |
|
36520 | 44 |
import java.lang.reflect.Module; |
33349 | 45 |
import java.util.HashMap; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
46 |
import java.util.Map; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
47 |
import java.util.Properties; |
12005 | 48 |
import javax.xml.XMLConstants; |
49 |
import javax.xml.transform.Templates; |
|
50 |
import javax.xml.transform.Transformer; |
|
51 |
import javax.xml.transform.TransformerConfigurationException; |
|
52 |
import javax.xml.transform.URIResolver; |
|
53 |
||
36520 | 54 |
import jdk.internal.module.Modules; |
55 |
||
12005 | 56 |
/** |
57 |
* @author Morten Jorgensen |
|
58 |
* @author G. Todd Millerj |
|
59 |
* @author Jochen Cordes <Jochen.Cordes@t-online.de> |
|
60 |
* @author Santiago Pericas-Geertsen |
|
61 |
*/ |
|
62 |
public final class TemplatesImpl implements Templates, Serializable { |
|
63 |
static final long serialVersionUID = 673094361519270707L; |
|
16953 | 64 |
public final static String DESERIALIZE_TRANSLET = "jdk.xml.enableTemplatesImplDeserialization"; |
65 |
||
12005 | 66 |
/** |
67 |
* Name of the superclass of all translets. This is needed to |
|
68 |
* determine which, among all classes comprising a translet, |
|
69 |
* is the main one. |
|
70 |
*/ |
|
71 |
private static String ABSTRACT_TRANSLET |
|
72 |
= "com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet"; |
|
73 |
||
74 |
/** |
|
75 |
* Name of the main class or default name if unknown. |
|
76 |
*/ |
|
77 |
private String _name = null; |
|
78 |
||
79 |
/** |
|
80 |
* Contains the actual class definition for the translet class and |
|
81 |
* any auxiliary classes. |
|
82 |
*/ |
|
83 |
private byte[][] _bytecodes = null; |
|
84 |
||
85 |
/** |
|
86 |
* Contains the translet class definition(s). These are created when |
|
87 |
* this Templates is created or when it is read back from disk. |
|
88 |
*/ |
|
89 |
private Class[] _class = null; |
|
90 |
||
91 |
/** |
|
92 |
* The index of the main translet class in the arrays _class[] and |
|
93 |
* _bytecodes. |
|
94 |
*/ |
|
95 |
private int _transletIndex = -1; |
|
96 |
||
97 |
/** |
|
98 |
* Contains the list of auxiliary class definitions. |
|
99 |
*/ |
|
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
100 |
private transient Map<String, Class<?>> _auxClasses = null; |
12005 | 101 |
|
102 |
/** |
|
103 |
* Output properties of this translet. |
|
104 |
*/ |
|
105 |
private Properties _outputProperties; |
|
106 |
||
107 |
/** |
|
108 |
* Number of spaces to add for output indentation. |
|
109 |
*/ |
|
110 |
private int _indentNumber; |
|
111 |
||
112 |
/** |
|
113 |
* This URIResolver is passed to all Transformers. |
|
114 |
* Declaring it transient to fix bug 22438 |
|
115 |
*/ |
|
116 |
private transient URIResolver _uriResolver = null; |
|
117 |
||
118 |
/** |
|
119 |
* Cache the DTM for the stylesheet in a thread local variable, |
|
120 |
* which is used by the document('') function. |
|
121 |
* Use ThreadLocal because a DTM cannot be shared between |
|
122 |
* multiple threads. |
|
123 |
* Declaring it transient to fix bug 22438 |
|
124 |
*/ |
|
125 |
private transient ThreadLocal _sdom = new ThreadLocal(); |
|
126 |
||
127 |
/** |
|
128 |
* A reference to the transformer factory that this templates |
|
129 |
* object belongs to. |
|
130 |
*/ |
|
131 |
private transient TransformerFactoryImpl _tfactory = null; |
|
132 |
||
33349 | 133 |
/** |
134 |
* A flag to determine whether the Service Mechanism is used |
|
135 |
*/ |
|
136 |
private transient boolean _useServicesMechanism; |
|
12458 | 137 |
|
17534 | 138 |
/** |
139 |
* protocols allowed for external references set by the stylesheet processing instruction, Import and Include element. |
|
140 |
*/ |
|
33349 | 141 |
private transient String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT; |
142 |
||
143 |
/** |
|
144 |
* @serialField _name String The Name of the main class |
|
145 |
* @serialField _bytecodes byte[][] Class definition |
|
146 |
* @serialField _class Class[] The translet class definition(s). |
|
147 |
* @serialField _transletIndex int The index of the main translet class |
|
148 |
* @serialField _outputProperties Properties Output properties of this translet. |
|
149 |
* @serialField _indentNumber int Number of spaces to add for output indentation. |
|
150 |
*/ |
|
151 |
private static final ObjectStreamField[] serialPersistentFields = |
|
152 |
new ObjectStreamField[] { |
|
153 |
new ObjectStreamField("_name", String.class), |
|
154 |
new ObjectStreamField("_bytecodes", byte[][].class), |
|
155 |
new ObjectStreamField("_class", Class[].class), |
|
156 |
new ObjectStreamField("_transletIndex", int.class), |
|
157 |
new ObjectStreamField("_outputProperties", Properties.class), |
|
158 |
new ObjectStreamField("_indentNumber", int.class), |
|
159 |
}; |
|
17534 | 160 |
|
12005 | 161 |
static final class TransletClassLoader extends ClassLoader { |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
162 |
private final Map<String,Class> _loadedExternalExtensionFunctions; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
163 |
|
33349 | 164 |
TransletClassLoader(ClassLoader parent) { |
165 |
super(parent); |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
166 |
_loadedExternalExtensionFunctions = null; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
167 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
168 |
|
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
169 |
TransletClassLoader(ClassLoader parent,Map<String, Class> mapEF) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
170 |
super(parent); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
171 |
_loadedExternalExtensionFunctions = mapEF; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
172 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
173 |
|
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
174 |
public Class<?> loadClass(String name) throws ClassNotFoundException { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
175 |
Class<?> ret = null; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
176 |
// The _loadedExternalExtensionFunctions will be empty when the |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
177 |
// SecurityManager is not set and the FSP is turned off |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
178 |
if (_loadedExternalExtensionFunctions != null) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
179 |
ret = _loadedExternalExtensionFunctions.get(name); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
180 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
181 |
if (ret == null) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
182 |
ret = super.loadClass(name); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
183 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
184 |
return ret; |
33349 | 185 |
} |
12005 | 186 |
|
187 |
/** |
|
188 |
* Access to final protected superclass member from outer class. |
|
189 |
*/ |
|
190 |
Class defineClass(final byte[] b) { |
|
191 |
return defineClass(null, b, 0, b.length); |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
||
196 |
/** |
|
197 |
* Create an XSLTC template object from the bytecodes. |
|
198 |
* The bytecodes for the translet and auxiliary classes, plus the name of |
|
199 |
* the main translet class, must be supplied. |
|
200 |
*/ |
|
201 |
protected TemplatesImpl(byte[][] bytecodes, String transletName, |
|
202 |
Properties outputProperties, int indentNumber, |
|
203 |
TransformerFactoryImpl tfactory) |
|
204 |
{ |
|
205 |
_bytecodes = bytecodes; |
|
12458 | 206 |
init(transletName, outputProperties, indentNumber, tfactory); |
12005 | 207 |
} |
208 |
||
209 |
/** |
|
210 |
* Create an XSLTC template object from the translet class definition(s). |
|
211 |
*/ |
|
212 |
protected TemplatesImpl(Class[] transletClasses, String transletName, |
|
213 |
Properties outputProperties, int indentNumber, |
|
214 |
TransformerFactoryImpl tfactory) |
|
215 |
{ |
|
216 |
_class = transletClasses; |
|
12458 | 217 |
_transletIndex = 0; |
218 |
init(transletName, outputProperties, indentNumber, tfactory); |
|
219 |
} |
|
220 |
||
221 |
private void init(String transletName, |
|
222 |
Properties outputProperties, int indentNumber, |
|
223 |
TransformerFactoryImpl tfactory) { |
|
12005 | 224 |
_name = transletName; |
225 |
_outputProperties = outputProperties; |
|
226 |
_indentNumber = indentNumber; |
|
227 |
_tfactory = tfactory; |
|
12458 | 228 |
_useServicesMechanism = tfactory.useServicesMechnism(); |
17534 | 229 |
_accessExternalStylesheet = (String) tfactory.getAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET); |
12005 | 230 |
} |
231 |
/** |
|
232 |
* Need for de-serialization, see readObject(). |
|
233 |
*/ |
|
234 |
public TemplatesImpl() { } |
|
235 |
||
236 |
/** |
|
237 |
* Overrides the default readObject implementation since we decided |
|
238 |
* it would be cleaner not to serialize the entire tranformer |
|
239 |
* factory. [ ref bugzilla 12317 ] |
|
240 |
* We need to check if the user defined class for URIResolver also |
|
241 |
* implemented Serializable |
|
242 |
* if yes then we need to deserialize the URIResolver |
|
243 |
* Fix for bugzilla bug 22438 |
|
244 |
*/ |
|
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
245 |
@SuppressWarnings("unchecked") |
12005 | 246 |
private void readObject(ObjectInputStream is) |
247 |
throws IOException, ClassNotFoundException |
|
248 |
{ |
|
16953 | 249 |
SecurityManager security = System.getSecurityManager(); |
250 |
if (security != null){ |
|
251 |
String temp = SecuritySupport.getSystemProperty(DESERIALIZE_TRANSLET); |
|
252 |
if (temp == null || !(temp.length()==0 || temp.equalsIgnoreCase("true"))) { |
|
253 |
ErrorMsg err = new ErrorMsg(ErrorMsg.DESERIALIZE_TRANSLET_ERR); |
|
254 |
throw new UnsupportedOperationException(err.toString()); |
|
255 |
} |
|
256 |
} |
|
257 |
||
33349 | 258 |
// We have to read serialized fields first. |
259 |
ObjectInputStream.GetField gf = is.readFields(); |
|
260 |
_name = (String)gf.get("_name", null); |
|
261 |
_bytecodes = (byte[][])gf.get("_bytecodes", null); |
|
262 |
_class = (Class[])gf.get("_class", null); |
|
263 |
_transletIndex = gf.get("_transletIndex", -1); |
|
264 |
||
265 |
_outputProperties = (Properties)gf.get("_outputProperties", null); |
|
266 |
_indentNumber = gf.get("_indentNumber", 0); |
|
267 |
||
12005 | 268 |
if (is.readBoolean()) { |
269 |
_uriResolver = (URIResolver) is.readObject(); |
|
270 |
} |
|
271 |
||
272 |
_tfactory = new TransformerFactoryImpl(); |
|
273 |
} |
|
274 |
||
275 |
||
276 |
/** |
|
277 |
* This is to fix bugzilla bug 22438 |
|
278 |
* If the user defined class implements URIResolver and Serializable |
|
279 |
* then we want it to get serialized |
|
280 |
*/ |
|
281 |
private void writeObject(ObjectOutputStream os) |
|
282 |
throws IOException, ClassNotFoundException { |
|
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
283 |
if (_auxClasses != null) { |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
284 |
//throw with the same message as when Hashtable was used for compatibility. |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
285 |
throw new NotSerializableException( |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
286 |
"com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable"); |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
287 |
} |
33349 | 288 |
|
289 |
// Write serialized fields |
|
290 |
ObjectOutputStream.PutField pf = os.putFields(); |
|
291 |
pf.put("_name", _name); |
|
292 |
pf.put("_bytecodes", _bytecodes); |
|
293 |
pf.put("_class", _class); |
|
294 |
pf.put("_transletIndex", _transletIndex); |
|
295 |
pf.put("_outputProperties", _outputProperties); |
|
296 |
pf.put("_indentNumber", _indentNumber); |
|
297 |
os.writeFields(); |
|
298 |
||
12005 | 299 |
if (_uriResolver instanceof Serializable) { |
300 |
os.writeBoolean(true); |
|
301 |
os.writeObject((Serializable) _uriResolver); |
|
302 |
} |
|
303 |
else { |
|
304 |
os.writeBoolean(false); |
|
305 |
} |
|
306 |
} |
|
307 |
||
12458 | 308 |
/** |
309 |
* Return the state of the services mechanism feature. |
|
310 |
*/ |
|
311 |
public boolean useServicesMechnism() { |
|
312 |
return _useServicesMechanism; |
|
313 |
} |
|
12005 | 314 |
|
315 |
/** |
|
316 |
* Store URIResolver needed for Transformers. |
|
317 |
*/ |
|
318 |
public synchronized void setURIResolver(URIResolver resolver) { |
|
319 |
_uriResolver = resolver; |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* The TransformerFactory must pass us the translet bytecodes using this |
|
324 |
* method before we can create any translet instances |
|
325 |
* |
|
326 |
* Note: This method is private for security reasons. See |
|
327 |
* CR 6537898. When merging with Apache, we must ensure |
|
328 |
* that the privateness of this method is maintained (that |
|
329 |
* is why it wasn't removed). |
|
330 |
*/ |
|
331 |
private synchronized void setTransletBytecodes(byte[][] bytecodes) { |
|
332 |
_bytecodes = bytecodes; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Returns the translet bytecodes stored in this template |
|
337 |
* |
|
338 |
* Note: This method is private for security reasons. See |
|
339 |
* CR 6537898. When merging with Apache, we must ensure |
|
340 |
* that the privateness of this method is maintained (that |
|
341 |
* is why it wasn't removed). |
|
342 |
*/ |
|
343 |
private synchronized byte[][] getTransletBytecodes() { |
|
344 |
return _bytecodes; |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Returns the translet bytecodes stored in this template |
|
349 |
* |
|
350 |
* Note: This method is private for security reasons. See |
|
351 |
* CR 6537898. When merging with Apache, we must ensure |
|
352 |
* that the privateness of this method is maintained (that |
|
353 |
* is why it wasn't removed). |
|
354 |
*/ |
|
355 |
private synchronized Class[] getTransletClasses() { |
|
356 |
try { |
|
357 |
if (_class == null) defineTransletClasses(); |
|
358 |
} |
|
359 |
catch (TransformerConfigurationException e) { |
|
360 |
// Falls through |
|
361 |
} |
|
362 |
return _class; |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Returns the index of the main class in array of bytecodes |
|
367 |
*/ |
|
368 |
public synchronized int getTransletIndex() { |
|
369 |
try { |
|
370 |
if (_class == null) defineTransletClasses(); |
|
371 |
} |
|
372 |
catch (TransformerConfigurationException e) { |
|
373 |
// Falls through |
|
374 |
} |
|
375 |
return _transletIndex; |
|
376 |
} |
|
377 |
||
378 |
/** |
|
379 |
* The TransformerFactory should call this method to set the translet name |
|
380 |
*/ |
|
381 |
protected synchronized void setTransletName(String name) { |
|
382 |
_name = name; |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Returns the name of the main translet class stored in this template |
|
387 |
*/ |
|
388 |
protected synchronized String getTransletName() { |
|
389 |
return _name; |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* Defines the translet class and auxiliary classes. |
|
394 |
* Returns a reference to the Class object that defines the main class |
|
395 |
*/ |
|
396 |
private void defineTransletClasses() |
|
397 |
throws TransformerConfigurationException { |
|
398 |
||
399 |
if (_bytecodes == null) { |
|
400 |
ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR); |
|
401 |
throw new TransformerConfigurationException(err.toString()); |
|
402 |
} |
|
403 |
||
404 |
TransletClassLoader loader = (TransletClassLoader) |
|
405 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
406 |
public Object run() { |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
407 |
return new TransletClassLoader(ObjectFactory.findClassLoader(),_tfactory.getExternalExtensionsMap()); |
12005 | 408 |
} |
409 |
}); |
|
410 |
||
411 |
try { |
|
412 |
final int classCount = _bytecodes.length; |
|
413 |
_class = new Class[classCount]; |
|
414 |
||
415 |
if (classCount > 1) { |
|
33349 | 416 |
_auxClasses = new HashMap<>(); |
12005 | 417 |
} |
418 |
||
36520 | 419 |
// create a module for the translet |
420 |
Module xmlModule = TemplatesImpl.class.getModule(); |
|
421 |
String pn = _tfactory.getPackageName(); |
|
422 |
assert pn != null && pn.length() > 0; |
|
423 |
||
424 |
Module m = Modules.defineModule(loader, "jdk.translet", |
|
425 |
Collections.singleton(pn)); |
|
426 |
||
427 |
// jdk.translate reads java.base && java.xml |
|
428 |
Modules.addReads(m, Object.class.getModule()); |
|
429 |
Modules.addReads(m, xmlModule); |
|
430 |
||
431 |
// jdk.translet needs access to runtime classes |
|
432 |
Arrays.asList(Constants.PKGS_USED_BY_TRANSLET_CLASSES).forEach(p -> { |
|
433 |
xmlModule.addExports(p, m); |
|
434 |
}); |
|
435 |
||
436 |
// jdk.translate also needs to be loose as the XSL may bind to |
|
437 |
// java types in an unnamed module |
|
438 |
Modules.addReads(m, null); |
|
439 |
||
440 |
// java.xml needs to instanitate the translate class |
|
441 |
xmlModule.addReads(m); |
|
442 |
Modules.addExports(m, pn, xmlModule); |
|
443 |
||
12005 | 444 |
for (int i = 0; i < classCount; i++) { |
445 |
_class[i] = loader.defineClass(_bytecodes[i]); |
|
446 |
final Class superClass = _class[i].getSuperclass(); |
|
447 |
||
448 |
// Check if this is the main class |
|
449 |
if (superClass.getName().equals(ABSTRACT_TRANSLET)) { |
|
450 |
_transletIndex = i; |
|
451 |
} |
|
452 |
else { |
|
453 |
_auxClasses.put(_class[i].getName(), _class[i]); |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
if (_transletIndex < 0) { |
|
458 |
ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name); |
|
459 |
throw new TransformerConfigurationException(err.toString()); |
|
460 |
} |
|
461 |
} |
|
462 |
catch (ClassFormatError e) { |
|
463 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name); |
|
464 |
throw new TransformerConfigurationException(err.toString()); |
|
465 |
} |
|
466 |
catch (LinkageError e) { |
|
467 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); |
|
36520 | 468 |
throw new TransformerConfigurationException(err.toString(), e); |
12005 | 469 |
} |
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* This method generates an instance of the translet class that is |
|
474 |
* wrapped inside this Template. The translet instance will later |
|
475 |
* be wrapped inside a Transformer object. |
|
476 |
*/ |
|
477 |
private Translet getTransletInstance() |
|
478 |
throws TransformerConfigurationException { |
|
479 |
try { |
|
480 |
if (_name == null) return null; |
|
481 |
||
482 |
if (_class == null) defineTransletClasses(); |
|
483 |
||
484 |
// The translet needs to keep a reference to all its auxiliary |
|
485 |
// class to prevent the GC from collecting them |
|
486 |
AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance(); |
|
487 |
translet.postInitialization(); |
|
488 |
translet.setTemplates(this); |
|
12458 | 489 |
translet.setServicesMechnism(_useServicesMechanism); |
17534 | 490 |
translet.setAllowedProtocols(_accessExternalStylesheet); |
12005 | 491 |
if (_auxClasses != null) { |
492 |
translet.setAuxiliaryClasses(_auxClasses); |
|
493 |
} |
|
494 |
||
495 |
return translet; |
|
496 |
} |
|
497 |
catch (InstantiationException e) { |
|
498 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); |
|
499 |
throw new TransformerConfigurationException(err.toString()); |
|
500 |
} |
|
501 |
catch (IllegalAccessException e) { |
|
502 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); |
|
503 |
throw new TransformerConfigurationException(err.toString()); |
|
504 |
} |
|
505 |
} |
|
506 |
||
507 |
/** |
|
508 |
* Implements JAXP's Templates.newTransformer() |
|
509 |
* |
|
510 |
* @throws TransformerConfigurationException |
|
511 |
*/ |
|
512 |
public synchronized Transformer newTransformer() |
|
513 |
throws TransformerConfigurationException |
|
514 |
{ |
|
515 |
TransformerImpl transformer; |
|
516 |
||
517 |
transformer = new TransformerImpl(getTransletInstance(), _outputProperties, |
|
518 |
_indentNumber, _tfactory); |
|
519 |
||
520 |
if (_uriResolver != null) { |
|
521 |
transformer.setURIResolver(_uriResolver); |
|
522 |
} |
|
523 |
||
524 |
if (_tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) { |
|
525 |
transformer.setSecureProcessing(true); |
|
526 |
} |
|
527 |
return transformer; |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Implements JAXP's Templates.getOutputProperties(). We need to |
|
532 |
* instanciate a translet to get the output settings, so |
|
533 |
* we might as well just instanciate a Transformer and use its |
|
534 |
* implementation of this method. |
|
535 |
*/ |
|
536 |
public synchronized Properties getOutputProperties() { |
|
537 |
try { |
|
538 |
return newTransformer().getOutputProperties(); |
|
539 |
} |
|
540 |
catch (TransformerConfigurationException e) { |
|
541 |
return null; |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* Return the thread local copy of the stylesheet DOM. |
|
547 |
*/ |
|
548 |
public DOM getStylesheetDOM() { |
|
549 |
return (DOM)_sdom.get(); |
|
550 |
} |
|
551 |
||
552 |
/** |
|
553 |
* Set the thread local copy of the stylesheet DOM. |
|
554 |
*/ |
|
555 |
public void setStylesheetDOM(DOM sdom) { |
|
556 |
_sdom.set(sdom); |
|
557 |
} |
|
558 |
} |