001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ExtensionHandler.java 468637 2006-10-28 06:51:02Z minchau $
020     */
021    package org.apache.xalan.extensions;
022    
023    import java.io.IOException;
024    import java.util.Vector;
025    
026    import javax.xml.transform.TransformerException;
027    
028    import org.apache.xalan.templates.ElemTemplateElement;
029    import org.apache.xalan.templates.Stylesheet;
030    import org.apache.xalan.transformer.TransformerImpl;
031    import org.apache.xpath.functions.FuncExtFunction;
032    
033    /**
034     * Abstract base class for handling an extension namespace for XPath.
035     * Provides functions to test a function's existence and call a function.
036     * Also provides functions for calling an element and testing for
037     * an element's existence.
038     *
039     * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
040     * @xsl.usage internal
041     */
042    public abstract class ExtensionHandler
043    {
044    
045      /** uri of the extension namespace */
046      protected String m_namespaceUri; 
047    
048      /** scripting language of implementation */
049      protected String m_scriptLang;
050    
051      /**
052       * This method loads a class using the context class loader if we're
053       * running under Java2 or higher.
054       * 
055       * @param className Name of the class to load
056       */
057      static Class getClassForName(String className)
058          throws ClassNotFoundException
059      {
060        // Hack for backwards compatibility with XalanJ1 stylesheets
061        if(className.equals("org.apache.xalan.xslt.extensions.Redirect")) {
062          className = "org.apache.xalan.lib.Redirect";
063        }
064    
065        return ObjectFactory.findProviderClass(
066            className, ObjectFactory.findClassLoader(), true);
067      }
068    
069      /**
070       * Construct a new extension namespace handler given all the information
071       * needed.
072       *
073       * @param namespaceUri the extension namespace URI that I'm implementing
074       * @param scriptLang   language of code implementing the extension
075       */
076      protected ExtensionHandler(String namespaceUri, String scriptLang)
077      {
078        m_namespaceUri = namespaceUri;
079        m_scriptLang = scriptLang;
080      }
081    
082      /**
083       * Tests whether a certain function name is known within this namespace.
084       * @param function name of the function being tested
085       * @return true if its known, false if not.
086       */
087      public abstract boolean isFunctionAvailable(String function);
088    
089      /**
090       * Tests whether a certain element name is known within this namespace.
091       * @param element Name of element to check
092       * @return true if its known, false if not.
093       */
094      public abstract boolean isElementAvailable(String element);
095    
096      /**
097       * Process a call to a function.
098       *
099       * @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       */
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       */
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       */
144      public abstract void processElement(
145        String localPart, ElemTemplateElement element, TransformerImpl transformer,
146          Stylesheet stylesheetTree, Object methodKey) throws TransformerException, IOException;
147    }