1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: ExtensionHandler.java 468637 2006-10-28 06:51:02Z minchau $
20  */
21 package org.apache.xalan.extensions;
22 
23 import java.io.IOException;
24 import java.util.Vector;
25 
26 import javax.xml.transform.TransformerException;
27 
28 import org.apache.xalan.templates.ElemTemplateElement;
29 import org.apache.xalan.templates.Stylesheet;
30 import org.apache.xalan.transformer.TransformerImpl;
31 import org.apache.xpath.functions.FuncExtFunction;
32 
33 /**
34  * Abstract base class for handling an extension namespace for XPath.
35  * Provides functions to test a function's existence and call a function.
36  * Also provides functions for calling an element and testing for
37  * an element's existence.
38  *
39  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
40  * @xsl.usage internal
41  */
42 public abstract class ExtensionHandler
43 {
44 
45   /** uri of the extension namespace */
46   protected String m_namespaceUri;
47 
48   /** scripting language of implementation */
49   protected String m_scriptLang;
50 
51   /**
52    * This method loads a class using the context class loader if we're
53    * running under Java2 or higher.
54    *
55    * @param className Name of the class to load
56    */
getClassForName(String className)57   static Class getClassForName(String className)
58       throws ClassNotFoundException
59   {
60     // Hack for backwards compatibility with XalanJ1 stylesheets
61     if(className.equals("org.apache.xalan.xslt.extensions.Redirect")) {
62       className = "org.apache.xalan.lib.Redirect";
63     }
64 
65     return ObjectFactory.findProviderClass(
66         className, ObjectFactory.findClassLoader(), true);
67   }
68 
69   /**
70    * Construct a new extension namespace handler given all the information
71    * needed.
72    *
73    * @param namespaceUri the extension namespace URI that I'm implementing
74    * @param scriptLang   language of code implementing the extension
75    */
ExtensionHandler(String namespaceUri, String scriptLang)76   protected ExtensionHandler(String namespaceUri, String scriptLang)
77   {
78     m_namespaceUri = namespaceUri;
79     m_scriptLang = scriptLang;
80   }
81 
82   /**
83    * Tests whether a certain function name is known within this namespace.
84    * @param function name of the function being tested
85    * @return true if its known, false if not.
86    */
isFunctionAvailable(String function)87   public abstract boolean isFunctionAvailable(String function);
88 
89   /**
90    * Tests whether a certain element name is known within this namespace.
91    * @param element Name of element to check
92    * @return true if its known, false if not.
93    */
isElementAvailable(String element)94   public abstract boolean isElementAvailable(String element);
95 
96   /**
97    * Process a call to a function.
98    *
99    * @param funcName Function name.
100    * @param args     The arguments of the function call.
101    * @param methodKey A key that uniquely identifies this class and method call.
102    * @param exprContext The context in which this expression is being executed.
103    *
104    * @return the return value of the function evaluation.
105    *
106    * @throws TransformerException          if parsing trouble
107    */
callFunction( String funcName, Vector args, Object methodKey, ExpressionContext exprContext)108   public abstract Object callFunction(
109     String funcName, Vector args, Object methodKey,
110       ExpressionContext exprContext) throws TransformerException;
111 
112   /**
113    * Process a call to a function.
114    *
115    * @param extFunction The XPath extension function.
116    * @param args     The arguments of the function call.
117    * @param exprContext The context in which this expression is being executed.
118    *
119    * @return the return value of the function evaluation.
120    *
121    * @throws TransformerException          if parsing trouble
122    */
callFunction( FuncExtFunction extFunction, Vector args, ExpressionContext exprContext)123   public abstract Object callFunction(
124     FuncExtFunction extFunction, Vector args,
125       ExpressionContext exprContext) throws TransformerException;
126 
127   /**
128    * Process a call to this extension namespace via an element. As a side
129    * effect, the results are sent to the TransformerImpl's result tree.
130    *
131    * @param localPart      Element name's local part.
132    * @param element        The extension element being processed.
133    * @param transformer    Handle to TransformerImpl.
134    * @param stylesheetTree The compiled stylesheet tree.
135    * @param methodKey      A key that uniquely identifies this class and method call.
136    *
137    * @throws XSLProcessorException thrown if something goes wrong
138    *            while running the extension handler.
139    * @throws MalformedURLException if loading trouble
140    * @throws FileNotFoundException if loading trouble
141    * @throws IOException           if loading trouble
142    * @throws TransformerException  if parsing trouble
143    */
processElement( String localPart, ElemTemplateElement element, TransformerImpl transformer, Stylesheet stylesheetTree, Object methodKey)144   public abstract void processElement(
145     String localPart, ElemTemplateElement element, TransformerImpl transformer,
146       Stylesheet stylesheetTree, Object methodKey) throws TransformerException, IOException;
147 }
148