1. /*
  2. * Copyright 1999-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: Function2Args.java,v 1.13 2004/02/17 04:34:01 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.functions;
  20. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  21. import com.sun.org.apache.xpath.internal.Expression;
  22. import com.sun.org.apache.xpath.internal.ExpressionOwner;
  23. import com.sun.org.apache.xpath.internal.XPathVisitor;
  24. /**
  25. * Base class for functions that accept two arguments.
  26. * @xsl.usage advanced
  27. */
  28. public class Function2Args extends FunctionOneArg
  29. {
  30. /** The second argument passed to the function (at index 1).
  31. * @serial */
  32. Expression m_arg1;
  33. /**
  34. * Return the second argument passed to the function (at index 1).
  35. *
  36. * @return An expression that represents the second argument passed to the
  37. * function.
  38. */
  39. public Expression getArg1()
  40. {
  41. return m_arg1;
  42. }
  43. /**
  44. * This function is used to fixup variables from QNames to stack frame
  45. * indexes at stylesheet build time.
  46. * @param vars List of QNames that correspond to variables. This list
  47. * should be searched backwards for the first qualified name that
  48. * corresponds to the variable reference qname. The position of the
  49. * QName in the vector from the start of the vector will be its position
  50. * in the stack frame (but variables above the globalsTop value will need
  51. * to be offset to the current stack frame).
  52. */
  53. public void fixupVariables(java.util.Vector vars, int globalsSize)
  54. {
  55. super.fixupVariables(vars, globalsSize);
  56. if(null != m_arg1)
  57. m_arg1.fixupVariables(vars, globalsSize);
  58. }
  59. /**
  60. * Set an argument expression for a function. This method is called by the
  61. * XPath compiler.
  62. *
  63. * @param arg non-null expression that represents the argument.
  64. * @param argNum The argument number index.
  65. *
  66. * @throws WrongNumberArgsException If the argNum parameter is greater than 1.
  67. */
  68. public void setArg(Expression arg, int argNum)
  69. throws WrongNumberArgsException
  70. {
  71. // System.out.println("argNum: "+argNum);
  72. if (argNum == 0)
  73. super.setArg(arg, argNum);
  74. else if (1 == argNum)
  75. {
  76. m_arg1 = arg;
  77. arg.exprSetParent(this);
  78. }
  79. else
  80. reportWrongNumberArgs();
  81. }
  82. /**
  83. * Check that the number of arguments passed to this function is correct.
  84. *
  85. *
  86. * @param argNum The number of arguments that is being passed to the function.
  87. *
  88. * @throws WrongNumberArgsException
  89. */
  90. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  91. {
  92. if (argNum != 2)
  93. reportWrongNumberArgs();
  94. }
  95. /**
  96. * Constructs and throws a WrongNumberArgException with the appropriate
  97. * message for this function object.
  98. *
  99. * @throws WrongNumberArgsException
  100. */
  101. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  102. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("two", null));
  103. }
  104. /**
  105. * Tell if this expression or it's subexpressions can traverse outside
  106. * the current subtree.
  107. *
  108. * @return true if traversal outside the context node's subtree can occur.
  109. */
  110. public boolean canTraverseOutsideSubtree()
  111. {
  112. return super.canTraverseOutsideSubtree()
  113. ? true : m_arg1.canTraverseOutsideSubtree();
  114. }
  115. class Arg1Owner implements ExpressionOwner
  116. {
  117. /**
  118. * @see ExpressionOwner#getExpression()
  119. */
  120. public Expression getExpression()
  121. {
  122. return m_arg1;
  123. }
  124. /**
  125. * @see ExpressionOwner#setExpression(Expression)
  126. */
  127. public void setExpression(Expression exp)
  128. {
  129. exp.exprSetParent(Function2Args.this);
  130. m_arg1 = exp;
  131. }
  132. }
  133. /**
  134. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  135. */
  136. public void callArgVisitors(XPathVisitor visitor)
  137. {
  138. super.callArgVisitors(visitor);
  139. if(null != m_arg1)
  140. m_arg1.callVisitors(new Arg1Owner(), visitor);
  141. }
  142. /**
  143. * @see Expression#deepEquals(Expression)
  144. */
  145. public boolean deepEquals(Expression expr)
  146. {
  147. if(!super.deepEquals(expr))
  148. return false;
  149. if(null != m_arg1)
  150. {
  151. if(null == ((Function2Args)expr).m_arg1)
  152. return false;
  153. if(!m_arg1.deepEquals(((Function2Args)expr).m_arg1))
  154. return false;
  155. }
  156. else if(null != ((Function2Args)expr).m_arg1)
  157. return false;
  158. return true;
  159. }
  160. }