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.xpath.Expression;
  60. import org.apache.xpath.ExpressionOwner;
  61. import org.apache.xpath.XPathVisitor;
  62. import org.apache.xpath.functions.Function3Args.Arg2Owner;
  63. import org.apache.xpath.res.XPATHErrorResources;
  64. import org.apache.xalan.res.XSLMessages;
  65. /**
  66. * <meta name="usage" content="advanced"/>
  67. * Base class for functions that accept an undetermined number of multiple
  68. * arguments.
  69. */
  70. public class FunctionMultiArgs extends Function3Args
  71. {
  72. /** Argument expressions that are at index 3 or greater.
  73. * @serial */
  74. Expression[] m_args;
  75. /**
  76. * Return an expression array containing arguments at index 3 or greater.
  77. *
  78. * @return An array that contains the arguments at index 3 or greater.
  79. */
  80. public Expression[] getArgs()
  81. {
  82. return m_args;
  83. }
  84. /**
  85. * Set an argument expression for a function. This method is called by the
  86. * XPath compiler.
  87. *
  88. * @param arg non-null expression that represents the argument.
  89. * @param argNum The argument number index.
  90. *
  91. * @throws WrongNumberArgsException If a derived class determines that the
  92. * number of arguments is incorrect.
  93. */
  94. public void setArg(Expression arg, int argNum)
  95. throws WrongNumberArgsException
  96. {
  97. if (argNum < 3)
  98. super.setArg(arg, argNum);
  99. else
  100. {
  101. if (null == m_args)
  102. {
  103. m_args = new Expression[1];
  104. m_args[0] = arg;
  105. }
  106. else
  107. {
  108. // Slow but space conservative.
  109. Expression[] args = new Expression[m_args.length + 1];
  110. System.arraycopy(m_args, 0, args, 0, m_args.length);
  111. args[m_args.length] = arg;
  112. m_args = args;
  113. }
  114. arg.exprSetParent(this);
  115. }
  116. }
  117. /**
  118. * This function is used to fixup variables from QNames to stack frame
  119. * indexes at stylesheet build time.
  120. * @param vars List of QNames that correspond to variables. This list
  121. * should be searched backwards for the first qualified name that
  122. * corresponds to the variable reference qname. The position of the
  123. * QName in the vector from the start of the vector will be its position
  124. * in the stack frame (but variables above the globalsTop value will need
  125. * to be offset to the current stack frame).
  126. */
  127. public void fixupVariables(java.util.Vector vars, int globalsSize)
  128. {
  129. super.fixupVariables(vars, globalsSize);
  130. if(null != m_args)
  131. {
  132. for (int i = 0; i < m_args.length; i++)
  133. {
  134. m_args[i].fixupVariables(vars, globalsSize);
  135. }
  136. }
  137. }
  138. /**
  139. * Check that the number of arguments passed to this function is correct.
  140. *
  141. *
  142. * @param argNum The number of arguments that is being passed to the function.
  143. *
  144. * @throws WrongNumberArgsException
  145. */
  146. public void checkNumberArgs(int argNum) throws WrongNumberArgsException{}
  147. /**
  148. * Constructs and throws a WrongNumberArgException with the appropriate
  149. * message for this function object. This class supports an arbitrary
  150. * number of arguments, so this method must never be called.
  151. *
  152. * @throws WrongNumberArgsException
  153. */
  154. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  155. String fMsg = XSLMessages.createXPATHMessage(
  156. XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION,
  157. new Object[]{ "Programmer's assertion: the method FunctionMultiArgs.reportWrongNumberArgs() should never be called." });
  158. throw new RuntimeException(fMsg);
  159. }
  160. /**
  161. * Tell if this expression or it's subexpressions can traverse outside
  162. * the current subtree.
  163. *
  164. * @return true if traversal outside the context node's subtree can occur.
  165. */
  166. public boolean canTraverseOutsideSubtree()
  167. {
  168. if (super.canTraverseOutsideSubtree())
  169. return true;
  170. else
  171. {
  172. int n = m_args.length;
  173. for (int i = 0; i < n; i++)
  174. {
  175. if (m_args[i].canTraverseOutsideSubtree())
  176. return true;
  177. }
  178. return false;
  179. }
  180. }
  181. class ArgMultiOwner implements ExpressionOwner
  182. {
  183. int m_argIndex;
  184. ArgMultiOwner(int index)
  185. {
  186. m_argIndex = index;
  187. }
  188. /**
  189. * @see ExpressionOwner#getExpression()
  190. */
  191. public Expression getExpression()
  192. {
  193. return m_args[m_argIndex];
  194. }
  195. /**
  196. * @see ExpressionOwner#setExpression(Expression)
  197. */
  198. public void setExpression(Expression exp)
  199. {
  200. exp.exprSetParent(FunctionMultiArgs.this);
  201. m_args[m_argIndex] = exp;
  202. }
  203. }
  204. /**
  205. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  206. */
  207. public void callArgVisitors(XPathVisitor visitor)
  208. {
  209. super.callArgVisitors(visitor);
  210. if (null != m_args)
  211. {
  212. int n = m_args.length;
  213. for (int i = 0; i < n; i++)
  214. {
  215. m_args[i].callVisitors(new ArgMultiOwner(i), visitor);
  216. }
  217. }
  218. }
  219. /**
  220. * @see Expression#deepEquals(Expression)
  221. */
  222. public boolean deepEquals(Expression expr)
  223. {
  224. if (!super.deepEquals(expr))
  225. return false;
  226. FunctionMultiArgs fma = (FunctionMultiArgs) expr;
  227. if (null != m_args)
  228. {
  229. int n = m_args.length;
  230. if ((null == fma) || (fma.m_args.length != n))
  231. return false;
  232. for (int i = 0; i < n; i++)
  233. {
  234. if (!m_args[i].deepEquals(fma.m_args[i]))
  235. return false;
  236. }
  237. }
  238. else if (null != fma.m_args)
  239. {
  240. return false;
  241. }
  242. return true;
  243. }
  244. }