1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001-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: StartsWithCall.java,v 1.2.4.1 2005/09/05 09:05:28 pvedula Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
  25 
  26 import java.util.Vector;
  27 
  28 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  29 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  30 import com.sun.org.apache.bcel.internal.generic.InstructionList;
  31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  36 
  37 /**
  38  * @author Jacek Ambroziak
  39  * @author Santiago Pericas-Geertsen
  40  * @author Morten Jorgensen
  41  */
  42 final class StartsWithCall extends FunctionCall {
  43 
  44     private Expression _base = null;
  45     private Expression _token = null;
  46 
  47     /**
  48      * Create a starts-with() call - two arguments, both strings
  49      */
  50     public StartsWithCall(QName fname, Vector arguments) {
  51         super(fname, arguments);
  52     }
  53 
  54     /**
  55      * Type check the two parameters for this function
  56      */
  57     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  58 
  59         // Check that the function was passed exactly two arguments
  60         if (argumentCount() != 2) {
  61             ErrorMsg err = new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR,
  62                                         getName(), this);
  63             throw new TypeCheckError(err);
  64         }
  65 
  66         // The first argument must be a String, or cast to a String
  67         _base = argument(0);
  68         Type baseType = _base.typeCheck(stable);
  69         if (baseType != Type.String)
  70             _base = new CastExpr(_base, Type.String);
  71 
  72         // The second argument must also be a String, or cast to a String
  73         _token = argument(1);
  74         Type tokenType = _token.typeCheck(stable);
  75         if (tokenType != Type.String)
  76             _token = new CastExpr(_token, Type.String);
  77 
  78         return _type = Type.Boolean;
  79     }
  80 
  81     /**
  82      * Compile the expression - leave boolean expression on stack
  83      */
  84     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  85         final ConstantPoolGen cpg = classGen.getConstantPool();
  86         final InstructionList il = methodGen.getInstructionList();
  87         _base.translate(classGen, methodGen);
  88         _token.translate(classGen, methodGen);
  89         il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
  90                                                      "startsWith",
  91                                                      "("+STRING_SIG+")Z")));
  92     }
  93 }