KickJava   Java API By Example, From Geeks To Geeks.

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


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: StartsWithCall.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.INVOKEVIRTUAL;
26 import org.apache.bcel.generic.InstructionList;
27 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
28 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
29 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
30 import org.apache.xalan.xsltc.compiler.util.Type;
31 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
32
33 /**
34  * @author Jacek Ambroziak
35  * @author Santiago Pericas-Geertsen
36  * @author Morten Jorgensen
37  */

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

46     public StartsWithCall(QName fname, Vector JavaDoc arguments) {
47     super(fname, arguments);
48     }
49
50     /**
51      * Type check the two parameters for this function
52      */

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

80     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
81     final ConstantPoolGen cpg = classGen.getConstantPool();
82     final InstructionList il = methodGen.getInstructionList();
83     _base.translate(classGen, methodGen);
84     _token.translate(classGen, methodGen);
85     il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
86                              "startsWith",
87                              "("+STRING_SIG+")Z")));
88     }
89 }
90
Popular Tags