KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > ElementAvailableCall


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import java.util.Vector JavaDoc;
23
24 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
25 import com.sun.org.apache.bcel.internal.generic.PUSH;
26 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
30 import com.sun.org.apache.xalan.internal.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 JavaDoc 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 JavaDoc 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 JavaDoc qname = arg.getValue();
70         final int index = qname.indexOf(':');
71         final String JavaDoc localName = (index > 0) ?
72         qname.substring(index + 1) : qname;
73         return getParser().elementSupported(arg.getNamespace(),
74                             localName);
75     }
76     catch (ClassCastException JavaDoc e) {
77         return false;
78     }
79     }
80
81     /**
82      * Calls to 'element-available' are resolved at compile time since
83      * the namespaces declared in the stylsheet are not available at run
84      * time. Consequently, arguments to this function must be literals.
85      */

86     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
87     final ConstantPoolGen cpg = classGen.getConstantPool();
88     final boolean result = getResult();
89     methodGen.getInstructionList().append(new PUSH(cpg, result));
90     }
91 }
92
Popular Tags