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: Function3Args.java,v 1.14 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 three arguments.
  26. * @xsl.usage advanced
  27. */
  28. public class Function3Args extends Function2Args
  29. {
  30. /** The third argument passed to the function (at index 2).
  31. * @serial */
  32. Expression m_arg2;
  33. /**
  34. * Return the third argument passed to the function (at index 2).
  35. *
  36. * @return An expression that represents the third argument passed to the
  37. * function.
  38. */
  39. public Expression getArg2()
  40. {
  41. return m_arg2;
  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_arg2)
  57. m_arg2.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 2.
  67. */
  68. public void setArg(Expression arg, int argNum)
  69. throws WrongNumberArgsException
  70. {
  71. if (argNum < 2)
  72. super.setArg(arg, argNum);
  73. else if (2 == argNum)
  74. {
  75. m_arg2 = arg;
  76. arg.exprSetParent(this);
  77. }
  78. else
  79. reportWrongNumberArgs();
  80. }
  81. /**
  82. * Check that the number of arguments passed to this function is correct.
  83. *
  84. *
  85. * @param argNum The number of arguments that is being passed to the function.
  86. *
  87. * @throws WrongNumberArgsException
  88. */
  89. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  90. {
  91. if (argNum != 3)
  92. reportWrongNumberArgs();
  93. }
  94. /**
  95. * Constructs and throws a WrongNumberArgException with the appropriate
  96. * message for this function object.
  97. *
  98. * @throws WrongNumberArgsException
  99. */
  100. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  101. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("three", null));
  102. }
  103. /**
  104. * Tell if this expression or it's subexpressions can traverse outside
  105. * the current subtree.
  106. *
  107. * @return true if traversal outside the context node's subtree can occur.
  108. */
  109. public boolean canTraverseOutsideSubtree()
  110. {
  111. return super.canTraverseOutsideSubtree()
  112. ? true : m_arg2.canTraverseOutsideSubtree();
  113. }
  114. class Arg2Owner implements ExpressionOwner
  115. {
  116. /**
  117. * @see ExpressionOwner#getExpression()
  118. */
  119. public Expression getExpression()
  120. {
  121. return m_arg2;
  122. }
  123. /**
  124. * @see ExpressionOwner#setExpression(Expression)
  125. */
  126. public void setExpression(Expression exp)
  127. {
  128. exp.exprSetParent(Function3Args.this);
  129. m_arg2 = exp;
  130. }
  131. }
  132. /**
  133. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  134. */
  135. public void callArgVisitors(XPathVisitor visitor)
  136. {
  137. super.callArgVisitors(visitor);
  138. if(null != m_arg2)
  139. m_arg2.callVisitors(new Arg2Owner(), visitor);
  140. }
  141. /**
  142. * @see Expression#deepEquals(Expression)
  143. */
  144. public boolean deepEquals(Expression expr)
  145. {
  146. if(!super.deepEquals(expr))
  147. return false;
  148. if(null != m_arg2)
  149. {
  150. if(null == ((Function3Args)expr).m_arg2)
  151. return false;
  152. if(!m_arg2.deepEquals(((Function3Args)expr).m_arg2))
  153. return false;
  154. }
  155. else if (null != ((Function3Args)expr).m_arg2)
  156. return false;
  157. return true;
  158. }
  159. }