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.xpath.Expression;
  60. import org.apache.xpath.ExpressionOwner;
  61. import org.apache.xpath.ExpressionNode;
  62. import org.apache.xpath.XPathContext;
  63. import org.apache.xpath.XPathVisitor;
  64. import org.apache.xpath.ExtensionsProvider;
  65. import org.apache.xpath.objects.*;
  66. import org.apache.xalan.transformer.TransformerImpl;
  67. import org.apache.xalan.extensions.ExtensionsTable;
  68. import org.apache.xml.dtm.DTMIterator;
  69. //import org.w3c.dom.Node;
  70. import org.w3c.dom.DocumentFragment;
  71. import org.w3c.dom.traversal.NodeIterator;
  72. import org.apache.xml.dtm.*;
  73. import org.apache.xpath.axes.*;
  74. import org.apache.xpath.res.XPATHErrorResources;
  75. import org.apache.xalan.res.XSLMessages;
  76. /**
  77. * <meta name="usage" content="advanced"/>
  78. * An object of this class represents an extension call expression. When
  79. * the expression executes, it calls ExtensionsTable#extFunction, and then
  80. * converts the result to the appropriate XObject.
  81. */
  82. public class FuncExtFunction extends Function
  83. {
  84. /**
  85. * The namespace for the extension function, which should not normally
  86. * be null or empty.
  87. * @serial
  88. */
  89. String m_namespace;
  90. /**
  91. * The local name of the extension.
  92. * @serial
  93. */
  94. String m_extensionName;
  95. /**
  96. * Unique method key, which is passed to ExtensionsTable#extFunction in
  97. * order to allow caching of the method.
  98. * @serial
  99. */
  100. Object m_methodKey;
  101. /**
  102. * Array of static expressions which represent the parameters to the
  103. * function.
  104. * @serial
  105. */
  106. Vector m_argVec = new Vector();
  107. /**
  108. * This function is used to fixup variables from QNames to stack frame
  109. * indexes at stylesheet build time.
  110. * @param vars List of QNames that correspond to variables. This list
  111. * should be searched backwards for the first qualified name that
  112. * corresponds to the variable reference qname. The position of the
  113. * QName in the vector from the start of the vector will be its position
  114. * in the stack frame (but variables above the globalsTop value will need
  115. * to be offset to the current stack frame).
  116. * NEEDSDOC @param globalsSize
  117. */
  118. public void fixupVariables(java.util.Vector vars, int globalsSize)
  119. {
  120. if (null != m_argVec)
  121. {
  122. int nArgs = m_argVec.size();
  123. for (int i = 0; i < nArgs; i++)
  124. {
  125. Expression arg = (Expression) m_argVec.elementAt(i);
  126. arg.fixupVariables(vars, globalsSize);
  127. }
  128. }
  129. }
  130. /**
  131. * Return the namespace of the extension function.
  132. *
  133. * @return The namespace of the extension function.
  134. */
  135. public String getNamespace()
  136. {
  137. return m_namespace;
  138. }
  139. /**
  140. * Return the name of the extension function.
  141. *
  142. * @return The name of the extension function.
  143. */
  144. public String getFunctionName()
  145. {
  146. return m_extensionName;
  147. }
  148. /**
  149. * Return the nth argument passed to the extension function.
  150. *
  151. * @param n The argument number index.
  152. * @return The Expression object at the given index.
  153. */
  154. public Expression getArg(int n) {
  155. if (n >= 0 && n < m_argVec.size())
  156. return (Expression) m_argVec.elementAt(n);
  157. else
  158. return null;
  159. }
  160. /**
  161. * Return the number of arguments that were passed
  162. * into this extension function.
  163. *
  164. * @return The number of arguments.
  165. */
  166. public int getArgCount() {
  167. return m_argVec.size();
  168. }
  169. /**
  170. * Create a new FuncExtFunction based on the qualified name of the extension,
  171. * and a unique method key.
  172. *
  173. * @param namespace The namespace for the extension function, which should
  174. * not normally be null or empty.
  175. * @param extensionName The local name of the extension.
  176. * @param methodKey Unique method key, which is passed to
  177. * ExtensionsTable#extFunction in order to allow caching
  178. * of the method.
  179. */
  180. public FuncExtFunction(java.lang.String namespace,
  181. java.lang.String extensionName, Object methodKey)
  182. {
  183. //try{throw new Exception("FuncExtFunction() " + namespace + " " + extensionName);} catch (Exception e){e.printStackTrace();}
  184. m_namespace = namespace;
  185. m_extensionName = extensionName;
  186. m_methodKey = methodKey;
  187. }
  188. /**
  189. * Execute the function. The function must return
  190. * a valid object.
  191. * @param xctxt The current execution context.
  192. * @return A valid XObject.
  193. *
  194. * @throws javax.xml.transform.TransformerException
  195. */
  196. public XObject execute(XPathContext xctxt)
  197. throws javax.xml.transform.TransformerException
  198. {
  199. XObject result;
  200. Vector argVec = new Vector();
  201. int nArgs = m_argVec.size();
  202. for (int i = 0; i < nArgs; i++)
  203. {
  204. Expression arg = (Expression) m_argVec.elementAt(i);
  205. XObject xobj = arg.execute(xctxt);
  206. argVec.addElement(xobj);
  207. }
  208. //dml
  209. ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject();
  210. Object val = extProvider.extFunction(m_namespace, m_extensionName,
  211. argVec, m_methodKey);
  212. if (null != val)
  213. {
  214. result = XObject.create(val, xctxt);
  215. }
  216. else
  217. {
  218. result = new XNull();
  219. }
  220. return result;
  221. }
  222. /**
  223. * Set an argument expression for a function. This method is called by the
  224. * XPath compiler.
  225. *
  226. * @param arg non-null expression that represents the argument.
  227. * @param argNum The argument number index.
  228. *
  229. * @throws WrongNumberArgsException If the argNum parameter is beyond what
  230. * is specified for this function.
  231. */
  232. public void setArg(Expression arg, int argNum)
  233. throws WrongNumberArgsException
  234. {
  235. m_argVec.addElement(arg);
  236. }
  237. /**
  238. * Check that the number of arguments passed to this function is correct.
  239. *
  240. *
  241. * @param argNum The number of arguments that is being passed to the function.
  242. *
  243. * @throws WrongNumberArgsException
  244. */
  245. public void checkNumberArgs(int argNum) throws WrongNumberArgsException{}
  246. class ArgExtOwner implements ExpressionOwner
  247. {
  248. Expression m_exp;
  249. ArgExtOwner(Expression exp)
  250. {
  251. m_exp = exp;
  252. }
  253. /**
  254. * @see ExpressionOwner#getExpression()
  255. */
  256. public Expression getExpression()
  257. {
  258. return m_exp;
  259. }
  260. /**
  261. * @see ExpressionOwner#setExpression(Expression)
  262. */
  263. public void setExpression(Expression exp)
  264. {
  265. exp.exprSetParent(FuncExtFunction.this);
  266. m_exp = exp;
  267. }
  268. }
  269. /**
  270. * Call the visitors for the function arguments.
  271. */
  272. public void callArgVisitors(XPathVisitor visitor)
  273. {
  274. for (int i = 0; i < m_argVec.size(); i++)
  275. {
  276. Expression exp = (Expression)m_argVec.elementAt(i);
  277. exp.callVisitors(new ArgExtOwner(exp), visitor);
  278. }
  279. }
  280. /**
  281. * Set the parent node.
  282. * For an extension function, we also need to set the parent
  283. * node for all argument expressions.
  284. *
  285. * @param n The parent node
  286. */
  287. public void exprSetParent(ExpressionNode n)
  288. {
  289. super.exprSetParent(n);
  290. int nArgs = m_argVec.size();
  291. for (int i = 0; i < nArgs; i++)
  292. {
  293. Expression arg = (Expression) m_argVec.elementAt(i);
  294. arg.exprSetParent(n);
  295. }
  296. }
  297. /**
  298. * Constructs and throws a WrongNumberArgException with the appropriate
  299. * message for this function object. This class supports an arbitrary
  300. * number of arguments, so this method must never be called.
  301. *
  302. * @throws WrongNumberArgsException
  303. */
  304. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  305. String fMsg = XSLMessages.createXPATHMessage(
  306. XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION,
  307. new Object[]{ "Programmer's assertion: the method FunctionMultiArgs.reportWrongNumberArgs() should never be called." });
  308. throw new RuntimeException(fMsg);
  309. }
  310. }