KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > Taglib


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;
30
31 import com.caucho.jsp.cfg.TldFunction;
32 import com.caucho.jsp.cfg.TldTag;
33 import com.caucho.jsp.cfg.TldTagFile;
34 import com.caucho.jsp.cfg.TldTaglib;
35 import com.caucho.jsp.cfg.TldValidator;
36 import com.caucho.log.Log;
37 import com.caucho.server.util.CauchoSystem;
38 import com.caucho.util.L10N;
39 import com.caucho.vfs.Path;
40
41 import javax.servlet.jsp.tagext.FunctionInfo JavaDoc;
42 import javax.servlet.jsp.tagext.TagExtraInfo JavaDoc;
43 import javax.servlet.jsp.tagext.TagFileInfo JavaDoc;
44 import javax.servlet.jsp.tagext.TagInfo JavaDoc;
45 import javax.servlet.jsp.tagext.TagLibraryInfo JavaDoc;
46 import javax.servlet.jsp.tagext.TagLibraryValidator JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Stores the entire information for a tag library.
52  */

53 public class Taglib extends TagLibraryInfo JavaDoc {
54   private static final Logger JavaDoc log = Log.open(Taglib.class);
55   static final L10N L = new L10N(Taglib.class);
56
57   private TldTaglib _tldTaglib;
58   private TagFileManager _tagFileManager;
59   
60   TagLibraryValidator JavaDoc _validator;
61   private ArrayList JavaDoc<TldFunction> _functionList = new ArrayList JavaDoc<TldFunction>();
62   private ArrayList JavaDoc<Taglib> _libraryList
63     = new ArrayList JavaDoc<Taglib>();
64   
65   Taglib(String JavaDoc prefix, String JavaDoc uri, TldTaglib tldTaglib,
66      TagFileManager tagFileManager)
67     throws JspParseException
68   {
69     super(prefix, uri);
70
71     try {
72       _tldTaglib = tldTaglib;
73       _tagFileManager = tagFileManager;
74       
75       fillTagLibraryInfo(tldTaglib, tagFileManager);
76
77       _libraryList.add(this);
78     } catch (JspParseException e) {
79       throw e;
80     } catch (Exception JavaDoc e) {
81       throw new JspParseException(e);
82     }
83   }
84
85   /**
86    * Gets a new instance of the validator to check the page.
87    */

88   public TagLibraryValidator JavaDoc getValidator()
89     throws JspParseException
90   {
91     return _validator;
92   }
93
94   /**
95    * Returns the functions.
96    */

97   public ArrayList JavaDoc<TldFunction> getFunctionList()
98   {
99     return _functionList;
100   }
101
102   /**
103    * Gets the path.
104    */

105   public Path getPath()
106   {
107     if (_tldTaglib != null)
108       return _tldTaglib.getJarPath();
109     else
110       return null;
111   }
112
113   /**
114    * Fills the tag library info from the tld
115    *
116    * <pre>
117    * taglib ::= tlib-version, jsp-version?, short-name, uri?,
118    * display-name?, small-icon?, large-icon?, description?,
119    * validator?, listener*, tag+, function*
120    * </pre>
121    */

122   private void fillTagLibraryInfo(TldTaglib taglib,
123                   TagFileManager tagFileManager)
124     throws Exception JavaDoc
125   {
126     this.tlibversion = taglib.getTlibVersion();
127     
128     this.jspversion = taglib.getJspVersion();
129
130     this.shortname = taglib.getShortName();
131     
132     this.urn = taglib.getURI();
133     this.info = taglib.getInfo();
134
135     if (taglib.getDescription() != null)
136       this.info = taglib.getDescription();
137
138     TldValidator validator = taglib.getValidator();
139
140     if (validator != null)
141       _validator = validator.getValidator();
142
143     ArrayList JavaDoc<TldTag> tagList = taglib.getTagList();
144
145     tags = new TagInfo JavaDoc[tagList.size()];
146
147     for (int i = 0; i < tagList.size(); i++) {
148       TldTag tag = tagList.get(i);
149
150       TagInfo JavaDoc tagInfo = new TagInfoImpl(tag, this);
151
152       tags[i] = tagInfo;
153     }
154
155     ArrayList JavaDoc<TldTagFile> tagFileList = taglib.getTagFileList();
156
157     this.tagFiles = new TagFileInfo JavaDoc[tagFileList.size()];
158
159     for (int i = 0; i < tagFileList.size(); i++) {
160       TldTagFile tagFile = tagFileList.get(i);
161       
162       TagFileInfo JavaDoc tagFileInfo = new TagFileInfoExt(tagFileManager,
163                            tagFile.getName(),
164                            tagFile.getPath());
165
166       this.tagFiles[i] = tagFileInfo;
167     }
168
169     _functionList = taglib.getFunctionList();
170
171     this.functions = new FunctionInfo JavaDoc[_functionList.size()];
172
173     for (int i = 0; i < _functionList.size(); i++) {
174       this.functions[i] = _functionList.get(i).toFunctionInfo();
175     }
176   }
177     
178   /**
179    * Returns the tag class for the given tag qname
180    *
181    * @param tagName the tag's qname
182    *
183    * @return the matching class or null
184    */

185   public Class JavaDoc getClass(String JavaDoc tagName)
186     throws Exception JavaDoc
187   {
188     TagInfo JavaDoc info = getTag(tagName);
189     String JavaDoc className = info == null ? null : info.getTagClassName();
190
191     if (className != null)
192       return CauchoSystem.loadClass(className);
193     else
194       return null;
195   }
196
197   /**
198    * Return the class names of all tags that are outside of packages.
199    */

200   public ArrayList JavaDoc<String JavaDoc> getSingleTagClassNames()
201   {
202     ArrayList JavaDoc<String JavaDoc> singleTags = new ArrayList JavaDoc<String JavaDoc>();
203
204     TagInfo JavaDoc []tags = getTags();
205     for (int i = 0; tags != null && i < tags.length; i++) {
206       String JavaDoc name = tags[i].getTagClassName();
207
208       if (name != null && name.indexOf('.') < 0)
209         singleTags.add(name);
210     }
211
212     return singleTags;
213   }
214
215   /**
216    * Returns the TagExtraInfo structure for the named tag.
217    */

218   TagExtraInfo JavaDoc getTagExtraInfo(String JavaDoc tagName)
219   {
220     TagInfo JavaDoc info = getTag(tagName);
221     
222     return info != null ? info.getTagExtraInfo() : null;
223   }
224
225   /**
226    * Hack to avoid JSDK problem.
227    */

228   public TagInfo JavaDoc getTag(String JavaDoc name)
229   {
230     if (tags == null)
231       return null;
232     
233     for (int i = 0; i < tags.length; i++) {
234       if (tags[i].getTagName().equals(name))
235     return tags[i];
236     }
237
238     return null;
239   }
240
241   /**
242    * Returns a matching tag file.
243    */

244   public String JavaDoc getTagFilePath(String JavaDoc name)
245   {
246     if (_tldTaglib == null)
247       return null;
248
249     ArrayList JavaDoc<TldTagFile> tagFiles = _tldTaglib.getTagFileList();
250     
251     for (int i = 0; i < tagFiles.size(); i++) {
252       TldTagFile tagFile = tagFiles.get(i);
253
254       if (tagFile.getName().equals(name))
255     return tagFile.getPath();
256     }
257
258     return null;
259   }
260
261   public void addTaglib(Taglib taglib)
262   {
263     if (! _libraryList.contains(taglib))
264       _libraryList.add(taglib);
265   }
266
267   public Taglib copy()
268     throws JspParseException
269   {
270     return new Taglib(getPrefixString(), getURI(),
271               _tldTaglib, _tagFileManager);
272   }
273
274   @Override JavaDoc
275   public TagLibraryInfo JavaDoc []getTagLibraryInfos()
276   {
277     TagLibraryInfo JavaDoc []infoArray = new TagLibraryInfo JavaDoc[_libraryList.size()];
278
279     _libraryList.toArray(infoArray);
280     
281     return infoArray;
282   }
283
284   public String JavaDoc toString()
285   {
286     return "Taglib[prefix=" + prefix + ",uri=" + uri + "]";
287   }
288
289   class TagFileInfoExt extends TagFileInfo JavaDoc
290   {
291     private TagFileManager _manager;
292     private TagInfo JavaDoc _tagInfo;
293     
294     TagFileInfoExt(TagFileManager manager, String JavaDoc name, String JavaDoc path)
295     {
296       super(name, path, null);
297       
298       _manager = manager;
299     }
300
301     public TagInfo JavaDoc getTagInfo()
302     {
303       if (_tagInfo == null) {
304     try {
305       _tagInfo = _manager.getTag("", getName(), getPath());
306     } catch (JspParseException e) {
307       throw new RuntimeException JavaDoc(e);
308     }
309       }
310
311       return _tagInfo;
312     }
313   }
314     
315 }
316
Popular Tags