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: FunctionOneArg.java,v 1.13 2004/02/17 04:34:00 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 one argument.
  26. * @xsl.usage advanced
  27. */
  28. public class FunctionOneArg extends Function implements ExpressionOwner
  29. {
  30. /** The first argument passed to the function (at index 0).
  31. * @serial */
  32. Expression m_arg0;
  33. /**
  34. * Return the first argument passed to the function (at index 0).
  35. *
  36. * @return An expression that represents the first argument passed to the
  37. * function.
  38. */
  39. public Expression getArg0()
  40. {
  41. return m_arg0;
  42. }
  43. /**
  44. * Set an argument expression for a function. This method is called by the
  45. * XPath compiler.
  46. *
  47. * @param arg non-null expression that represents the argument.
  48. * @param argNum The argument number index.
  49. *
  50. * @throws WrongNumberArgsException If the argNum parameter is greater than 0.
  51. */
  52. public void setArg(Expression arg, int argNum)
  53. throws WrongNumberArgsException
  54. {
  55. if (0 == argNum)
  56. {
  57. m_arg0 = arg;
  58. arg.exprSetParent(this);
  59. }
  60. else
  61. reportWrongNumberArgs();
  62. }
  63. /**
  64. * Check that the number of arguments passed to this function is correct.
  65. *
  66. *
  67. * @param argNum The number of arguments that is being passed to the function.
  68. *
  69. * @throws WrongNumberArgsException
  70. */
  71. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  72. {
  73. if (argNum != 1)
  74. reportWrongNumberArgs();
  75. }
  76. /**
  77. * Constructs and throws a WrongNumberArgException with the appropriate
  78. * message for this function object.
  79. *
  80. * @throws WrongNumberArgsException
  81. */
  82. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  83. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("one", null));
  84. }
  85. /**
  86. * Tell if this expression or it's subexpressions can traverse outside
  87. * the current subtree.
  88. *
  89. * @return true if traversal outside the context node's subtree can occur.
  90. */
  91. public boolean canTraverseOutsideSubtree()
  92. {
  93. return m_arg0.canTraverseOutsideSubtree();
  94. }
  95. /**
  96. * This function is used to fixup variables from QNames to stack frame
  97. * indexes at stylesheet build time.
  98. * @param vars List of QNames that correspond to variables. This list
  99. * should be searched backwards for the first qualified name that
  100. * corresponds to the variable reference qname. The position of the
  101. * QName in the vector from the start of the vector will be its position
  102. * in the stack frame (but variables above the globalsTop value will need
  103. * to be offset to the current stack frame).
  104. */
  105. public void fixupVariables(java.util.Vector vars, int globalsSize)
  106. {
  107. if(null != m_arg0)
  108. m_arg0.fixupVariables(vars, globalsSize);
  109. }
  110. /**
  111. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  112. */
  113. public void callArgVisitors(XPathVisitor visitor)
  114. {
  115. if(null != m_arg0)
  116. m_arg0.callVisitors(this, visitor);
  117. }
  118. /**
  119. * @see ExpressionOwner#getExpression()
  120. */
  121. public Expression getExpression()
  122. {
  123. return m_arg0;
  124. }
  125. /**
  126. * @see ExpressionOwner#setExpression(Expression)
  127. */
  128. public void setExpression(Expression exp)
  129. {
  130. exp.exprSetParent(this);
  131. m_arg0 = exp;
  132. }
  133. /**
  134. * @see Expression#deepEquals(Expression)
  135. */
  136. public boolean deepEquals(Expression expr)
  137. {
  138. if(!super.deepEquals(expr))
  139. return false;
  140. if(null != m_arg0)
  141. {
  142. if(null == ((FunctionOneArg)expr).m_arg0)
  143. return false;
  144. if(!m_arg0.deepEquals(((FunctionOneArg)expr).m_arg0))
  145. return false;
  146. }
  147. else if(null != ((FunctionOneArg)expr).m_arg0)
  148. return false;
  149. return true;
  150. }
  151. }