KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > ContainsCall


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: ContainsCall.java,v 1.7 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import java.util.Vector JavaDoc;
23
24 import org.apache.bcel.generic.ConstantPoolGen;
25 import org.apache.bcel.generic.IFLT;
26 import org.apache.bcel.generic.INVOKEVIRTUAL;
27 import org.apache.bcel.generic.InstructionList;
28 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
29 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
30 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
31 import org.apache.xalan.xsltc.compiler.util.Type;
32 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
33
34 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  * @author Morten Jorgensen
38  */

39 final class ContainsCall extends FunctionCall {
40
41     private Expression _base = null;
42     private Expression _token = null;
43
44     /**
45      * Create a contains() call - two arguments, both strings
46      */

47     public ContainsCall(QName fname, Vector JavaDoc arguments) {
48     super(fname, arguments);
49     }
50
51     /**
52      * This XPath function returns true/false values
53      */

54     public boolean isBoolean() {
55     return true;
56     }
57
58     /**
59      * Type check the two parameters for this function
60      */

61     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
62
63     // Check that the function was passed exactly two arguments
64
if (argumentCount() != 2) {
65         throw new TypeCheckError(ErrorMsg.ILLEGAL_ARG_ERR, getName(), this);
66     }
67
68     // The first argument must be a String, or cast to a String
69
_base = argument(0);
70     Type baseType = _base.typeCheck(stable);
71     if (baseType != Type.String)
72         _base = new CastExpr(_base, Type.String);
73
74     // The second argument must also be a String, or cast to a String
75
_token = argument(1);
76     Type tokenType = _token.typeCheck(stable);
77     if (tokenType != Type.String)
78         _token = new CastExpr(_token, Type.String);
79
80     return _type = Type.Boolean;
81     }
82
83     /**
84      * Compile the expression - leave boolean expression on stack
85      */

86     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
87     translateDesynthesized(classGen, methodGen);
88     synthesize(classGen, methodGen);
89     }
90
91     /**
92      * Compile expression and update true/false-lists
93      */

94     public void translateDesynthesized(ClassGenerator classGen,
95                        MethodGenerator methodGen) {
96     final ConstantPoolGen cpg = classGen.getConstantPool();
97     final InstructionList il = methodGen.getInstructionList();
98     _base.translate(classGen, methodGen);
99     _token.translate(classGen, methodGen);
100     il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
101                              "indexOf",
102                              "("+STRING_SIG+")I")));
103     _falseList.add(il.append(new IFLT(null)));
104     }
105 }
106
Popular Tags