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 two arguments.
  66. */
  67. public class Function2Args extends FunctionOneArg
  68. {
  69. /** The second argument passed to the function (at index 1).
  70. * @serial */
  71. Expression m_arg1;
  72. /**
  73. * Return the second argument passed to the function (at index 1).
  74. *
  75. * @return An expression that represents the second argument passed to the
  76. * function.
  77. */
  78. public Expression getArg1()
  79. {
  80. return m_arg1;
  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_arg1)
  96. m_arg1.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 1.
  106. */
  107. public void setArg(Expression arg, int argNum)
  108. throws WrongNumberArgsException
  109. {
  110. // System.out.println("argNum: "+argNum);
  111. if (argNum == 0)
  112. super.setArg(arg, argNum);
  113. else if (1 == argNum)
  114. {
  115. m_arg1 = arg;
  116. arg.exprSetParent(this);
  117. }
  118. else
  119. reportWrongNumberArgs();
  120. }
  121. /**
  122. * Check that the number of arguments passed to this function is correct.
  123. *
  124. *
  125. * @param argNum The number of arguments that is being passed to the function.
  126. *
  127. * @throws WrongNumberArgsException
  128. */
  129. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  130. {
  131. if (argNum != 2)
  132. reportWrongNumberArgs();
  133. }
  134. /**
  135. * Constructs and throws a WrongNumberArgException with the appropriate
  136. * message for this function object.
  137. *
  138. * @throws WrongNumberArgsException
  139. */
  140. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  141. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("two", null));
  142. }
  143. /**
  144. * Tell if this expression or it's subexpressions can traverse outside
  145. * the current subtree.
  146. *
  147. * @return true if traversal outside the context node's subtree can occur.
  148. */
  149. public boolean canTraverseOutsideSubtree()
  150. {
  151. return super.canTraverseOutsideSubtree()
  152. ? true : m_arg1.canTraverseOutsideSubtree();
  153. }
  154. class Arg1Owner implements ExpressionOwner
  155. {
  156. /**
  157. * @see ExpressionOwner#getExpression()
  158. */
  159. public Expression getExpression()
  160. {
  161. return m_arg1;
  162. }
  163. /**
  164. * @see ExpressionOwner#setExpression(Expression)
  165. */
  166. public void setExpression(Expression exp)
  167. {
  168. exp.exprSetParent(Function2Args.this);
  169. m_arg1 = exp;
  170. }
  171. }
  172. /**
  173. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  174. */
  175. public void callArgVisitors(XPathVisitor visitor)
  176. {
  177. super.callArgVisitors(visitor);
  178. if(null != m_arg1)
  179. m_arg1.callVisitors(new Arg1Owner(), visitor);
  180. }
  181. /**
  182. * @see Expression#deepEquals(Expression)
  183. */
  184. public boolean deepEquals(Expression expr)
  185. {
  186. if(!super.deepEquals(expr))
  187. return false;
  188. if(null != m_arg1)
  189. {
  190. if(null == ((Function2Args)expr).m_arg1)
  191. return false;
  192. if(!m_arg1.deepEquals(((Function2Args)expr).m_arg1))
  193. return false;
  194. }
  195. else if(null != ((Function2Args)expr).m_arg1)
  196. return false;
  197. return true;
  198. }
  199. }