View Javadoc
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: ContainsCall.java,v 1.2.4.1 2005/09/01 12:12:06 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.IFLT;
30  import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
31  import com.sun.org.apache.bcel.internal.generic.InstructionList;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
33  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
37  
38  /**
39   * @author Jacek Ambroziak
40   * @author Santiago Pericas-Geertsen
41   * @author Morten Jorgensen
42   */
43  final class ContainsCall extends FunctionCall {
44  
45      private Expression _base = null;
46      private Expression _token = null;
47  
48      /**
49       * Create a contains() call - two arguments, both strings
50       */
51      public ContainsCall(QName fname, Vector arguments) {
52          super(fname, arguments);
53      }
54  
55      /**
56       * This XPath function returns true/false values
57       */
58      public boolean isBoolean() {
59          return true;
60      }
61  
62      /**
63       * Type check the two parameters for this function
64       */
65      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
66  
67          // Check that the function was passed exactly two arguments
68          if (argumentCount() != 2) {
69              throw new TypeCheckError(ErrorMsg.ILLEGAL_ARG_ERR, getName(), this);
70          }
71  
72          // The first argument must be a String, or cast to a String
73          _base = argument(0);
74          Type baseType = _base.typeCheck(stable);
75          if (baseType != Type.String)
76              _base = new CastExpr(_base, Type.String);
77  
78          // The second argument must also be a String, or cast to a String
79          _token = argument(1);
80          Type tokenType = _token.typeCheck(stable);
81          if (tokenType != Type.String)
82              _token = new CastExpr(_token, Type.String);
83  
84          return _type = Type.Boolean;
85      }
86  
87      /**
88       * Compile the expression - leave boolean expression on stack
89       */
90      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
91          translateDesynthesized(classGen, methodGen);
92          synthesize(classGen, methodGen);
93      }
94  
95      /**
96       * Compile expression and update true/false-lists
97       */
98      public void translateDesynthesized(ClassGenerator classGen,
99                                         MethodGenerator methodGen) {
100         final ConstantPoolGen cpg = classGen.getConstantPool();
101         final InstructionList il = methodGen.getInstructionList();
102         _base.translate(classGen, methodGen);
103         _token.translate(classGen, methodGen);
104         il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
105                                                      "indexOf",
106                                                      "("+STRING_SIG+")I")));
107         _falseList.add(il.append(new IFLT(null)));
108     }
109 }