01: /*
02: * Copyright 2001-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: /*
17: * $Id: ElementAvailableCall.java,v 1.10 2004/02/16 22:24:28 minchau Exp $
18: */
19:
20: package org.apache.xalan.xsltc.compiler;
21:
22: import java.util.Vector;
23:
24: import org.apache.bcel.generic.ConstantPoolGen;
25: import org.apache.bcel.generic.PUSH;
26: import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
27: import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
28: import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
29: import org.apache.xalan.xsltc.compiler.util.Type;
30: import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
31:
32: /**
33: * @author Jacek Ambroziak
34: * @author Santiago Pericas-Geertsen
35: */
36: final class ElementAvailableCall extends FunctionCall {
37:
38: public ElementAvailableCall(QName fname, Vector arguments) {
39: super (fname, arguments);
40: }
41:
42: /**
43: * Force the argument to this function to be a literal string.
44: */
45: public Type typeCheck(SymbolTable stable) throws TypeCheckError {
46: if (argument() instanceof LiteralExpr) {
47: return _type = Type.Boolean;
48: }
49: ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
50: "element-available", this );
51: throw new TypeCheckError(err);
52: }
53:
54: /**
55: * Returns an object representing the compile-time evaluation
56: * of an expression. We are only using this for function-available
57: * and element-available at this time.
58: */
59: public Object evaluateAtCompileTime() {
60: return getResult() ? Boolean.TRUE : Boolean.FALSE;
61: }
62:
63: /**
64: * Returns the result that this function will return
65: */
66: public boolean getResult() {
67: try {
68: final LiteralExpr arg = (LiteralExpr) argument();
69: final String qname = arg.getValue();
70: final int index = qname.indexOf(':');
71: final String localName = (index > 0) ? qname
72: .substring(index + 1) : qname;
73: return getParser().elementSupported(arg.getNamespace(),
74: localName);
75: } catch (ClassCastException e) {
76: return false;
77: }
78: }
79:
80: /**
81: * Calls to 'element-available' are resolved at compile time since
82: * the namespaces declared in the stylsheet are not available at run
83: * time. Consequently, arguments to this function must be literals.
84: */
85: public void translate(ClassGenerator classGen,
86: MethodGenerator methodGen) {
87: final ConstantPoolGen cpg = classGen.getConstantPool();
88: final boolean result = getResult();
89: methodGen.getInstructionList().append(new PUSH(cpg, result));
90: }
91: }
|