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 one argument.
  66. */
  67. public class FunctionOneArg extends Function implements ExpressionOwner
  68. {
  69. /** The first argument passed to the function (at index 0).
  70. * @serial */
  71. Expression m_arg0;
  72. /**
  73. * Return the first argument passed to the function (at index 0).
  74. *
  75. * @return An expression that represents the first argument passed to the
  76. * function.
  77. */
  78. public Expression getArg0()
  79. {
  80. return m_arg0;
  81. }
  82. /**
  83. * Set an argument expression for a function. This method is called by the
  84. * XPath compiler.
  85. *
  86. * @param arg non-null expression that represents the argument.
  87. * @param argNum The argument number index.
  88. *
  89. * @throws WrongNumberArgsException If the argNum parameter is greater than 0.
  90. */
  91. public void setArg(Expression arg, int argNum)
  92. throws WrongNumberArgsException
  93. {
  94. if (0 == argNum)
  95. {
  96. m_arg0 = arg;
  97. arg.exprSetParent(this);
  98. }
  99. else
  100. reportWrongNumberArgs();
  101. }
  102. /**
  103. * Check that the number of arguments passed to this function is correct.
  104. *
  105. *
  106. * @param argNum The number of arguments that is being passed to the function.
  107. *
  108. * @throws WrongNumberArgsException
  109. */
  110. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  111. {
  112. if (argNum != 1)
  113. reportWrongNumberArgs();
  114. }
  115. /**
  116. * Constructs and throws a WrongNumberArgException with the appropriate
  117. * message for this function object.
  118. *
  119. * @throws WrongNumberArgsException
  120. */
  121. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  122. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("one", null));
  123. }
  124. /**
  125. * Tell if this expression or it's subexpressions can traverse outside
  126. * the current subtree.
  127. *
  128. * @return true if traversal outside the context node's subtree can occur.
  129. */
  130. public boolean canTraverseOutsideSubtree()
  131. {
  132. return m_arg0.canTraverseOutsideSubtree();
  133. }
  134. /**
  135. * This function is used to fixup variables from QNames to stack frame
  136. * indexes at stylesheet build time.
  137. * @param vars List of QNames that correspond to variables. This list
  138. * should be searched backwards for the first qualified name that
  139. * corresponds to the variable reference qname. The position of the
  140. * QName in the vector from the start of the vector will be its position
  141. * in the stack frame (but variables above the globalsTop value will need
  142. * to be offset to the current stack frame).
  143. */
  144. public void fixupVariables(java.util.Vector vars, int globalsSize)
  145. {
  146. if(null != m_arg0)
  147. m_arg0.fixupVariables(vars, globalsSize);
  148. }
  149. /**
  150. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  151. */
  152. public void callArgVisitors(XPathVisitor visitor)
  153. {
  154. if(null != m_arg0)
  155. m_arg0.callVisitors(this, visitor);
  156. }
  157. /**
  158. * @see ExpressionOwner#getExpression()
  159. */
  160. public Expression getExpression()
  161. {
  162. return m_arg0;
  163. }
  164. /**
  165. * @see ExpressionOwner#setExpression(Expression)
  166. */
  167. public void setExpression(Expression exp)
  168. {
  169. exp.exprSetParent(this);
  170. m_arg0 = exp;
  171. }
  172. /**
  173. * @see Expression#deepEquals(Expression)
  174. */
  175. public boolean deepEquals(Expression expr)
  176. {
  177. if(!super.deepEquals(expr))
  178. return false;
  179. if(null != m_arg0)
  180. {
  181. if(null == ((FunctionOneArg)expr).m_arg0)
  182. return false;
  183. if(!m_arg0.deepEquals(((FunctionOneArg)expr).m_arg0))
  184. return false;
  185. }
  186. else if(null != ((FunctionOneArg)expr).m_arg0)
  187. return false;
  188. return true;
  189. }
  190. }