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.Vector;
  59. import java.lang.reflect.Method;
  60. import java.io.IOException;
  61. import javax.xml.transform.TransformerException;
  62. import org.w3c.dom.Element;
  63. import org.w3c.dom.Node;
  64. import org.apache.xalan.templates.Stylesheet;
  65. import org.apache.xalan.templates.ElemTemplateElement;
  66. import org.apache.xml.utils.QName;
  67. // Temp??
  68. import org.apache.xalan.transformer.TransformerImpl;
  69. /**
  70. * <meta name="usage" content="internal"/>
  71. * Abstract base class for handling an extension namespace for XPath.
  72. * Provides functions to test a function's existence and call a function.
  73. * Also provides functions for calling an element and testing for
  74. * an element's existence.
  75. *
  76. * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
  77. */
  78. public abstract class ExtensionHandler
  79. {
  80. /** uri of the extension namespace */
  81. protected String m_namespaceUri;
  82. /** scripting language of implementation */
  83. protected String m_scriptLang;
  84. /** a zero length Object array used in getClassForName() */
  85. private static final Object NO_OBJS[] = new Object[0];
  86. /** the Method object for getContextClassLoader */
  87. private static Method getCCL;
  88. static
  89. {
  90. try
  91. {
  92. getCCL = Thread.class.getMethod("getContextClassLoader", new Class[0]);
  93. }
  94. catch (Exception e)
  95. {
  96. getCCL = null;
  97. }
  98. }
  99. /**
  100. * Replacement for Class.forName. This method loads a class using the context class loader
  101. * if we're running under Java2 or higher. If we're running under Java1, this
  102. * method just uses Class.forName to load the class.
  103. *
  104. * @param className Name of the class to load
  105. */
  106. public static Class getClassForName(String className)
  107. throws ClassNotFoundException
  108. {
  109. Class result = null;
  110. // Hack for backwards compatibility with XalanJ1 stylesheets
  111. if(className.equals("org.apache.xalan.xslt.extensions.Redirect"))
  112. className = "org.apache.xalan.lib.Redirect";
  113. if (getCCL != null)
  114. {
  115. try {
  116. ClassLoader contextClassLoader =
  117. (ClassLoader) getCCL.invoke(Thread.currentThread(), NO_OBJS);
  118. result = contextClassLoader.loadClass(className);
  119. }
  120. catch (ClassNotFoundException cnfe)
  121. {
  122. result = Class.forName(className);
  123. }
  124. catch (Exception e)
  125. {
  126. getCCL = null;
  127. result = Class.forName(className);
  128. }
  129. }
  130. else
  131. result = Class.forName(className);
  132. return result;
  133. }
  134. /**
  135. * Construct a new extension namespace handler given all the information
  136. * needed.
  137. *
  138. * @param namespaceUri the extension namespace URI that I'm implementing
  139. * @param scriptLang language of code implementing the extension
  140. */
  141. protected ExtensionHandler(String namespaceUri, String scriptLang)
  142. {
  143. m_namespaceUri = namespaceUri;
  144. m_scriptLang = scriptLang;
  145. }
  146. /**
  147. * Tests whether a certain function name is known within this namespace.
  148. * @param function name of the function being tested
  149. * @return true if its known, false if not.
  150. */
  151. public abstract boolean isFunctionAvailable(String function);
  152. /**
  153. * Tests whether a certain element name is known within this namespace.
  154. * @param function name of the function being tested
  155. *
  156. * @param element Name of element to check
  157. * @return true if its known, false if not.
  158. */
  159. public abstract boolean isElementAvailable(String element);
  160. /**
  161. * Process a call to a function.
  162. *
  163. * @param funcName Function name.
  164. * @param args The arguments of the function call.
  165. * @param methodKey A key that uniquely identifies this class and method call.
  166. * @param exprContext The context in which this expression is being executed.
  167. *
  168. * @return the return value of the function evaluation.
  169. *
  170. * @throws TransformerException if parsing trouble
  171. */
  172. public abstract Object callFunction(
  173. String funcName, Vector args, Object methodKey,
  174. ExpressionContext exprContext) throws TransformerException;
  175. /**
  176. * Process a call to this extension namespace via an element. As a side
  177. * effect, the results are sent to the TransformerImpl's result tree.
  178. *
  179. * @param localPart Element name's local part.
  180. * @param element The extension element being processed.
  181. * @param transformer Handle to TransformerImpl.
  182. * @param stylesheetTree The compiled stylesheet tree.
  183. * @param mode The current mode.
  184. * @param sourceTree The root of the source tree (but don't assume
  185. * it's a Document).
  186. * @param sourceNode The current context node.
  187. * @param methodKey A key that uniquely identifies this class and method call.
  188. *
  189. * @throws XSLProcessorException thrown if something goes wrong
  190. * while running the extension handler.
  191. * @throws MalformedURLException if loading trouble
  192. * @throws FileNotFoundException if loading trouble
  193. * @throws IOException if loading trouble
  194. * @throws TransformerException if parsing trouble
  195. */
  196. public abstract void processElement(
  197. String localPart, ElemTemplateElement element, TransformerImpl transformer,
  198. Stylesheet stylesheetTree, Object methodKey) throws TransformerException, IOException;
  199. }