author | joehw |
Sat, 18 Apr 2015 00:17:19 -0700 | |
changeset 33349 | 975138b77cff |
parent 29945 | 4912169d22d3 |
child 36520 | cb52aa47ff0a |
permissions | -rw-r--r-- |
6 | 1 |
/* |
33349 | 2 |
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
6 | 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 |
|
6 | 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: XSLTC.java,v 1.2.4.1 2005/09/05 09:51:38 pvedula Exp $ |
|
22 |
*/ |
|
23 |
||
24 |
package com.sun.org.apache.xalan.internal.xsltc.compiler; |
|
25 |
||
26 |
import com.sun.org.apache.bcel.internal.classfile.JavaClass; |
|
17534 | 27 |
import com.sun.org.apache.xalan.internal.XalanConstants; |
21467
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
28 |
import com.sun.org.apache.xalan.internal.utils.FeatureManager; |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
29 |
import com.sun.org.apache.xalan.internal.utils.FeatureManager.Feature; |
16953 | 30 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport; |
20968 | 31 |
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager; |
6 | 32 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; |
33 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util; |
|
34 |
import com.sun.org.apache.xml.internal.dtm.DTM; |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
35 |
import java.io.BufferedOutputStream; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
36 |
import java.io.ByteArrayOutputStream; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
37 |
import java.io.File; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
38 |
import java.io.FileOutputStream; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
39 |
import java.io.IOException; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
40 |
import java.io.InputStream; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
41 |
import java.net.URL; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
42 |
import java.security.AccessController; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
43 |
import java.security.PrivilegedAction; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
44 |
import java.util.Collections; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
45 |
import java.util.Date; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
46 |
import java.util.Enumeration; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
47 |
import java.util.HashMap; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
48 |
import java.util.Map; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
49 |
import java.util.Properties; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
50 |
import java.util.Vector; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
51 |
import java.util.jar.JarEntry; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
52 |
import java.util.jar.JarOutputStream; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
53 |
import java.util.jar.Manifest; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
54 |
import javax.xml.XMLConstants; |
6 | 55 |
import org.xml.sax.InputSource; |
56 |
import org.xml.sax.XMLReader; |
|
57 |
||
58 |
/** |
|
59 |
* @author Jacek Ambroziak |
|
60 |
* @author Santiago Pericas-Geertsen |
|
61 |
* @author G. Todd Miller |
|
62 |
* @author Morten Jorgensen |
|
63 |
* @author John Howard (johnh@schemasoft.com) |
|
64 |
*/ |
|
65 |
public final class XSLTC { |
|
66 |
||
67 |
// A reference to the main stylesheet parser object. |
|
68 |
private Parser _parser; |
|
69 |
||
70 |
// A reference to an external XMLReader (SAX parser) passed to us |
|
71 |
private XMLReader _reader = null; |
|
72 |
||
73 |
// A reference to an external SourceLoader (for use with include/import) |
|
74 |
private SourceLoader _loader = null; |
|
75 |
||
76 |
// A reference to the stylesheet being compiled. |
|
77 |
private Stylesheet _stylesheet; |
|
78 |
||
79 |
// Counters used by various classes to generate unique names. |
|
80 |
// private int _variableSerial = 1; |
|
81 |
private int _modeSerial = 1; |
|
82 |
private int _stylesheetSerial = 1; |
|
83 |
private int _stepPatternSerial = 1; |
|
84 |
private int _helperClassSerial = 0; |
|
85 |
private int _attributeSetSerial = 0; |
|
86 |
||
87 |
private int[] _numberFieldIndexes; |
|
88 |
||
89 |
// Name index tables |
|
90 |
private int _nextGType; // Next available element type |
|
91 |
private Vector _namesIndex; // Index of all registered QNames |
|
33349 | 92 |
private Map<String, Integer> _elements; // Map of all registered elements |
93 |
private Map<String, Integer> _attributes; // Map of all registered attributes |
|
6 | 94 |
|
95 |
// Namespace index tables |
|
96 |
private int _nextNSType; // Next available namespace type |
|
97 |
private Vector _namespaceIndex; // Index of all registered namespaces |
|
33349 | 98 |
private Map<String, Integer> _namespaces; // Map of all registered namespaces |
99 |
private Map<String, Integer> _namespacePrefixes;// Map of all registered namespace prefixes |
|
6 | 100 |
|
101 |
||
102 |
// All literal text in the stylesheet |
|
103 |
private Vector m_characterData; |
|
104 |
||
105 |
// These define the various methods for outputting the translet |
|
106 |
public static final int FILE_OUTPUT = 0; |
|
107 |
public static final int JAR_OUTPUT = 1; |
|
108 |
public static final int BYTEARRAY_OUTPUT = 2; |
|
109 |
public static final int CLASSLOADER_OUTPUT = 3; |
|
110 |
public static final int BYTEARRAY_AND_FILE_OUTPUT = 4; |
|
111 |
public static final int BYTEARRAY_AND_JAR_OUTPUT = 5; |
|
112 |
||
113 |
||
114 |
// Compiler options (passed from command line or XSLTC client) |
|
115 |
private boolean _debug = false; // -x |
|
116 |
private String _jarFileName = null; // -j <jar-file-name> |
|
117 |
private String _className = null; // -o <class-name> |
|
118 |
private String _packageName = null; // -p <package-name> |
|
119 |
private File _destDir = null; // -d <directory-name> |
|
120 |
private int _outputType = FILE_OUTPUT; // by default |
|
121 |
||
122 |
private Vector _classes; |
|
123 |
private Vector _bcelClasses; |
|
124 |
private boolean _callsNodeset = false; |
|
125 |
private boolean _multiDocument = false; |
|
126 |
private boolean _hasIdCall = false; |
|
127 |
||
128 |
/** |
|
129 |
* Set to true if template inlining is requested. Template |
|
130 |
* inlining used to be the default, but we have found that |
|
131 |
* Hotspots does a better job with shorter methods, so the |
|
132 |
* default is *not* to inline now. |
|
133 |
*/ |
|
134 |
private boolean _templateInlining = false; |
|
135 |
||
136 |
/** |
|
137 |
* State of the secure processing feature. |
|
138 |
*/ |
|
139 |
private boolean _isSecureProcessing = false; |
|
140 |
||
12458 | 141 |
private boolean _useServicesMechanism = true; |
142 |
||
6 | 143 |
/** |
17534 | 144 |
* protocols allowed for external references set by the stylesheet processing instruction, Import and Include element. |
145 |
*/ |
|
146 |
private String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT; |
|
147 |
/** |
|
148 |
* protocols allowed for external DTD references in source file and/or stylesheet. |
|
149 |
*/ |
|
150 |
private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT; |
|
151 |
||
20968 | 152 |
private XMLSecurityManager _xmlSecurityManager; |
17534 | 153 |
|
21467
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
154 |
private final FeatureManager _featureManager; |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
155 |
|
17534 | 156 |
/** |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
157 |
* Extension function class loader variables |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
158 |
*/ |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
159 |
|
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
160 |
/* Class loader reference that will be used to external extension functions loading */ |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
161 |
private ClassLoader _extensionClassLoader; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
162 |
|
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
163 |
/** |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
164 |
* HashSet with the loaded classes |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
165 |
*/ |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
166 |
private final Map<String, Class> _externalExtensionFunctions; |
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 |
/** |
6 | 169 |
* XSLTC compiler constructor |
170 |
*/ |
|
21467
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
171 |
public XSLTC(boolean useServicesMechanism, FeatureManager featureManager) { |
12458 | 172 |
_parser = new Parser(this, useServicesMechanism); |
21467
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
173 |
_featureManager = featureManager; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
174 |
_extensionClassLoader = null; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
175 |
_externalExtensionFunctions = new HashMap<>(); |
6 | 176 |
} |
177 |
||
178 |
/** |
|
179 |
* Set the state of the secure processing feature. |
|
180 |
*/ |
|
181 |
public void setSecureProcessing(boolean flag) { |
|
182 |
_isSecureProcessing = flag; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Return the state of the secure processing feature. |
|
187 |
*/ |
|
188 |
public boolean isSecureProcessing() { |
|
189 |
return _isSecureProcessing; |
|
190 |
} |
|
12458 | 191 |
/** |
192 |
* Return the state of the services mechanism feature. |
|
193 |
*/ |
|
194 |
public boolean useServicesMechnism() { |
|
195 |
return _useServicesMechanism; |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Set the state of the services mechanism feature. |
|
200 |
*/ |
|
201 |
public void setServicesMechnism(boolean flag) { |
|
202 |
_useServicesMechanism = flag; |
|
203 |
} |
|
6 | 204 |
|
21467
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
205 |
/** |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
206 |
* Return the value of the specified feature |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
207 |
* @param name name of the feature |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
208 |
* @return true if the feature is enabled, false otherwise |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
209 |
*/ |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
210 |
public boolean getFeature(Feature name) { |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
211 |
return _featureManager.isFeatureEnabled(name); |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
212 |
} |
ed77f0ff062c
8004476: XSLT Extension Functions Don't Work in WebStart
joehw
parents:
20968
diff
changeset
|
213 |
|
6 | 214 |
/** |
17534 | 215 |
* Return allowed protocols for accessing external stylesheet. |
216 |
*/ |
|
20968 | 217 |
public Object getProperty(String name) { |
17534 | 218 |
if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) { |
219 |
return _accessExternalStylesheet; |
|
220 |
} |
|
221 |
else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) { |
|
222 |
return _accessExternalDTD; |
|
20968 | 223 |
} else if (name.equals(XalanConstants.SECURITY_MANAGER)) { |
224 |
return _xmlSecurityManager; |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
225 |
} else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
226 |
return _extensionClassLoader; |
17534 | 227 |
} |
228 |
return null; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Set allowed protocols for accessing external stylesheet. |
|
233 |
*/ |
|
20968 | 234 |
public void setProperty(String name, Object value) { |
17534 | 235 |
if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) { |
236 |
_accessExternalStylesheet = (String)value; |
|
237 |
} |
|
238 |
else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) { |
|
239 |
_accessExternalDTD = (String)value; |
|
20968 | 240 |
} else if (name.equals(XalanConstants.SECURITY_MANAGER)) { |
241 |
_xmlSecurityManager = (XMLSecurityManager)value; |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
242 |
} else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
243 |
_extensionClassLoader = (ClassLoader) value; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
244 |
/* Clear the external extension functions HashMap if extension class |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
245 |
loader was changed */ |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
246 |
_externalExtensionFunctions.clear(); |
17534 | 247 |
} |
248 |
} |
|
249 |
||
250 |
/** |
|
6 | 251 |
* Only for user by the internal TrAX implementation. |
252 |
*/ |
|
253 |
public Parser getParser() { |
|
254 |
return _parser; |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Only for user by the internal TrAX implementation. |
|
259 |
*/ |
|
260 |
public void setOutputType(int type) { |
|
261 |
_outputType = type; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Only for user by the internal TrAX implementation. |
|
266 |
*/ |
|
267 |
public Properties getOutputProperties() { |
|
268 |
return _parser.getOutputProperties(); |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Initializes the compiler to compile a new stylesheet |
|
273 |
*/ |
|
274 |
public void init() { |
|
275 |
reset(); |
|
276 |
_reader = null; |
|
277 |
_classes = new Vector(); |
|
278 |
_bcelClasses = new Vector(); |
|
279 |
} |
|
280 |
||
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
281 |
private void setExternalExtensionFunctions(String name, Class clazz) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
282 |
if (_isSecureProcessing && clazz != null && !_externalExtensionFunctions.containsKey(name)) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
283 |
_externalExtensionFunctions.put(name, clazz); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
284 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
285 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
286 |
|
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
287 |
/* |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
288 |
* Function loads an external external extension functions. |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
289 |
* The filtering of function types (external,internal) takes place in FunctionCall class |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
290 |
* |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
291 |
*/ |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
292 |
Class loadExternalFunction(String name) throws ClassNotFoundException { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
293 |
Class loaded = null; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
294 |
//Check if the function is not loaded already |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
295 |
if (_externalExtensionFunctions.containsKey(name)) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
296 |
loaded = _externalExtensionFunctions.get(name); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
297 |
} else if (_extensionClassLoader != null) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
298 |
loaded = Class.forName(name, true, _extensionClassLoader); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
299 |
setExternalExtensionFunctions(name, loaded); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
300 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
301 |
if (loaded == null) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
302 |
throw new ClassNotFoundException(name); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
303 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
304 |
//Return loaded class |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
305 |
return (Class) loaded; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
306 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
307 |
|
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
308 |
/* |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
309 |
* Returns unmodifiable view of HashMap with loaded external extension |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
310 |
* functions - will be needed for the TransformerImpl |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
311 |
*/ |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
312 |
public Map<String, Class> getExternalExtensionFunctions() { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
313 |
return Collections.unmodifiableMap(_externalExtensionFunctions); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
314 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
315 |
|
6 | 316 |
/** |
317 |
* Initializes the compiler to produce a new translet |
|
318 |
*/ |
|
319 |
private void reset() { |
|
320 |
_nextGType = DTM.NTYPES; |
|
33349 | 321 |
_elements = new HashMap<>(); |
322 |
_attributes = new HashMap<>(); |
|
323 |
_namespaces = new HashMap<>(); |
|
6 | 324 |
_namespaces.put("",new Integer(_nextNSType)); |
325 |
_namesIndex = new Vector(128); |
|
326 |
_namespaceIndex = new Vector(32); |
|
33349 | 327 |
_namespacePrefixes = new HashMap<>(); |
6 | 328 |
_stylesheet = null; |
329 |
_parser.init(); |
|
330 |
//_variableSerial = 1; |
|
331 |
_modeSerial = 1; |
|
332 |
_stylesheetSerial = 1; |
|
333 |
_stepPatternSerial = 1; |
|
334 |
_helperClassSerial = 0; |
|
335 |
_attributeSetSerial = 0; |
|
336 |
_multiDocument = false; |
|
337 |
_hasIdCall = false; |
|
338 |
_numberFieldIndexes = new int[] { |
|
339 |
-1, // LEVEL_SINGLE |
|
340 |
-1, // LEVEL_MULTIPLE |
|
341 |
-1 // LEVEL_ANY |
|
342 |
}; |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
343 |
_externalExtensionFunctions.clear(); |
6 | 344 |
} |
345 |
||
346 |
/** |
|
347 |
* Defines an external SourceLoader to provide the compiler with documents |
|
348 |
* referenced in xsl:include/import |
|
349 |
* @param loader The SourceLoader to use for include/import |
|
350 |
*/ |
|
351 |
public void setSourceLoader(SourceLoader loader) { |
|
352 |
_loader = loader; |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Set a flag indicating if templates are to be inlined or not. The |
|
357 |
* default is to do inlining, but this causes problems when the |
|
358 |
* stylesheets have a large number of templates (e.g. branch targets |
|
359 |
* exceeding 64K or a length of a method exceeding 64K). |
|
360 |
*/ |
|
361 |
public void setTemplateInlining(boolean templateInlining) { |
|
362 |
_templateInlining = templateInlining; |
|
363 |
} |
|
12458 | 364 |
/** |
365 |
* Return the state of the template inlining feature. |
|
366 |
*/ |
|
367 |
public boolean getTemplateInlining() { |
|
368 |
return _templateInlining; |
|
369 |
} |
|
6 | 370 |
|
371 |
/** |
|
372 |
* Set the parameters to use to locate the correct <?xml-stylesheet ...?> |
|
373 |
* processing instruction in the case where the input document to the |
|
374 |
* compiler (and parser) is an XML document. |
|
375 |
* @param media The media attribute to be matched. May be null, in which |
|
376 |
* case the prefered templates will be used (i.e. alternate = no). |
|
377 |
* @param title The value of the title attribute to match. May be null. |
|
378 |
* @param charset The value of the charset attribute to match. May be null. |
|
379 |
*/ |
|
380 |
public void setPIParameters(String media, String title, String charset) { |
|
381 |
_parser.setPIParameters(media, title, charset); |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* Compiles an XSL stylesheet pointed to by a URL |
|
386 |
* @param url An URL containing the input XSL stylesheet |
|
387 |
*/ |
|
388 |
public boolean compile(URL url) { |
|
389 |
try { |
|
390 |
// Open input stream from URL and wrap inside InputSource |
|
391 |
final InputStream stream = url.openStream(); |
|
392 |
final InputSource input = new InputSource(stream); |
|
393 |
input.setSystemId(url.toString()); |
|
394 |
return compile(input, _className); |
|
395 |
} |
|
396 |
catch (IOException e) { |
|
16953 | 397 |
_parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e)); |
6 | 398 |
return false; |
399 |
} |
|
400 |
} |
|
401 |
||
402 |
/** |
|
403 |
* Compiles an XSL stylesheet pointed to by a URL |
|
404 |
* @param url An URL containing the input XSL stylesheet |
|
405 |
* @param name The name to assign to the translet class |
|
406 |
*/ |
|
407 |
public boolean compile(URL url, String name) { |
|
408 |
try { |
|
409 |
// Open input stream from URL and wrap inside InputSource |
|
410 |
final InputStream stream = url.openStream(); |
|
411 |
final InputSource input = new InputSource(stream); |
|
412 |
input.setSystemId(url.toString()); |
|
413 |
return compile(input, name); |
|
414 |
} |
|
415 |
catch (IOException e) { |
|
16953 | 416 |
_parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e)); |
6 | 417 |
return false; |
418 |
} |
|
419 |
} |
|
420 |
||
421 |
/** |
|
422 |
* Compiles an XSL stylesheet passed in through an InputStream |
|
423 |
* @param stream An InputStream that will pass in the stylesheet contents |
|
424 |
* @param name The name of the translet class to generate |
|
425 |
* @return 'true' if the compilation was successful |
|
426 |
*/ |
|
427 |
public boolean compile(InputStream stream, String name) { |
|
428 |
final InputSource input = new InputSource(stream); |
|
429 |
input.setSystemId(name); // We have nothing else!!! |
|
430 |
return compile(input, name); |
|
431 |
} |
|
432 |
||
433 |
/** |
|
434 |
* Compiles an XSL stylesheet passed in through an InputStream |
|
435 |
* @param input An InputSource that will pass in the stylesheet contents |
|
436 |
* @param name The name of the translet class to generate - can be null |
|
437 |
* @return 'true' if the compilation was successful |
|
438 |
*/ |
|
439 |
public boolean compile(InputSource input, String name) { |
|
440 |
try { |
|
441 |
// Reset globals in case we're called by compile(Vector v); |
|
442 |
reset(); |
|
443 |
||
444 |
// The systemId may not be set, so we'll have to check the URL |
|
445 |
String systemId = null; |
|
446 |
if (input != null) { |
|
447 |
systemId = input.getSystemId(); |
|
448 |
} |
|
449 |
||
450 |
// Set the translet class name if not already set |
|
451 |
if (_className == null) { |
|
452 |
if (name != null) { |
|
453 |
setClassName(name); |
|
454 |
} |
|
455 |
else if (systemId != null && !systemId.equals("")) { |
|
456 |
setClassName(Util.baseName(systemId)); |
|
457 |
} |
|
458 |
||
459 |
// Ensure we have a non-empty class name at this point |
|
460 |
if (_className == null || _className.length() == 0) { |
|
461 |
setClassName("GregorSamsa"); // default translet name |
|
462 |
} |
|
463 |
} |
|
464 |
||
465 |
// Get the root node of the abstract syntax tree |
|
466 |
SyntaxTreeNode element = null; |
|
467 |
if (_reader == null) { |
|
468 |
element = _parser.parse(input); |
|
469 |
} |
|
470 |
else { |
|
471 |
element = _parser.parse(_reader, input); |
|
472 |
} |
|
473 |
||
474 |
// Compile the translet - this is where the work is done! |
|
475 |
if ((!_parser.errorsFound()) && (element != null)) { |
|
476 |
// Create a Stylesheet element from the root node |
|
477 |
_stylesheet = _parser.makeStylesheet(element); |
|
478 |
_stylesheet.setSourceLoader(_loader); |
|
479 |
_stylesheet.setSystemId(systemId); |
|
480 |
_stylesheet.setParentStylesheet(null); |
|
481 |
_stylesheet.setTemplateInlining(_templateInlining); |
|
482 |
_parser.setCurrentStylesheet(_stylesheet); |
|
483 |
||
484 |
// Create AST under the Stylesheet element (parse & type-check) |
|
485 |
_parser.createAST(_stylesheet); |
|
486 |
} |
|
487 |
// Generate the bytecodes and output the translet class(es) |
|
488 |
if ((!_parser.errorsFound()) && (_stylesheet != null)) { |
|
489 |
_stylesheet.setCallsNodeset(_callsNodeset); |
|
490 |
_stylesheet.setMultiDocument(_multiDocument); |
|
491 |
_stylesheet.setHasIdCall(_hasIdCall); |
|
492 |
||
493 |
// Class synchronization is needed for BCEL |
|
494 |
synchronized (getClass()) { |
|
495 |
_stylesheet.translate(); |
|
496 |
} |
|
497 |
} |
|
498 |
} |
|
499 |
catch (Exception e) { |
|
500 |
/*if (_debug)*/ e.printStackTrace(); |
|
16953 | 501 |
_parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e)); |
6 | 502 |
} |
503 |
catch (Error e) { |
|
504 |
if (_debug) e.printStackTrace(); |
|
16953 | 505 |
_parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e)); |
6 | 506 |
} |
507 |
finally { |
|
508 |
_reader = null; // reset this here to be sure it is not re-used |
|
509 |
} |
|
510 |
return !_parser.errorsFound(); |
|
511 |
} |
|
512 |
||
513 |
/** |
|
514 |
* Compiles a set of stylesheets pointed to by a Vector of URLs |
|
515 |
* @param stylesheets A Vector containing URLs pointing to the stylesheets |
|
516 |
* @return 'true' if the compilation was successful |
|
517 |
*/ |
|
518 |
public boolean compile(Vector stylesheets) { |
|
519 |
// Get the number of stylesheets (ie. URLs) in the vector |
|
520 |
final int count = stylesheets.size(); |
|
521 |
||
522 |
// Return straight away if the vector is empty |
|
523 |
if (count == 0) return true; |
|
524 |
||
525 |
// Special handling needed if the URL count is one, becuase the |
|
526 |
// _className global must not be reset if it was set explicitly |
|
527 |
if (count == 1) { |
|
528 |
final Object url = stylesheets.firstElement(); |
|
529 |
if (url instanceof URL) |
|
530 |
return compile((URL)url); |
|
531 |
else |
|
532 |
return false; |
|
533 |
} |
|
534 |
else { |
|
535 |
// Traverse all elements in the vector and compile |
|
536 |
final Enumeration urls = stylesheets.elements(); |
|
537 |
while (urls.hasMoreElements()) { |
|
538 |
_className = null; // reset, so that new name will be computed |
|
539 |
final Object url = urls.nextElement(); |
|
540 |
if (url instanceof URL) { |
|
541 |
if (!compile((URL)url)) return false; |
|
542 |
} |
|
543 |
} |
|
544 |
} |
|
545 |
return true; |
|
546 |
} |
|
547 |
||
548 |
/** |
|
549 |
* Returns an array of bytecode arrays generated by a compilation. |
|
550 |
* @return JVM bytecodes that represent translet class definition |
|
551 |
*/ |
|
552 |
public byte[][] getBytecodes() { |
|
553 |
final int count = _classes.size(); |
|
554 |
final byte[][] result = new byte[count][1]; |
|
555 |
for (int i = 0; i < count; i++) |
|
556 |
result[i] = (byte[])_classes.elementAt(i); |
|
557 |
return result; |
|
558 |
} |
|
559 |
||
560 |
/** |
|
561 |
* Compiles a stylesheet pointed to by a URL. The result is put in a |
|
562 |
* set of byte arrays. One byte array for each generated class. |
|
563 |
* @param name The name of the translet class to generate |
|
564 |
* @param input An InputSource that will pass in the stylesheet contents |
|
565 |
* @param outputType The output type |
|
566 |
* @return JVM bytecodes that represent translet class definition |
|
567 |
*/ |
|
568 |
public byte[][] compile(String name, InputSource input, int outputType) { |
|
569 |
_outputType = outputType; |
|
570 |
if (compile(input, name)) |
|
571 |
return getBytecodes(); |
|
572 |
else |
|
573 |
return null; |
|
574 |
} |
|
575 |
||
576 |
/** |
|
577 |
* Compiles a stylesheet pointed to by a URL. The result is put in a |
|
578 |
* set of byte arrays. One byte array for each generated class. |
|
579 |
* @param name The name of the translet class to generate |
|
580 |
* @param input An InputSource that will pass in the stylesheet contents |
|
581 |
* @return JVM bytecodes that represent translet class definition |
|
582 |
*/ |
|
583 |
public byte[][] compile(String name, InputSource input) { |
|
584 |
return compile(name, input, BYTEARRAY_OUTPUT); |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Set the XMLReader to use for parsing the next input stylesheet |
|
589 |
* @param reader XMLReader (SAX2 parser) to use |
|
590 |
*/ |
|
591 |
public void setXMLReader(XMLReader reader) { |
|
592 |
_reader = reader; |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* Get the XMLReader to use for parsing the next input stylesheet |
|
597 |
*/ |
|
598 |
public XMLReader getXMLReader() { |
|
599 |
return _reader ; |
|
600 |
} |
|
601 |
||
602 |
/** |
|
603 |
* Get a Vector containing all compile error messages |
|
604 |
* @return A Vector containing all compile error messages |
|
605 |
*/ |
|
606 |
public Vector getErrors() { |
|
607 |
return _parser.getErrors(); |
|
608 |
} |
|
609 |
||
610 |
/** |
|
611 |
* Get a Vector containing all compile warning messages |
|
612 |
* @return A Vector containing all compile error messages |
|
613 |
*/ |
|
614 |
public Vector getWarnings() { |
|
615 |
return _parser.getWarnings(); |
|
616 |
} |
|
617 |
||
618 |
/** |
|
619 |
* Print all compile error messages to standard output |
|
620 |
*/ |
|
621 |
public void printErrors() { |
|
622 |
_parser.printErrors(); |
|
623 |
} |
|
624 |
||
625 |
/** |
|
626 |
* Print all compile warning messages to standard output |
|
627 |
*/ |
|
628 |
public void printWarnings() { |
|
629 |
_parser.printWarnings(); |
|
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
* This method is called by the XPathParser when it encounters a call |
|
634 |
* to the document() function. Affects the DOM used by the translet. |
|
635 |
*/ |
|
636 |
protected void setMultiDocument(boolean flag) { |
|
637 |
_multiDocument = flag; |
|
638 |
} |
|
639 |
||
640 |
public boolean isMultiDocument() { |
|
641 |
return _multiDocument; |
|
642 |
} |
|
643 |
||
644 |
/** |
|
645 |
* This method is called by the XPathParser when it encounters a call |
|
646 |
* to the nodeset() extension function. Implies multi document. |
|
647 |
*/ |
|
648 |
protected void setCallsNodeset(boolean flag) { |
|
649 |
if (flag) setMultiDocument(flag); |
|
650 |
_callsNodeset = flag; |
|
651 |
} |
|
652 |
||
653 |
public boolean callsNodeset() { |
|
654 |
return _callsNodeset; |
|
655 |
} |
|
656 |
||
657 |
protected void setHasIdCall(boolean flag) { |
|
658 |
_hasIdCall = flag; |
|
659 |
} |
|
660 |
||
661 |
public boolean hasIdCall() { |
|
662 |
return _hasIdCall; |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* Set the class name for the generated translet. This class name is |
|
667 |
* overridden if multiple stylesheets are compiled in one go using the |
|
668 |
* compile(Vector urls) method. |
|
669 |
* @param className The name to assign to the translet class |
|
670 |
*/ |
|
671 |
public void setClassName(String className) { |
|
672 |
final String base = Util.baseName(className); |
|
673 |
final String noext = Util.noExtName(base); |
|
674 |
String name = Util.toJavaName(noext); |
|
675 |
||
676 |
if (_packageName == null) |
|
677 |
_className = name; |
|
678 |
else |
|
679 |
_className = _packageName + '.' + name; |
|
680 |
} |
|
681 |
||
682 |
/** |
|
683 |
* Get the class name for the generated translet. |
|
684 |
*/ |
|
685 |
public String getClassName() { |
|
686 |
return _className; |
|
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* Convert for Java class name of local system file name. |
|
691 |
* (Replace '.' with '/' on UNIX and replace '.' by '\' on Windows/DOS.) |
|
692 |
*/ |
|
693 |
private String classFileName(final String className) { |
|
694 |
return className.replace('.', File.separatorChar) + ".class"; |
|
695 |
} |
|
696 |
||
697 |
/** |
|
698 |
* Generate an output File object to send the translet to |
|
699 |
*/ |
|
700 |
private File getOutputFile(String className) { |
|
701 |
if (_destDir != null) |
|
702 |
return new File(_destDir, classFileName(className)); |
|
703 |
else |
|
704 |
return new File(classFileName(className)); |
|
705 |
} |
|
706 |
||
707 |
/** |
|
708 |
* Set the destination directory for the translet. |
|
709 |
* The current working directory will be used by default. |
|
710 |
*/ |
|
711 |
public boolean setDestDirectory(String dstDirName) { |
|
712 |
final File dir = new File(dstDirName); |
|
16953 | 713 |
if (SecuritySupport.getFileExists(dir) || dir.mkdirs()) { |
6 | 714 |
_destDir = dir; |
715 |
return true; |
|
716 |
} |
|
717 |
else { |
|
718 |
_destDir = null; |
|
719 |
return false; |
|
720 |
} |
|
721 |
} |
|
722 |
||
723 |
/** |
|
724 |
* Set an optional package name for the translet and auxiliary classes |
|
725 |
*/ |
|
726 |
public void setPackageName(String packageName) { |
|
727 |
_packageName = packageName; |
|
728 |
if (_className != null) setClassName(_className); |
|
729 |
} |
|
730 |
||
731 |
/** |
|
732 |
* Set the name of an optional JAR-file to dump the translet and |
|
733 |
* auxiliary classes to |
|
734 |
*/ |
|
735 |
public void setJarFileName(String jarFileName) { |
|
736 |
final String JAR_EXT = ".jar"; |
|
737 |
if (jarFileName.endsWith(JAR_EXT)) |
|
738 |
_jarFileName = jarFileName; |
|
739 |
else |
|
740 |
_jarFileName = jarFileName + JAR_EXT; |
|
741 |
_outputType = JAR_OUTPUT; |
|
742 |
} |
|
743 |
||
744 |
public String getJarFileName() { |
|
745 |
return _jarFileName; |
|
746 |
} |
|
747 |
||
748 |
/** |
|
749 |
* Set the top-level stylesheet |
|
750 |
*/ |
|
751 |
public void setStylesheet(Stylesheet stylesheet) { |
|
752 |
if (_stylesheet == null) _stylesheet = stylesheet; |
|
753 |
} |
|
754 |
||
755 |
/** |
|
756 |
* Returns the top-level stylesheet |
|
757 |
*/ |
|
758 |
public Stylesheet getStylesheet() { |
|
759 |
return _stylesheet; |
|
760 |
} |
|
761 |
||
762 |
/** |
|
763 |
* Registers an attribute and gives it a type so that it can be mapped to |
|
764 |
* DOM attribute types at run-time. |
|
765 |
*/ |
|
766 |
public int registerAttribute(QName name) { |
|
33349 | 767 |
Integer code = _attributes.get(name.toString()); |
6 | 768 |
if (code == null) { |
33349 | 769 |
code = _nextGType++; |
6 | 770 |
_attributes.put(name.toString(), code); |
771 |
final String uri = name.getNamespace(); |
|
772 |
final String local = "@"+name.getLocalPart(); |
|
773 |
if ((uri != null) && (!uri.equals(""))) |
|
774 |
_namesIndex.addElement(uri+":"+local); |
|
775 |
else |
|
776 |
_namesIndex.addElement(local); |
|
777 |
if (name.getLocalPart().equals("*")) { |
|
778 |
registerNamespace(name.getNamespace()); |
|
779 |
} |
|
780 |
} |
|
781 |
return code.intValue(); |
|
782 |
} |
|
783 |
||
784 |
/** |
|
785 |
* Registers an element and gives it a type so that it can be mapped to |
|
786 |
* DOM element types at run-time. |
|
787 |
*/ |
|
788 |
public int registerElement(QName name) { |
|
789 |
// Register element (full QName) |
|
33349 | 790 |
Integer code = _elements.get(name.toString()); |
6 | 791 |
if (code == null) { |
33349 | 792 |
_elements.put(name.toString(), code = _nextGType++); |
6 | 793 |
_namesIndex.addElement(name.toString()); |
794 |
} |
|
795 |
if (name.getLocalPart().equals("*")) { |
|
796 |
registerNamespace(name.getNamespace()); |
|
797 |
} |
|
798 |
return code.intValue(); |
|
799 |
} |
|
800 |
||
801 |
/** |
|
802 |
* Registers a namespace prefix and gives it a type so that it can be mapped to |
|
803 |
* DOM namespace types at run-time. |
|
804 |
*/ |
|
805 |
||
806 |
public int registerNamespacePrefix(QName name) { |
|
807 |
||
33349 | 808 |
Integer code = _namespacePrefixes.get(name.toString()); |
6 | 809 |
if (code == null) { |
33349 | 810 |
code = _nextGType++; |
6 | 811 |
_namespacePrefixes.put(name.toString(), code); |
812 |
final String uri = name.getNamespace(); |
|
813 |
if ((uri != null) && (!uri.equals(""))){ |
|
814 |
// namespace::ext2:ped2 will be made empty in TypedNamespaceIterator |
|
815 |
_namesIndex.addElement("?"); |
|
816 |
} else{ |
|
817 |
_namesIndex.addElement("?"+name.getLocalPart()); |
|
818 |
} |
|
819 |
} |
|
820 |
return code.intValue(); |
|
821 |
} |
|
822 |
||
823 |
/** |
|
824 |
* Registers a namespace and gives it a type so that it can be mapped to |
|
825 |
* DOM namespace types at run-time. |
|
826 |
*/ |
|
827 |
public int registerNamespace(String namespaceURI) { |
|
33349 | 828 |
Integer code = _namespaces.get(namespaceURI); |
6 | 829 |
if (code == null) { |
33349 | 830 |
code = _nextNSType++; |
6 | 831 |
_namespaces.put(namespaceURI,code); |
832 |
_namespaceIndex.addElement(namespaceURI); |
|
833 |
} |
|
834 |
return code.intValue(); |
|
835 |
} |
|
836 |
||
837 |
public int nextModeSerial() { |
|
838 |
return _modeSerial++; |
|
839 |
} |
|
840 |
||
841 |
public int nextStylesheetSerial() { |
|
842 |
return _stylesheetSerial++; |
|
843 |
} |
|
844 |
||
845 |
public int nextStepPatternSerial() { |
|
846 |
return _stepPatternSerial++; |
|
847 |
} |
|
848 |
||
849 |
public int[] getNumberFieldIndexes() { |
|
850 |
return _numberFieldIndexes; |
|
851 |
} |
|
852 |
||
853 |
public int nextHelperClassSerial() { |
|
854 |
return _helperClassSerial++; |
|
855 |
} |
|
856 |
||
857 |
public int nextAttributeSetSerial() { |
|
858 |
return _attributeSetSerial++; |
|
859 |
} |
|
860 |
||
861 |
public Vector getNamesIndex() { |
|
862 |
return _namesIndex; |
|
863 |
} |
|
864 |
||
865 |
public Vector getNamespaceIndex() { |
|
866 |
return _namespaceIndex; |
|
867 |
} |
|
868 |
||
869 |
/** |
|
870 |
* Returns a unique name for every helper class needed to |
|
871 |
* execute a translet. |
|
872 |
*/ |
|
873 |
public String getHelperClassName() { |
|
874 |
return getClassName() + '$' + _helperClassSerial++; |
|
875 |
} |
|
876 |
||
877 |
public void dumpClass(JavaClass clazz) { |
|
878 |
||
879 |
if (_outputType == FILE_OUTPUT || |
|
880 |
_outputType == BYTEARRAY_AND_FILE_OUTPUT) |
|
881 |
{ |
|
882 |
File outFile = getOutputFile(clazz.getClassName()); |
|
883 |
String parentDir = outFile.getParent(); |
|
884 |
if (parentDir != null) { |
|
885 |
File parentFile = new File(parentDir); |
|
16953 | 886 |
if (!SecuritySupport.getFileExists(parentFile)) |
6 | 887 |
parentFile.mkdirs(); |
888 |
} |
|
889 |
} |
|
890 |
||
891 |
try { |
|
892 |
switch (_outputType) { |
|
893 |
case FILE_OUTPUT: |
|
894 |
clazz.dump( |
|
895 |
new BufferedOutputStream( |
|
896 |
new FileOutputStream( |
|
897 |
getOutputFile(clazz.getClassName())))); |
|
898 |
break; |
|
899 |
case JAR_OUTPUT: |
|
900 |
_bcelClasses.addElement(clazz); |
|
901 |
break; |
|
902 |
case BYTEARRAY_OUTPUT: |
|
903 |
case BYTEARRAY_AND_FILE_OUTPUT: |
|
904 |
case BYTEARRAY_AND_JAR_OUTPUT: |
|
905 |
case CLASSLOADER_OUTPUT: |
|
906 |
ByteArrayOutputStream out = new ByteArrayOutputStream(2048); |
|
907 |
clazz.dump(out); |
|
908 |
_classes.addElement(out.toByteArray()); |
|
909 |
||
910 |
if (_outputType == BYTEARRAY_AND_FILE_OUTPUT) |
|
911 |
clazz.dump(new BufferedOutputStream( |
|
912 |
new FileOutputStream(getOutputFile(clazz.getClassName())))); |
|
913 |
else if (_outputType == BYTEARRAY_AND_JAR_OUTPUT) |
|
914 |
_bcelClasses.addElement(clazz); |
|
915 |
||
916 |
break; |
|
917 |
} |
|
918 |
} |
|
919 |
catch (Exception e) { |
|
920 |
e.printStackTrace(); |
|
921 |
} |
|
922 |
} |
|
923 |
||
924 |
/** |
|
925 |
* File separators are converted to forward slashes for ZIP files. |
|
926 |
*/ |
|
927 |
private String entryName(File f) throws IOException { |
|
928 |
return f.getName().replace(File.separatorChar, '/'); |
|
929 |
} |
|
930 |
||
931 |
/** |
|
932 |
* Generate output JAR-file and packages |
|
933 |
*/ |
|
934 |
public void outputToJar() throws IOException { |
|
935 |
// create the manifest |
|
936 |
final Manifest manifest = new Manifest(); |
|
937 |
final java.util.jar.Attributes atrs = manifest.getMainAttributes(); |
|
938 |
atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION,"1.2"); |
|
939 |
||
940 |
final Map map = manifest.getEntries(); |
|
941 |
// create manifest |
|
942 |
Enumeration classes = _bcelClasses.elements(); |
|
943 |
final String now = (new Date()).toString(); |
|
944 |
final java.util.jar.Attributes.Name dateAttr = |
|
945 |
new java.util.jar.Attributes.Name("Date"); |
|
946 |
while (classes.hasMoreElements()) { |
|
947 |
final JavaClass clazz = (JavaClass)classes.nextElement(); |
|
948 |
final String className = clazz.getClassName().replace('.','/'); |
|
949 |
final java.util.jar.Attributes attr = new java.util.jar.Attributes(); |
|
950 |
attr.put(dateAttr, now); |
|
951 |
map.put(className+".class", attr); |
|
952 |
} |
|
953 |
||
954 |
final File jarFile = new File(_destDir, _jarFileName); |
|
955 |
final JarOutputStream jos = |
|
956 |
new JarOutputStream(new FileOutputStream(jarFile), manifest); |
|
957 |
classes = _bcelClasses.elements(); |
|
958 |
while (classes.hasMoreElements()) { |
|
959 |
final JavaClass clazz = (JavaClass)classes.nextElement(); |
|
960 |
final String className = clazz.getClassName().replace('.','/'); |
|
961 |
jos.putNextEntry(new JarEntry(className+".class")); |
|
962 |
final ByteArrayOutputStream out = new ByteArrayOutputStream(2048); |
|
963 |
clazz.dump(out); // dump() closes it's output stream |
|
964 |
out.writeTo(jos); |
|
965 |
} |
|
966 |
jos.close(); |
|
967 |
} |
|
968 |
||
969 |
/** |
|
970 |
* Turn debugging messages on/off |
|
971 |
*/ |
|
972 |
public void setDebug(boolean debug) { |
|
973 |
_debug = debug; |
|
974 |
} |
|
975 |
||
976 |
/** |
|
977 |
* Get current debugging message setting |
|
978 |
*/ |
|
979 |
public boolean debug() { |
|
980 |
return _debug; |
|
981 |
} |
|
982 |
||
983 |
||
984 |
/** |
|
985 |
* Retrieve a string representation of the character data to be stored |
|
986 |
* in the translet as a <code>char[]</code>. There may be more than |
|
987 |
* one such array required. |
|
988 |
* @param index The index of the <code>char[]</code>. Zero-based. |
|
989 |
* @return String The character data to be stored in the corresponding |
|
990 |
* <code>char[]</code>. |
|
991 |
*/ |
|
992 |
public String getCharacterData(int index) { |
|
993 |
return ((StringBuffer) m_characterData.elementAt(index)).toString(); |
|
994 |
} |
|
995 |
||
996 |
/** |
|
997 |
* Get the number of char[] arrays, thus far, that will be created to |
|
998 |
* store literal text in the stylesheet. |
|
999 |
*/ |
|
1000 |
public int getCharacterDataCount() { |
|
1001 |
return (m_characterData != null) ? m_characterData.size() : 0; |
|
1002 |
} |
|
1003 |
||
1004 |
/** |
|
1005 |
* Add literal text to char arrays that will be used to store character |
|
1006 |
* data in the stylesheet. |
|
1007 |
* @param newData String data to be added to char arrays. |
|
1008 |
* Pre-condition: <code>newData.length() ≤ 21845</code> |
|
1009 |
* @return int offset at which character data will be stored |
|
1010 |
*/ |
|
1011 |
public int addCharacterData(String newData) { |
|
1012 |
StringBuffer currData; |
|
1013 |
if (m_characterData == null) { |
|
1014 |
m_characterData = new Vector(); |
|
1015 |
currData = new StringBuffer(); |
|
1016 |
m_characterData.addElement(currData); |
|
1017 |
} else { |
|
1018 |
currData = (StringBuffer) m_characterData |
|
1019 |
.elementAt(m_characterData.size()-1); |
|
1020 |
} |
|
1021 |
||
1022 |
// Character data could take up to three-times as much space when |
|
1023 |
// written to the class file as UTF-8. The maximum size for a |
|
1024 |
// constant is 65535/3. If we exceed that, |
|
1025 |
// (We really should use some "bin packing".) |
|
1026 |
if (newData.length() + currData.length() > 21845) { |
|
1027 |
currData = new StringBuffer(); |
|
1028 |
m_characterData.addElement(currData); |
|
1029 |
} |
|
1030 |
||
1031 |
int newDataOffset = currData.length(); |
|
1032 |
currData.append(newData); |
|
1033 |
||
1034 |
return newDataOffset; |
|
1035 |
} |
|
1036 |
} |