001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: StartsWithCall.java 468650 2006-10-28 07:03:30Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.compiler;
023    
024    import java.util.Vector;
025    
026    import org.apache.bcel.generic.ConstantPoolGen;
027    import org.apache.bcel.generic.INVOKEVIRTUAL;
028    import org.apache.bcel.generic.InstructionList;
029    import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
030    import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
031    import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
032    import org.apache.xalan.xsltc.compiler.util.Type;
033    import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
034    
035    /**
036     * @author Jacek Ambroziak
037     * @author Santiago Pericas-Geertsen
038     * @author Morten Jorgensen
039     */
040    final class StartsWithCall extends FunctionCall {
041    
042        private Expression _base = null;
043        private Expression _token = null;
044    
045        /**
046         * Create a starts-with() call - two arguments, both strings
047         */
048        public StartsWithCall(QName fname, Vector arguments) {
049            super(fname, arguments);
050        }
051    
052        /**
053         * Type check the two parameters for this function
054         */
055        public Type typeCheck(SymbolTable stable) throws TypeCheckError {
056    
057            // Check that the function was passed exactly two arguments
058            if (argumentCount() != 2) {
059                ErrorMsg err = new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR,
060                                            getName(), this);
061                throw new TypeCheckError(err);
062            }
063    
064            // The first argument must be a String, or cast to a String
065            _base = argument(0);
066            Type baseType = _base.typeCheck(stable);        
067            if (baseType != Type.String)
068                _base = new CastExpr(_base, Type.String);
069    
070            // The second argument must also be a String, or cast to a String
071            _token = argument(1);
072            Type tokenType = _token.typeCheck(stable);      
073            if (tokenType != Type.String)
074                _token = new CastExpr(_token, Type.String);
075    
076            return _type = Type.Boolean;
077        }
078    
079        /**
080         * Compile the expression - leave boolean expression on stack
081         */
082        public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
083            final ConstantPoolGen cpg = classGen.getConstantPool();
084            final InstructionList il = methodGen.getInstructionList();
085            _base.translate(classGen, methodGen);
086            _token.translate(classGen, methodGen);
087            il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
088                                                         "startsWith", 
089                                                         "("+STRING_SIG+")Z")));
090        }
091    }