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 org.w3c.dom.Node;
  59. import org.apache.xpath.XPathContext;
  60. import org.apache.xpath.objects.XNodeSet;
  61. import org.apache.xpath.objects.XNumber;
  62. import org.apache.xpath.objects.XString;
  63. import org.apache.xalan.res.XSLMessages;
  64. import org.apache.xpath.res.XPATHErrorResources;
  65. import org.apache.xml.utils.XMLString;
  66. import org.apache.xml.dtm.DTM;
  67. /**
  68. * <meta name="usage" content="advanced"/>
  69. * Base class for functions that accept one argument that can be defaulted if
  70. * not specified.
  71. */
  72. public class FunctionDef1Arg extends FunctionOneArg
  73. {
  74. /**
  75. * Execute the first argument expression that is expected to return a
  76. * nodeset. If the argument is null, then return the current context node.
  77. *
  78. * @param xctxt Runtime XPath context.
  79. *
  80. * @return The first node of the executed nodeset, or the current context
  81. * node if the first argument is null.
  82. *
  83. * @throws javax.xml.transform.TransformerException if an error occurs while
  84. * executing the argument expression.
  85. */
  86. protected int getArg0AsNode(XPathContext xctxt)
  87. throws javax.xml.transform.TransformerException
  88. {
  89. return (null == m_arg0)
  90. ? xctxt.getCurrentNode() : m_arg0.asNode(xctxt);
  91. }
  92. /**
  93. * Tell if the expression is a nodeset expression.
  94. * @return true if the expression can be represented as a nodeset.
  95. */
  96. public boolean Arg0IsNodesetExpr()
  97. {
  98. return (null == m_arg0) ? true : m_arg0.isNodesetExpr();
  99. }
  100. /**
  101. * Execute the first argument expression that is expected to return a
  102. * string. If the argument is null, then get the string value from the
  103. * current context node.
  104. *
  105. * @param xctxt Runtime XPath context.
  106. *
  107. * @return The string value of the first argument, or the string value of the
  108. * current context node if the first argument is null.
  109. *
  110. * @throws javax.xml.transform.TransformerException if an error occurs while
  111. * executing the argument expression.
  112. */
  113. protected XMLString getArg0AsString(XPathContext xctxt)
  114. throws javax.xml.transform.TransformerException
  115. {
  116. if(null == m_arg0)
  117. {
  118. int currentNode = xctxt.getCurrentNode();
  119. if(DTM.NULL == currentNode)
  120. return XString.EMPTYSTRING;
  121. else
  122. {
  123. DTM dtm = xctxt.getDTM(currentNode);
  124. return dtm.getStringValue(currentNode);
  125. }
  126. }
  127. else
  128. return m_arg0.execute(xctxt).xstr();
  129. }
  130. /**
  131. * Execute the first argument expression that is expected to return a
  132. * number. If the argument is null, then get the number value from the
  133. * current context node.
  134. *
  135. * @param xctxt Runtime XPath context.
  136. *
  137. * @return The number value of the first argument, or the number value of the
  138. * current context node if the first argument is null.
  139. *
  140. * @throws javax.xml.transform.TransformerException if an error occurs while
  141. * executing the argument expression.
  142. */
  143. protected double getArg0AsNumber(XPathContext xctxt)
  144. throws javax.xml.transform.TransformerException
  145. {
  146. if(null == m_arg0)
  147. {
  148. int currentNode = xctxt.getCurrentNode();
  149. if(DTM.NULL == currentNode)
  150. return 0;
  151. else
  152. {
  153. DTM dtm = xctxt.getDTM(currentNode);
  154. XMLString str = dtm.getStringValue(currentNode);
  155. return str.toDouble();
  156. }
  157. }
  158. else
  159. return m_arg0.execute(xctxt).num();
  160. }
  161. /**
  162. * Check that the number of arguments passed to this function is correct.
  163. *
  164. * @param argNum The number of arguments that is being passed to the function.
  165. *
  166. * @throws WrongNumberArgsException if the number of arguments is not 0 or 1.
  167. */
  168. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  169. {
  170. if (argNum > 1)
  171. reportWrongNumberArgs();
  172. }
  173. /**
  174. * Constructs and throws a WrongNumberArgException with the appropriate
  175. * message for this function object.
  176. *
  177. * @throws WrongNumberArgsException
  178. */
  179. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  180. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ZERO_OR_ONE, null)); //"0 or 1");
  181. }
  182. /**
  183. * Tell if this expression or it's subexpressions can traverse outside
  184. * the current subtree.
  185. *
  186. * @return true if traversal outside the context node's subtree can occur.
  187. */
  188. public boolean canTraverseOutsideSubtree()
  189. {
  190. return (null == m_arg0) ? false : super.canTraverseOutsideSubtree();
  191. }
  192. }