KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > cfg > TldTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.cfg;
30
31 import com.caucho.config.Config;
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.DependencyBean;
34 import com.caucho.jsp.JspParseException;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.PersistentDependency;
37
38 import javax.servlet.jsp.tagext.SimpleTag JavaDoc;
39 import javax.servlet.jsp.tagext.Tag JavaDoc;
40 import javax.servlet.jsp.tagext.TagAttributeInfo JavaDoc;
41 import javax.servlet.jsp.tagext.TagExtraInfo JavaDoc;
42 import javax.servlet.jsp.tagext.TagVariableInfo JavaDoc;
43 import javax.servlet.jsp.tagext.VariableInfo JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Configuration for the taglib tag in the .tld
49  */

50 public class TldTag implements DependencyBean {
51   private final static L10N L = new L10N(TldTag.class);
52   private final static Logger JavaDoc log
53     = Logger.getLogger(TldTag.class.getName());
54   
55   private String JavaDoc _name;
56   private String JavaDoc _tagClassName;
57   private String JavaDoc _teiClassName;
58   private String JavaDoc _bodyContent;
59   private String JavaDoc _displayName;
60   private String JavaDoc _info;
61   private String JavaDoc _smallIcon;
62   private String JavaDoc _largeIcon;
63   private String JavaDoc _description;
64   private ArrayList JavaDoc<TagVariableInfo JavaDoc> _variableList =
65     new ArrayList JavaDoc<TagVariableInfo JavaDoc>();
66   private ArrayList JavaDoc<TagAttributeInfo JavaDoc> _attributeList =
67     new ArrayList JavaDoc<TagAttributeInfo JavaDoc>();
68   private ArrayList JavaDoc<TldFragmentAttribute> _fragmentAttributeList =
69     new ArrayList JavaDoc<TldFragmentAttribute>();
70   private boolean _dynamicAttributes;
71   private String JavaDoc _dynamicAttributeName;
72   private String JavaDoc _example;
73
74   private String JavaDoc _configLocation;
75   private JspParseException _configException;
76   
77   private ArrayList JavaDoc<PersistentDependency> _dependencyList
78     = new ArrayList JavaDoc<PersistentDependency>();
79
80   /**
81    * Sets the config location.
82    */

83   public void setConfigLocation(String JavaDoc filename, int line)
84   {
85     _configLocation = filename + ":" + line + ": ";
86   }
87
88   /**
89    * Adds a dependency.
90    */

91   public void addDependency(PersistentDependency dependency)
92   {
93     _dependencyList.add(dependency);
94   }
95
96   /**
97    * Returns the dependency.
98    */

99   public ArrayList JavaDoc<PersistentDependency> getDependencyList()
100   {
101     return _dependencyList;
102   }
103
104   /**
105    * Sets the tag name, i.e. the local name.
106    */

107   public void setName(String JavaDoc name)
108   {
109     _name = name;
110   }
111
112   /**
113    * Gets the tag name, i.e. the local name.
114    */

115   public String JavaDoc getName()
116   {
117     return _name;
118   }
119
120   /**
121    * Sets the icon.
122    */

123   public void setIcon(com.caucho.config.types.Icon icon)
124   {
125     if (icon != null) {
126       _smallIcon = icon.getSmallIcon();
127       _largeIcon = icon.getLargeIcon();
128     }
129   }
130
131   /**
132    * Sets the tag class
133    */

134   public void setTagClass(String JavaDoc tagClassName)
135     throws ConfigException
136   {
137     _tagClassName = tagClassName;
138
139     Class JavaDoc tagClass = null;
140
141     try {
142       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
143       
144       tagClass = Class.forName(tagClassName, false, loader);
145
146       Config.checkCanInstantiate(tagClass);
147     } catch (ConfigException e) {
148       throw e;
149     } catch (Throwable JavaDoc e) {
150       log.warning(_configLocation + e);
151
152       if (_configException == null)
153     _configException = new JspParseException(_configLocation + e);
154
155       return;
156     }
157
158     if (! Tag JavaDoc.class.isAssignableFrom(tagClass) &&
159         ! SimpleTag JavaDoc.class.isAssignableFrom(tagClass))
160       throw new ConfigException(L.l("{0} must either implement Tag or SimpleTag.",
161                     tagClass.getName()));
162   }
163
164   /**
165    * Sets the tei class
166    */

167   public void setTagclass(String JavaDoc tagClassName)
168     throws ConfigException, InstantiationException JavaDoc, IllegalAccessException JavaDoc
169   {
170     setTagClass(tagClassName);
171   }
172
173   /**
174    * Gets the tag class
175    */

176   public Class JavaDoc getTagClass()
177   {
178     try {
179       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
180       
181       return Class.forName(_tagClassName, false, loader);
182     } catch (ClassNotFoundException JavaDoc e) {
183       throw new RuntimeException JavaDoc(e);
184     }
185   }
186
187   /**
188    * Gets the tag class
189    */

190   public String JavaDoc getTagClassName()
191   {
192     return _tagClassName;
193   }
194
195   /**
196    * Sets the tei class
197    */

198   public void setTeiClass(String JavaDoc teiClassName)
199     throws ConfigException
200   {
201     _teiClassName = teiClassName;
202
203     try {
204       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
205       
206       Class JavaDoc teiClass = Class.forName(teiClassName, false, loader);
207
208       Config.validate(teiClass, TagExtraInfo JavaDoc.class);
209     } catch (ConfigException e) {
210       throw e;
211     } catch (Throwable JavaDoc e) {
212       log.warning(_configLocation + e);
213
214       if (_configException == null)
215     _configException = new JspParseException(_configLocation + e);
216
217       return;
218     }
219   }
220   
221   /**
222    * Old-style setting of the tei class
223    */

224   public void setTeiclass(String JavaDoc teiClassName)
225     throws ConfigException
226   {
227     setTeiClass(teiClassName);
228   }
229
230   /**
231    * Gets the tei class
232    */

233   public String JavaDoc getTeiClassName()
234   {
235     return _teiClassName;
236   }
237
238   /**
239    * Gets the tei object
240    */

241   public TagExtraInfo JavaDoc getTagExtraInfo()
242   {
243     try {
244       if (_teiClassName == null)
245     return null;
246       else {
247     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
248       
249     Class JavaDoc teiClass = Class.forName(_teiClassName, false, loader);
250
251     return (TagExtraInfo JavaDoc) teiClass.newInstance();
252       }
253     } catch (Exception JavaDoc e) {
254       throw new RuntimeException JavaDoc(e);
255     }
256   }
257
258   /**
259    * Sets the body-content
260    */

261   public void setBodyContent(String JavaDoc bodyContent)
262   {
263     _bodyContent = bodyContent;
264   }
265
266   /**
267    * Gets the body-content
268    */

269   public String JavaDoc getBodyContent()
270   {
271     return _bodyContent;
272   }
273
274   /**
275    * Sets the display-name
276    */

277   public void setDisplayName(String JavaDoc displayName)
278   {
279     _displayName = displayName;
280   }
281
282   /**
283    * Gets the display-name
284    */

285   public String JavaDoc getDisplayName()
286   {
287     return _displayName;
288   }
289
290   /**
291    * Sets the info
292    */

293   public void setInfo(String JavaDoc info)
294   {
295     _info = info;
296   }
297
298   /**
299    * Gets the info
300    */

301   public String JavaDoc getInfo()
302   {
303     return _info;
304   }
305
306   /**
307    * Sets the small-icon
308    */

309   public void setSmallIcon(String JavaDoc smallIcon)
310   {
311     _smallIcon = smallIcon;
312   }
313
314   /**
315    * Gets the small-icon
316    */

317   public String JavaDoc getSmallIcon()
318   {
319     return _smallIcon;
320   }
321
322   /**
323    * Sets the large-icon
324    */

325   public void setLargeIcon(String JavaDoc largeIcon)
326   {
327     _largeIcon = largeIcon;
328   }
329
330   /**
331    * Gets the large-icon
332    */

333   public String JavaDoc getLargeIcon()
334   {
335     return _largeIcon;
336   }
337
338   /**
339    * Sets the description
340    */

341   public void setDescription(String JavaDoc description)
342   {
343     _description = description;
344   }
345
346   /**
347    * Gets the description
348    */

349   public String JavaDoc getDescription()
350   {
351     return _description;
352   }
353
354   /**
355    * Adds a variable.
356    */

357   public void addVariable(TldVariable variable)
358     throws ConfigException
359   {
360     TagVariableInfo JavaDoc varInfo;
361
362     String JavaDoc scopeName = variable.getScope();
363     int scope;
364
365     if (scopeName == null)
366       scope = VariableInfo.NESTED;
367     else if (scopeName.equals("NESTED"))
368       scope = VariableInfo.NESTED;
369     else if (scopeName.equals("AT_BEGIN"))
370       scope = VariableInfo.AT_BEGIN;
371     else if (scopeName.equals("AT_END"))
372       scope = VariableInfo.AT_END;
373     else
374       throw new ConfigException(L.l("{0} expects a valid scope at `{1}'",
375                     variable.getNameGiven(), scopeName));
376
377     varInfo = new TagVariableInfo JavaDoc(variable.getNameGiven(),
378                                   variable.getNameFromAttribute(),
379                                   variable.getVariableClass(),
380                                   variable.getDeclare(),
381                                   scope);
382
383     _variableList.add(varInfo);
384   }
385
386   /**
387    * Gets the variables
388    */

389   public ArrayList JavaDoc<TagVariableInfo JavaDoc> getVariableList()
390   {
391     return _variableList;
392   }
393
394   /**
395    * Returns the variables.
396    */

397   public TagVariableInfo JavaDoc []getVariables()
398   {
399     TagVariableInfo JavaDoc []variables;
400
401     variables = new TagVariableInfo JavaDoc[_variableList.size()];
402
403     return _variableList.toArray(variables);
404   }
405
406   /**
407    * Adds an attribute.
408    */

409   public void addAttribute(TldAttribute attribute)
410   {
411     TagAttributeInfo JavaDoc attrInfo;
412
413     Class JavaDoc type = attribute.getType();
414     attrInfo = new TagAttributeInfo JavaDoc(attribute.getName(),
415                                     attribute.getRequired(),
416                                     type == null ? null : type.getName(),
417                                     attribute.getRtexprvalue(),
418                     attribute.isFragment(),
419                     attribute.getDescription(),
420                     attribute.getDeferredValue() != null,
421                     attribute.getDeferredMethod() != null,
422                     attribute.getExpectedType(),
423                     attribute.getDeferredMethodSignature());
424     
425     _attributeList.add(attrInfo);
426   }
427
428   /**
429    * Gets the attributes
430    */

431   public ArrayList JavaDoc getAttributeList()
432   {
433     return _attributeList;
434   }
435
436   /**
437    * Returns the attributes.
438    */

439   public TagAttributeInfo JavaDoc []getAttributes()
440   {
441     TagAttributeInfo JavaDoc []attributes;
442
443     attributes = new TagAttributeInfo JavaDoc[_attributeList.size()];
444     
445     return (TagAttributeInfo JavaDoc []) _attributeList.toArray(attributes);
446   }
447
448   /**
449    * Adds an fragmentAttribute.
450    */

451   public void addFragmentAttribute(TldFragmentAttribute attribute)
452   {
453     _fragmentAttributeList.add(attribute);
454   }
455
456   /**
457    * Returns the attributes.
458    */

459   public ArrayList JavaDoc getFragmentAttributes()
460   {
461     return _fragmentAttributeList;
462   }
463
464   /**
465    * Sets the example
466    */

467   public void setExample(String JavaDoc example)
468   {
469     _example = example;
470   }
471
472   /**
473    * Gets the dynamic-attributes
474    */

475   public boolean getDynamicAttributes()
476   {
477     return _dynamicAttributes;
478   }
479
480   /**
481    * Sets the dynamic-attributes
482    */

483   public void setDynamicAttributes(boolean dynamicAttributes)
484   {
485     _dynamicAttributes = dynamicAttributes;
486   }
487
488   /**
489    * Gets the dynamic-attrisavesavebutes
490    */

491   public String JavaDoc getDynamicAttributeName()
492   {
493     return _dynamicAttributeName;
494   }
495
496   /**
497    * Sets the dynamic-attributes
498    */

499   public void setDynamicAttributeName(String JavaDoc name)
500   {
501     _dynamicAttributeName = name;
502   }
503
504   /**
505    * Gets the example
506    */

507   public String JavaDoc getExample()
508   {
509     return _example;
510   }
511   
512   /**
513    * validates.
514    */

515   public void validate()
516     throws JspParseException
517   {
518     if (_configException != null)
519       throw _configException;
520   }
521 }
522
Popular Tags