KickJava   Java API By Example, From Geeks To Geeks.

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


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: ExtensionNamespacesManager.java,v 1.8 2004/02/11 05:26:23 minchau Exp $
18  */

19 package org.apache.xalan.extensions;
20
21 import java.util.Vector JavaDoc;
22
23 import org.apache.xalan.templates.Constants;
24
25 /**
26  * Used during assembly of a stylesheet to collect the information for each
27  * extension namespace that is required during the transformation process
28  * to generate an {@link ExtensionHandler}.
29  *
30  */

31 public class ExtensionNamespacesManager
32 {
33   /**
34    * Vector of ExtensionNamespaceSupport objects to be used to generate ExtensionHandlers.
35    */

36   private Vector JavaDoc m_extensions = new Vector JavaDoc();
37   /**
38    * Vector of ExtensionNamespaceSupport objects for predefined ExtensionNamespaces. Elements
39    * from this vector are added to the m_extensions vector when encountered in the stylesheet.
40    */

41   private Vector JavaDoc m_predefExtensions = new Vector JavaDoc(7);
42   /**
43    * Vector of extension namespaces for which sufficient information is not yet available to
44    * complete the registration process.
45    */

46   private Vector JavaDoc m_unregisteredExtensions = new Vector JavaDoc();
47   
48   /**
49    * An ExtensionNamespacesManager is instantiated the first time an extension function or
50    * element is found in the stylesheet. During initialization, a vector of ExtensionNamespaceSupport
51    * objects is created, one for each predefined extension namespace.
52    */

53   public ExtensionNamespacesManager()
54   {
55     setPredefinedNamespaces();
56   }
57   
58   /**
59    * If necessary, register the extension namespace found compiling a function or
60    * creating an extension element.
61    *
62    * If it is a predefined namespace, create a
63    * support object to simplify the instantiate of an appropriate ExtensionHandler
64    * during transformation runtime. Otherwise, add the namespace, if necessary,
65    * to a vector of undefined extension namespaces, to be defined later.
66    *
67    */

68   public void registerExtension(String JavaDoc namespace)
69   {
70     if (namespaceIndex(namespace, m_extensions) == -1)
71     {
72       int predef = namespaceIndex(namespace, m_predefExtensions);
73       if (predef !=-1)
74         m_extensions.addElement(m_predefExtensions.elementAt(predef));
75       else if (!(m_unregisteredExtensions.contains(namespace)))
76         m_unregisteredExtensions.addElement(namespace);
77     }
78   }
79   
80   /**
81    * Register the extension namespace for an ElemExtensionDecl or ElemFunction,
82    * and prepare a support object to launch the appropriate ExtensionHandler at
83    * transformation runtime.
84    */

85   public void registerExtension(ExtensionNamespaceSupport extNsSpt)
86   {
87     String JavaDoc namespace = extNsSpt.getNamespace();
88     if (namespaceIndex(namespace, m_extensions) == -1)
89     {
90       m_extensions.addElement(extNsSpt);
91       if (m_unregisteredExtensions.contains(namespace))
92         m_unregisteredExtensions.removeElement(namespace);
93     }
94     
95   }
96   
97   /**
98    * Get the index for a namespace entry in the extension namespace Vector, -1 if
99    * no such entry yet exists.
100    */

101   public int namespaceIndex(String JavaDoc namespace, Vector JavaDoc extensions)
102   {
103     for (int i = 0; i < extensions.size(); i++)
104     {
105       if (((ExtensionNamespaceSupport)extensions.elementAt(i)).getNamespace().equals(namespace))
106         return i;
107     }
108     return -1;
109   }
110   
111     
112   /**
113    * Get the vector of extension namespaces. Used to provide
114    * the extensions table access to a list of extension
115    * namespaces encountered during composition of a stylesheet.
116    */

117   public Vector JavaDoc getExtensions()
118   {
119     return m_extensions;
120   }
121   
122   /**
123    * Attempt to register any unregistered extension namespaces.
124    */

125   public void registerUnregisteredNamespaces()
126   {
127     for (int i = 0; i < m_unregisteredExtensions.size(); i++)
128     {
129       String JavaDoc ns = (String JavaDoc)m_unregisteredExtensions.elementAt(i);
130       ExtensionNamespaceSupport extNsSpt = defineJavaNamespace(ns);
131       if (extNsSpt != null)
132         m_extensions.addElement(extNsSpt);
133     }
134   }
135   
136     /**
137    * For any extension namespace that is not either predefined or defined
138    * by a "component" declaration or exslt function declaration, attempt
139    * to create an ExtensionNamespaceSuport object for the appropriate
140    * Java class or Java package Extension Handler.
141    *
142    * Called by StylesheetRoot.recompose(), after all ElemTemplate compose()
143    * operations have taken place, in order to set up handlers for
144    * the remaining extension namespaces.
145    *
146    * @param ns The extension namespace URI.
147    * @return An ExtensionNamespaceSupport object for this namespace
148    * (which defines the ExtensionHandler to be used), or null if such
149    * an object cannot be created.
150    *
151    * @throws javax.xml.transform.TransformerException
152    */

153   public ExtensionNamespaceSupport defineJavaNamespace(String JavaDoc ns)
154   {
155     return defineJavaNamespace(ns, ns);
156   }
157   public ExtensionNamespaceSupport defineJavaNamespace(String JavaDoc ns, String JavaDoc classOrPackage)
158   {
159     if(null == ns || ns.trim().length() == 0) // defensive. I don't think it's needed. -sb
160
return null;
161
162     // Prepare the name of the actual class or package, stripping
163
// out any leading "class:". Next, see if there is a /. If so,
164
// only look at the text to the right of the rightmost /.
165
String JavaDoc className = classOrPackage;
166     if (className.startsWith("class:"))
167       className = className.substring(6);
168
169     int lastSlash = className.lastIndexOf("/");
170     if (-1 != lastSlash)
171       className = className.substring(lastSlash + 1);
172       
173     // The className can be null here, and can cause an error in getClassForName
174
// in JDK 1.8.
175
if(null == className || className.trim().length() == 0)
176       return null;
177     
178     try
179     {
180       ExtensionHandler.getClassForName(className);
181       return new ExtensionNamespaceSupport(
182                            ns,
183                            "org.apache.xalan.extensions.ExtensionHandlerJavaClass",
184                            new Object JavaDoc[]{ns, "javaclass", className});
185     }
186     catch (ClassNotFoundException JavaDoc e)
187     {
188       return new ExtensionNamespaceSupport(
189                             ns,
190                             "org.apache.xalan.extensions.ExtensionHandlerJavaPackage",
191                             new Object JavaDoc[]{ns, "javapackage", className + "."});
192     }
193   }
194   
195 /*
196   public ExtensionNamespaceSupport getSupport(int index, Vector extensions)
197   {
198     return (ExtensionNamespaceSupport)extensions.elementAt(index);
199   }
200 */

201   
202   
203   /**
204    * Set up a Vector for predefined extension namespaces.
205    */

206   private void setPredefinedNamespaces()
207   {
208     String JavaDoc uri = Constants.S_EXTENSIONS_JAVA_URL;
209     String JavaDoc handlerClassName = "org.apache.xalan.extensions.ExtensionHandlerJavaPackage";
210     String JavaDoc lang = "javapackage";
211     String JavaDoc lib = "";
212     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
213                                              new Object JavaDoc[]{uri, lang, lib}));
214    
215     uri = Constants.S_EXTENSIONS_OLD_JAVA_URL;
216     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
217                                              new Object JavaDoc[]{uri, lang, lib}));
218     
219     uri = Constants.S_EXTENSIONS_LOTUSXSL_JAVA_URL;
220     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
221                                              new Object JavaDoc[]{uri, lang, lib}));
222     
223     uri = Constants.S_BUILTIN_EXTENSIONS_URL;
224     handlerClassName = "org.apache.xalan.extensions.ExtensionHandlerJavaClass";
225     lang = "javaclass"; // for remaining predefined extension namespaces.
226
lib = "org.apache.xalan.lib.Extensions";
227     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
228                                              new Object JavaDoc[]{uri, lang, lib}));
229     
230     uri = Constants.S_BUILTIN_OLD_EXTENSIONS_URL;
231     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
232                                              new Object JavaDoc[]{uri, lang, lib}));
233     
234     // Xalan extension namespaces (redirect, pipe and SQL).
235
uri = Constants.S_EXTENSIONS_REDIRECT_URL;
236     lib = "org.apache.xalan.lib.Redirect";
237     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
238                                              new Object JavaDoc[]{uri, lang, lib}));
239  
240     uri = Constants.S_EXTENSIONS_PIPE_URL;
241     lib = "org.apache.xalan.lib.PipeDocument";
242     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
243                                              new Object JavaDoc[]{uri, lang, lib}));
244  
245     uri = Constants.S_EXTENSIONS_SQL_URL;
246     lib = "org.apache.xalan.lib.sql.XConnection";
247     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
248                                              new Object JavaDoc[]{uri, lang, lib}));
249  
250     
251     //EXSLT namespaces (not including EXSLT function namespaces which are
252
// registered by the associated ElemFunction.
253
uri = Constants.S_EXSLT_COMMON_URL;
254     lib = "org.apache.xalan.lib.ExsltCommon";
255     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
256                                              new Object JavaDoc[]{uri, lang, lib}));
257
258     uri = Constants.S_EXSLT_MATH_URL;
259     lib = "org.apache.xalan.lib.ExsltMath";
260     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
261                                              new Object JavaDoc[]{uri, lang, lib}));
262     
263     uri = Constants.S_EXSLT_SETS_URL;
264     lib = "org.apache.xalan.lib.ExsltSets";
265     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
266                                              new Object JavaDoc[]{uri, lang, lib}));
267     
268     uri = Constants.S_EXSLT_DATETIME_URL;
269     lib = "org.apache.xalan.lib.ExsltDatetime";
270     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
271                                              new Object JavaDoc[]{uri, lang, lib}));
272                                              
273     uri = Constants.S_EXSLT_DYNAMIC_URL;
274     lib = "org.apache.xalan.lib.ExsltDynamic";
275     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
276                                              new Object JavaDoc[]{uri, lang, lib}));
277
278     uri = Constants.S_EXSLT_STRINGS_URL;
279     lib = "org.apache.xalan.lib.ExsltStrings";
280     m_predefExtensions.addElement(new ExtensionNamespaceSupport(uri, handlerClassName,
281                                              new Object JavaDoc[]{uri, lang, lib}));
282   }
283   
284 }
285
Popular Tags