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