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