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: Function.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.XPathContext;
  24. import com.sun.org.apache.xpath.internal.XPathVisitor;
  25. import com.sun.org.apache.xpath.internal.compiler.Compiler;
  26. import com.sun.org.apache.xpath.internal.objects.XObject;
  27. /**
  28. * This is a superclass of all XPath functions. This allows two
  29. * ways for the class to be called. One method is that the
  30. * super class processes the arguments and hands the results to
  31. * the derived class, the other method is that the derived
  32. * class may process it's own arguments, which is faster since
  33. * the arguments don't have to be added to an array, but causes
  34. * a larger code footprint.
  35. * @xsl.usage advanced
  36. */
  37. public abstract class Function extends Expression
  38. {
  39. /**
  40. * Set an argument expression for a function. This method is called by the
  41. * XPath compiler.
  42. *
  43. * @param arg non-null expression that represents the argument.
  44. * @param argNum The argument number index.
  45. *
  46. * @throws WrongNumberArgsException If the argNum parameter is beyond what
  47. * is specified for this function.
  48. */
  49. public void setArg(Expression arg, int argNum)
  50. throws WrongNumberArgsException
  51. {
  52. // throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
  53. reportWrongNumberArgs();
  54. }
  55. /**
  56. * Check that the number of arguments passed to this function is correct.
  57. * This method is meant to be overloaded by derived classes, to check for
  58. * the number of arguments for a specific function type. This method is
  59. * called by the compiler for static number of arguments checking.
  60. *
  61. * @param argNum The number of arguments that is being passed to the function.
  62. *
  63. * @throws WrongNumberArgsException
  64. */
  65. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  66. {
  67. if (argNum != 0)
  68. reportWrongNumberArgs();
  69. }
  70. /**
  71. * Constructs and throws a WrongNumberArgException with the appropriate
  72. * message for this function object. This method is meant to be overloaded
  73. * by derived classes so that the message will be as specific as possible.
  74. *
  75. * @throws WrongNumberArgsException
  76. */
  77. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  78. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
  79. }
  80. /**
  81. * Execute an XPath function object. The function must return
  82. * a valid object.
  83. * @param xctxt The execution current context.
  84. * @return A valid XObject.
  85. *
  86. * @throws javax.xml.transform.TransformerException
  87. */
  88. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  89. {
  90. // Programmer's assert. (And, no, I don't want the method to be abstract).
  91. System.out.println("Error! Function.execute should not be called!");
  92. return null;
  93. }
  94. /**
  95. * Call the visitors for the function arguments.
  96. */
  97. public void callArgVisitors(XPathVisitor visitor)
  98. {
  99. }
  100. /**
  101. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  102. */
  103. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  104. {
  105. if(visitor.visitFunction(owner, this))
  106. {
  107. callArgVisitors(visitor);
  108. }
  109. }
  110. /**
  111. * @see Expression#deepEquals(Expression)
  112. */
  113. public boolean deepEquals(Expression expr)
  114. {
  115. if(!isSameClass(expr))
  116. return false;
  117. return true;
  118. }
  119. /**
  120. * This function is currently only being used by Position()
  121. * and Last(). See respective functions for more detail.
  122. */
  123. public void postCompileStep(Compiler compiler)
  124. {
  125. // no default action
  126. }
  127. }