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.extensions;
  58. import java.util.Hashtable;
  59. import java.util.Vector;
  60. import org.apache.xml.utils.StringVector;
  61. import org.apache.xpath.objects.XNull;
  62. import org.apache.xpath.XPathProcessorException;
  63. import org.apache.xalan.res.XSLMessages;
  64. import org.apache.xalan.res.XSLTErrorResources;
  65. import org.apache.xalan.transformer.TransformerImpl;
  66. import org.apache.xalan.templates.Constants;
  67. import org.apache.xalan.templates.ElemTemplateElement;
  68. import org.apache.xalan.templates.ElemTemplate;
  69. import org.apache.xalan.templates.StylesheetRoot;
  70. import org.apache.xalan.templates.TemplateList;
  71. import org.apache.xpath.XPathContext;
  72. import org.apache.xml.utils.QName;
  73. import org.w3c.dom.Node;
  74. import org.w3c.dom.NodeList;
  75. /**
  76. * <meta name="usage" content="internal"/>
  77. * Class holding a table registered extension namespace handlers
  78. */
  79. public class ExtensionsTable
  80. {
  81. /**
  82. * <meta name="usage" content="internal"/>
  83. * Table of extensions that may be called from the expression language
  84. * via the call(name, ...) function. Objects are keyed on the call
  85. * name.
  86. */
  87. public Hashtable m_extensionFunctionNamespaces = new Hashtable();
  88. /**
  89. * The StylesheetRoot associated with this extensions table.
  90. */
  91. private StylesheetRoot m_sroot;
  92. /**
  93. * <meta name="usage" content="advanced"/>
  94. * The constructor (called from TransformerImpl) registers the
  95. * StylesheetRoot for the transformation and instantiates an
  96. * ExtensionHandler for each extension namespace.
  97. */
  98. public ExtensionsTable(StylesheetRoot sroot)
  99. throws javax.xml.transform.TransformerException
  100. {
  101. m_sroot = sroot;
  102. Vector extensions = m_sroot.getExtensions();
  103. for (int i = 0; i < extensions.size(); i++)
  104. {
  105. ExtensionNamespaceSupport extNamespaceSpt =
  106. (ExtensionNamespaceSupport)extensions.elementAt(i);
  107. ExtensionHandler extHandler = extNamespaceSpt.launch();
  108. if (extHandler != null)
  109. addExtensionNamespace(extNamespaceSpt.getNamespace(), extHandler);
  110. }
  111. }
  112. /**
  113. * Get an ExtensionHandler object that represents the
  114. * given namespace.
  115. * @param extns A valid extension namespace.
  116. *
  117. * @return ExtensionHandler object that represents the
  118. * given namespace.
  119. */
  120. public ExtensionHandler get(String extns)
  121. {
  122. return (ExtensionHandler) m_extensionFunctionNamespaces.get(extns);
  123. }
  124. /**
  125. * <meta name="usage" content="advanced"/>
  126. * Register an extension namespace handler. This handler provides
  127. * functions for testing whether a function is known within the
  128. * namespace and also for invoking the functions.
  129. *
  130. * @param uri the URI for the extension.
  131. * @param extNS the extension handler.
  132. */
  133. public void addExtensionNamespace(String uri, ExtensionHandler extNS)
  134. {
  135. m_extensionFunctionNamespaces.put(uri, extNS);
  136. }
  137. /**
  138. * Execute the function-available() function.
  139. * @param ns the URI of namespace in which the function is needed
  140. * @param funcName the function name being tested
  141. *
  142. * @return whether the given function is available or not.
  143. *
  144. * @throws javax.xml.transform.TransformerException
  145. */
  146. public boolean functionAvailable(String ns, String funcName)
  147. throws javax.xml.transform.TransformerException
  148. {
  149. boolean isAvailable = false;
  150. if (null != ns)
  151. {
  152. ExtensionHandler extNS =
  153. (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
  154. if (extNS != null)
  155. isAvailable = extNS.isFunctionAvailable(funcName);
  156. }
  157. return isAvailable;
  158. }
  159. /**
  160. * Execute the element-available() function.
  161. * @param ns the URI of namespace in which the function is needed
  162. * @param elemName name of element being tested
  163. *
  164. * @return whether the given element is available or not.
  165. *
  166. * @throws javax.xml.transform.TransformerException
  167. */
  168. public boolean elementAvailable(String ns, String elemName)
  169. throws javax.xml.transform.TransformerException
  170. {
  171. boolean isAvailable = false;
  172. if (null != ns)
  173. {
  174. ExtensionHandler extNS =
  175. (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
  176. if (extNS != null) // defensive
  177. isAvailable = extNS.isElementAvailable(elemName);
  178. }
  179. return isAvailable;
  180. }
  181. /**
  182. * Handle an extension function.
  183. * @param ns the URI of namespace in which the function is needed
  184. * @param funcName the function name being called
  185. * @param argVec arguments to the function in a vector
  186. * @param methodKey a unique key identifying this function instance in the
  187. * stylesheet
  188. * @param exprContext a context which may be passed to an extension function
  189. * and provides callback functions to access various
  190. * areas in the environment
  191. *
  192. * @return result of executing the function
  193. *
  194. * @throws javax.xml.transform.TransformerException
  195. */
  196. public Object extFunction(String ns, String funcName,
  197. Vector argVec, Object methodKey,
  198. ExpressionContext exprContext)
  199. throws javax.xml.transform.TransformerException
  200. {
  201. Object result = null;
  202. if (null != ns)
  203. {
  204. ExtensionHandler extNS =
  205. (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
  206. if (null != extNS)
  207. {
  208. try
  209. {
  210. result = extNS.callFunction(funcName, argVec, methodKey,
  211. exprContext);
  212. }
  213. catch (javax.xml.transform.TransformerException e)
  214. {
  215. throw e;
  216. }
  217. catch (Exception e)
  218. {
  219. throw new javax.xml.transform.TransformerException(e);
  220. }
  221. }
  222. else
  223. {
  224. throw new XPathProcessorException(XSLMessages.createMessage(XSLTErrorResources.ER_EXTENSION_FUNC_UNKNOWN, new Object[]{ns, funcName }));
  225. //"Extension function '" + ns + ":" + funcName + "' is unknown");
  226. }
  227. }
  228. return result;
  229. }
  230. }