001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp;
030:
031: import com.caucho.jsp.cfg.TldFunction;
032: import com.caucho.jsp.cfg.TldTag;
033: import com.caucho.jsp.cfg.TldTagFile;
034: import com.caucho.jsp.cfg.TldTaglib;
035: import com.caucho.jsp.cfg.TldValidator;
036: import com.caucho.log.Log;
037: import com.caucho.server.util.CauchoSystem;
038: import com.caucho.util.L10N;
039: import com.caucho.vfs.*;
040:
041: import javax.servlet.jsp.tagext.FunctionInfo;
042: import javax.servlet.jsp.tagext.TagExtraInfo;
043: import javax.servlet.jsp.tagext.TagFileInfo;
044: import javax.servlet.jsp.tagext.TagInfo;
045: import javax.servlet.jsp.tagext.TagLibraryInfo;
046: import javax.servlet.jsp.tagext.TagLibraryValidator;
047: import java.util.ArrayList;
048: import java.util.logging.Logger;
049:
050: /**
051: * Stores the entire information for a tag library.
052: */
053: public class Taglib extends TagLibraryInfo {
054: private static final Logger log = Log.open(Taglib.class);
055: static final L10N L = new L10N(Taglib.class);
056:
057: private TldTaglib _tldTaglib;
058: private TagFileManager _tagFileManager;
059:
060: TagLibraryValidator _validator;
061: private ArrayList<TldFunction> _functionList = new ArrayList<TldFunction>();
062: private ArrayList<Taglib> _libraryList = new ArrayList<Taglib>();
063:
064: Taglib(String prefix, String uri, TldTaglib tldTaglib,
065: TagFileManager tagFileManager) throws JspParseException {
066: super (prefix, uri);
067:
068: try {
069: _tldTaglib = tldTaglib;
070: _tagFileManager = tagFileManager;
071:
072: fillTagLibraryInfo(tldTaglib, tagFileManager);
073:
074: _libraryList.add(this );
075: } catch (JspParseException e) {
076: throw e;
077: } catch (Exception e) {
078: throw new JspParseException(e);
079: }
080: }
081:
082: /**
083: * Gets a new instance of the validator to check the page.
084: */
085: public TagLibraryValidator getValidator() throws JspParseException {
086: return _validator;
087: }
088:
089: /**
090: * Returns the functions.
091: */
092: public ArrayList<TldFunction> getFunctionList() {
093: return _functionList;
094: }
095:
096: /**
097: * Gets the path.
098: */
099: public Path getPath() {
100: if (_tldTaglib != null)
101: return _tldTaglib.getJarPath();
102: else
103: return null;
104: }
105:
106: /**
107: * Fills the tag library info from the tld
108: *
109: * <pre>
110: * taglib ::= tlib-version, jsp-version?, short-name, uri?,
111: * display-name?, small-icon?, large-icon?, description?,
112: * validator?, listener*, tag+, function*
113: * </pre>
114: */
115: private void fillTagLibraryInfo(TldTaglib taglib,
116: TagFileManager tagFileManager) throws Exception {
117: this .tlibversion = taglib.getTlibVersion();
118:
119: this .jspversion = taglib.getJspVersion();
120:
121: this .shortname = taglib.getShortName();
122:
123: this .urn = taglib.getURI();
124: this .info = taglib.getInfo();
125:
126: if (taglib.getDescription() != null)
127: this .info = taglib.getDescription();
128:
129: TldValidator validator = taglib.getValidator();
130:
131: if (validator != null)
132: _validator = validator.getValidator();
133:
134: ArrayList<TldTag> tagList = taglib.getTagList();
135:
136: tags = new TagInfo[tagList.size()];
137:
138: for (int i = 0; i < tagList.size(); i++) {
139: TldTag tag = tagList.get(i);
140:
141: TagInfo tagInfo;
142:
143: if (tag.getBaseTag() != null)
144: tagInfo = new TagInfoImpl(tag, tag.getBaseTag(), this );
145: else
146: tagInfo = new TagInfoImpl(tag, this );
147:
148: tags[i] = tagInfo;
149: }
150:
151: ArrayList<TldTagFile> tagFileList = taglib.getTagFileList();
152:
153: this .tagFiles = new TagFileInfo[tagFileList.size()];
154:
155: for (int i = 0; i < tagFileList.size(); i++) {
156: TldTagFile tagFile = tagFileList.get(i);
157:
158: TagFileInfo tagFileInfo = new TagFileInfoExt(
159: tagFileManager, tagFile.getName(), tagFile
160: .getPath());
161:
162: this .tagFiles[i] = tagFileInfo;
163: }
164:
165: _functionList = taglib.getFunctionList();
166:
167: this .functions = new FunctionInfo[_functionList.size()];
168:
169: for (int i = 0; i < _functionList.size(); i++) {
170: this .functions[i] = _functionList.get(i).toFunctionInfo();
171: }
172: }
173:
174: /**
175: * Returns the tag class for the given tag qname
176: *
177: * @param tagName the tag's qname
178: *
179: * @return the matching class or null
180: */
181: public Class getClass(String tagName) throws Exception {
182: TagInfo info = getTag(tagName);
183: String className = info == null ? null : info.getTagClassName();
184:
185: if (className != null)
186: return CauchoSystem.loadClass(className);
187: else
188: return null;
189: }
190:
191: /**
192: * Return the class names of all tags that are outside of packages.
193: */
194: public ArrayList<String> getSingleTagClassNames() {
195: ArrayList<String> singleTags = new ArrayList<String>();
196:
197: TagInfo[] tags = getTags();
198: for (int i = 0; tags != null && i < tags.length; i++) {
199: String name = tags[i].getTagClassName();
200:
201: if (name != null && name.indexOf('.') < 0)
202: singleTags.add(name);
203: }
204:
205: return singleTags;
206: }
207:
208: /**
209: * Returns the TagExtraInfo structure for the named tag.
210: */
211: TagExtraInfo getTagExtraInfo(String tagName) {
212: TagInfo info = getTag(tagName);
213:
214: return info != null ? info.getTagExtraInfo() : null;
215: }
216:
217: /**
218: * Hack to avoid JSDK problem.
219: */
220: public TagInfo getTag(String name) {
221: if (tags == null)
222: return null;
223:
224: for (int i = 0; i < tags.length; i++) {
225: if (tags[i].getTagName().equals(name))
226: return tags[i];
227: }
228:
229: return null;
230: }
231:
232: /**
233: * Returns a matching tag file.
234: */
235: public String getTagFilePath(String name) {
236: if (_tldTaglib == null)
237: return null;
238:
239: ArrayList<TldTagFile> tagFiles = _tldTaglib.getTagFileList();
240:
241: for (int i = 0; i < tagFiles.size(); i++) {
242: TldTagFile tagFile = tagFiles.get(i);
243:
244: if (tagFile.getName().equals(name))
245: return tagFile.getPath();
246: }
247:
248: /*
249: String uri = getURI();
250:
251:
252: if (uri != null && uri.startsWith("urn:jsptagdir:"))
253: return Vfs.lookup().lookup("./" + uri.substring("urn:jsptagdir:".length()) + "/" + name + ".tag").getNativePath();
254: */
255:
256: return null;
257: }
258:
259: public void addTaglib(Taglib taglib) {
260: if (!_libraryList.contains(taglib))
261: _libraryList.add(taglib);
262: }
263:
264: public Taglib copy() throws JspParseException {
265: return new Taglib(getPrefixString(), getURI(), _tldTaglib,
266: _tagFileManager);
267: }
268:
269: @Override
270: public TagLibraryInfo[] getTagLibraryInfos() {
271: TagLibraryInfo[] infoArray = new TagLibraryInfo[_libraryList
272: .size()];
273:
274: _libraryList.toArray(infoArray);
275:
276: return infoArray;
277: }
278:
279: public String toString() {
280: return "Taglib[prefix=" + prefix + ",uri=" + uri + "]";
281: }
282:
283: class TagFileInfoExt extends TagFileInfo {
284: private TagFileManager _manager;
285: private TagInfo _tagInfo;
286:
287: TagFileInfoExt(TagFileManager manager, String name, String path) {
288: super (name, path, null);
289:
290: _manager = manager;
291: }
292:
293: public TagInfo getTagInfo() {
294: if (_tagInfo == null) {
295: try {
296: _tagInfo = _manager
297: .getTag("", getName(), getPath());
298: } catch (JspParseException e) {
299: throw new RuntimeException(e);
300: }
301: }
302:
303: return _tagInfo;
304: }
305: }
306:
307: }
|