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: Parser.java,v 1.2.4.1 2005/09/13 12:14:32 pvedula Exp $
|
|
22 |
*/
|
|
23 |
|
|
24 |
package com.sun.org.apache.xalan.internal.xsltc.compiler;
|
|
25 |
|
|
26 |
import java.io.File;
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.io.StringReader;
|
|
29 |
import java.util.Dictionary;
|
|
30 |
import java.util.Enumeration;
|
|
31 |
import java.util.Hashtable;
|
|
32 |
import java.util.Properties;
|
|
33 |
import java.util.Stack;
|
|
34 |
import java.util.StringTokenizer;
|
|
35 |
import java.util.Vector;
|
|
36 |
|
|
37 |
import com.sun.java_cup.internal.runtime.Symbol;
|
|
38 |
import javax.xml.XMLConstants;
|
|
39 |
import javax.xml.parsers.ParserConfigurationException;
|
|
40 |
import javax.xml.parsers.SAXParser;
|
|
41 |
import javax.xml.parsers.SAXParserFactory;
|
|
42 |
|
|
43 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
|
|
44 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
|
|
45 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
|
|
46 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
|
12458
|
47 |
import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
|
|
48 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
|
6
|
49 |
import org.xml.sax.Attributes;
|
|
50 |
import org.xml.sax.helpers.AttributesImpl;
|
|
51 |
import org.xml.sax.ContentHandler;
|
|
52 |
import org.xml.sax.InputSource;
|
|
53 |
import org.xml.sax.Locator;
|
|
54 |
import org.xml.sax.SAXException;
|
|
55 |
import org.xml.sax.SAXParseException;
|
|
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 Erwin Bolwidt <ejb@klomp.org>
|
|
64 |
*/
|
|
65 |
public class Parser implements Constants, ContentHandler {
|
|
66 |
|
|
67 |
private static final String XSL = "xsl"; // standard prefix
|
|
68 |
private static final String TRANSLET = "translet"; // extension prefix
|
|
69 |
|
|
70 |
private Locator _locator = null;
|
|
71 |
|
|
72 |
private XSLTC _xsltc; // Reference to the compiler object.
|
|
73 |
private XPathParser _xpathParser; // Reference to the XPath parser.
|
|
74 |
private Vector _errors; // Contains all compilation errors
|
|
75 |
private Vector _warnings; // Contains all compilation errors
|
|
76 |
|
|
77 |
private Hashtable _instructionClasses; // Maps instructions to classes
|
|
78 |
private Hashtable _instructionAttrs;; // reqd and opt attrs
|
|
79 |
private Hashtable _qNames;
|
|
80 |
private Hashtable _namespaces;
|
|
81 |
private QName _useAttributeSets;
|
|
82 |
private QName _excludeResultPrefixes;
|
|
83 |
private QName _extensionElementPrefixes;
|
|
84 |
private Hashtable _variableScope;
|
|
85 |
private Stylesheet _currentStylesheet;
|
|
86 |
private SymbolTable _symbolTable; // Maps QNames to syntax-tree nodes
|
|
87 |
private Output _output;
|
|
88 |
private Template _template; // Reference to the template being parsed.
|
|
89 |
|
|
90 |
private boolean _rootNamespaceDef; // Used for validity check
|
|
91 |
|
|
92 |
private SyntaxTreeNode _root;
|
|
93 |
|
|
94 |
private String _target;
|
|
95 |
|
|
96 |
private int _currentImportPrecedence;
|
|
97 |
|
12458
|
98 |
private boolean _useServicesMechanism = true;
|
|
99 |
|
|
100 |
public Parser(XSLTC xsltc, boolean useServicesMechanism) {
|
6
|
101 |
_xsltc = xsltc;
|
12458
|
102 |
_useServicesMechanism = useServicesMechanism;
|
6
|
103 |
}
|
|
104 |
|
|
105 |
public void init() {
|
|
106 |
_qNames = new Hashtable(512);
|
|
107 |
_namespaces = new Hashtable();
|
|
108 |
_instructionClasses = new Hashtable();
|
|
109 |
_instructionAttrs = new Hashtable();
|
|
110 |
_variableScope = new Hashtable();
|
|
111 |
_template = null;
|
|
112 |
_errors = new Vector();
|
|
113 |
_warnings = new Vector();
|
|
114 |
_symbolTable = new SymbolTable();
|
|
115 |
_xpathParser = new XPathParser(this);
|
|
116 |
_currentStylesheet = null;
|
|
117 |
_output = null;
|
|
118 |
_root = null;
|
|
119 |
_rootNamespaceDef = false;
|
|
120 |
_currentImportPrecedence = 1;
|
|
121 |
|
|
122 |
initStdClasses();
|
|
123 |
initInstructionAttrs();
|
|
124 |
initExtClasses();
|
|
125 |
initSymbolTable();
|
|
126 |
|
|
127 |
_useAttributeSets =
|
|
128 |
getQName(XSLT_URI, XSL, "use-attribute-sets");
|
|
129 |
_excludeResultPrefixes =
|
|
130 |
getQName(XSLT_URI, XSL, "exclude-result-prefixes");
|
|
131 |
_extensionElementPrefixes =
|
|
132 |
getQName(XSLT_URI, XSL, "extension-element-prefixes");
|
|
133 |
}
|
|
134 |
|
|
135 |
public void setOutput(Output output) {
|
|
136 |
if (_output != null) {
|
|
137 |
if (_output.getImportPrecedence() <= output.getImportPrecedence()) {
|
|
138 |
String cdata = _output.getCdata();
|
|
139 |
output.mergeOutput(_output);
|
|
140 |
_output.disable();
|
|
141 |
_output = output;
|
|
142 |
}
|
|
143 |
else {
|
|
144 |
output.disable();
|
|
145 |
}
|
|
146 |
}
|
|
147 |
else {
|
|
148 |
_output = output;
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
public Output getOutput() {
|
|
153 |
return _output;
|
|
154 |
}
|
|
155 |
|
|
156 |
public Properties getOutputProperties() {
|
|
157 |
return getTopLevelStylesheet().getOutputProperties();
|
|
158 |
}
|
|
159 |
|
|
160 |
public void addVariable(Variable var) {
|
|
161 |
addVariableOrParam(var);
|
|
162 |
}
|
|
163 |
|
|
164 |
public void addParameter(Param param) {
|
|
165 |
addVariableOrParam(param);
|
|
166 |
}
|
|
167 |
|
|
168 |
private void addVariableOrParam(VariableBase var) {
|
|
169 |
Object existing = _variableScope.get(var.getName());
|
|
170 |
if (existing != null) {
|
|
171 |
if (existing instanceof Stack) {
|
|
172 |
Stack stack = (Stack)existing;
|
|
173 |
stack.push(var);
|
|
174 |
}
|
|
175 |
else if (existing instanceof VariableBase) {
|
|
176 |
Stack stack = new Stack();
|
|
177 |
stack.push(existing);
|
|
178 |
stack.push(var);
|
|
179 |
_variableScope.put(var.getName(), stack);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
else {
|
|
183 |
_variableScope.put(var.getName(), var);
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
public void removeVariable(QName name) {
|
|
188 |
Object existing = _variableScope.get(name);
|
|
189 |
if (existing instanceof Stack) {
|
|
190 |
Stack stack = (Stack)existing;
|
|
191 |
if (!stack.isEmpty()) stack.pop();
|
|
192 |
if (!stack.isEmpty()) return;
|
|
193 |
}
|
|
194 |
_variableScope.remove(name);
|
|
195 |
}
|
|
196 |
|
|
197 |
public VariableBase lookupVariable(QName name) {
|
|
198 |
Object existing = _variableScope.get(name);
|
|
199 |
if (existing instanceof VariableBase) {
|
|
200 |
return((VariableBase)existing);
|
|
201 |
}
|
|
202 |
else if (existing instanceof Stack) {
|
|
203 |
Stack stack = (Stack)existing;
|
|
204 |
return((VariableBase)stack.peek());
|
|
205 |
}
|
|
206 |
return(null);
|
|
207 |
}
|
|
208 |
|
|
209 |
public void setXSLTC(XSLTC xsltc) {
|
|
210 |
_xsltc = xsltc;
|
|
211 |
}
|
|
212 |
|
|
213 |
public XSLTC getXSLTC() {
|
|
214 |
return _xsltc;
|
|
215 |
}
|
|
216 |
|
|
217 |
public int getCurrentImportPrecedence() {
|
|
218 |
return _currentImportPrecedence;
|
|
219 |
}
|
|
220 |
|
|
221 |
public int getNextImportPrecedence() {
|
|
222 |
return ++_currentImportPrecedence;
|
|
223 |
}
|
|
224 |
|
|
225 |
public void setCurrentStylesheet(Stylesheet stylesheet) {
|
|
226 |
_currentStylesheet = stylesheet;
|
|
227 |
}
|
|
228 |
|
|
229 |
public Stylesheet getCurrentStylesheet() {
|
|
230 |
return _currentStylesheet;
|
|
231 |
}
|
|
232 |
|
|
233 |
public Stylesheet getTopLevelStylesheet() {
|
|
234 |
return _xsltc.getStylesheet();
|
|
235 |
}
|
|
236 |
|
|
237 |
public QName getQNameSafe(final String stringRep) {
|
|
238 |
// parse and retrieve namespace
|
|
239 |
final int colon = stringRep.lastIndexOf(':');
|
|
240 |
if (colon != -1) {
|
|
241 |
final String prefix = stringRep.substring(0, colon);
|
|
242 |
final String localname = stringRep.substring(colon + 1);
|
|
243 |
String namespace = null;
|
|
244 |
|
|
245 |
// Get the namespace uri from the symbol table
|
|
246 |
if (prefix.equals(XMLNS_PREFIX) == false) {
|
|
247 |
namespace = _symbolTable.lookupNamespace(prefix);
|
|
248 |
if (namespace == null) namespace = EMPTYSTRING;
|
|
249 |
}
|
|
250 |
return getQName(namespace, prefix, localname);
|
|
251 |
}
|
|
252 |
else {
|
|
253 |
final String uri = stringRep.equals(XMLNS_PREFIX) ? null
|
|
254 |
: _symbolTable.lookupNamespace(EMPTYSTRING);
|
|
255 |
return getQName(uri, null, stringRep);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
public QName getQName(final String stringRep) {
|
|
260 |
return getQName(stringRep, true, false);
|
|
261 |
}
|
|
262 |
|
|
263 |
public QName getQNameIgnoreDefaultNs(final String stringRep) {
|
|
264 |
return getQName(stringRep, true, true);
|
|
265 |
}
|
|
266 |
|
|
267 |
public QName getQName(final String stringRep, boolean reportError) {
|
|
268 |
return getQName(stringRep, reportError, false);
|
|
269 |
}
|
|
270 |
|
|
271 |
private QName getQName(final String stringRep, boolean reportError,
|
|
272 |
boolean ignoreDefaultNs)
|
|
273 |
{
|
|
274 |
// parse and retrieve namespace
|
|
275 |
final int colon = stringRep.lastIndexOf(':');
|
|
276 |
if (colon != -1) {
|
|
277 |
final String prefix = stringRep.substring(0, colon);
|
|
278 |
final String localname = stringRep.substring(colon + 1);
|
|
279 |
String namespace = null;
|
|
280 |
|
|
281 |
// Get the namespace uri from the symbol table
|
|
282 |
if (prefix.equals(XMLNS_PREFIX) == false) {
|
|
283 |
namespace = _symbolTable.lookupNamespace(prefix);
|
|
284 |
if (namespace == null && reportError) {
|
|
285 |
final int line = getLineNumber();
|
|
286 |
ErrorMsg err = new ErrorMsg(ErrorMsg.NAMESPACE_UNDEF_ERR,
|
|
287 |
line, prefix);
|
|
288 |
reportError(ERROR, err);
|
|
289 |
}
|
|
290 |
}
|
|
291 |
return getQName(namespace, prefix, localname);
|
|
292 |
}
|
|
293 |
else {
|
|
294 |
if (stringRep.equals(XMLNS_PREFIX)) {
|
|
295 |
ignoreDefaultNs = true;
|
|
296 |
}
|
|
297 |
final String defURI = ignoreDefaultNs ? null
|
|
298 |
: _symbolTable.lookupNamespace(EMPTYSTRING);
|
|
299 |
return getQName(defURI, null, stringRep);
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
public QName getQName(String namespace, String prefix, String localname) {
|
|
304 |
if (namespace == null || namespace.equals(EMPTYSTRING)) {
|
|
305 |
QName name = (QName)_qNames.get(localname);
|
|
306 |
if (name == null) {
|
|
307 |
name = new QName(null, prefix, localname);
|
|
308 |
_qNames.put(localname, name);
|
|
309 |
}
|
|
310 |
return name;
|
|
311 |
}
|
|
312 |
else {
|
|
313 |
Dictionary space = (Dictionary)_namespaces.get(namespace);
|
12458
|
314 |
String lexicalQName =
|
|
315 |
(prefix == null || prefix.length() == 0)
|
|
316 |
? localname
|
|
317 |
: (prefix + ':' + localname);
|
|
318 |
|
6
|
319 |
if (space == null) {
|
|
320 |
final QName name = new QName(namespace, prefix, localname);
|
|
321 |
_namespaces.put(namespace, space = new Hashtable());
|
12458
|
322 |
space.put(lexicalQName, name);
|
6
|
323 |
return name;
|
|
324 |
}
|
|
325 |
else {
|
12458
|
326 |
QName name = (QName)space.get(lexicalQName);
|
6
|
327 |
if (name == null) {
|
|
328 |
name = new QName(namespace, prefix, localname);
|
12458
|
329 |
space.put(lexicalQName, name);
|
6
|
330 |
}
|
|
331 |
return name;
|
|
332 |
}
|
|
333 |
}
|
|
334 |
}
|
|
335 |
|
|
336 |
public QName getQName(String scope, String name) {
|
|
337 |
return getQName(scope + name);
|
|
338 |
}
|
|
339 |
|
|
340 |
public QName getQName(QName scope, QName name) {
|
|
341 |
return getQName(scope.toString() + name.toString());
|
|
342 |
}
|
|
343 |
|
|
344 |
public QName getUseAttributeSets() {
|
|
345 |
return _useAttributeSets;
|
|
346 |
}
|
|
347 |
|
|
348 |
public QName getExtensionElementPrefixes() {
|
|
349 |
return _extensionElementPrefixes;
|
|
350 |
}
|
|
351 |
|
|
352 |
public QName getExcludeResultPrefixes() {
|
|
353 |
return _excludeResultPrefixes;
|
|
354 |
}
|
|
355 |
|
|
356 |
/**
|
|
357 |
* Create an instance of the <code>Stylesheet</code> class,
|
|
358 |
* and then parse, typecheck and compile the instance.
|
|
359 |
* Must be called after <code>parse()</code>.
|
|
360 |
*/
|
|
361 |
public Stylesheet makeStylesheet(SyntaxTreeNode element)
|
|
362 |
throws CompilerException {
|
|
363 |
try {
|
|
364 |
Stylesheet stylesheet;
|
|
365 |
|
|
366 |
if (element instanceof Stylesheet) {
|
|
367 |
stylesheet = (Stylesheet)element;
|
|
368 |
}
|
|
369 |
else {
|
|
370 |
stylesheet = new Stylesheet();
|
|
371 |
stylesheet.setSimplified();
|
|
372 |
stylesheet.addElement(element);
|
|
373 |
stylesheet.setAttributes((AttributesImpl) element.getAttributes());
|
|
374 |
|
|
375 |
// Map the default NS if not already defined
|
|
376 |
if (element.lookupNamespace(EMPTYSTRING) == null) {
|
|
377 |
element.addPrefixMapping(EMPTYSTRING, EMPTYSTRING);
|
|
378 |
}
|
|
379 |
}
|
|
380 |
stylesheet.setParser(this);
|
|
381 |
return stylesheet;
|
|
382 |
}
|
|
383 |
catch (ClassCastException e) {
|
|
384 |
ErrorMsg err = new ErrorMsg(ErrorMsg.NOT_STYLESHEET_ERR, element);
|
|
385 |
throw new CompilerException(err.toString());
|
|
386 |
}
|
|
387 |
}
|
|
388 |
|
|
389 |
/**
|
|
390 |
* Instanciates a SAX2 parser and generate the AST from the input.
|
|
391 |
*/
|
|
392 |
public void createAST(Stylesheet stylesheet) {
|
|
393 |
try {
|
|
394 |
if (stylesheet != null) {
|
|
395 |
stylesheet.parseContents(this);
|
|
396 |
final int precedence = stylesheet.getImportPrecedence();
|
|
397 |
final Enumeration elements = stylesheet.elements();
|
|
398 |
while (elements.hasMoreElements()) {
|
|
399 |
Object child = elements.nextElement();
|
|
400 |
if (child instanceof Text) {
|
|
401 |
final int l = getLineNumber();
|
|
402 |
ErrorMsg err =
|
|
403 |
new ErrorMsg(ErrorMsg.ILLEGAL_TEXT_NODE_ERR,l,null);
|
|
404 |
reportError(ERROR, err);
|
|
405 |
}
|
|
406 |
}
|
|
407 |
if (!errorsFound()) {
|
|
408 |
stylesheet.typeCheck(_symbolTable);
|
|
409 |
}
|
|
410 |
}
|
|
411 |
}
|
|
412 |
catch (TypeCheckError e) {
|
16953
|
413 |
reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
|
6
|
414 |
}
|
|
415 |
}
|
|
416 |
|
|
417 |
/**
|
|
418 |
* Parses a stylesheet and builds the internal abstract syntax tree
|
|
419 |
* @param reader A SAX2 SAXReader (parser)
|
|
420 |
* @param input A SAX2 InputSource can be passed to a SAX reader
|
|
421 |
* @return The root of the abstract syntax tree
|
|
422 |
*/
|
|
423 |
public SyntaxTreeNode parse(XMLReader reader, InputSource input) {
|
|
424 |
try {
|
|
425 |
// Parse the input document and build the abstract syntax tree
|
|
426 |
reader.setContentHandler(this);
|
|
427 |
reader.parse(input);
|
|
428 |
// Find the start of the stylesheet within the tree
|
|
429 |
return (SyntaxTreeNode)getStylesheet(_root);
|
|
430 |
}
|
|
431 |
catch (IOException e) {
|
|
432 |
if (_xsltc.debug()) e.printStackTrace();
|
16953
|
433 |
reportError(ERROR,new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
|
6
|
434 |
}
|
|
435 |
catch (SAXException e) {
|
|
436 |
Throwable ex = e.getException();
|
|
437 |
if (_xsltc.debug()) {
|
|
438 |
e.printStackTrace();
|
|
439 |
if (ex != null) ex.printStackTrace();
|
|
440 |
}
|
16953
|
441 |
reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
|
6
|
442 |
}
|
|
443 |
catch (CompilerException e) {
|
|
444 |
if (_xsltc.debug()) e.printStackTrace();
|
16953
|
445 |
reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
|
6
|
446 |
}
|
|
447 |
catch (Exception e) {
|
|
448 |
if (_xsltc.debug()) e.printStackTrace();
|
16953
|
449 |
reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
|
6
|
450 |
}
|
|
451 |
return null;
|
|
452 |
}
|
|
453 |
|
|
454 |
/**
|
|
455 |
* Parses a stylesheet and builds the internal abstract syntax tree
|
|
456 |
* @param input A SAX2 InputSource can be passed to a SAX reader
|
|
457 |
* @return The root of the abstract syntax tree
|
|
458 |
*/
|
|
459 |
public SyntaxTreeNode parse(InputSource input) {
|
|
460 |
try {
|
|
461 |
// Create a SAX parser and get the XMLReader object it uses
|
12458
|
462 |
final SAXParserFactory factory = FactoryImpl.getSAXFactory(_useServicesMechanism);
|
6
|
463 |
|
|
464 |
if (_xsltc.isSecureProcessing()) {
|
|
465 |
try {
|
|
466 |
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
467 |
}
|
|
468 |
catch (SAXException e) {}
|
|
469 |
}
|
|
470 |
|
|
471 |
try {
|
|
472 |
factory.setFeature(Constants.NAMESPACE_FEATURE,true);
|
|
473 |
}
|
|
474 |
catch (Exception e) {
|
|
475 |
factory.setNamespaceAware(true);
|
|
476 |
}
|
|
477 |
final SAXParser parser = factory.newSAXParser();
|
|
478 |
final XMLReader reader = parser.getXMLReader();
|
|
479 |
return(parse(reader, input));
|
|
480 |
}
|
|
481 |
catch (ParserConfigurationException e) {
|
|
482 |
ErrorMsg err = new ErrorMsg(ErrorMsg.SAX_PARSER_CONFIG_ERR);
|
|
483 |
reportError(ERROR, err);
|
|
484 |
}
|
|
485 |
catch (SAXParseException e){
|
|
486 |
reportError(ERROR, new ErrorMsg(e.getMessage(),e.getLineNumber()));
|
|
487 |
}
|
|
488 |
catch (SAXException e) {
|
|
489 |
reportError(ERROR, new ErrorMsg(e.getMessage()));
|
|
490 |
}
|
|
491 |
return null;
|
|
492 |
}
|
|
493 |
|
|
494 |
public SyntaxTreeNode getDocumentRoot() {
|
|
495 |
return _root;
|
|
496 |
}
|
|
497 |
|
|
498 |
private String _PImedia = null;
|
|
499 |
private String _PItitle = null;
|
|
500 |
private String _PIcharset = null;
|
|
501 |
|
|
502 |
/**
|
|
503 |
* Set the parameters to use to locate the correct <?xml-stylesheet ...?>
|
|
504 |
* processing instruction in the case where the input document is an
|
|
505 |
* XML document with one or more references to a stylesheet.
|
|
506 |
* @param media The media attribute to be matched. May be null, in which
|
|
507 |
* case the prefered templates will be used (i.e. alternate = no).
|
|
508 |
* @param title The value of the title attribute to match. May be null.
|
|
509 |
* @param charset The value of the charset attribute to match. May be null.
|
|
510 |
*/
|
|
511 |
protected void setPIParameters(String media, String title, String charset) {
|
|
512 |
_PImedia = media;
|
|
513 |
_PItitle = title;
|
|
514 |
_PIcharset = charset;
|
|
515 |
}
|
|
516 |
|
|
517 |
/**
|
|
518 |
* Extracts the DOM for the stylesheet. In the case of an embedded
|
|
519 |
* stylesheet, it extracts the DOM subtree corresponding to the
|
|
520 |
* embedded stylesheet that has an 'id' attribute whose value is the
|
|
521 |
* same as the value declared in the <?xml-stylesheet...?> processing
|
|
522 |
* instruction (P.I.). In the xml-stylesheet P.I. the value is labeled
|
|
523 |
* as the 'href' data of the P.I. The extracted DOM representing the
|
|
524 |
* stylesheet is returned as an Element object.
|
|
525 |
*/
|
|
526 |
private SyntaxTreeNode getStylesheet(SyntaxTreeNode root)
|
|
527 |
throws CompilerException {
|
|
528 |
|
|
529 |
// Assume that this is a pure XSL stylesheet if there is not
|
|
530 |
// <?xml-stylesheet ....?> processing instruction
|
|
531 |
if (_target == null) {
|
|
532 |
if (!_rootNamespaceDef) {
|
|
533 |
ErrorMsg msg = new ErrorMsg(ErrorMsg.MISSING_XSLT_URI_ERR);
|
|
534 |
throw new CompilerException(msg.toString());
|
|
535 |
}
|
|
536 |
return(root);
|
|
537 |
}
|
|
538 |
|
|
539 |
// Find the xsl:stylesheet or xsl:transform with this reference
|
|
540 |
if (_target.charAt(0) == '#') {
|
|
541 |
SyntaxTreeNode element = findStylesheet(root, _target.substring(1));
|
|
542 |
if (element == null) {
|
|
543 |
ErrorMsg msg = new ErrorMsg(ErrorMsg.MISSING_XSLT_TARGET_ERR,
|
|
544 |
_target, root);
|
|
545 |
throw new CompilerException(msg.toString());
|
|
546 |
}
|
|
547 |
return(element);
|
|
548 |
}
|
|
549 |
else {
|
|
550 |
return(loadExternalStylesheet(_target));
|
|
551 |
}
|
|
552 |
}
|
|
553 |
|
|
554 |
/**
|
|
555 |
* Find a Stylesheet element with a specific ID attribute value.
|
|
556 |
* This method is used to find a Stylesheet node that is referred
|
|
557 |
* in a <?xml-stylesheet ... ?> processing instruction.
|
|
558 |
*/
|
|
559 |
private SyntaxTreeNode findStylesheet(SyntaxTreeNode root, String href) {
|
|
560 |
|
|
561 |
if (root == null) return null;
|
|
562 |
|
|
563 |
if (root instanceof Stylesheet) {
|
|
564 |
String id = root.getAttribute("id");
|
|
565 |
if (id.equals(href)) return root;
|
|
566 |
}
|
|
567 |
Vector children = root.getContents();
|
|
568 |
if (children != null) {
|
|
569 |
final int count = children.size();
|
|
570 |
for (int i = 0; i < count; i++) {
|
|
571 |
SyntaxTreeNode child = (SyntaxTreeNode)children.elementAt(i);
|
|
572 |
SyntaxTreeNode node = findStylesheet(child, href);
|
|
573 |
if (node != null) return node;
|
|
574 |
}
|
|
575 |
}
|
|
576 |
return null;
|
|
577 |
}
|
|
578 |
|
|
579 |
/**
|
|
580 |
* For embedded stylesheets: Load an external file with stylesheet
|
|
581 |
*/
|
|
582 |
private SyntaxTreeNode loadExternalStylesheet(String location)
|
|
583 |
throws CompilerException {
|
|
584 |
|
|
585 |
InputSource source;
|
|
586 |
|
|
587 |
// Check if the location is URL or a local file
|
|
588 |
if ((new File(location)).exists())
|
|
589 |
source = new InputSource("file:"+location);
|
|
590 |
else
|
|
591 |
source = new InputSource(location);
|
|
592 |
|
|
593 |
SyntaxTreeNode external = (SyntaxTreeNode)parse(source);
|
|
594 |
return(external);
|
|
595 |
}
|
|
596 |
|
|
597 |
private void initAttrTable(String elementName, String[] attrs) {
|
|
598 |
_instructionAttrs.put(getQName(XSLT_URI, XSL, elementName),
|
|
599 |
attrs);
|
|
600 |
}
|
|
601 |
|
|
602 |
private void initInstructionAttrs() {
|
|
603 |
initAttrTable("template",
|
|
604 |
new String[] {"match", "name", "priority", "mode"});
|
|
605 |
initAttrTable("stylesheet",
|
|
606 |
new String[] {"id", "version", "extension-element-prefixes",
|
|
607 |
"exclude-result-prefixes"});
|
|
608 |
initAttrTable("transform",
|
|
609 |
new String[] {"id", "version", "extension-element-prefixes",
|
|
610 |
"exclude-result-prefixes"});
|
|
611 |
initAttrTable("text", new String[] {"disable-output-escaping"});
|
|
612 |
initAttrTable("if", new String[] {"test"});
|
|
613 |
initAttrTable("choose", new String[] {});
|
|
614 |
initAttrTable("when", new String[] {"test"});
|
|
615 |
initAttrTable("otherwise", new String[] {});
|
|
616 |
initAttrTable("for-each", new String[] {"select"});
|
|
617 |
initAttrTable("message", new String[] {"terminate"});
|
|
618 |
initAttrTable("number",
|
|
619 |
new String[] {"level", "count", "from", "value", "format", "lang",
|
|
620 |
"letter-value", "grouping-separator", "grouping-size"});
|
|
621 |
initAttrTable("comment", new String[] {});
|
|
622 |
initAttrTable("copy", new String[] {"use-attribute-sets"});
|
|
623 |
initAttrTable("copy-of", new String[] {"select"});
|
|
624 |
initAttrTable("param", new String[] {"name", "select"});
|
|
625 |
initAttrTable("with-param", new String[] {"name", "select"});
|
|
626 |
initAttrTable("variable", new String[] {"name", "select"});
|
|
627 |
initAttrTable("output",
|
|
628 |
new String[] {"method", "version", "encoding",
|
|
629 |
"omit-xml-declaration", "standalone", "doctype-public",
|
|
630 |
"doctype-system", "cdata-section-elements", "indent",
|
|
631 |
"media-type"});
|
|
632 |
initAttrTable("sort",
|
|
633 |
new String[] {"select", "order", "case-order", "lang", "data-type"});
|
|
634 |
initAttrTable("key", new String[] {"name", "match", "use"});
|
|
635 |
initAttrTable("fallback", new String[] {});
|
|
636 |
initAttrTable("attribute", new String[] {"name", "namespace"});
|
|
637 |
initAttrTable("attribute-set",
|
|
638 |
new String[] {"name", "use-attribute-sets"});
|
|
639 |
initAttrTable("value-of",
|
|
640 |
new String[] {"select", "disable-output-escaping"});
|
|
641 |
initAttrTable("element",
|
|
642 |
new String[] {"name", "namespace", "use-attribute-sets"});
|
|
643 |
initAttrTable("call-template", new String[] {"name"});
|
|
644 |
initAttrTable("apply-templates", new String[] {"select", "mode"});
|
|
645 |
initAttrTable("apply-imports", new String[] {});
|
|
646 |
initAttrTable("decimal-format",
|
|
647 |
new String[] {"name", "decimal-separator", "grouping-separator",
|
|
648 |
"infinity", "minus-sign", "NaN", "percent", "per-mille",
|
|
649 |
"zero-digit", "digit", "pattern-separator"});
|
|
650 |
initAttrTable("import", new String[] {"href"});
|
|
651 |
initAttrTable("include", new String[] {"href"});
|
|
652 |
initAttrTable("strip-space", new String[] {"elements"});
|
|
653 |
initAttrTable("preserve-space", new String[] {"elements"});
|
|
654 |
initAttrTable("processing-instruction", new String[] {"name"});
|
|
655 |
initAttrTable("namespace-alias",
|
|
656 |
new String[] {"stylesheet-prefix", "result-prefix"});
|
|
657 |
}
|
|
658 |
|
|
659 |
|
|
660 |
|
|
661 |
/**
|
|
662 |
* Initialize the _instructionClasses Hashtable, which maps XSL element
|
|
663 |
* names to Java classes in this package.
|
|
664 |
*/
|
|
665 |
private void initStdClasses() {
|
|
666 |
initStdClass("template", "Template");
|
|
667 |
initStdClass("stylesheet", "Stylesheet");
|
|
668 |
initStdClass("transform", "Stylesheet");
|
|
669 |
initStdClass("text", "Text");
|
|
670 |
initStdClass("if", "If");
|
|
671 |
initStdClass("choose", "Choose");
|
|
672 |
initStdClass("when", "When");
|
|
673 |
initStdClass("otherwise", "Otherwise");
|
|
674 |
initStdClass("for-each", "ForEach");
|
|
675 |
initStdClass("message", "Message");
|
|
676 |
initStdClass("number", "Number");
|
|
677 |
initStdClass("comment", "Comment");
|
|
678 |
initStdClass("copy", "Copy");
|
|
679 |
initStdClass("copy-of", "CopyOf");
|
|
680 |
initStdClass("param", "Param");
|
|
681 |
initStdClass("with-param", "WithParam");
|
|
682 |
initStdClass("variable", "Variable");
|
|
683 |
initStdClass("output", "Output");
|
|
684 |
initStdClass("sort", "Sort");
|
|
685 |
initStdClass("key", "Key");
|
|
686 |
initStdClass("fallback", "Fallback");
|
|
687 |
initStdClass("attribute", "XslAttribute");
|
|
688 |
initStdClass("attribute-set", "AttributeSet");
|
|
689 |
initStdClass("value-of", "ValueOf");
|
|
690 |
initStdClass("element", "XslElement");
|
|
691 |
initStdClass("call-template", "CallTemplate");
|
|
692 |
initStdClass("apply-templates", "ApplyTemplates");
|
|
693 |
initStdClass("apply-imports", "ApplyImports");
|
|
694 |
initStdClass("decimal-format", "DecimalFormatting");
|
|
695 |
initStdClass("import", "Import");
|
|
696 |
initStdClass("include", "Include");
|
|
697 |
initStdClass("strip-space", "Whitespace");
|
|
698 |
initStdClass("preserve-space", "Whitespace");
|
|
699 |
initStdClass("processing-instruction", "ProcessingInstruction");
|
|
700 |
initStdClass("namespace-alias", "NamespaceAlias");
|
|
701 |
}
|
|
702 |
|
|
703 |
private void initStdClass(String elementName, String className) {
|
|
704 |
_instructionClasses.put(getQName(XSLT_URI, XSL, elementName),
|
|
705 |
COMPILER_PACKAGE + '.' + className);
|
|
706 |
}
|
|
707 |
|
|
708 |
public boolean elementSupported(String namespace, String localName) {
|
|
709 |
return(_instructionClasses.get(getQName(namespace, XSL, localName)) != null);
|
|
710 |
}
|
|
711 |
|
|
712 |
public boolean functionSupported(String fname) {
|
|
713 |
return(_symbolTable.lookupPrimop(fname) != null);
|
|
714 |
}
|
|
715 |
|
|
716 |
private void initExtClasses() {
|
|
717 |
initExtClass("output", "TransletOutput");
|
|
718 |
initExtClass(REDIRECT_URI, "write", "TransletOutput");
|
|
719 |
}
|
|
720 |
|
|
721 |
private void initExtClass(String elementName, String className) {
|
|
722 |
_instructionClasses.put(getQName(TRANSLET_URI, TRANSLET, elementName),
|
|
723 |
COMPILER_PACKAGE + '.' + className);
|
|
724 |
}
|
|
725 |
|
|
726 |
private void initExtClass(String namespace, String elementName, String className) {
|
|
727 |
_instructionClasses.put(getQName(namespace, TRANSLET, elementName),
|
|
728 |
COMPILER_PACKAGE + '.' + className);
|
|
729 |
}
|
|
730 |
|
|
731 |
/**
|
|
732 |
* Add primops and base functions to the symbol table.
|
|
733 |
*/
|
|
734 |
private void initSymbolTable() {
|
|
735 |
MethodType I_V = new MethodType(Type.Int, Type.Void);
|
|
736 |
MethodType I_R = new MethodType(Type.Int, Type.Real);
|
|
737 |
MethodType I_S = new MethodType(Type.Int, Type.String);
|
|
738 |
MethodType I_D = new MethodType(Type.Int, Type.NodeSet);
|
|
739 |
MethodType R_I = new MethodType(Type.Real, Type.Int);
|
|
740 |
MethodType R_V = new MethodType(Type.Real, Type.Void);
|
|
741 |
MethodType R_R = new MethodType(Type.Real, Type.Real);
|
|
742 |
MethodType R_D = new MethodType(Type.Real, Type.NodeSet);
|
|
743 |
MethodType R_O = new MethodType(Type.Real, Type.Reference);
|
|
744 |
MethodType I_I = new MethodType(Type.Int, Type.Int);
|
|
745 |
MethodType D_O = new MethodType(Type.NodeSet, Type.Reference);
|
|
746 |
MethodType D_V = new MethodType(Type.NodeSet, Type.Void);
|
|
747 |
MethodType D_S = new MethodType(Type.NodeSet, Type.String);
|
|
748 |
MethodType D_D = new MethodType(Type.NodeSet, Type.NodeSet);
|
|
749 |
MethodType A_V = new MethodType(Type.Node, Type.Void);
|
|
750 |
MethodType S_V = new MethodType(Type.String, Type.Void);
|
|
751 |
MethodType S_S = new MethodType(Type.String, Type.String);
|
|
752 |
MethodType S_A = new MethodType(Type.String, Type.Node);
|
|
753 |
MethodType S_D = new MethodType(Type.String, Type.NodeSet);
|
|
754 |
MethodType S_O = new MethodType(Type.String, Type.Reference);
|
|
755 |
MethodType B_O = new MethodType(Type.Boolean, Type.Reference);
|
|
756 |
MethodType B_V = new MethodType(Type.Boolean, Type.Void);
|
|
757 |
MethodType B_B = new MethodType(Type.Boolean, Type.Boolean);
|
|
758 |
MethodType B_S = new MethodType(Type.Boolean, Type.String);
|
|
759 |
MethodType D_X = new MethodType(Type.NodeSet, Type.Object);
|
|
760 |
MethodType R_RR = new MethodType(Type.Real, Type.Real, Type.Real);
|
|
761 |
MethodType I_II = new MethodType(Type.Int, Type.Int, Type.Int);
|
|
762 |
MethodType B_RR = new MethodType(Type.Boolean, Type.Real, Type.Real);
|
|
763 |
MethodType B_II = new MethodType(Type.Boolean, Type.Int, Type.Int);
|
|
764 |
MethodType S_SS = new MethodType(Type.String, Type.String, Type.String);
|
|
765 |
MethodType S_DS = new MethodType(Type.String, Type.Real, Type.String);
|
|
766 |
MethodType S_SR = new MethodType(Type.String, Type.String, Type.Real);
|
|
767 |
MethodType O_SO = new MethodType(Type.Reference, Type.String, Type.Reference);
|
|
768 |
|
|
769 |
MethodType D_SS =
|
|
770 |
new MethodType(Type.NodeSet, Type.String, Type.String);
|
|
771 |
MethodType D_SD =
|
|
772 |
new MethodType(Type.NodeSet, Type.String, Type.NodeSet);
|
|
773 |
MethodType B_BB =
|
|
774 |
new MethodType(Type.Boolean, Type.Boolean, Type.Boolean);
|
|
775 |
MethodType B_SS =
|
|
776 |
new MethodType(Type.Boolean, Type.String, Type.String);
|
|
777 |
MethodType S_SD =
|
|
778 |
new MethodType(Type.String, Type.String, Type.NodeSet);
|
|
779 |
MethodType S_DSS =
|
|
780 |
new MethodType(Type.String, Type.Real, Type.String, Type.String);
|
|
781 |
MethodType S_SRR =
|
|
782 |
new MethodType(Type.String, Type.String, Type.Real, Type.Real);
|
|
783 |
MethodType S_SSS =
|
|
784 |
new MethodType(Type.String, Type.String, Type.String, Type.String);
|
|
785 |
|
|
786 |
/*
|
|
787 |
* Standard functions: implemented but not in this table concat().
|
|
788 |
* When adding a new function make sure to uncomment
|
|
789 |
* the corresponding line in <tt>FunctionAvailableCall</tt>.
|
|
790 |
*/
|
|
791 |
|
|
792 |
// The following functions are inlined
|
|
793 |
|
|
794 |
_symbolTable.addPrimop("current", A_V);
|
|
795 |
_symbolTable.addPrimop("last", I_V);
|
|
796 |
_symbolTable.addPrimop("position", I_V);
|
|
797 |
_symbolTable.addPrimop("true", B_V);
|
|
798 |
_symbolTable.addPrimop("false", B_V);
|
|
799 |
_symbolTable.addPrimop("not", B_B);
|
|
800 |
_symbolTable.addPrimop("name", S_V);
|
|
801 |
_symbolTable.addPrimop("name", S_A);
|
|
802 |
_symbolTable.addPrimop("generate-id", S_V);
|
|
803 |
_symbolTable.addPrimop("generate-id", S_A);
|
|
804 |
_symbolTable.addPrimop("ceiling", R_R);
|
|
805 |
_symbolTable.addPrimop("floor", R_R);
|
|
806 |
_symbolTable.addPrimop("round", R_R);
|
|
807 |
_symbolTable.addPrimop("contains", B_SS);
|
|
808 |
_symbolTable.addPrimop("number", R_O);
|
|
809 |
_symbolTable.addPrimop("number", R_V);
|
|
810 |
_symbolTable.addPrimop("boolean", B_O);
|
|
811 |
_symbolTable.addPrimop("string", S_O);
|
|
812 |
_symbolTable.addPrimop("string", S_V);
|
|
813 |
_symbolTable.addPrimop("translate", S_SSS);
|
|
814 |
_symbolTable.addPrimop("string-length", I_V);
|
|
815 |
_symbolTable.addPrimop("string-length", I_S);
|
|
816 |
_symbolTable.addPrimop("starts-with", B_SS);
|
|
817 |
_symbolTable.addPrimop("format-number", S_DS);
|
|
818 |
_symbolTable.addPrimop("format-number", S_DSS);
|
|
819 |
_symbolTable.addPrimop("unparsed-entity-uri", S_S);
|
|
820 |
_symbolTable.addPrimop("key", D_SS);
|
|
821 |
_symbolTable.addPrimop("key", D_SD);
|
|
822 |
_symbolTable.addPrimop("id", D_S);
|
|
823 |
_symbolTable.addPrimop("id", D_D);
|
|
824 |
_symbolTable.addPrimop("namespace-uri", S_V);
|
|
825 |
_symbolTable.addPrimop("function-available", B_S);
|
|
826 |
_symbolTable.addPrimop("element-available", B_S);
|
|
827 |
_symbolTable.addPrimop("document", D_S);
|
|
828 |
_symbolTable.addPrimop("document", D_V);
|
|
829 |
|
|
830 |
// The following functions are implemented in the basis library
|
|
831 |
_symbolTable.addPrimop("count", I_D);
|
|
832 |
_symbolTable.addPrimop("sum", R_D);
|
|
833 |
_symbolTable.addPrimop("local-name", S_V);
|
|
834 |
_symbolTable.addPrimop("local-name", S_D);
|
|
835 |
_symbolTable.addPrimop("namespace-uri", S_V);
|
|
836 |
_symbolTable.addPrimop("namespace-uri", S_D);
|
|
837 |
_symbolTable.addPrimop("substring", S_SR);
|
|
838 |
_symbolTable.addPrimop("substring", S_SRR);
|
|
839 |
_symbolTable.addPrimop("substring-after", S_SS);
|
|
840 |
_symbolTable.addPrimop("substring-before", S_SS);
|
|
841 |
_symbolTable.addPrimop("normalize-space", S_V);
|
|
842 |
_symbolTable.addPrimop("normalize-space", S_S);
|
|
843 |
_symbolTable.addPrimop("system-property", S_S);
|
|
844 |
|
|
845 |
// Extensions
|
|
846 |
_symbolTable.addPrimop("nodeset", D_O);
|
|
847 |
_symbolTable.addPrimop("objectType", S_O);
|
|
848 |
_symbolTable.addPrimop("cast", O_SO);
|
|
849 |
|
|
850 |
// Operators +, -, *, /, % defined on real types.
|
|
851 |
_symbolTable.addPrimop("+", R_RR);
|
|
852 |
_symbolTable.addPrimop("-", R_RR);
|
|
853 |
_symbolTable.addPrimop("*", R_RR);
|
|
854 |
_symbolTable.addPrimop("/", R_RR);
|
|
855 |
_symbolTable.addPrimop("%", R_RR);
|
|
856 |
|
|
857 |
// Operators +, -, * defined on integer types.
|
|
858 |
// Operators / and % are not defined on integers (may cause exception)
|
|
859 |
_symbolTable.addPrimop("+", I_II);
|
|
860 |
_symbolTable.addPrimop("-", I_II);
|
|
861 |
_symbolTable.addPrimop("*", I_II);
|
|
862 |
|
|
863 |
// Operators <, <= >, >= defined on real types.
|
|
864 |
_symbolTable.addPrimop("<", B_RR);
|
|
865 |
_symbolTable.addPrimop("<=", B_RR);
|
|
866 |
_symbolTable.addPrimop(">", B_RR);
|
|
867 |
_symbolTable.addPrimop(">=", B_RR);
|
|
868 |
|
|
869 |
// Operators <, <= >, >= defined on int types.
|
|
870 |
_symbolTable.addPrimop("<", B_II);
|
|
871 |
_symbolTable.addPrimop("<=", B_II);
|
|
872 |
_symbolTable.addPrimop(">", B_II);
|
|
873 |
_symbolTable.addPrimop(">=", B_II);
|
|
874 |
|
|
875 |
// Operators <, <= >, >= defined on boolean types.
|
|
876 |
_symbolTable.addPrimop("<", B_BB);
|
|
877 |
_symbolTable.addPrimop("<=", B_BB);
|
|
878 |
_symbolTable.addPrimop(">", B_BB);
|
|
879 |
_symbolTable.addPrimop(">=", B_BB);
|
|
880 |
|
|
881 |
// Operators 'and' and 'or'.
|
|
882 |
_symbolTable.addPrimop("or", B_BB);
|
|
883 |
_symbolTable.addPrimop("and", B_BB);
|
|
884 |
|
|
885 |
// Unary minus.
|
|
886 |
_symbolTable.addPrimop("u-", R_R);
|
|
887 |
_symbolTable.addPrimop("u-", I_I);
|
|
888 |
}
|
|
889 |
|
|
890 |
public SymbolTable getSymbolTable() {
|
|
891 |
return _symbolTable;
|
|
892 |
}
|
|
893 |
|
|
894 |
public Template getTemplate() {
|
|
895 |
return _template;
|
|
896 |
}
|
|
897 |
|
|
898 |
public void setTemplate(Template template) {
|
|
899 |
_template = template;
|
|
900 |
}
|
|
901 |
|
|
902 |
private int _templateIndex = 0;
|
|
903 |
|
|
904 |
public int getTemplateIndex() {
|
|
905 |
return(_templateIndex++);
|
|
906 |
}
|
|
907 |
|
|
908 |
/**
|
|
909 |
* Creates a new node in the abstract syntax tree. This node can be
|
|
910 |
* o) a supported XSLT 1.0 element
|
|
911 |
* o) an unsupported XSLT element (post 1.0)
|
|
912 |
* o) a supported XSLT extension
|
|
913 |
* o) an unsupported XSLT extension
|
|
914 |
* o) a literal result element (not an XSLT element and not an extension)
|
|
915 |
* Unsupported elements do not directly generate an error. We have to wait
|
|
916 |
* until we have received all child elements of an unsupported element to
|
|
917 |
* see if any <xsl:fallback> elements exist.
|
|
918 |
*/
|
|
919 |
|
|
920 |
private boolean versionIsOne = true;
|
|
921 |
|
|
922 |
public SyntaxTreeNode makeInstance(String uri, String prefix,
|
|
923 |
String local, Attributes attributes)
|
|
924 |
{
|
|
925 |
SyntaxTreeNode node = null;
|
|
926 |
QName qname = getQName(uri, prefix, local);
|
|
927 |
String className = (String)_instructionClasses.get(qname);
|
|
928 |
|
|
929 |
if (className != null) {
|
|
930 |
try {
|
12458
|
931 |
final Class clazz = ObjectFactory.findProviderClass(className, true);
|
6
|
932 |
node = (SyntaxTreeNode)clazz.newInstance();
|
|
933 |
node.setQName(qname);
|
|
934 |
node.setParser(this);
|
|
935 |
if (_locator != null) {
|
|
936 |
node.setLineNumber(getLineNumber());
|
|
937 |
}
|
|
938 |
if (node instanceof Stylesheet) {
|
|
939 |
_xsltc.setStylesheet((Stylesheet)node);
|
|
940 |
}
|
|
941 |
checkForSuperfluousAttributes(node, attributes);
|
|
942 |
}
|
|
943 |
catch (ClassNotFoundException e) {
|
|
944 |
ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, node);
|
|
945 |
reportError(ERROR, err);
|
|
946 |
}
|
|
947 |
catch (Exception e) {
|
|
948 |
ErrorMsg err = new ErrorMsg(ErrorMsg.INTERNAL_ERR,
|
|
949 |
e.getMessage(), node);
|
|
950 |
reportError(FATAL, err);
|
|
951 |
}
|
|
952 |
}
|
|
953 |
else {
|
|
954 |
if (uri != null) {
|
|
955 |
// Check if the element belongs in our namespace
|
|
956 |
if (uri.equals(XSLT_URI)) {
|
|
957 |
node = new UnsupportedElement(uri, prefix, local, false);
|
|
958 |
UnsupportedElement element = (UnsupportedElement)node;
|
|
959 |
ErrorMsg msg = new ErrorMsg(ErrorMsg.UNSUPPORTED_XSL_ERR,
|
|
960 |
getLineNumber(),local);
|
|
961 |
element.setErrorMessage(msg);
|
|
962 |
if (versionIsOne) {
|
|
963 |
reportError(UNSUPPORTED,msg);
|
|
964 |
}
|
|
965 |
}
|
|
966 |
// Check if this is an XSLTC extension element
|
|
967 |
else if (uri.equals(TRANSLET_URI)) {
|
|
968 |
node = new UnsupportedElement(uri, prefix, local, true);
|
|
969 |
UnsupportedElement element = (UnsupportedElement)node;
|
|
970 |
ErrorMsg msg = new ErrorMsg(ErrorMsg.UNSUPPORTED_EXT_ERR,
|
|
971 |
getLineNumber(),local);
|
|
972 |
element.setErrorMessage(msg);
|
|
973 |
}
|
|
974 |
// Check if this is an extension of some other XSLT processor
|
|
975 |
else {
|
|
976 |
Stylesheet sheet = _xsltc.getStylesheet();
|
|
977 |
if ((sheet != null) && (sheet.isExtension(uri))) {
|
|
978 |
if (sheet != (SyntaxTreeNode)_parentStack.peek()) {
|
|
979 |
node = new UnsupportedElement(uri, prefix, local, true);
|
|
980 |
UnsupportedElement elem = (UnsupportedElement)node;
|
|
981 |
ErrorMsg msg =
|
|
982 |
new ErrorMsg(ErrorMsg.UNSUPPORTED_EXT_ERR,
|
|
983 |
getLineNumber(),
|
|
984 |
prefix+":"+local);
|
|
985 |
elem.setErrorMessage(msg);
|
|
986 |
}
|
|
987 |
}
|
|
988 |
}
|
|
989 |
}
|
|
990 |
if (node == null) {
|
|
991 |
node = new LiteralElement();
|
|
992 |
node.setLineNumber(getLineNumber());
|
|
993 |
}
|
|
994 |
}
|
|
995 |
if ((node != null) && (node instanceof LiteralElement)) {
|
|
996 |
((LiteralElement)node).setQName(qname);
|
|
997 |
}
|
|
998 |
return(node);
|
|
999 |
}
|
|
1000 |
|
|
1001 |
/**
|
|
1002 |
* checks the list of attributes against a list of allowed attributes
|
|
1003 |
* for a particular element node.
|
|
1004 |
*/
|
|
1005 |
private void checkForSuperfluousAttributes(SyntaxTreeNode node,
|
|
1006 |
Attributes attrs)
|
|
1007 |
{
|
|
1008 |
QName qname = node.getQName();
|
|
1009 |
boolean isStylesheet = (node instanceof Stylesheet);
|
|
1010 |
String[] legal = (String[]) _instructionAttrs.get(qname);
|
|
1011 |
if (versionIsOne && legal != null) {
|
|
1012 |
int j;
|
|
1013 |
final int n = attrs.getLength();
|
|
1014 |
|
|
1015 |
for (int i = 0; i < n; i++) {
|
|
1016 |
final String attrQName = attrs.getQName(i);
|
|
1017 |
|
|
1018 |
if (isStylesheet && attrQName.equals("version")) {
|
|
1019 |
versionIsOne = attrs.getValue(i).equals("1.0");
|
|
1020 |
}
|
|
1021 |
|
|
1022 |
// Ignore if special or if it has a prefix
|
|
1023 |
if (attrQName.startsWith("xml") ||
|
|
1024 |
attrQName.indexOf(':') > 0) continue;
|
|
1025 |
|
|
1026 |
for (j = 0; j < legal.length; j++) {
|
|
1027 |
if (attrQName.equalsIgnoreCase(legal[j])) {
|
|
1028 |
break;
|
|
1029 |
}
|
|
1030 |
}
|
|
1031 |
if (j == legal.length) {
|
|
1032 |
final ErrorMsg err =
|
|
1033 |
new ErrorMsg(ErrorMsg.ILLEGAL_ATTRIBUTE_ERR,
|
|
1034 |
attrQName, node);
|
|
1035 |
// Workaround for the TCK failure ErrorListener.errorTests.error001..
|
|
1036 |
err.setWarningError(true);
|
|
1037 |
reportError(WARNING, err);
|
|
1038 |
}
|
|
1039 |
}
|
|
1040 |
}
|
|
1041 |
}
|
|
1042 |
|
|
1043 |
|
|
1044 |
/**
|
|
1045 |
* Parse an XPath expression:
|
|
1046 |
* @param parent - XSL element where the expression occured
|
|
1047 |
* @param exp - textual representation of the expression
|
|
1048 |
*/
|
|
1049 |
public Expression parseExpression(SyntaxTreeNode parent, String exp) {
|
|
1050 |
return (Expression)parseTopLevel(parent, "<EXPRESSION>"+exp, null);
|
|
1051 |
}
|
|
1052 |
|
|
1053 |
/**
|
|
1054 |
* Parse an XPath expression:
|
|
1055 |
* @param parent - XSL element where the expression occured
|
|
1056 |
* @param attr - name of this element's attribute to get expression from
|
|
1057 |
* @param def - default expression (if the attribute was not found)
|
|
1058 |
*/
|
|
1059 |
public Expression parseExpression(SyntaxTreeNode parent,
|
|
1060 |
String attr, String def) {
|
|
1061 |
// Get the textual representation of the expression (if any)
|
|
1062 |
String exp = parent.getAttribute(attr);
|
|
1063 |
// Use the default expression if none was found
|
|
1064 |
if ((exp.length() == 0) && (def != null)) exp = def;
|
|
1065 |
// Invoke the XPath parser
|
|
1066 |
return (Expression)parseTopLevel(parent, "<EXPRESSION>"+exp, exp);
|
|
1067 |
}
|
|
1068 |
|
|
1069 |
/**
|
|
1070 |
* Parse an XPath pattern:
|
|
1071 |
* @param parent - XSL element where the pattern occured
|
|
1072 |
* @param pattern - textual representation of the pattern
|
|
1073 |
*/
|
|
1074 |
public Pattern parsePattern(SyntaxTreeNode parent, String pattern) {
|
|
1075 |
return (Pattern)parseTopLevel(parent, "<PATTERN>"+pattern, pattern);
|
|
1076 |
}
|
|
1077 |
|
|
1078 |
/**
|
|
1079 |
* Parse an XPath pattern:
|
|
1080 |
* @param parent - XSL element where the pattern occured
|
|
1081 |
* @param attr - name of this element's attribute to get pattern from
|
|
1082 |
* @param def - default pattern (if the attribute was not found)
|
|
1083 |
*/
|
|
1084 |
public Pattern parsePattern(SyntaxTreeNode parent,
|
|
1085 |
String attr, String def) {
|
|
1086 |
// Get the textual representation of the pattern (if any)
|
|
1087 |
String pattern = parent.getAttribute(attr);
|
|
1088 |
// Use the default pattern if none was found
|
|
1089 |
if ((pattern.length() == 0) && (def != null)) pattern = def;
|
|
1090 |
// Invoke the XPath parser
|
|
1091 |
return (Pattern)parseTopLevel(parent, "<PATTERN>"+pattern, pattern);
|
|
1092 |
}
|
|
1093 |
|
|
1094 |
/**
|
|
1095 |
* Parse an XPath expression or pattern using the generated XPathParser
|
|
1096 |
* The method will return a Dummy node if the XPath parser fails.
|
|
1097 |
*/
|
|
1098 |
private SyntaxTreeNode parseTopLevel(SyntaxTreeNode parent, String text,
|
|
1099 |
String expression) {
|
|
1100 |
int line = getLineNumber();
|
|
1101 |
|
|
1102 |
try {
|
|
1103 |
_xpathParser.setScanner(new XPathLexer(new StringReader(text)));
|
|
1104 |
Symbol result = _xpathParser.parse(expression, line);
|
|
1105 |
if (result != null) {
|
|
1106 |
final SyntaxTreeNode node = (SyntaxTreeNode)result.value;
|
|
1107 |
if (node != null) {
|
|
1108 |
node.setParser(this);
|
|
1109 |
node.setParent(parent);
|
|
1110 |
node.setLineNumber(line);
|
|
1111 |
// System.out.println("e = " + text + " " + node);
|
|
1112 |
return node;
|
|
1113 |
}
|
|
1114 |
}
|
|
1115 |
reportError(ERROR, new ErrorMsg(ErrorMsg.XPATH_PARSER_ERR,
|
|
1116 |
expression, parent));
|
|
1117 |
}
|
|
1118 |
catch (Exception e) {
|
|
1119 |
if (_xsltc.debug()) e.printStackTrace();
|
|
1120 |
reportError(ERROR, new ErrorMsg(ErrorMsg.XPATH_PARSER_ERR,
|
|
1121 |
expression, parent));
|
|
1122 |
}
|
|
1123 |
|
|
1124 |
// Return a dummy pattern (which is an expression)
|
|
1125 |
SyntaxTreeNode.Dummy.setParser(this);
|
|
1126 |
return SyntaxTreeNode.Dummy;
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
/************************ ERROR HANDLING SECTION ************************/
|
|
1130 |
|
|
1131 |
/**
|
|
1132 |
* Returns true if there were any errors during compilation
|
|
1133 |
*/
|
|
1134 |
public boolean errorsFound() {
|
|
1135 |
return _errors.size() > 0;
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
/**
|
|
1139 |
* Prints all compile-time errors
|
|
1140 |
*/
|
|
1141 |
public void printErrors() {
|
|
1142 |
final int size = _errors.size();
|
|
1143 |
if (size > 0) {
|
|
1144 |
System.err.println(new ErrorMsg(ErrorMsg.COMPILER_ERROR_KEY));
|
|
1145 |
for (int i = 0; i < size; i++) {
|
|
1146 |
System.err.println(" " + _errors.elementAt(i));
|
|
1147 |
}
|
|
1148 |
}
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
/**
|
|
1152 |
* Prints all compile-time warnings
|
|
1153 |
*/
|
|
1154 |
public void printWarnings() {
|
|
1155 |
final int size = _warnings.size();
|
|
1156 |
if (size > 0) {
|
|
1157 |
System.err.println(new ErrorMsg(ErrorMsg.COMPILER_WARNING_KEY));
|
|
1158 |
for (int i = 0; i < size; i++) {
|
|
1159 |
System.err.println(" " + _warnings.elementAt(i));
|
|
1160 |
}
|
|
1161 |
}
|
|
1162 |
}
|
|
1163 |
|
|
1164 |
/**
|
|
1165 |
* Common error/warning message handler
|
|
1166 |
*/
|
|
1167 |
public void reportError(final int category, final ErrorMsg error) {
|
|
1168 |
switch (category) {
|
|
1169 |
case Constants.INTERNAL:
|
|
1170 |
// Unexpected internal errors, such as null-ptr exceptions, etc.
|
|
1171 |
// Immediately terminates compilation, no translet produced
|
|
1172 |
_errors.addElement(error);
|
|
1173 |
break;
|
|
1174 |
case Constants.UNSUPPORTED:
|
|
1175 |
// XSLT elements that are not implemented and unsupported ext.
|
|
1176 |
// Immediately terminates compilation, no translet produced
|
|
1177 |
_errors.addElement(error);
|
|
1178 |
break;
|
|
1179 |
case Constants.FATAL:
|
|
1180 |
// Fatal error in the stylesheet input (parsing or content)
|
|
1181 |
// Immediately terminates compilation, no translet produced
|
|
1182 |
_errors.addElement(error);
|
|
1183 |
break;
|
|
1184 |
case Constants.ERROR:
|
|
1185 |
// Other error in the stylesheet input (parsing or content)
|
|
1186 |
// Does not terminate compilation, no translet produced
|
|
1187 |
_errors.addElement(error);
|
|
1188 |
break;
|
|
1189 |
case Constants.WARNING:
|
|
1190 |
// Other error in the stylesheet input (content errors only)
|
|
1191 |
// Does not terminate compilation, a translet is produced
|
|
1192 |
_warnings.addElement(error);
|
|
1193 |
break;
|
|
1194 |
}
|
|
1195 |
}
|
|
1196 |
|
|
1197 |
public Vector getErrors() {
|
|
1198 |
return _errors;
|
|
1199 |
}
|
|
1200 |
|
|
1201 |
public Vector getWarnings() {
|
|
1202 |
return _warnings;
|
|
1203 |
}
|
|
1204 |
|
|
1205 |
/************************ SAX2 ContentHandler INTERFACE *****************/
|
|
1206 |
|
|
1207 |
private Stack _parentStack = null;
|
|
1208 |
private Hashtable _prefixMapping = null;
|
|
1209 |
|
|
1210 |
/**
|
|
1211 |
* SAX2: Receive notification of the beginning of a document.
|
|
1212 |
*/
|
|
1213 |
public void startDocument() {
|
|
1214 |
_root = null;
|
|
1215 |
_target = null;
|
|
1216 |
_prefixMapping = null;
|
|
1217 |
_parentStack = new Stack();
|
|
1218 |
}
|
|
1219 |
|
|
1220 |
/**
|
|
1221 |
* SAX2: Receive notification of the end of a document.
|
|
1222 |
*/
|
|
1223 |
public void endDocument() { }
|
|
1224 |
|
|
1225 |
|
|
1226 |
/**
|
|
1227 |
* SAX2: Begin the scope of a prefix-URI Namespace mapping.
|
|
1228 |
* This has to be passed on to the symbol table!
|
|
1229 |
*/
|
|
1230 |
public void startPrefixMapping(String prefix, String uri) {
|
|
1231 |
if (_prefixMapping == null) {
|
|
1232 |
_prefixMapping = new Hashtable();
|
|
1233 |
}
|
|
1234 |
_prefixMapping.put(prefix, uri);
|
|
1235 |
}
|
|
1236 |
|
|
1237 |
/**
|
|
1238 |
* SAX2: End the scope of a prefix-URI Namespace mapping.
|
|
1239 |
* This has to be passed on to the symbol table!
|
|
1240 |
*/
|
|
1241 |
public void endPrefixMapping(String prefix) { }
|
|
1242 |
|
|
1243 |
/**
|
|
1244 |
* SAX2: Receive notification of the beginning of an element.
|
|
1245 |
* The parser may re-use the attribute list that we're passed so
|
|
1246 |
* we clone the attributes in our own Attributes implementation
|
|
1247 |
*/
|
|
1248 |
public void startElement(String uri, String localname,
|
|
1249 |
String qname, Attributes attributes)
|
|
1250 |
throws SAXException {
|
|
1251 |
final int col = qname.lastIndexOf(':');
|
|
1252 |
final String prefix = (col == -1) ? null : qname.substring(0, col);
|
|
1253 |
|
|
1254 |
SyntaxTreeNode element = makeInstance(uri, prefix,
|
|
1255 |
localname, attributes);
|
|
1256 |
if (element == null) {
|
|
1257 |
ErrorMsg err = new ErrorMsg(ErrorMsg.ELEMENT_PARSE_ERR,
|
|
1258 |
prefix+':'+localname);
|
|
1259 |
throw new SAXException(err.toString());
|
|
1260 |
}
|
|
1261 |
|
|
1262 |
// If this is the root element of the XML document we need to make sure
|
|
1263 |
// that it contains a definition of the XSL namespace URI
|
|
1264 |
if (_root == null) {
|
|
1265 |
if ((_prefixMapping == null) ||
|
|
1266 |
(_prefixMapping.containsValue(Constants.XSLT_URI) == false))
|
|
1267 |
_rootNamespaceDef = false;
|
|
1268 |
else
|
|
1269 |
_rootNamespaceDef = true;
|
|
1270 |
_root = element;
|
|
1271 |
}
|
|
1272 |
else {
|
|
1273 |
SyntaxTreeNode parent = (SyntaxTreeNode)_parentStack.peek();
|
|
1274 |
parent.addElement(element);
|
|
1275 |
element.setParent(parent);
|
|
1276 |
}
|
|
1277 |
element.setAttributes(new AttributesImpl(attributes));
|
|
1278 |
element.setPrefixMapping(_prefixMapping);
|
|
1279 |
|
|
1280 |
if (element instanceof Stylesheet) {
|
|
1281 |
// Extension elements and excluded elements have to be
|
|
1282 |
// handled at this point in order to correctly generate
|
|
1283 |
// Fallback elements from <xsl:fallback>s.
|
|
1284 |
getSymbolTable().setCurrentNode(element);
|
12458
|
1285 |
((Stylesheet)element).declareExtensionPrefixes(this);
|
6
|
1286 |
}
|
|
1287 |
|
|
1288 |
_prefixMapping = null;
|
|
1289 |
_parentStack.push(element);
|
|
1290 |
}
|
|
1291 |
|
|
1292 |
/**
|
|
1293 |
* SAX2: Receive notification of the end of an element.
|
|
1294 |
*/
|
|
1295 |
public void endElement(String uri, String localname, String qname) {
|
|
1296 |
_parentStack.pop();
|
|
1297 |
}
|
|
1298 |
|
|
1299 |
/**
|
|
1300 |
* SAX2: Receive notification of character data.
|
|
1301 |
*/
|
|
1302 |
public void characters(char[] ch, int start, int length) {
|
|
1303 |
String string = new String(ch, start, length);
|
|
1304 |
SyntaxTreeNode parent = (SyntaxTreeNode)_parentStack.peek();
|
|
1305 |
|
|
1306 |
if (string.length() == 0) return;
|
|
1307 |
|
|
1308 |
// If this text occurs within an <xsl:text> element we append it
|
|
1309 |
// as-is to the existing text element
|
|
1310 |
if (parent instanceof Text) {
|
|
1311 |
((Text)parent).setText(string);
|
|
1312 |
return;
|
|
1313 |
}
|
|
1314 |
|
|
1315 |
// Ignore text nodes that occur directly under <xsl:stylesheet>
|
|
1316 |
if (parent instanceof Stylesheet) return;
|
|
1317 |
|
|
1318 |
SyntaxTreeNode bro = parent.lastChild();
|
|
1319 |
if ((bro != null) && (bro instanceof Text)) {
|
|
1320 |
Text text = (Text)bro;
|
|
1321 |
if (!text.isTextElement()) {
|
|
1322 |
if ((length > 1) || ( ((int)ch[0]) < 0x100)) {
|
|
1323 |
text.setText(string);
|
|
1324 |
return;
|
|
1325 |
}
|
|
1326 |
}
|
|
1327 |
}
|
|
1328 |
|
|
1329 |
// Add it as a regular text node otherwise
|
|
1330 |
parent.addElement(new Text(string));
|
|
1331 |
}
|
|
1332 |
|
|
1333 |
private String getTokenValue(String token) {
|
|
1334 |
final int start = token.indexOf('"');
|
|
1335 |
final int stop = token.lastIndexOf('"');
|
|
1336 |
return token.substring(start+1, stop);
|
|
1337 |
}
|
|
1338 |
|
|
1339 |
/**
|
|
1340 |
* SAX2: Receive notification of a processing instruction.
|
|
1341 |
* These require special handling for stylesheet PIs.
|
|
1342 |
*/
|
|
1343 |
public void processingInstruction(String name, String value) {
|
|
1344 |
// We only handle the <?xml-stylesheet ...?> PI
|
|
1345 |
if ((_target == null) && (name.equals("xml-stylesheet"))) {
|
|
1346 |
|
|
1347 |
String href = null; // URI of stylesheet found
|
|
1348 |
String media = null; // Media of stylesheet found
|
|
1349 |
String title = null; // Title of stylesheet found
|
|
1350 |
String charset = null; // Charset of stylesheet found
|
|
1351 |
|
|
1352 |
// Get the attributes from the processing instruction
|
|
1353 |
StringTokenizer tokens = new StringTokenizer(value);
|
|
1354 |
while (tokens.hasMoreElements()) {
|
|
1355 |
String token = (String)tokens.nextElement();
|
|
1356 |
if (token.startsWith("href"))
|
|
1357 |
href = getTokenValue(token);
|
|
1358 |
else if (token.startsWith("media"))
|
|
1359 |
media = getTokenValue(token);
|
|
1360 |
else if (token.startsWith("title"))
|
|
1361 |
title = getTokenValue(token);
|
|
1362 |
else if (token.startsWith("charset"))
|
|
1363 |
charset = getTokenValue(token);
|
|
1364 |
}
|
|
1365 |
|
|
1366 |
// Set the target to this PI's href if the parameters are
|
|
1367 |
// null or match the corresponding attributes of this PI.
|
|
1368 |
if ( ((_PImedia == null) || (_PImedia.equals(media))) &&
|
|
1369 |
((_PItitle == null) || (_PImedia.equals(title))) &&
|
|
1370 |
((_PIcharset == null) || (_PImedia.equals(charset))) ) {
|
|
1371 |
_target = href;
|
|
1372 |
}
|
|
1373 |
}
|
|
1374 |
}
|
|
1375 |
|
|
1376 |
/**
|
|
1377 |
* IGNORED - all ignorable whitespace is ignored
|
|
1378 |
*/
|
|
1379 |
public void ignorableWhitespace(char[] ch, int start, int length) { }
|
|
1380 |
|
|
1381 |
/**
|
|
1382 |
* IGNORED - we do not have to do anything with skipped entities
|
|
1383 |
*/
|
|
1384 |
public void skippedEntity(String name) { }
|
|
1385 |
|
|
1386 |
/**
|
|
1387 |
* Store the document locator to later retrieve line numbers of all
|
|
1388 |
* elements from the stylesheet
|
|
1389 |
*/
|
|
1390 |
public void setDocumentLocator(Locator locator) {
|
|
1391 |
_locator = locator;
|
|
1392 |
}
|
|
1393 |
|
|
1394 |
/**
|
|
1395 |
* Get the line number, or zero
|
|
1396 |
* if there is no _locator.
|
|
1397 |
*/
|
|
1398 |
private int getLineNumber() {
|
|
1399 |
int line = 0;
|
|
1400 |
if (_locator != null)
|
|
1401 |
line = _locator.getLineNumber();
|
|
1402 |
return line;
|
|
1403 |
}
|
|
1404 |
|
|
1405 |
}
|