6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-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: Compiler.java,v 1.2.4.1 2005/09/14 19:47:10 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xpath.internal.compiler;
|
|
24 |
|
|
25 |
import javax.xml.transform.ErrorListener;
|
|
26 |
import javax.xml.transform.SourceLocator;
|
|
27 |
import javax.xml.transform.TransformerException;
|
|
28 |
|
|
29 |
import com.sun.org.apache.xalan.internal.res.XSLMessages;
|
|
30 |
import com.sun.org.apache.xml.internal.dtm.Axis;
|
|
31 |
import com.sun.org.apache.xml.internal.dtm.DTMFilter;
|
|
32 |
import com.sun.org.apache.xml.internal.dtm.DTMIterator;
|
|
33 |
import com.sun.org.apache.xml.internal.utils.PrefixResolver;
|
|
34 |
import com.sun.org.apache.xml.internal.utils.QName;
|
|
35 |
import com.sun.org.apache.xml.internal.utils.SAXSourceLocator;
|
|
36 |
import com.sun.org.apache.xpath.internal.Expression;
|
|
37 |
import com.sun.org.apache.xpath.internal.axes.UnionPathIterator;
|
|
38 |
import com.sun.org.apache.xpath.internal.axes.WalkerFactory;
|
|
39 |
import com.sun.org.apache.xpath.internal.functions.FuncExtFunction;
|
|
40 |
import com.sun.org.apache.xpath.internal.functions.FuncExtFunctionAvailable;
|
|
41 |
import com.sun.org.apache.xpath.internal.functions.Function;
|
|
42 |
import com.sun.org.apache.xpath.internal.functions.WrongNumberArgsException;
|
|
43 |
import com.sun.org.apache.xpath.internal.objects.XNumber;
|
|
44 |
import com.sun.org.apache.xpath.internal.objects.XString;
|
|
45 |
import com.sun.org.apache.xpath.internal.operations.And;
|
|
46 |
import com.sun.org.apache.xpath.internal.operations.Div;
|
|
47 |
import com.sun.org.apache.xpath.internal.operations.Equals;
|
|
48 |
import com.sun.org.apache.xpath.internal.operations.Gt;
|
|
49 |
import com.sun.org.apache.xpath.internal.operations.Gte;
|
|
50 |
import com.sun.org.apache.xpath.internal.operations.Lt;
|
|
51 |
import com.sun.org.apache.xpath.internal.operations.Lte;
|
|
52 |
import com.sun.org.apache.xpath.internal.operations.Minus;
|
|
53 |
import com.sun.org.apache.xpath.internal.operations.Mod;
|
|
54 |
import com.sun.org.apache.xpath.internal.operations.Mult;
|
|
55 |
import com.sun.org.apache.xpath.internal.operations.Neg;
|
|
56 |
import com.sun.org.apache.xpath.internal.operations.NotEquals;
|
|
57 |
import com.sun.org.apache.xpath.internal.operations.Operation;
|
|
58 |
import com.sun.org.apache.xpath.internal.operations.Or;
|
|
59 |
import com.sun.org.apache.xpath.internal.operations.Plus;
|
|
60 |
import com.sun.org.apache.xpath.internal.operations.UnaryOperation;
|
|
61 |
import com.sun.org.apache.xpath.internal.operations.Variable;
|
|
62 |
import com.sun.org.apache.xpath.internal.patterns.FunctionPattern;
|
|
63 |
import com.sun.org.apache.xpath.internal.patterns.NodeTest;
|
|
64 |
import com.sun.org.apache.xpath.internal.patterns.StepPattern;
|
|
65 |
import com.sun.org.apache.xpath.internal.patterns.UnionPattern;
|
|
66 |
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* An instance of this class compiles an XPath string expression into
|
|
70 |
* a Expression object. This class compiles the string into a sequence
|
|
71 |
* of operation codes (op map) and then builds from that into an Expression
|
|
72 |
* tree.
|
|
73 |
* @xsl.usage advanced
|
|
74 |
*/
|
|
75 |
public class Compiler extends OpMap
|
|
76 |
{
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Construct a Compiler object with a specific ErrorListener and
|
|
80 |
* SourceLocator where the expression is located.
|
|
81 |
*
|
|
82 |
* @param errorHandler Error listener where messages will be sent, or null
|
|
83 |
* if messages should be sent to System err.
|
|
84 |
* @param locator The location object where the expression lives, which
|
|
85 |
* may be null, but which, if not null, must be valid over
|
|
86 |
* the long haul, in other words, it will not be cloned.
|
|
87 |
* @param fTable The FunctionTable object where the xpath build-in
|
|
88 |
* functions are stored.
|
|
89 |
*/
|
|
90 |
public Compiler(ErrorListener errorHandler, SourceLocator locator,
|
|
91 |
FunctionTable fTable)
|
|
92 |
{
|
|
93 |
m_errorHandler = errorHandler;
|
|
94 |
m_locator = locator;
|
|
95 |
m_functionTable = fTable;
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Construct a Compiler instance that has a null error listener and a
|
|
100 |
* null source locator.
|
|
101 |
*/
|
|
102 |
public Compiler()
|
|
103 |
{
|
|
104 |
m_errorHandler = null;
|
|
105 |
m_locator = null;
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Execute the XPath object from a given opcode position.
|
|
110 |
* @param opPos The current position in the xpath.m_opMap array.
|
|
111 |
* @return The result of the XPath.
|
|
112 |
*
|
|
113 |
* @throws TransformerException if there is a syntax or other error.
|
|
114 |
* @xsl.usage advanced
|
|
115 |
*/
|
|
116 |
public Expression compile(int opPos) throws TransformerException
|
|
117 |
{
|
|
118 |
|
|
119 |
int op = getOp(opPos);
|
|
120 |
|
|
121 |
Expression expr = null;
|
|
122 |
// System.out.println(getPatternString()+"op: "+op);
|
|
123 |
switch (op)
|
|
124 |
{
|
|
125 |
case OpCodes.OP_XPATH :
|
|
126 |
expr = compile(opPos + 2); break;
|
|
127 |
case OpCodes.OP_OR :
|
|
128 |
expr = or(opPos); break;
|
|
129 |
case OpCodes.OP_AND :
|
|
130 |
expr = and(opPos); break;
|
|
131 |
case OpCodes.OP_NOTEQUALS :
|
|
132 |
expr = notequals(opPos); break;
|
|
133 |
case OpCodes.OP_EQUALS :
|
|
134 |
expr = equals(opPos); break;
|
|
135 |
case OpCodes.OP_LTE :
|
|
136 |
expr = lte(opPos); break;
|
|
137 |
case OpCodes.OP_LT :
|
|
138 |
expr = lt(opPos); break;
|
|
139 |
case OpCodes.OP_GTE :
|
|
140 |
expr = gte(opPos); break;
|
|
141 |
case OpCodes.OP_GT :
|
|
142 |
expr = gt(opPos); break;
|
|
143 |
case OpCodes.OP_PLUS :
|
|
144 |
expr = plus(opPos); break;
|
|
145 |
case OpCodes.OP_MINUS :
|
|
146 |
expr = minus(opPos); break;
|
|
147 |
case OpCodes.OP_MULT :
|
|
148 |
expr = mult(opPos); break;
|
|
149 |
case OpCodes.OP_DIV :
|
|
150 |
expr = div(opPos); break;
|
|
151 |
case OpCodes.OP_MOD :
|
|
152 |
expr = mod(opPos); break;
|
|
153 |
// case OpCodes.OP_QUO :
|
|
154 |
// expr = quo(opPos); break;
|
|
155 |
case OpCodes.OP_NEG :
|
|
156 |
expr = neg(opPos); break;
|
|
157 |
case OpCodes.OP_STRING :
|
|
158 |
expr = string(opPos); break;
|
|
159 |
case OpCodes.OP_BOOL :
|
|
160 |
expr = bool(opPos); break;
|
|
161 |
case OpCodes.OP_NUMBER :
|
|
162 |
expr = number(opPos); break;
|
|
163 |
case OpCodes.OP_UNION :
|
|
164 |
expr = union(opPos); break;
|
|
165 |
case OpCodes.OP_LITERAL :
|
|
166 |
expr = literal(opPos); break;
|
|
167 |
case OpCodes.OP_VARIABLE :
|
|
168 |
expr = variable(opPos); break;
|
|
169 |
case OpCodes.OP_GROUP :
|
|
170 |
expr = group(opPos); break;
|
|
171 |
case OpCodes.OP_NUMBERLIT :
|
|
172 |
expr = numberlit(opPos); break;
|
|
173 |
case OpCodes.OP_ARGUMENT :
|
|
174 |
expr = arg(opPos); break;
|
|
175 |
case OpCodes.OP_EXTFUNCTION :
|
|
176 |
expr = compileExtension(opPos); break;
|
|
177 |
case OpCodes.OP_FUNCTION :
|
|
178 |
expr = compileFunction(opPos); break;
|
|
179 |
case OpCodes.OP_LOCATIONPATH :
|
|
180 |
expr = locationPath(opPos); break;
|
|
181 |
case OpCodes.OP_PREDICATE :
|
|
182 |
expr = null; break; // should never hit this here.
|
|
183 |
case OpCodes.OP_MATCHPATTERN :
|
|
184 |
expr = matchPattern(opPos + 2); break;
|
|
185 |
case OpCodes.OP_LOCATIONPATHPATTERN :
|
|
186 |
expr = locationPathPattern(opPos); break;
|
|
187 |
case OpCodes.OP_QUO:
|
|
188 |
error(XPATHErrorResources.ER_UNKNOWN_OPCODE,
|
|
189 |
new Object[]{ "quo" }); //"ERROR! Unknown op code: "+m_opMap[opPos]);
|
|
190 |
break;
|
|
191 |
default :
|
|
192 |
error(XPATHErrorResources.ER_UNKNOWN_OPCODE,
|
|
193 |
new Object[]{ Integer.toString(getOp(opPos)) }); //"ERROR! Unknown op code: "+m_opMap[opPos]);
|
|
194 |
}
|
|
195 |
// if(null != expr)
|
|
196 |
// expr.setSourceLocator(m_locator);
|
|
197 |
|
|
198 |
return expr;
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Bottle-neck compilation of an operation with left and right operands.
|
|
203 |
*
|
|
204 |
* @param operation non-null reference to parent operation.
|
|
205 |
* @param opPos The op map position of the parent operation.
|
|
206 |
*
|
|
207 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Operation} instance.
|
|
208 |
*
|
|
209 |
* @throws TransformerException if there is a syntax or other error.
|
|
210 |
*/
|
|
211 |
private Expression compileOperation(Operation operation, int opPos)
|
|
212 |
throws TransformerException
|
|
213 |
{
|
|
214 |
|
|
215 |
int leftPos = getFirstChildPos(opPos);
|
|
216 |
int rightPos = getNextOpPos(leftPos);
|
|
217 |
|
|
218 |
operation.setLeftRight(compile(leftPos), compile(rightPos));
|
|
219 |
|
|
220 |
return operation;
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Bottle-neck compilation of a unary operation.
|
|
225 |
*
|
|
226 |
* @param unary The parent unary operation.
|
|
227 |
* @param opPos The position in the op map of the parent operation.
|
|
228 |
*
|
|
229 |
* @return The unary argument.
|
|
230 |
*
|
|
231 |
* @throws TransformerException if syntax or other error occurs.
|
|
232 |
*/
|
|
233 |
private Expression compileUnary(UnaryOperation unary, int opPos)
|
|
234 |
throws TransformerException
|
|
235 |
{
|
|
236 |
|
|
237 |
int rightPos = getFirstChildPos(opPos);
|
|
238 |
|
|
239 |
unary.setRight(compile(rightPos));
|
|
240 |
|
|
241 |
return unary;
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Compile an 'or' operation.
|
|
246 |
*
|
|
247 |
* @param opPos The current position in the m_opMap array.
|
|
248 |
*
|
|
249 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Or} instance.
|
|
250 |
*
|
|
251 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
252 |
*/
|
|
253 |
protected Expression or(int opPos) throws TransformerException
|
|
254 |
{
|
|
255 |
return compileOperation(new Or(), opPos);
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Compile an 'and' operation.
|
|
260 |
*
|
|
261 |
* @param opPos The current position in the m_opMap array.
|
|
262 |
*
|
|
263 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.And} instance.
|
|
264 |
*
|
|
265 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
266 |
*/
|
|
267 |
protected Expression and(int opPos) throws TransformerException
|
|
268 |
{
|
|
269 |
return compileOperation(new And(), opPos);
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Compile a '!=' operation.
|
|
274 |
*
|
|
275 |
* @param opPos The current position in the m_opMap array.
|
|
276 |
*
|
|
277 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.NotEquals} instance.
|
|
278 |
*
|
|
279 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
280 |
*/
|
|
281 |
protected Expression notequals(int opPos) throws TransformerException
|
|
282 |
{
|
|
283 |
return compileOperation(new NotEquals(), opPos);
|
|
284 |
}
|
|
285 |
|
|
286 |
/**
|
|
287 |
* Compile a '=' operation.
|
|
288 |
*
|
|
289 |
* @param opPos The current position in the m_opMap array.
|
|
290 |
*
|
|
291 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Equals} instance.
|
|
292 |
*
|
|
293 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
294 |
*/
|
|
295 |
protected Expression equals(int opPos) throws TransformerException
|
|
296 |
{
|
|
297 |
return compileOperation(new Equals(), opPos);
|
|
298 |
}
|
|
299 |
|
|
300 |
/**
|
|
301 |
* Compile a '<=' operation.
|
|
302 |
*
|
|
303 |
* @param opPos The current position in the m_opMap array.
|
|
304 |
*
|
|
305 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Lte} instance.
|
|
306 |
*
|
|
307 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
308 |
*/
|
|
309 |
protected Expression lte(int opPos) throws TransformerException
|
|
310 |
{
|
|
311 |
return compileOperation(new Lte(), opPos);
|
|
312 |
}
|
|
313 |
|
|
314 |
/**
|
|
315 |
* Compile a '<' operation.
|
|
316 |
*
|
|
317 |
* @param opPos The current position in the m_opMap array.
|
|
318 |
*
|
|
319 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Lt} instance.
|
|
320 |
*
|
|
321 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
322 |
*/
|
|
323 |
protected Expression lt(int opPos) throws TransformerException
|
|
324 |
{
|
|
325 |
return compileOperation(new Lt(), opPos);
|
|
326 |
}
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Compile a '>=' operation.
|
|
330 |
*
|
|
331 |
* @param opPos The current position in the m_opMap array.
|
|
332 |
*
|
|
333 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Gte} instance.
|
|
334 |
*
|
|
335 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
336 |
*/
|
|
337 |
protected Expression gte(int opPos) throws TransformerException
|
|
338 |
{
|
|
339 |
return compileOperation(new Gte(), opPos);
|
|
340 |
}
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Compile a '>' operation.
|
|
344 |
*
|
|
345 |
* @param opPos The current position in the m_opMap array.
|
|
346 |
*
|
|
347 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Gt} instance.
|
|
348 |
*
|
|
349 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
350 |
*/
|
|
351 |
protected Expression gt(int opPos) throws TransformerException
|
|
352 |
{
|
|
353 |
return compileOperation(new Gt(), opPos);
|
|
354 |
}
|
|
355 |
|
|
356 |
/**
|
|
357 |
* Compile a '+' operation.
|
|
358 |
*
|
|
359 |
* @param opPos The current position in the m_opMap array.
|
|
360 |
*
|
|
361 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Plus} instance.
|
|
362 |
*
|
|
363 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
364 |
*/
|
|
365 |
protected Expression plus(int opPos) throws TransformerException
|
|
366 |
{
|
|
367 |
return compileOperation(new Plus(), opPos);
|
|
368 |
}
|
|
369 |
|
|
370 |
/**
|
|
371 |
* Compile a '-' operation.
|
|
372 |
*
|
|
373 |
* @param opPos The current position in the m_opMap array.
|
|
374 |
*
|
|
375 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Minus} instance.
|
|
376 |
*
|
|
377 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
378 |
*/
|
|
379 |
protected Expression minus(int opPos) throws TransformerException
|
|
380 |
{
|
|
381 |
return compileOperation(new Minus(), opPos);
|
|
382 |
}
|
|
383 |
|
|
384 |
/**
|
|
385 |
* Compile a '*' operation.
|
|
386 |
*
|
|
387 |
* @param opPos The current position in the m_opMap array.
|
|
388 |
*
|
|
389 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Mult} instance.
|
|
390 |
*
|
|
391 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
392 |
*/
|
|
393 |
protected Expression mult(int opPos) throws TransformerException
|
|
394 |
{
|
|
395 |
return compileOperation(new Mult(), opPos);
|
|
396 |
}
|
|
397 |
|
|
398 |
/**
|
|
399 |
* Compile a 'div' operation.
|
|
400 |
*
|
|
401 |
* @param opPos The current position in the m_opMap array.
|
|
402 |
*
|
|
403 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Div} instance.
|
|
404 |
*
|
|
405 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
406 |
*/
|
|
407 |
protected Expression div(int opPos) throws TransformerException
|
|
408 |
{
|
|
409 |
return compileOperation(new Div(), opPos);
|
|
410 |
}
|
|
411 |
|
|
412 |
/**
|
|
413 |
* Compile a 'mod' operation.
|
|
414 |
*
|
|
415 |
* @param opPos The current position in the m_opMap array.
|
|
416 |
*
|
|
417 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Mod} instance.
|
|
418 |
*
|
|
419 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
420 |
*/
|
|
421 |
protected Expression mod(int opPos) throws TransformerException
|
|
422 |
{
|
|
423 |
return compileOperation(new Mod(), opPos);
|
|
424 |
}
|
|
425 |
|
|
426 |
/*
|
|
427 |
* Compile a 'quo' operation.
|
|
428 |
*
|
|
429 |
* @param opPos The current position in the m_opMap array.
|
|
430 |
*
|
|
431 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Quo} instance.
|
|
432 |
*
|
|
433 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
434 |
*/
|
|
435 |
// protected Expression quo(int opPos) throws TransformerException
|
|
436 |
// {
|
|
437 |
// return compileOperation(new Quo(), opPos);
|
|
438 |
// }
|
|
439 |
|
|
440 |
/**
|
|
441 |
* Compile a unary '-' operation.
|
|
442 |
*
|
|
443 |
* @param opPos The current position in the m_opMap array.
|
|
444 |
*
|
|
445 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Neg} instance.
|
|
446 |
*
|
|
447 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
448 |
*/
|
|
449 |
protected Expression neg(int opPos) throws TransformerException
|
|
450 |
{
|
|
451 |
return compileUnary(new Neg(), opPos);
|
|
452 |
}
|
|
453 |
|
|
454 |
/**
|
|
455 |
* Compile a 'string(...)' operation.
|
|
456 |
*
|
|
457 |
* @param opPos The current position in the m_opMap array.
|
|
458 |
*
|
|
459 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.String} instance.
|
|
460 |
*
|
|
461 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
462 |
*/
|
|
463 |
protected Expression string(int opPos) throws TransformerException
|
|
464 |
{
|
|
465 |
return compileUnary(new com.sun.org.apache.xpath.internal.operations.String(), opPos);
|
|
466 |
}
|
|
467 |
|
|
468 |
/**
|
|
469 |
* Compile a 'boolean(...)' operation.
|
|
470 |
*
|
|
471 |
* @param opPos The current position in the m_opMap array.
|
|
472 |
*
|
|
473 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Bool} instance.
|
|
474 |
*
|
|
475 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
476 |
*/
|
|
477 |
protected Expression bool(int opPos) throws TransformerException
|
|
478 |
{
|
|
479 |
return compileUnary(new com.sun.org.apache.xpath.internal.operations.Bool(), opPos);
|
|
480 |
}
|
|
481 |
|
|
482 |
/**
|
|
483 |
* Compile a 'number(...)' operation.
|
|
484 |
*
|
|
485 |
* @param opPos The current position in the m_opMap array.
|
|
486 |
*
|
|
487 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Number} instance.
|
|
488 |
*
|
|
489 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
490 |
*/
|
|
491 |
protected Expression number(int opPos) throws TransformerException
|
|
492 |
{
|
|
493 |
return compileUnary(new com.sun.org.apache.xpath.internal.operations.Number(), opPos);
|
|
494 |
}
|
|
495 |
|
|
496 |
/**
|
|
497 |
* Compile a literal string value.
|
|
498 |
*
|
|
499 |
* @param opPos The current position in the m_opMap array.
|
|
500 |
*
|
|
501 |
* @return reference to {@link com.sun.org.apache.xpath.internal.objects.XString} instance.
|
|
502 |
*
|
|
503 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
504 |
*/
|
|
505 |
protected Expression literal(int opPos)
|
|
506 |
{
|
|
507 |
|
|
508 |
opPos = getFirstChildPos(opPos);
|
|
509 |
|
|
510 |
return (XString) getTokenQueue().elementAt(getOp(opPos));
|
|
511 |
}
|
|
512 |
|
|
513 |
/**
|
|
514 |
* Compile a literal number value.
|
|
515 |
*
|
|
516 |
* @param opPos The current position in the m_opMap array.
|
|
517 |
*
|
|
518 |
* @return reference to {@link com.sun.org.apache.xpath.internal.objects.XNumber} instance.
|
|
519 |
*
|
|
520 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
521 |
*/
|
|
522 |
protected Expression numberlit(int opPos)
|
|
523 |
{
|
|
524 |
|
|
525 |
opPos = getFirstChildPos(opPos);
|
|
526 |
|
|
527 |
return (XNumber) getTokenQueue().elementAt(getOp(opPos));
|
|
528 |
}
|
|
529 |
|
|
530 |
/**
|
|
531 |
* Compile a variable reference.
|
|
532 |
*
|
|
533 |
* @param opPos The current position in the m_opMap array.
|
|
534 |
*
|
|
535 |
* @return reference to {@link com.sun.org.apache.xpath.internal.operations.Variable} instance.
|
|
536 |
*
|
|
537 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
538 |
*/
|
|
539 |
protected Expression variable(int opPos) throws TransformerException
|
|
540 |
{
|
|
541 |
|
|
542 |
Variable var = new Variable();
|
|
543 |
|
|
544 |
opPos = getFirstChildPos(opPos);
|
|
545 |
|
|
546 |
int nsPos = getOp(opPos);
|
|
547 |
java.lang.String namespace
|
|
548 |
= (OpCodes.EMPTY == nsPos) ? null
|
|
549 |
: (java.lang.String) getTokenQueue().elementAt(nsPos);
|
|
550 |
java.lang.String localname
|
|
551 |
= (java.lang.String) getTokenQueue().elementAt(getOp(opPos+1));
|
|
552 |
QName qname = new QName(namespace, localname);
|
|
553 |
|
|
554 |
var.setQName(qname);
|
|
555 |
|
|
556 |
return var;
|
|
557 |
}
|
|
558 |
|
|
559 |
/**
|
|
560 |
* Compile an expression group.
|
|
561 |
*
|
|
562 |
* @param opPos The current position in the m_opMap array.
|
|
563 |
*
|
|
564 |
* @return reference to the contained expression.
|
|
565 |
*
|
|
566 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
567 |
*/
|
|
568 |
protected Expression group(int opPos) throws TransformerException
|
|
569 |
{
|
|
570 |
|
|
571 |
// no-op
|
|
572 |
return compile(opPos + 2);
|
|
573 |
}
|
|
574 |
|
|
575 |
/**
|
|
576 |
* Compile a function argument.
|
|
577 |
*
|
|
578 |
* @param opPos The current position in the m_opMap array.
|
|
579 |
*
|
|
580 |
* @return reference to the argument expression.
|
|
581 |
*
|
|
582 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
583 |
*/
|
|
584 |
protected Expression arg(int opPos) throws TransformerException
|
|
585 |
{
|
|
586 |
|
|
587 |
// no-op
|
|
588 |
return compile(opPos + 2);
|
|
589 |
}
|
|
590 |
|
|
591 |
/**
|
|
592 |
* Compile a location path union. The UnionPathIterator itself may create
|
|
593 |
* {@link com.sun.org.apache.xpath.internal.axes.LocPathIterator} children.
|
|
594 |
*
|
|
595 |
* @param opPos The current position in the m_opMap array.
|
|
596 |
*
|
|
597 |
* @return reference to {@link com.sun.org.apache.xpath.internal.axes.LocPathIterator} instance.
|
|
598 |
*
|
|
599 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
600 |
*/
|
|
601 |
protected Expression union(int opPos) throws TransformerException
|
|
602 |
{
|
|
603 |
locPathDepth++;
|
|
604 |
try
|
|
605 |
{
|
|
606 |
return UnionPathIterator.createUnionIterator(this, opPos);
|
|
607 |
}
|
|
608 |
finally
|
|
609 |
{
|
|
610 |
locPathDepth--;
|
|
611 |
}
|
|
612 |
}
|
|
613 |
|
|
614 |
private int locPathDepth = -1;
|
|
615 |
|
|
616 |
/**
|
|
617 |
* Get the level of the location path or union being constructed.
|
|
618 |
* @return 0 if it is a top-level path.
|
|
619 |
*/
|
|
620 |
public int getLocationPathDepth()
|
|
621 |
{
|
|
622 |
return locPathDepth;
|
|
623 |
}
|
|
624 |
|
|
625 |
/**
|
|
626 |
* Get the function table
|
|
627 |
*/
|
|
628 |
FunctionTable getFunctionTable()
|
|
629 |
{
|
|
630 |
return m_functionTable;
|
|
631 |
}
|
|
632 |
|
|
633 |
/**
|
|
634 |
* Compile a location path. The LocPathIterator itself may create
|
|
635 |
* {@link com.sun.org.apache.xpath.internal.axes.AxesWalker} children.
|
|
636 |
*
|
|
637 |
* @param opPos The current position in the m_opMap array.
|
|
638 |
*
|
|
639 |
* @return reference to {@link com.sun.org.apache.xpath.internal.axes.LocPathIterator} instance.
|
|
640 |
*
|
|
641 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
642 |
*/
|
|
643 |
public Expression locationPath(int opPos) throws TransformerException
|
|
644 |
{
|
|
645 |
locPathDepth++;
|
|
646 |
try
|
|
647 |
{
|
|
648 |
DTMIterator iter = WalkerFactory.newDTMIterator(this, opPos, (locPathDepth == 0));
|
|
649 |
return (Expression)iter; // cast OK, I guess.
|
|
650 |
}
|
|
651 |
finally
|
|
652 |
{
|
|
653 |
locPathDepth--;
|
|
654 |
}
|
|
655 |
}
|
|
656 |
|
|
657 |
/**
|
|
658 |
* Compile a location step predicate expression.
|
|
659 |
*
|
|
660 |
* @param opPos The current position in the m_opMap array.
|
|
661 |
*
|
|
662 |
* @return the contained predicate expression.
|
|
663 |
*
|
|
664 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
665 |
*/
|
|
666 |
public Expression predicate(int opPos) throws TransformerException
|
|
667 |
{
|
|
668 |
return compile(opPos + 2);
|
|
669 |
}
|
|
670 |
|
|
671 |
/**
|
|
672 |
* Compile an entire match pattern expression.
|
|
673 |
*
|
|
674 |
* @param opPos The current position in the m_opMap array.
|
|
675 |
*
|
|
676 |
* @return reference to {@link com.sun.org.apache.xpath.internal.patterns.UnionPattern} instance.
|
|
677 |
*
|
|
678 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
679 |
*/
|
|
680 |
protected Expression matchPattern(int opPos) throws TransformerException
|
|
681 |
{
|
|
682 |
locPathDepth++;
|
|
683 |
try
|
|
684 |
{
|
|
685 |
// First, count...
|
|
686 |
int nextOpPos = opPos;
|
|
687 |
int i;
|
|
688 |
|
|
689 |
for (i = 0; getOp(nextOpPos) == OpCodes.OP_LOCATIONPATHPATTERN; i++)
|
|
690 |
{
|
|
691 |
nextOpPos = getNextOpPos(nextOpPos);
|
|
692 |
}
|
|
693 |
|
|
694 |
if (i == 1)
|
|
695 |
return compile(opPos);
|
|
696 |
|
|
697 |
UnionPattern up = new UnionPattern();
|
|
698 |
StepPattern[] patterns = new StepPattern[i];
|
|
699 |
|
|
700 |
for (i = 0; getOp(opPos) == OpCodes.OP_LOCATIONPATHPATTERN; i++)
|
|
701 |
{
|
|
702 |
nextOpPos = getNextOpPos(opPos);
|
|
703 |
patterns[i] = (StepPattern) compile(opPos);
|
|
704 |
opPos = nextOpPos;
|
|
705 |
}
|
|
706 |
|
|
707 |
up.setPatterns(patterns);
|
|
708 |
|
|
709 |
return up;
|
|
710 |
}
|
|
711 |
finally
|
|
712 |
{
|
|
713 |
locPathDepth--;
|
|
714 |
}
|
|
715 |
}
|
|
716 |
|
|
717 |
/**
|
|
718 |
* Compile a location match pattern unit expression.
|
|
719 |
*
|
|
720 |
* @param opPos The current position in the m_opMap array.
|
|
721 |
*
|
|
722 |
* @return reference to {@link com.sun.org.apache.xpath.internal.patterns.StepPattern} instance.
|
|
723 |
*
|
|
724 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
725 |
*/
|
|
726 |
public Expression locationPathPattern(int opPos)
|
|
727 |
throws TransformerException
|
|
728 |
{
|
|
729 |
|
|
730 |
opPos = getFirstChildPos(opPos);
|
|
731 |
|
|
732 |
return stepPattern(opPos, 0, null);
|
|
733 |
}
|
|
734 |
|
|
735 |
/**
|
|
736 |
* Get a {@link org.w3c.dom.traversal.NodeFilter} bit set that tells what
|
|
737 |
* to show for a given node test.
|
|
738 |
*
|
|
739 |
* @param opPos the op map position for the location step.
|
|
740 |
*
|
|
741 |
* @return {@link org.w3c.dom.traversal.NodeFilter} bit set that tells what
|
|
742 |
* to show for a given node test.
|
|
743 |
*/
|
|
744 |
public int getWhatToShow(int opPos)
|
|
745 |
{
|
|
746 |
|
|
747 |
int axesType = getOp(opPos);
|
|
748 |
int testType = getOp(opPos + 3);
|
|
749 |
|
|
750 |
// System.out.println("testType: "+testType);
|
|
751 |
switch (testType)
|
|
752 |
{
|
|
753 |
case OpCodes.NODETYPE_COMMENT :
|
|
754 |
return DTMFilter.SHOW_COMMENT;
|
|
755 |
case OpCodes.NODETYPE_TEXT :
|
|
756 |
// return DTMFilter.SHOW_TEXT | DTMFilter.SHOW_COMMENT;
|
|
757 |
return DTMFilter.SHOW_TEXT | DTMFilter.SHOW_CDATA_SECTION ;
|
|
758 |
case OpCodes.NODETYPE_PI :
|
|
759 |
return DTMFilter.SHOW_PROCESSING_INSTRUCTION;
|
|
760 |
case OpCodes.NODETYPE_NODE :
|
|
761 |
// return DTMFilter.SHOW_ALL;
|
|
762 |
switch (axesType)
|
|
763 |
{
|
|
764 |
case OpCodes.FROM_NAMESPACE:
|
|
765 |
return DTMFilter.SHOW_NAMESPACE;
|
|
766 |
case OpCodes.FROM_ATTRIBUTES :
|
|
767 |
case OpCodes.MATCH_ATTRIBUTE :
|
|
768 |
return DTMFilter.SHOW_ATTRIBUTE;
|
|
769 |
case OpCodes.FROM_SELF:
|
|
770 |
case OpCodes.FROM_ANCESTORS_OR_SELF:
|
|
771 |
case OpCodes.FROM_DESCENDANTS_OR_SELF:
|
|
772 |
return DTMFilter.SHOW_ALL;
|
|
773 |
default:
|
|
774 |
if (getOp(0) == OpCodes.OP_MATCHPATTERN)
|
|
775 |
return ~DTMFilter.SHOW_ATTRIBUTE
|
|
776 |
& ~DTMFilter.SHOW_DOCUMENT
|
|
777 |
& ~DTMFilter.SHOW_DOCUMENT_FRAGMENT;
|
|
778 |
else
|
|
779 |
return ~DTMFilter.SHOW_ATTRIBUTE;
|
|
780 |
}
|
|
781 |
case OpCodes.NODETYPE_ROOT :
|
|
782 |
return DTMFilter.SHOW_DOCUMENT | DTMFilter.SHOW_DOCUMENT_FRAGMENT;
|
|
783 |
case OpCodes.NODETYPE_FUNCTEST :
|
|
784 |
return NodeTest.SHOW_BYFUNCTION;
|
|
785 |
case OpCodes.NODENAME :
|
|
786 |
switch (axesType)
|
|
787 |
{
|
|
788 |
case OpCodes.FROM_NAMESPACE :
|
|
789 |
return DTMFilter.SHOW_NAMESPACE;
|
|
790 |
case OpCodes.FROM_ATTRIBUTES :
|
|
791 |
case OpCodes.MATCH_ATTRIBUTE :
|
|
792 |
return DTMFilter.SHOW_ATTRIBUTE;
|
|
793 |
|
|
794 |
// break;
|
|
795 |
case OpCodes.MATCH_ANY_ANCESTOR :
|
|
796 |
case OpCodes.MATCH_IMMEDIATE_ANCESTOR :
|
|
797 |
return DTMFilter.SHOW_ELEMENT;
|
|
798 |
|
|
799 |
// break;
|
|
800 |
default :
|
|
801 |
return DTMFilter.SHOW_ELEMENT;
|
|
802 |
}
|
|
803 |
default :
|
|
804 |
// System.err.println("We should never reach here.");
|
|
805 |
return DTMFilter.SHOW_ALL;
|
|
806 |
}
|
|
807 |
}
|
|
808 |
|
|
809 |
private static final boolean DEBUG = false;
|
|
810 |
|
|
811 |
/**
|
|
812 |
* Compile a step pattern unit expression, used for both location paths
|
|
813 |
* and match patterns.
|
|
814 |
*
|
|
815 |
* @param opPos The current position in the m_opMap array.
|
|
816 |
* @param stepCount The number of steps to expect.
|
|
817 |
* @param ancestorPattern The owning StepPattern, which may be null.
|
|
818 |
*
|
|
819 |
* @return reference to {@link com.sun.org.apache.xpath.internal.patterns.StepPattern} instance.
|
|
820 |
*
|
|
821 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
822 |
*/
|
|
823 |
protected StepPattern stepPattern(
|
|
824 |
int opPos, int stepCount, StepPattern ancestorPattern)
|
|
825 |
throws TransformerException
|
|
826 |
{
|
|
827 |
|
|
828 |
int startOpPos = opPos;
|
|
829 |
int stepType = getOp(opPos);
|
|
830 |
|
|
831 |
if (OpCodes.ENDOP == stepType)
|
|
832 |
{
|
|
833 |
return null;
|
|
834 |
}
|
|
835 |
|
|
836 |
boolean addMagicSelf = true;
|
|
837 |
|
|
838 |
int endStep = getNextOpPos(opPos);
|
|
839 |
|
|
840 |
// int nextStepType = getOpMap()[endStep];
|
|
841 |
StepPattern pattern;
|
|
842 |
|
|
843 |
// boolean isSimple = ((OpCodes.ENDOP == nextStepType) && (stepCount == 0));
|
|
844 |
int argLen;
|
|
845 |
|
|
846 |
switch (stepType)
|
|
847 |
{
|
|
848 |
case OpCodes.OP_FUNCTION :
|
|
849 |
if(DEBUG)
|
|
850 |
System.out.println("MATCH_FUNCTION: "+m_currentPattern);
|
|
851 |
addMagicSelf = false;
|
|
852 |
argLen = getOp(opPos + OpMap.MAPINDEX_LENGTH);
|
|
853 |
pattern = new FunctionPattern(compileFunction(opPos), Axis.PARENT, Axis.CHILD);
|
|
854 |
break;
|
|
855 |
case OpCodes.FROM_ROOT :
|
|
856 |
if(DEBUG)
|
|
857 |
System.out.println("FROM_ROOT, "+m_currentPattern);
|
|
858 |
addMagicSelf = false;
|
|
859 |
argLen = getArgLengthOfStep(opPos);
|
|
860 |
opPos = getFirstChildPosOfStep(opPos);
|
|
861 |
pattern = new StepPattern(DTMFilter.SHOW_DOCUMENT |
|
|
862 |
DTMFilter.SHOW_DOCUMENT_FRAGMENT,
|
|
863 |
Axis.PARENT, Axis.CHILD);
|
|
864 |
break;
|
|
865 |
case OpCodes.MATCH_ATTRIBUTE :
|
|
866 |
if(DEBUG)
|
|
867 |
System.out.println("MATCH_ATTRIBUTE: "+getStepLocalName(startOpPos)+", "+m_currentPattern);
|
|
868 |
argLen = getArgLengthOfStep(opPos);
|
|
869 |
opPos = getFirstChildPosOfStep(opPos);
|
|
870 |
pattern = new StepPattern(DTMFilter.SHOW_ATTRIBUTE,
|
|
871 |
getStepNS(startOpPos),
|
|
872 |
getStepLocalName(startOpPos),
|
|
873 |
Axis.PARENT, Axis.ATTRIBUTE);
|
|
874 |
break;
|
|
875 |
case OpCodes.MATCH_ANY_ANCESTOR :
|
|
876 |
if(DEBUG)
|
|
877 |
System.out.println("MATCH_ANY_ANCESTOR: "+getStepLocalName(startOpPos)+", "+m_currentPattern);
|
|
878 |
argLen = getArgLengthOfStep(opPos);
|
|
879 |
opPos = getFirstChildPosOfStep(opPos);
|
|
880 |
int what = getWhatToShow(startOpPos);
|
|
881 |
// bit-o-hackery, but this code is due for the morgue anyway...
|
|
882 |
if(0x00000500 == what)
|
|
883 |
addMagicSelf = false;
|
|
884 |
pattern = new StepPattern(getWhatToShow(startOpPos),
|
|
885 |
getStepNS(startOpPos),
|
|
886 |
getStepLocalName(startOpPos),
|
|
887 |
Axis.ANCESTOR, Axis.CHILD);
|
|
888 |
break;
|
|
889 |
case OpCodes.MATCH_IMMEDIATE_ANCESTOR :
|
|
890 |
if(DEBUG)
|
|
891 |
System.out.println("MATCH_IMMEDIATE_ANCESTOR: "+getStepLocalName(startOpPos)+", "+m_currentPattern);
|
|
892 |
argLen = getArgLengthOfStep(opPos);
|
|
893 |
opPos = getFirstChildPosOfStep(opPos);
|
|
894 |
pattern = new StepPattern(getWhatToShow(startOpPos),
|
|
895 |
getStepNS(startOpPos),
|
|
896 |
getStepLocalName(startOpPos),
|
|
897 |
Axis.PARENT, Axis.CHILD);
|
|
898 |
break;
|
|
899 |
default :
|
|
900 |
error(XPATHErrorResources.ER_UNKNOWN_MATCH_OPERATION, null); //"unknown match operation!");
|
|
901 |
|
|
902 |
return null;
|
|
903 |
}
|
|
904 |
|
|
905 |
pattern.setPredicates(getCompiledPredicates(opPos + argLen));
|
|
906 |
if(null == ancestorPattern)
|
|
907 |
{
|
|
908 |
// This is the magic and invisible "." at the head of every
|
|
909 |
// match pattern, and corresponds to the current node in the context
|
|
910 |
// list, from where predicates are counted.
|
|
911 |
// So, in order to calculate "foo[3]", it has to count from the
|
|
912 |
// current node in the context list, so, from that current node,
|
|
913 |
// the full pattern is really "self::node()/child::foo[3]". If you
|
|
914 |
// translate this to a select pattern from the node being tested,
|
|
915 |
// which is really how we're treating match patterns, it works out to
|
|
916 |
// self::foo/parent::node[child::foo[3]]", or close enough.
|
|
917 |
/* if(addMagicSelf && pattern.getPredicateCount() > 0)
|
|
918 |
{
|
|
919 |
StepPattern selfPattern = new StepPattern(DTMFilter.SHOW_ALL,
|
|
920 |
Axis.PARENT, Axis.CHILD);
|
|
921 |
// We need to keep the new nodetest from affecting the score...
|
|
922 |
XNumber score = pattern.getStaticScore();
|
|
923 |
pattern.setRelativePathPattern(selfPattern);
|
|
924 |
pattern.setStaticScore(score);
|
|
925 |
selfPattern.setStaticScore(score);
|
|
926 |
}*/
|
|
927 |
}
|
|
928 |
else
|
|
929 |
{
|
|
930 |
// System.out.println("Setting "+ancestorPattern+" as relative to "+pattern);
|
|
931 |
pattern.setRelativePathPattern(ancestorPattern);
|
|
932 |
}
|
|
933 |
|
|
934 |
StepPattern relativePathPattern = stepPattern(endStep, stepCount + 1,
|
|
935 |
pattern);
|
|
936 |
|
|
937 |
return (null != relativePathPattern) ? relativePathPattern : pattern;
|
|
938 |
}
|
|
939 |
|
|
940 |
/**
|
|
941 |
* Compile a zero or more predicates for a given match pattern.
|
|
942 |
*
|
|
943 |
* @param opPos The position of the first predicate the m_opMap array.
|
|
944 |
*
|
|
945 |
* @return reference to array of {@link com.sun.org.apache.xpath.internal.Expression} instances.
|
|
946 |
*
|
|
947 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
948 |
*/
|
|
949 |
public Expression[] getCompiledPredicates(int opPos)
|
|
950 |
throws TransformerException
|
|
951 |
{
|
|
952 |
|
|
953 |
int count = countPredicates(opPos);
|
|
954 |
|
|
955 |
if (count > 0)
|
|
956 |
{
|
|
957 |
Expression[] predicates = new Expression[count];
|
|
958 |
|
|
959 |
compilePredicates(opPos, predicates);
|
|
960 |
|
|
961 |
return predicates;
|
|
962 |
}
|
|
963 |
|
|
964 |
return null;
|
|
965 |
}
|
|
966 |
|
|
967 |
/**
|
|
968 |
* Count the number of predicates in the step.
|
|
969 |
*
|
|
970 |
* @param opPos The position of the first predicate the m_opMap array.
|
|
971 |
*
|
|
972 |
* @return The number of predicates for this step.
|
|
973 |
*
|
|
974 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
975 |
*/
|
|
976 |
public int countPredicates(int opPos) throws TransformerException
|
|
977 |
{
|
|
978 |
|
|
979 |
int count = 0;
|
|
980 |
|
|
981 |
while (OpCodes.OP_PREDICATE == getOp(opPos))
|
|
982 |
{
|
|
983 |
count++;
|
|
984 |
|
|
985 |
opPos = getNextOpPos(opPos);
|
|
986 |
}
|
|
987 |
|
|
988 |
return count;
|
|
989 |
}
|
|
990 |
|
|
991 |
/**
|
|
992 |
* Compiles predicates in the step.
|
|
993 |
*
|
|
994 |
* @param opPos The position of the first predicate the m_opMap array.
|
|
995 |
* @param predicates An empty pre-determined array of
|
|
996 |
* {@link com.sun.org.apache.xpath.internal.Expression}s, that will be filled in.
|
|
997 |
*
|
|
998 |
* @throws TransformerException
|
|
999 |
*/
|
|
1000 |
private void compilePredicates(int opPos, Expression[] predicates)
|
|
1001 |
throws TransformerException
|
|
1002 |
{
|
|
1003 |
|
|
1004 |
for (int i = 0; OpCodes.OP_PREDICATE == getOp(opPos); i++)
|
|
1005 |
{
|
|
1006 |
predicates[i] = predicate(opPos);
|
|
1007 |
opPos = getNextOpPos(opPos);
|
|
1008 |
}
|
|
1009 |
}
|
|
1010 |
|
|
1011 |
/**
|
|
1012 |
* Compile a built-in XPath function.
|
|
1013 |
*
|
|
1014 |
* @param opPos The current position in the m_opMap array.
|
|
1015 |
*
|
|
1016 |
* @return reference to {@link com.sun.org.apache.xpath.internal.functions.Function} instance.
|
|
1017 |
*
|
|
1018 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
1019 |
*/
|
|
1020 |
Expression compileFunction(int opPos) throws TransformerException
|
|
1021 |
{
|
|
1022 |
|
|
1023 |
int endFunc = opPos + getOp(opPos + 1) - 1;
|
|
1024 |
|
|
1025 |
opPos = getFirstChildPos(opPos);
|
|
1026 |
|
|
1027 |
int funcID = getOp(opPos);
|
|
1028 |
|
|
1029 |
opPos++;
|
|
1030 |
|
|
1031 |
if (-1 != funcID)
|
|
1032 |
{
|
|
1033 |
Function func = m_functionTable.getFunction(funcID);
|
|
1034 |
|
|
1035 |
/**
|
|
1036 |
* It is a trick for function-available. Since the function table is an
|
|
1037 |
* instance field, insert this table at compilation time for later usage
|
|
1038 |
*/
|
|
1039 |
|
|
1040 |
if (func instanceof FuncExtFunctionAvailable)
|
|
1041 |
((FuncExtFunctionAvailable) func).setFunctionTable(m_functionTable);
|
|
1042 |
|
|
1043 |
func.postCompileStep(this);
|
|
1044 |
|
|
1045 |
try
|
|
1046 |
{
|
|
1047 |
int i = 0;
|
|
1048 |
|
|
1049 |
for (int p = opPos; p < endFunc; p = getNextOpPos(p), i++)
|
|
1050 |
{
|
|
1051 |
|
|
1052 |
// System.out.println("argPos: "+ p);
|
|
1053 |
// System.out.println("argCode: "+ m_opMap[p]);
|
|
1054 |
func.setArg(compile(p), i);
|
|
1055 |
}
|
|
1056 |
|
|
1057 |
func.checkNumberArgs(i);
|
|
1058 |
}
|
|
1059 |
catch (WrongNumberArgsException wnae)
|
|
1060 |
{
|
|
1061 |
java.lang.String name = m_functionTable.getFunctionName(funcID);
|
|
1062 |
|
|
1063 |
m_errorHandler.fatalError( new TransformerException(
|
|
1064 |
XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ONLY_ALLOWS,
|
|
1065 |
new Object[]{name, wnae.getMessage()}), m_locator));
|
|
1066 |
//"name + " only allows " + wnae.getMessage() + " arguments", m_locator));
|
|
1067 |
}
|
|
1068 |
|
|
1069 |
return func;
|
|
1070 |
}
|
|
1071 |
else
|
|
1072 |
{
|
|
1073 |
error(XPATHErrorResources.ER_FUNCTION_TOKEN_NOT_FOUND, null); //"function token not found.");
|
|
1074 |
|
|
1075 |
return null;
|
|
1076 |
}
|
|
1077 |
}
|
|
1078 |
|
|
1079 |
// The current id for extension functions.
|
|
1080 |
private static long s_nextMethodId = 0;
|
|
1081 |
|
|
1082 |
/**
|
|
1083 |
* Get the next available method id
|
|
1084 |
*/
|
|
1085 |
synchronized private long getNextMethodId()
|
|
1086 |
{
|
|
1087 |
if (s_nextMethodId == Long.MAX_VALUE)
|
|
1088 |
s_nextMethodId = 0;
|
|
1089 |
|
|
1090 |
return s_nextMethodId++;
|
|
1091 |
}
|
|
1092 |
|
|
1093 |
/**
|
|
1094 |
* Compile an extension function.
|
|
1095 |
*
|
|
1096 |
* @param opPos The current position in the m_opMap array.
|
|
1097 |
*
|
|
1098 |
* @return reference to {@link com.sun.org.apache.xpath.internal.functions.FuncExtFunction} instance.
|
|
1099 |
*
|
|
1100 |
* @throws TransformerException if a error occurs creating the Expression.
|
|
1101 |
*/
|
|
1102 |
private Expression compileExtension(int opPos)
|
|
1103 |
throws TransformerException
|
|
1104 |
{
|
|
1105 |
|
|
1106 |
int endExtFunc = opPos + getOp(opPos + 1) - 1;
|
|
1107 |
|
|
1108 |
opPos = getFirstChildPos(opPos);
|
|
1109 |
|
|
1110 |
java.lang.String ns = (java.lang.String) getTokenQueue().elementAt(getOp(opPos));
|
|
1111 |
|
|
1112 |
opPos++;
|
|
1113 |
|
|
1114 |
java.lang.String funcName =
|
|
1115 |
(java.lang.String) getTokenQueue().elementAt(getOp(opPos));
|
|
1116 |
|
|
1117 |
opPos++;
|
|
1118 |
|
|
1119 |
// We create a method key to uniquely identify this function so that we
|
|
1120 |
// can cache the object needed to invoke it. This way, we only pay the
|
|
1121 |
// reflection overhead on the first call.
|
|
1122 |
|
|
1123 |
Function extension = new FuncExtFunction(ns, funcName, String.valueOf(getNextMethodId()));
|
|
1124 |
|
|
1125 |
try
|
|
1126 |
{
|
|
1127 |
int i = 0;
|
|
1128 |
|
|
1129 |
while (opPos < endExtFunc)
|
|
1130 |
{
|
|
1131 |
int nextOpPos = getNextOpPos(opPos);
|
|
1132 |
|
|
1133 |
extension.setArg(this.compile(opPos), i);
|
|
1134 |
|
|
1135 |
opPos = nextOpPos;
|
|
1136 |
|
|
1137 |
i++;
|
|
1138 |
}
|
|
1139 |
}
|
|
1140 |
catch (WrongNumberArgsException wnae)
|
|
1141 |
{
|
|
1142 |
; // should never happen
|
|
1143 |
}
|
|
1144 |
|
|
1145 |
return extension;
|
|
1146 |
}
|
|
1147 |
|
|
1148 |
/**
|
|
1149 |
* Warn the user of an problem.
|
|
1150 |
*
|
|
1151 |
* @param msg An error msgkey that corresponds to one of the constants found
|
|
1152 |
* in {@link com.sun.org.apache.xpath.internal.res.XPATHErrorResources}, which is
|
|
1153 |
* a key for a format string.
|
|
1154 |
* @param args An array of arguments represented in the format string, which
|
|
1155 |
* may be null.
|
|
1156 |
*
|
|
1157 |
* @throws TransformerException if the current ErrorListoner determines to
|
|
1158 |
* throw an exception.
|
|
1159 |
*/
|
|
1160 |
public void warn(String msg, Object[] args) throws TransformerException
|
|
1161 |
{
|
|
1162 |
|
|
1163 |
java.lang.String fmsg = XSLMessages.createXPATHWarning(msg, args);
|
|
1164 |
|
|
1165 |
if (null != m_errorHandler)
|
|
1166 |
{
|
|
1167 |
m_errorHandler.warning(new TransformerException(fmsg, m_locator));
|
|
1168 |
}
|
|
1169 |
else
|
|
1170 |
{
|
|
1171 |
System.out.println(fmsg
|
|
1172 |
+"; file "+m_locator.getSystemId()
|
|
1173 |
+"; line "+m_locator.getLineNumber()
|
|
1174 |
+"; column "+m_locator.getColumnNumber());
|
|
1175 |
}
|
|
1176 |
}
|
|
1177 |
|
|
1178 |
/**
|
|
1179 |
* Tell the user of an assertion error, and probably throw an
|
|
1180 |
* exception.
|
|
1181 |
*
|
|
1182 |
* @param b If false, a runtime exception will be thrown.
|
|
1183 |
* @param msg The assertion message, which should be informative.
|
|
1184 |
*
|
|
1185 |
* @throws RuntimeException if the b argument is false.
|
|
1186 |
*/
|
|
1187 |
public void assertion(boolean b, java.lang.String msg)
|
|
1188 |
{
|
|
1189 |
|
|
1190 |
if (!b)
|
|
1191 |
{
|
|
1192 |
java.lang.String fMsg = XSLMessages.createXPATHMessage(
|
|
1193 |
XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION,
|
|
1194 |
new Object[]{ msg });
|
|
1195 |
|
|
1196 |
throw new RuntimeException(fMsg);
|
|
1197 |
}
|
|
1198 |
}
|
|
1199 |
|
|
1200 |
/**
|
|
1201 |
* Tell the user of an error, and probably throw an
|
|
1202 |
* exception.
|
|
1203 |
*
|
|
1204 |
* @param msg An error msgkey that corresponds to one of the constants found
|
|
1205 |
* in {@link com.sun.org.apache.xpath.internal.res.XPATHErrorResources}, which is
|
|
1206 |
* a key for a format string.
|
|
1207 |
* @param args An array of arguments represented in the format string, which
|
|
1208 |
* may be null.
|
|
1209 |
*
|
|
1210 |
* @throws TransformerException if the current ErrorListoner determines to
|
|
1211 |
* throw an exception.
|
|
1212 |
*/
|
|
1213 |
public void error(String msg, Object[] args) throws TransformerException
|
|
1214 |
{
|
|
1215 |
|
|
1216 |
java.lang.String fmsg = XSLMessages.createXPATHMessage(msg, args);
|
|
1217 |
|
|
1218 |
|
|
1219 |
if (null != m_errorHandler)
|
|
1220 |
{
|
|
1221 |
m_errorHandler.fatalError(new TransformerException(fmsg, m_locator));
|
|
1222 |
}
|
|
1223 |
else
|
|
1224 |
{
|
|
1225 |
|
|
1226 |
// System.out.println(te.getMessage()
|
|
1227 |
// +"; file "+te.getSystemId()
|
|
1228 |
// +"; line "+te.getLineNumber()
|
|
1229 |
// +"; column "+te.getColumnNumber());
|
|
1230 |
throw new TransformerException(fmsg, (SAXSourceLocator)m_locator);
|
|
1231 |
}
|
|
1232 |
}
|
|
1233 |
|
|
1234 |
/**
|
|
1235 |
* The current prefixResolver for the execution context.
|
|
1236 |
*/
|
|
1237 |
private PrefixResolver m_currentPrefixResolver = null;
|
|
1238 |
|
|
1239 |
/**
|
|
1240 |
* Get the current namespace context for the xpath.
|
|
1241 |
*
|
|
1242 |
* @return The current prefix resolver, *may* be null, though hopefully not.
|
|
1243 |
*/
|
|
1244 |
public PrefixResolver getNamespaceContext()
|
|
1245 |
{
|
|
1246 |
return m_currentPrefixResolver;
|
|
1247 |
}
|
|
1248 |
|
|
1249 |
/**
|
|
1250 |
* Set the current namespace context for the xpath.
|
|
1251 |
*
|
|
1252 |
* @param pr The resolver for prefixes in the XPath expression.
|
|
1253 |
*/
|
|
1254 |
public void setNamespaceContext(PrefixResolver pr)
|
|
1255 |
{
|
|
1256 |
m_currentPrefixResolver = pr;
|
|
1257 |
}
|
|
1258 |
|
|
1259 |
/** The error listener where errors will be sent. If this is null, errors
|
|
1260 |
* and warnings will be sent to System.err. May be null. */
|
|
1261 |
ErrorListener m_errorHandler;
|
|
1262 |
|
|
1263 |
/** The source locator for the expression being compiled. May be null. */
|
|
1264 |
SourceLocator m_locator;
|
|
1265 |
|
|
1266 |
/**
|
|
1267 |
* The FunctionTable for all xpath build-in functions
|
|
1268 |
*/
|
|
1269 |
private FunctionTable m_functionTable;
|
|
1270 |
}
|