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.xalan.templates;
  58. //import org.w3c.dom.*;
  59. import org.apache.xml.dtm.DTM;
  60. import org.xml.sax.*;
  61. import org.apache.xpath.*;
  62. import org.apache.xpath.Expression;
  63. import org.apache.xpath.objects.XObject;
  64. import org.apache.xpath.objects.XString;
  65. import org.apache.xpath.objects.XNumber;
  66. import org.apache.xpath.objects.XRTreeFrag;
  67. import org.apache.xpath.objects.XRTreeFragSelectWrapper;
  68. import org.apache.xml.utils.QName;
  69. import org.apache.xalan.trace.SelectionEvent;
  70. import org.apache.xalan.res.XSLTErrorResources;
  71. import org.apache.xalan.transformer.TransformerImpl;
  72. import org.apache.xalan.extensions.ExtensionsTable;
  73. import javax.xml.transform.TransformerException;
  74. import org.w3c.dom.Node;
  75. import org.w3c.dom.NodeList;
  76. import org.apache.xalan.extensions.ExtensionNamespaceSupport;
  77. import org.apache.xalan.extensions.ExtensionHandlerExsltFunction;
  78. /**
  79. * <meta name="usage" content="advanced"/>
  80. * Implement func:function.
  81. */
  82. public class ElemExsltFunction extends ElemTemplate
  83. {
  84. /**
  85. * Get an integer representation of the element type.
  86. *
  87. * @return An integer representation of the element, defined in the
  88. * Constants class.
  89. * @see org.apache.xalan.templates.Constants
  90. */
  91. public int getXSLToken()
  92. {
  93. return Constants.EXSLT_ELEMNAME_FUNCTION;
  94. }
  95. /**
  96. * Return the node name, defined in the
  97. * Constants class.
  98. * @see org.apache.xalan.templates.Constants.
  99. * @return The node name
  100. *
  101. */
  102. public String getNodeName()
  103. {
  104. return Constants.EXSLT_ELEMNAME_FUNCTION_STRING;
  105. }
  106. public void execute(TransformerImpl transformer, XObject[] args)
  107. throws TransformerException
  108. {
  109. XPathContext xctxt = transformer.getXPathContext();
  110. VariableStack vars = xctxt.getVarStack();
  111. // Set parameters.
  112. NodeList children = this.getChildNodes();
  113. int numparams =0;
  114. for (int i = 0; i < args.length; i ++)
  115. {
  116. Node child = children.item(i);
  117. if (children.item(i) instanceof ElemParam)
  118. {
  119. numparams++;
  120. ElemParam param = (ElemParam)children.item(i);
  121. vars.setLocalVariable (param.m_index, args[i]);
  122. }
  123. }
  124. if (numparams < args.length)
  125. throw new TransformerException ("function called with too many args");
  126. // Removed ElemTemplate 'push' and 'pop' of RTFContext, in order to avoid losing the RTF context
  127. // before a value can be returned. ElemExsltFunction operates in the scope of the template that called
  128. // the function.
  129. // xctxt.pushRTFContext();
  130. if (TransformerImpl.S_DEBUG)
  131. transformer.getTraceManager().fireTraceEvent(this);
  132. // Be sure the return value is not set (so can verify that only one result
  133. // is generated per ElemExsltFunction execute).
  134. vars.setLocalVariable(_resultIndex, null);
  135. transformer.executeChildTemplates(this, true);
  136. if (TransformerImpl.S_DEBUG)
  137. transformer.getTraceManager().fireTraceEndEvent(this);
  138. // Following ElemTemplate 'pop' removed -- see above.
  139. // xctxt.popRTFContext();
  140. }
  141. //int m_inArgsSize;
  142. //public int m_frameSize;
  143. /**
  144. * Called after everything else has been
  145. * recomposed, and allows the function to set remaining
  146. * values that may be based on some other property that
  147. * depends on recomposition. Also adds a slot to the variable
  148. * stack for the return value. The result element will place
  149. * its value in this slot.
  150. */
  151. public void compose(StylesheetRoot sroot) throws TransformerException
  152. {
  153. super.compose(sroot);
  154. StylesheetRoot.ComposeState cstate = sroot.getComposeState();
  155. // Add a position on the variable stack for the return value.
  156. setResultIndex(cstate.addVariableName
  157. (new QName(Constants.S_EXSLT_COMMON_URL, "result")));
  158. // Register the function namespace (if not already registered).
  159. String namespace = getName().getNamespace();
  160. String handlerClass = "org.apache.xalan.extensions.ExtensionHandlerExsltFunction";
  161. Object[] args ={namespace, sroot};
  162. ExtensionNamespaceSupport extNsSpt =
  163. new ExtensionNamespaceSupport(namespace, handlerClass, args);
  164. sroot.getExtensionNamespacesManager().registerExtension(extNsSpt);
  165. // Make sure there is a handler for the EXSLT functions namespace
  166. // -- for isElementAvailable().
  167. if (!(namespace.equals(Constants.S_EXSLT_FUNCTIONS_URL)))
  168. {
  169. namespace = Constants.S_EXSLT_FUNCTIONS_URL;
  170. args = new Object[]{namespace, sroot};
  171. extNsSpt = new ExtensionNamespaceSupport(namespace, handlerClass, args);
  172. sroot.getExtensionNamespacesManager().registerExtension(extNsSpt);
  173. }
  174. }
  175. /**
  176. * Add the namespace to the StylesheetRoot vector of extension namespaces. Be sure the
  177. * exslt:function namespace is also added.
  178. */
  179. /* public void runtimeInit(TransformerImpl transformer) throws TransformerException
  180. {
  181. //System.out.println("ElemExsltFunction.runtimeInit()");
  182. String namespace = getName().getNamespace();
  183. ExtensionsTable etable = transformer.getExtensionsTable();
  184. StylesheetRoot sroot = transformer.getStylesheet();
  185. ExtensionHandlerExsltFunction exsltHandler =
  186. new ExtensionHandlerExsltFunction(namespace, sroot);
  187. //etable.addExtensionNamespace(namespace, exsltHandler);
  188. // Make sure there is a handler for the EXSLT functions namespace
  189. // -- for isElementAvailable().
  190. if (!(namespace.equals(Constants.S_EXSLT_FUNCTIONS_URL)))
  191. {
  192. exsltHandler = new ExtensionHandlerExsltFunction(
  193. Constants.S_EXSLT_FUNCTIONS_URL,
  194. sroot);
  195. // etable.addExtensionNamespace(Constants.S_EXSLT_FUNCTIONS_URL,
  196. // exsltHandler);
  197. }
  198. }
  199. */
  200. private int _resultIndex;
  201. /**
  202. * Sets aside a position on the local variable stack index
  203. * to refer to the result element return value.
  204. */
  205. void setResultIndex(int stackIndex)
  206. {
  207. _resultIndex = stackIndex;
  208. }
  209. /**
  210. * Provides the EXSLT extension handler access to the return value.
  211. */
  212. public int getResultIndex()
  213. {
  214. return _resultIndex;
  215. }
  216. }