1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.functions;
  58. import java.util.Vector;
  59. import org.apache.xalan.res.XSLMessages;
  60. import org.apache.xpath.Expression;
  61. import org.apache.xpath.ExpressionOwner;
  62. import org.apache.xpath.XPathVisitor;
  63. /**
  64. * <meta name="usage" content="advanced"/>
  65. * Base class for functions that accept three arguments.
  66. */
  67. public class Function3Args extends Function2Args
  68. {
  69. /** The third argument passed to the function (at index 2).
  70. * @serial */
  71. Expression m_arg2;
  72. /**
  73. * Return the third argument passed to the function (at index 2).
  74. *
  75. * @return An expression that represents the third argument passed to the
  76. * function.
  77. */
  78. public Expression getArg2()
  79. {
  80. return m_arg2;
  81. }
  82. /**
  83. * This function is used to fixup variables from QNames to stack frame
  84. * indexes at stylesheet build time.
  85. * @param vars List of QNames that correspond to variables. This list
  86. * should be searched backwards for the first qualified name that
  87. * corresponds to the variable reference qname. The position of the
  88. * QName in the vector from the start of the vector will be its position
  89. * in the stack frame (but variables above the globalsTop value will need
  90. * to be offset to the current stack frame).
  91. */
  92. public void fixupVariables(java.util.Vector vars, int globalsSize)
  93. {
  94. super.fixupVariables(vars, globalsSize);
  95. if(null != m_arg2)
  96. m_arg2.fixupVariables(vars, globalsSize);
  97. }
  98. /**
  99. * Set an argument expression for a function. This method is called by the
  100. * XPath compiler.
  101. *
  102. * @param arg non-null expression that represents the argument.
  103. * @param argNum The argument number index.
  104. *
  105. * @throws WrongNumberArgsException If the argNum parameter is greater than 2.
  106. */
  107. public void setArg(Expression arg, int argNum)
  108. throws WrongNumberArgsException
  109. {
  110. if (argNum < 2)
  111. super.setArg(arg, argNum);
  112. else if (2 == argNum)
  113. {
  114. m_arg2 = arg;
  115. arg.exprSetParent(this);
  116. }
  117. else
  118. reportWrongNumberArgs();
  119. }
  120. /**
  121. * Check that the number of arguments passed to this function is correct.
  122. *
  123. *
  124. * @param argNum The number of arguments that is being passed to the function.
  125. *
  126. * @throws WrongNumberArgsException
  127. */
  128. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  129. {
  130. if (argNum != 3)
  131. reportWrongNumberArgs();
  132. }
  133. /**
  134. * Constructs and throws a WrongNumberArgException with the appropriate
  135. * message for this function object.
  136. *
  137. * @throws WrongNumberArgsException
  138. */
  139. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  140. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("three", null));
  141. }
  142. /**
  143. * Tell if this expression or it's subexpressions can traverse outside
  144. * the current subtree.
  145. *
  146. * @return true if traversal outside the context node's subtree can occur.
  147. */
  148. public boolean canTraverseOutsideSubtree()
  149. {
  150. return super.canTraverseOutsideSubtree()
  151. ? true : m_arg2.canTraverseOutsideSubtree();
  152. }
  153. class Arg2Owner implements ExpressionOwner
  154. {
  155. /**
  156. * @see ExpressionOwner#getExpression()
  157. */
  158. public Expression getExpression()
  159. {
  160. return m_arg2;
  161. }
  162. /**
  163. * @see ExpressionOwner#setExpression(Expression)
  164. */
  165. public void setExpression(Expression exp)
  166. {
  167. exp.exprSetParent(Function3Args.this);
  168. m_arg2 = exp;
  169. }
  170. }
  171. /**
  172. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  173. */
  174. public void callArgVisitors(XPathVisitor visitor)
  175. {
  176. super.callArgVisitors(visitor);
  177. if(null != m_arg2)
  178. m_arg2.callVisitors(new Arg2Owner(), visitor);
  179. }
  180. /**
  181. * @see Expression#deepEquals(Expression)
  182. */
  183. public boolean deepEquals(Expression expr)
  184. {
  185. if(!super.deepEquals(expr))
  186. return false;
  187. if(null != m_arg2)
  188. {
  189. if(null == ((Function3Args)expr).m_arg2)
  190. return false;
  191. if(!m_arg2.deepEquals(((Function3Args)expr).m_arg2))
  192. return false;
  193. }
  194. else if (null != ((Function3Args)expr).m_arg2)
  195. return false;
  196. return true;
  197. }
  198. }