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: ContainsCall.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.IFLT;
028    import org.apache.bcel.generic.INVOKEVIRTUAL;
029    import org.apache.bcel.generic.InstructionList;
030    import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
031    import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
032    import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
033    import org.apache.xalan.xsltc.compiler.util.Type;
034    import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
035    
036    /**
037     * @author Jacek Ambroziak
038     * @author Santiago Pericas-Geertsen
039     * @author Morten Jorgensen
040     */
041    final class ContainsCall extends FunctionCall {
042    
043        private Expression _base = null;
044        private Expression _token = null;
045    
046        /**
047         * Create a contains() call - two arguments, both strings
048         */
049        public ContainsCall(QName fname, Vector arguments) {
050            super(fname, arguments);
051        }
052    
053        /**
054         * This XPath function returns true/false values
055         */
056        public boolean isBoolean() {
057            return true;
058        }
059    
060        /**
061         * Type check the two parameters for this function
062         */
063        public Type typeCheck(SymbolTable stable) throws TypeCheckError {
064    
065            // Check that the function was passed exactly two arguments
066            if (argumentCount() != 2) {
067                throw new TypeCheckError(ErrorMsg.ILLEGAL_ARG_ERR, getName(), this);
068            }
069    
070            // The first argument must be a String, or cast to a String
071            _base = argument(0);
072            Type baseType = _base.typeCheck(stable);        
073            if (baseType != Type.String)
074                _base = new CastExpr(_base, Type.String);
075    
076            // The second argument must also be a String, or cast to a String
077            _token = argument(1);
078            Type tokenType = _token.typeCheck(stable);      
079            if (tokenType != Type.String)
080                _token = new CastExpr(_token, Type.String);
081    
082            return _type = Type.Boolean;
083        }
084    
085        /**
086         * Compile the expression - leave boolean expression on stack
087         */
088        public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
089            translateDesynthesized(classGen, methodGen);
090            synthesize(classGen, methodGen);
091        }
092    
093        /**
094         * Compile expression and update true/false-lists
095         */
096        public void translateDesynthesized(ClassGenerator classGen,
097                                           MethodGenerator methodGen) {
098            final ConstantPoolGen cpg = classGen.getConstantPool();
099            final InstructionList il = methodGen.getInstructionList();
100            _base.translate(classGen, methodGen);
101            _token.translate(classGen, methodGen);
102            il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
103                                                         "indexOf",
104                                                         "("+STRING_SIG+")I")));
105            _falseList.add(il.append(new IFLT(null)));
106        }
107    }