KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > ImplicitTagLibraryInfo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.compiler;
19
20 import java.io.InputStream JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import javax.servlet.jsp.tagext.FunctionInfo JavaDoc;
28 import javax.servlet.jsp.tagext.TagFileInfo JavaDoc;
29 import javax.servlet.jsp.tagext.TagInfo JavaDoc;
30 import javax.servlet.jsp.tagext.TagLibraryInfo JavaDoc;
31
32 import org.apache.jasper.JasperException;
33 import org.apache.jasper.JspCompilationContext;
34 import org.apache.jasper.xmlparser.ParserUtils;
35 import org.apache.jasper.xmlparser.TreeNode;
36
37 /**
38  * Class responsible for generating an implicit tag library containing tag
39  * handlers corresponding to the tag files in "/WEB-INF/tags/" or a
40  * subdirectory of it.
41  *
42  * @author Jan Luehe
43  */

44 class ImplicitTagLibraryInfo extends TagLibraryInfo JavaDoc {
45
46     private static final String JavaDoc WEB_INF_TAGS = "/WEB-INF/tags";
47     private static final String JavaDoc TAG_FILE_SUFFIX = ".tag";
48     private static final String JavaDoc TAGX_FILE_SUFFIX = ".tagx";
49     private static final String JavaDoc TAGS_SHORTNAME = "tags";
50     private static final String JavaDoc TLIB_VERSION = "1.0";
51     private static final String JavaDoc JSP_VERSION = "2.0";
52     private static final String JavaDoc IMPLICIT_TLD = "implicit.tld";
53
54     // Maps tag names to tag file paths
55
private Hashtable JavaDoc tagFileMap;
56
57     private ParserController pc;
58     private PageInfo pi;
59     private Vector JavaDoc vec;
60
61     /**
62      * Constructor.
63      */

64     public ImplicitTagLibraryInfo(JspCompilationContext ctxt,
65             ParserController pc,
66             PageInfo pi,
67             String JavaDoc prefix,
68             String JavaDoc tagdir,
69             ErrorDispatcher err) throws JasperException {
70         super(prefix, null);
71         this.pc = pc;
72         this.pi = pi;
73         this.tagFileMap = new Hashtable JavaDoc();
74         this.vec = new Vector JavaDoc();
75
76         // Implicit tag libraries have no functions:
77
this.functions = new FunctionInfo JavaDoc[0];
78
79         tlibversion = TLIB_VERSION;
80         jspversion = JSP_VERSION;
81
82         if (!tagdir.startsWith(WEB_INF_TAGS)) {
83             err.jspError("jsp.error.invalid.tagdir", tagdir);
84         }
85
86         // Determine the value of the <short-name> subelement of the
87
// "imaginary" <taglib> element
88
if (tagdir.equals(WEB_INF_TAGS)
89                 || tagdir.equals( WEB_INF_TAGS + "/")) {
90             shortname = TAGS_SHORTNAME;
91         } else {
92             shortname = tagdir.substring(WEB_INF_TAGS.length());
93             shortname = shortname.replace('/', '-');
94         }
95
96         // Populate mapping of tag names to tag file paths
97
Set JavaDoc dirList = ctxt.getResourcePaths(tagdir);
98         if (dirList != null) {
99             Iterator JavaDoc it = dirList.iterator();
100             while (it.hasNext()) {
101                 String JavaDoc path = (String JavaDoc) it.next();
102                 if (path.endsWith(TAG_FILE_SUFFIX)
103                         || path.endsWith(TAGX_FILE_SUFFIX)) {
104                     /*
105                      * Use the filename of the tag file, without the .tag or
106                      * .tagx extension, respectively, as the <name> subelement
107                      * of the "imaginary" <tag-file> element
108                      */

109                     String JavaDoc suffix = path.endsWith(TAG_FILE_SUFFIX) ?
110                             TAG_FILE_SUFFIX : TAGX_FILE_SUFFIX;
111                     String JavaDoc tagName = path.substring(path.lastIndexOf("/") + 1);
112                     tagName = tagName.substring(0,
113                             tagName.lastIndexOf(suffix));
114                     tagFileMap.put(tagName, path);
115                 } else if (path.endsWith(IMPLICIT_TLD)) {
116                     InputStream JavaDoc in = null;
117                     try {
118                         in = ctxt.getResourceAsStream(path);
119                         if (in != null) {
120                             
121                             // Add implicit TLD to dependency list
122
if (pi != null) {
123                                 pi.addDependant(path);
124                             }
125                             
126                             ParserUtils pu = new ParserUtils();
127                             TreeNode tld = pu.parseXMLDocument(uri, in);
128
129                             if (tld.findAttribute("version") != null) {
130                                 this.jspversion = tld.findAttribute("version");
131                             }
132
133                             // Process each child element of our <taglib> element
134
Iterator JavaDoc list = tld.findChildren();
135
136                             while (list.hasNext()) {
137                                 TreeNode element = (TreeNode) list.next();
138                                 String JavaDoc tname = element.getName();
139
140                                 if ("tlibversion".equals(tname) // JSP 1.1
141
|| "tlib-version".equals(tname)) { // JSP 1.2
142
this.tlibversion = element.getBody();
143                                 } else if ("jspversion".equals(tname)
144                                         || "jsp-version".equals(tname)) {
145                                     this.jspversion = element.getBody();
146                                 } else if ("shortname".equals(tname) || "short-name".equals(tname)) {
147                                     // Ignore
148
} else {
149                                     // All other elements are invalid
150
err.jspError("jsp.error.invalid.implicit", path);
151                                 }
152                             }
153                             try {
154                                 double version = Double.parseDouble(this.jspversion);
155                                 if (version < 2.0) {
156                                     err.jspError("jsp.error.invalid.implicit.version", path);
157                                 }
158                             } catch (NumberFormatException JavaDoc e) {
159                                 err.jspError("jsp.error.invalid.implicit.version", path);
160                             }
161                         }
162                     } finally {
163                         if (in != null) {
164                             try {
165                                 in.close();
166                             } catch (Throwable JavaDoc t) {
167                             }
168                         }
169                     }
170                 }
171             }
172         }
173         
174     }
175
176     /**
177      * Checks to see if the given tag name maps to a tag file path,
178      * and if so, parses the corresponding tag file.
179      *
180      * @return The TagFileInfo corresponding to the given tag name, or null if
181      * the given tag name is not implemented as a tag file
182      */

183     public TagFileInfo JavaDoc getTagFile(String JavaDoc shortName) {
184
185         TagFileInfo JavaDoc tagFile = super.getTagFile(shortName);
186         if (tagFile == null) {
187             String JavaDoc path = (String JavaDoc) tagFileMap.get(shortName);
188             if (path == null) {
189                 return null;
190             }
191
192             TagInfo JavaDoc tagInfo = null;
193             try {
194                 tagInfo = TagFileProcessor.parseTagFileDirectives(pc,
195                         shortName,
196                         path,
197                         this);
198             } catch (JasperException je) {
199                 throw new RuntimeException JavaDoc(je.toString(), je);
200             }
201
202             tagFile = new TagFileInfo JavaDoc(shortName, path, tagInfo);
203             vec.addElement(tagFile);
204
205             this.tagFiles = new TagFileInfo JavaDoc[vec.size()];
206             vec.copyInto(this.tagFiles);
207         }
208
209         return tagFile;
210     }
211
212     public TagLibraryInfo JavaDoc[] getTagLibraryInfos() {
213         Collection JavaDoc coll = pi.getTaglibs();
214         return (TagLibraryInfo JavaDoc[]) coll.toArray(new TagLibraryInfo JavaDoc[0]);
215     }
216
217 }
218
Popular Tags