KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > ElemExtensionDecl


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: ElemExtensionDecl.java,v 1.21 2004/02/16 20:32:33 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.extensions.ExtensionNamespaceSupport;
24 import org.apache.xalan.extensions.ExtensionNamespacesManager;
25 import org.apache.xalan.res.XSLMessages;
26 import org.apache.xalan.res.XSLTErrorResources;
27 import org.apache.xalan.transformer.TransformerImpl;
28 import org.apache.xml.utils.StringVector;
29
30 /**
31  * Implement the declaration of an extension element
32  * @xsl.usage internal
33  */

34 public class ElemExtensionDecl extends ElemTemplateElement
35 {
36
37   /**
38    * Constructor ElemExtensionDecl
39    *
40    */

41   public ElemExtensionDecl()
42   {
43
44     // System.out.println("ElemExtensionDecl ctor");
45
}
46
47   /** Prefix string for this extension element.
48    * @serial */

49   private String JavaDoc m_prefix = null;
50
51   /**
52    * Set the prefix for this extension element
53    *
54    *
55    * @param v Prefix to set for this extension element
56    */

57   public void setPrefix(String JavaDoc v)
58   {
59     m_prefix = v;
60   }
61
62   /**
63    * Get the prefix for this extension element
64    *
65    *
66    * @return Prefix for this extension element
67    */

68   public String JavaDoc getPrefix()
69   {
70     return m_prefix;
71   }
72
73   /** StringVector holding the names of functions defined in this extension.
74    * @serial */

75   private StringVector m_functions = new StringVector();
76
77   /**
78    * Set the names of functions defined in this extension
79    *
80    *
81    * @param v StringVector holding the names of functions defined in this extension
82    */

83   public void setFunctions(StringVector v)
84   {
85     m_functions = v;
86   }
87
88   /**
89    * Get the names of functions defined in this extension
90    *
91    *
92    * @return StringVector holding the names of functions defined in this extension
93    */

94   public StringVector getFunctions()
95   {
96     return m_functions;
97   }
98
99   /**
100    * Get a function at a given index in this extension element
101    *
102    *
103    * @param i Index of function to get
104    *
105    * @return Name of Function at given index
106    *
107    * @throws ArrayIndexOutOfBoundsException
108    */

109   public String JavaDoc getFunction(int i) throws ArrayIndexOutOfBoundsException JavaDoc
110   {
111
112     if (null == m_functions)
113       throw new ArrayIndexOutOfBoundsException JavaDoc();
114
115     return (String JavaDoc) m_functions.elementAt(i);
116   }
117
118   /**
119    * Get count of functions defined in this extension element
120    *
121    *
122    * @return count of functions defined in this extension element
123    */

124   public int getFunctionCount()
125   {
126     return (null != m_functions) ? m_functions.size() : 0;
127   }
128
129   /** StringVector of elements defined in this extension.
130    * @serial */

131   private StringVector m_elements = null;
132
133   /**
134    * Set StringVector of elements for this extension
135    *
136    *
137    * @param v StringVector of elements to set
138    */

139   public void setElements(StringVector v)
140   {
141     m_elements = v;
142   }
143
144   /**
145    * Get StringVector of elements defined for this extension
146    *
147    *
148    * @return StringVector of elements defined for this extension
149    */

150   public StringVector getElements()
151   {
152     return m_elements;
153   }
154
155   /**
156    * Get the element at the given index
157    *
158    *
159    * @param i Index of element to get
160    *
161    * @return The element at the given index
162    *
163    * @throws ArrayIndexOutOfBoundsException
164    */

165   public String JavaDoc getElement(int i) throws ArrayIndexOutOfBoundsException JavaDoc
166   {
167
168     if (null == m_elements)
169       throw new ArrayIndexOutOfBoundsException JavaDoc();
170
171     return (String JavaDoc) m_elements.elementAt(i);
172   }
173
174   /**
175    * Return the count of elements defined for this extension element
176    *
177    *
178    * @return the count of elements defined for this extension element
179    */

180   public int getElementCount()
181   {
182     return (null != m_elements) ? m_elements.size() : 0;
183   }
184
185   /**
186    * Get an int constant identifying the type of element.
187    * @see org.apache.xalan.templates.Constants
188    *
189    * @return The token ID for this element
190    */

191   public int getXSLToken()
192   {
193     return Constants.ELEMNAME_EXTENSIONDECL;
194   }
195   
196   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
197   {
198     super.compose(sroot);
199     String JavaDoc prefix = getPrefix();
200     String JavaDoc declNamespace = getNamespaceForPrefix(prefix);
201     String JavaDoc lang = null;
202     String JavaDoc srcURL = null;
203     String JavaDoc scriptSrc = null;
204     if (null == declNamespace)
205       throw new TransformerException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_NO_NAMESPACE_DECL, new Object JavaDoc[]{prefix}));
206       //"Prefix " + prefix does not have a corresponding namespace declaration");
207
for (ElemTemplateElement child = getFirstChildElem(); child != null;
208           child = child.getNextSiblingElem())
209     {
210       if (Constants.ELEMNAME_EXTENSIONSCRIPT == child.getXSLToken())
211       {
212         ElemExtensionScript sdecl = (ElemExtensionScript) child;
213         lang = sdecl.getLang();
214         srcURL = sdecl.getSrc();
215         ElemTemplateElement childOfSDecl = sdecl.getFirstChildElem();
216         if (null != childOfSDecl)
217         {
218           if (Constants.ELEMNAME_TEXTLITERALRESULT
219                   == childOfSDecl.getXSLToken())
220           {
221             ElemTextLiteral tl = (ElemTextLiteral) childOfSDecl;
222             char[] chars = tl.getChars();
223             scriptSrc = new String JavaDoc(chars);
224             if (scriptSrc.trim().length() == 0)
225               scriptSrc = null;
226           }
227         }
228       }
229     }
230     if (null == lang)
231       lang = "javaclass";
232     if (lang.equals("javaclass") && (scriptSrc != null))
233         throw new TransformerException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_ELEM_CONTENT_NOT_ALLOWED, new Object JavaDoc[]{scriptSrc}));
234         //"Element content not allowed for lang=javaclass " + scriptSrc);
235

236     // Register the extension namespace if it has not already been registered.
237
ExtensionNamespaceSupport extNsSpt = null;
238     ExtensionNamespacesManager extNsMgr = sroot.getExtensionNamespacesManager();
239     if (extNsMgr.namespaceIndex(declNamespace,
240                                 extNsMgr.getExtensions()) == -1)
241     {
242       if (lang.equals("javaclass"))
243       {
244         if (null == srcURL)
245         {
246            extNsSpt = extNsMgr.defineJavaNamespace(declNamespace);
247         }
248         else if (extNsMgr.namespaceIndex(srcURL,
249                                          extNsMgr.getExtensions()) == -1)
250         {
251           extNsSpt = extNsMgr.defineJavaNamespace(declNamespace, srcURL);
252         }
253       }
254       else // not java
255
{
256         String JavaDoc handler = "org.apache.xalan.extensions.ExtensionHandlerGeneral";
257         Object JavaDoc [] args = {declNamespace, this.m_elements, this.m_functions,
258                           lang, srcURL, scriptSrc, getSystemId()};
259         extNsSpt = new ExtensionNamespaceSupport(declNamespace, handler, args);
260       }
261     }
262     if (extNsSpt != null)
263       extNsMgr.registerExtension(extNsSpt);
264   }
265
266   
267   /**
268    * This function will be called on top-level elements
269    * only, just before the transform begins.
270    *
271    * @param transformer The XSLT TransformerFactory.
272    *
273    * @throws TransformerException
274    */

275   public void runtimeInit(TransformerImpl transformer) throws TransformerException JavaDoc
276   {
277 /* //System.out.println("ElemExtensionDecl.runtimeInit()");
278     String lang = null;
279     String srcURL = null;
280     String scriptSrc = null;
281     String prefix = getPrefix();
282     String declNamespace = getNamespaceForPrefix(prefix);
283
284     if (null == declNamespace)
285       throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_NAMESPACE_DECL, new Object[]{prefix}));
286       //"Prefix " + prefix does not have a corresponding namespace declaration");
287
288     for (ElemTemplateElement child = getFirstChildElem(); child != null;
289             child = child.getNextSiblingElem())
290     {
291       if (Constants.ELEMNAME_EXTENSIONSCRIPT == child.getXSLToken())
292       {
293         ElemExtensionScript sdecl = (ElemExtensionScript) child;
294
295         lang = sdecl.getLang();
296         srcURL = sdecl.getSrc();
297
298         ElemTemplateElement childOfSDecl = sdecl.getFirstChildElem();
299
300         if (null != childOfSDecl)
301         {
302           if (Constants.ELEMNAME_TEXTLITERALRESULT
303                   == childOfSDecl.getXSLToken())
304           {
305             ElemTextLiteral tl = (ElemTextLiteral) childOfSDecl;
306             char[] chars = tl.getChars();
307
308             scriptSrc = new String(chars);
309
310             if (scriptSrc.trim().length() == 0)
311               scriptSrc = null;
312           }
313         }
314       }
315     }
316
317     if (null == lang)
318       lang = "javaclass";
319
320     if (lang.equals("javaclass") && (scriptSrc != null))
321       throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_ELEM_CONTENT_NOT_ALLOWED, new Object[]{scriptSrc}));
322       //"Element content not allowed for lang=javaclass " + scriptSrc);
323     
324     // Instantiate a handler for this extension namespace.
325     ExtensionsTable etable = transformer.getExtensionsTable();
326     ExtensionHandler nsh = etable.get(declNamespace);
327
328     // If we have no prior ExtensionHandler for this namespace, we need to
329     // create one.
330     // If the script element is for javaclass, this is our special compiled java.
331     // Element content is not supported for this so we throw an exception if
332     // it is provided. Otherwise, we look up the srcURL to see if we already have
333     // an ExtensionHandler.
334     if (null == nsh)
335     {
336       if (lang.equals("javaclass"))
337       {
338         if (null == srcURL)
339         {
340           nsh = etable.makeJavaNamespace(declNamespace);
341         }
342         else
343         {
344           nsh = etable.get(srcURL);
345
346           if (null == nsh)
347           {
348             nsh = etable.makeJavaNamespace(srcURL);
349           }
350         }
351       }
352       else // not java
353       {
354         nsh = new ExtensionHandlerGeneral(declNamespace, this.m_elements,
355                                           this.m_functions, lang, srcURL,
356                                           scriptSrc, getSystemId());
357
358         // System.out.println("Adding NS Handler: declNamespace = "+
359         // declNamespace+", lang = "+lang+", srcURL = "+
360         // srcURL+", scriptSrc="+scriptSrc);
361       }
362
363       etable.addExtensionNamespace(declNamespace, nsh);
364     }*/

365   }
366 }
367
Popular Tags