KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > extensions > ExtensionsTable


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ExtensionsTable.java,v 1.24 2004/02/11 05:26:23 minchau Exp $
18  */

19 package org.apache.xalan.extensions;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.xalan.res.XSLMessages;
25 import org.apache.xalan.res.XSLTErrorResources;
26 import org.apache.xalan.templates.StylesheetRoot;
27 import org.apache.xpath.XPathProcessorException;
28 import org.apache.xpath.functions.FuncExtFunction;
29
30 /**
31  * Class holding a table registered extension namespace handlers
32  * @xsl.usage internal
33  */

34 public class ExtensionsTable
35 {
36   /**
37    * Table of extensions that may be called from the expression language
38    * via the call(name, ...) function. Objects are keyed on the call
39    * name.
40    * @xsl.usage internal
41    */

42   public Hashtable JavaDoc m_extensionFunctionNamespaces = new Hashtable JavaDoc();
43   
44   /**
45    * The StylesheetRoot associated with this extensions table.
46    */

47   private StylesheetRoot m_sroot;
48   
49   /**
50    * The constructor (called from TransformerImpl) registers the
51    * StylesheetRoot for the transformation and instantiates an
52    * ExtensionHandler for each extension namespace.
53    * @xsl.usage advanced
54    */

55   public ExtensionsTable(StylesheetRoot sroot)
56     throws javax.xml.transform.TransformerException JavaDoc
57   {
58     m_sroot = sroot;
59     Vector JavaDoc extensions = m_sroot.getExtensions();
60     for (int i = 0; i < extensions.size(); i++)
61     {
62       ExtensionNamespaceSupport extNamespaceSpt =
63                  (ExtensionNamespaceSupport)extensions.elementAt(i);
64       ExtensionHandler extHandler = extNamespaceSpt.launch();
65         if (extHandler != null)
66           addExtensionNamespace(extNamespaceSpt.getNamespace(), extHandler);
67       }
68     }
69        
70   /**
71    * Get an ExtensionHandler object that represents the
72    * given namespace.
73    * @param extns A valid extension namespace.
74    *
75    * @return ExtensionHandler object that represents the
76    * given namespace.
77    */

78   public ExtensionHandler get(String JavaDoc extns)
79   {
80     return (ExtensionHandler) m_extensionFunctionNamespaces.get(extns);
81   }
82
83   /**
84    * Register an extension namespace handler. This handler provides
85    * functions for testing whether a function is known within the
86    * namespace and also for invoking the functions.
87    *
88    * @param uri the URI for the extension.
89    * @param extNS the extension handler.
90    * @xsl.usage advanced
91    */

92   public void addExtensionNamespace(String JavaDoc uri, ExtensionHandler extNS)
93   {
94     m_extensionFunctionNamespaces.put(uri, extNS);
95   }
96
97   /**
98    * Execute the function-available() function.
99    * @param ns the URI of namespace in which the function is needed
100    * @param funcName the function name being tested
101    *
102    * @return whether the given function is available or not.
103    *
104    * @throws javax.xml.transform.TransformerException
105    */

106   public boolean functionAvailable(String JavaDoc ns, String JavaDoc funcName)
107           throws javax.xml.transform.TransformerException JavaDoc
108   {
109     boolean isAvailable = false;
110     
111     if (null != ns)
112     {
113       ExtensionHandler extNS =
114            (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
115       if (extNS != null)
116         isAvailable = extNS.isFunctionAvailable(funcName);
117     }
118     return isAvailable;
119   }
120   
121   /**
122    * Execute the element-available() function.
123    * @param ns the URI of namespace in which the function is needed
124    * @param elemName name of element being tested
125    *
126    * @return whether the given element is available or not.
127    *
128    * @throws javax.xml.transform.TransformerException
129    */

130   public boolean elementAvailable(String JavaDoc ns, String JavaDoc elemName)
131           throws javax.xml.transform.TransformerException JavaDoc
132   {
133     boolean isAvailable = false;
134     if (null != ns)
135     {
136       ExtensionHandler extNS =
137                (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
138       if (extNS != null) // defensive
139
isAvailable = extNS.isElementAvailable(elemName);
140     }
141     return isAvailable;
142   }
143   
144   /**
145    * Handle an extension function.
146    * @param ns the URI of namespace in which the function is needed
147    * @param funcName the function name being called
148    * @param argVec arguments to the function in a vector
149    * @param methodKey a unique key identifying this function instance in the
150    * stylesheet
151    * @param exprContext a context which may be passed to an extension function
152    * and provides callback functions to access various
153    * areas in the environment
154    *
155    * @return result of executing the function
156    *
157    * @throws javax.xml.transform.TransformerException
158    */

159   public Object JavaDoc extFunction(String JavaDoc ns, String JavaDoc funcName,
160                             Vector JavaDoc argVec, Object JavaDoc methodKey,
161                             ExpressionContext exprContext)
162             throws javax.xml.transform.TransformerException JavaDoc
163   {
164     Object JavaDoc result = null;
165     if (null != ns)
166     {
167       ExtensionHandler extNS =
168         (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
169       if (null != extNS)
170       {
171         try
172         {
173           result = extNS.callFunction(funcName, argVec, methodKey,
174                                       exprContext);
175         }
176         catch (javax.xml.transform.TransformerException JavaDoc e)
177         {
178           throw e;
179         }
180         catch (Exception JavaDoc e)
181         {
182           throw new javax.xml.transform.TransformerException JavaDoc(e);
183         }
184       }
185       else
186       {
187         throw new XPathProcessorException(XSLMessages.createMessage(XSLTErrorResources.ER_EXTENSION_FUNC_UNKNOWN, new Object JavaDoc[]{ns, funcName }));
188         //"Extension function '" + ns + ":" + funcName + "' is unknown");
189
}
190     }
191     return result;
192   }
193   
194   /**
195    * Handle an extension function.
196    * @param extFunction the extension function
197    * @param argVec arguments to the function in a vector
198    * @param exprContext a context which may be passed to an extension function
199    * and provides callback functions to access various
200    * areas in the environment
201    *
202    * @return result of executing the function
203    *
204    * @throws javax.xml.transform.TransformerException
205    */

206   public Object JavaDoc extFunction(FuncExtFunction extFunction, Vector JavaDoc argVec,
207                             ExpressionContext exprContext)
208          throws javax.xml.transform.TransformerException JavaDoc
209   {
210     Object JavaDoc result = null;
211     String JavaDoc ns = extFunction.getNamespace();
212     if (null != ns)
213     {
214       ExtensionHandler extNS =
215         (ExtensionHandler) m_extensionFunctionNamespaces.get(ns);
216       if (null != extNS)
217       {
218         try
219         {
220           result = extNS.callFunction(extFunction, argVec, exprContext);
221         }
222         catch (javax.xml.transform.TransformerException JavaDoc e)
223         {
224           throw e;
225         }
226         catch (Exception JavaDoc e)
227         {
228           throw new javax.xml.transform.TransformerException JavaDoc(e);
229         }
230       }
231       else
232       {
233         throw new XPathProcessorException(XSLMessages.createMessage(XSLTErrorResources.ER_EXTENSION_FUNC_UNKNOWN,
234                                           new Object JavaDoc[]{ns, extFunction.getFunctionName()}));
235       }
236     }
237     return result;
238   }
239 }
240
Popular Tags