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: FunctionDef1Arg.java,v 1.12 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.xml.internal.dtm.DTM;
  22. import com.sun.org.apache.xml.internal.utils.XMLString;
  23. import com.sun.org.apache.xpath.internal.XPathContext;
  24. import com.sun.org.apache.xpath.internal.objects.XString;
  25. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  26. /**
  27. * Base class for functions that accept one argument that can be defaulted if
  28. * not specified.
  29. * @xsl.usage advanced
  30. */
  31. public class FunctionDef1Arg extends FunctionOneArg
  32. {
  33. /**
  34. * Execute the first argument expression that is expected to return a
  35. * nodeset. If the argument is null, then return the current context node.
  36. *
  37. * @param xctxt Runtime XPath context.
  38. *
  39. * @return The first node of the executed nodeset, or the current context
  40. * node if the first argument is null.
  41. *
  42. * @throws javax.xml.transform.TransformerException if an error occurs while
  43. * executing the argument expression.
  44. */
  45. protected int getArg0AsNode(XPathContext xctxt)
  46. throws javax.xml.transform.TransformerException
  47. {
  48. return (null == m_arg0)
  49. ? xctxt.getCurrentNode() : m_arg0.asNode(xctxt);
  50. }
  51. /**
  52. * Tell if the expression is a nodeset expression.
  53. * @return true if the expression can be represented as a nodeset.
  54. */
  55. public boolean Arg0IsNodesetExpr()
  56. {
  57. return (null == m_arg0) ? true : m_arg0.isNodesetExpr();
  58. }
  59. /**
  60. * Execute the first argument expression that is expected to return a
  61. * string. If the argument is null, then get the string value from the
  62. * current context node.
  63. *
  64. * @param xctxt Runtime XPath context.
  65. *
  66. * @return The string value of the first argument, or the string value of the
  67. * current context node if the first argument is null.
  68. *
  69. * @throws javax.xml.transform.TransformerException if an error occurs while
  70. * executing the argument expression.
  71. */
  72. protected XMLString getArg0AsString(XPathContext xctxt)
  73. throws javax.xml.transform.TransformerException
  74. {
  75. if(null == m_arg0)
  76. {
  77. int currentNode = xctxt.getCurrentNode();
  78. if(DTM.NULL == currentNode)
  79. return XString.EMPTYSTRING;
  80. else
  81. {
  82. DTM dtm = xctxt.getDTM(currentNode);
  83. return dtm.getStringValue(currentNode);
  84. }
  85. }
  86. else
  87. return m_arg0.execute(xctxt).xstr();
  88. }
  89. /**
  90. * Execute the first argument expression that is expected to return a
  91. * number. If the argument is null, then get the number value from the
  92. * current context node.
  93. *
  94. * @param xctxt Runtime XPath context.
  95. *
  96. * @return The number value of the first argument, or the number value of the
  97. * current context node if the first argument is null.
  98. *
  99. * @throws javax.xml.transform.TransformerException if an error occurs while
  100. * executing the argument expression.
  101. */
  102. protected double getArg0AsNumber(XPathContext xctxt)
  103. throws javax.xml.transform.TransformerException
  104. {
  105. if(null == m_arg0)
  106. {
  107. int currentNode = xctxt.getCurrentNode();
  108. if(DTM.NULL == currentNode)
  109. return 0;
  110. else
  111. {
  112. DTM dtm = xctxt.getDTM(currentNode);
  113. XMLString str = dtm.getStringValue(currentNode);
  114. return str.toDouble();
  115. }
  116. }
  117. else
  118. return m_arg0.execute(xctxt).num();
  119. }
  120. /**
  121. * Check that the number of arguments passed to this function is correct.
  122. *
  123. * @param argNum The number of arguments that is being passed to the function.
  124. *
  125. * @throws WrongNumberArgsException if the number of arguments is not 0 or 1.
  126. */
  127. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  128. {
  129. if (argNum > 1)
  130. reportWrongNumberArgs();
  131. }
  132. /**
  133. * Constructs and throws a WrongNumberArgException with the appropriate
  134. * message for this function object.
  135. *
  136. * @throws WrongNumberArgsException
  137. */
  138. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  139. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ZERO_OR_ONE, null)); //"0 or 1");
  140. }
  141. /**
  142. * Tell if this expression or it's subexpressions can traverse outside
  143. * the current subtree.
  144. *
  145. * @return true if traversal outside the context node's subtree can occur.
  146. */
  147. public boolean canTraverseOutsideSubtree()
  148. {
  149. return (null == m_arg0) ? false : super.canTraverseOutsideSubtree();
  150. }
  151. }