KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.Path;
35 import com.caucho.xml.QName;
36
37 import javax.servlet.jsp.tagext.TagInfo JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Stores the information for the .tags
44  */

45 public class ParseTagManager {
46   static final L10N L = new L10N(ParseTagManager.class);
47   private static final Logger JavaDoc log = Log.open(ParseTagManager.class);
48
49   private JspResourceManager _resourceManager;
50   private TaglibManager _taglibManager;
51   private TagFileManager _tagFileManager;
52
53   private HashMap JavaDoc<QName,TagInfo JavaDoc> _tagMap = new HashMap JavaDoc<QName,TagInfo JavaDoc>();
54   private HashMap JavaDoc<String JavaDoc,Taglib> _taglibMap = new HashMap JavaDoc<String JavaDoc,Taglib>();
55
56   public ParseTagManager(JspResourceManager resourceManager,
57                          TaglibManager taglibManager,
58                          TagFileManager tagFileManager)
59     throws JspParseException, IOException JavaDoc
60   {
61     _resourceManager = resourceManager;
62     _taglibManager = taglibManager;
63
64     _tagFileManager = tagFileManager;
65   }
66
67   /**
68    * Analyzes the tag.
69    */

70   public synchronized AnalyzedTag analyzeTag(Class JavaDoc cl)
71   {
72     return _taglibManager.analyzeTag(cl);
73   }
74
75   /**
76    * Returns the tag with the given qname.
77    */

78   public synchronized TagInfo JavaDoc getTag(QName qname)
79     throws JspParseException
80   {
81     TagInfo JavaDoc tag = getTagImpl(qname);
82
83     if (tag instanceof TagInfoImpl)
84       ((TagInfoImpl) tag).validate();
85
86     return tag;
87   }
88
89   /**
90    * Returns the tag with the given qname.
91    */

92   private TagInfo JavaDoc getTagImpl(QName qname)
93     throws JspParseException
94   {
95     TagInfo JavaDoc tag = _tagMap.get(qname);
96
97     if (tag != null)
98       return tag;
99     
100     tag = _tagFileManager.getTag(qname.getPrefix(),
101                  qname.getLocalName(),
102                  qname.getNamespaceURI());
103     _tagMap.put(qname, tag);
104
105     if (tag != null)
106       return tag;
107     
108     Taglib taglib = addTaglib(qname);
109     if (taglib == null)
110       return null;
111     
112     String JavaDoc name = qname.getName();
113     String JavaDoc tail = qname.getLocalName();
114
115     if (qname.getNamespaceURI() == null) {
116       int p = name.lastIndexOf(':');
117
118       if (p < 0)
119     return null;
120     
121       tail = name.substring(p + 1);
122     }
123
124     if (taglib != null)
125       tag = taglib.getTag(tail);
126
127     if (tag == null) {
128       String JavaDoc tagLocation = taglib.getTagFilePath(tail);
129       Path path = taglib.getPath();
130
131       if (path != null && tagLocation != null) {
132     path = path.lookup(tagLocation);
133
134     tag = _tagFileManager.getTag(path, qname.getPrefix(), tagLocation);
135
136     if (tag != null) {
137       return tag;
138     }
139       }
140       
141       if (tagLocation != null) {
142     tag = _tagFileManager.getTag(qname.getPrefix(), tagLocation);
143
144     if (tag == null)
145       throw new JspParseException(L.l("'{0}' is an unknown tag-file in tag library '{1}'.",
146                       tagLocation, taglib.getURI()));
147       }
148     }
149
150     if (tag == null)
151       throw new JspParseException(L.l("'{0}' is an unknown tag in tag library '{1}'.",
152                       tail, taglib.getURI()));
153
154     _tagMap.put(qname, tag);
155
156     return tag;
157   }
158
159   /**
160    * Returns the tag with the given qname.
161    */

162   public synchronized Class JavaDoc getTagClass(QName qname)
163     throws Exception JavaDoc
164   {
165     TagInfo JavaDoc tagInfo = getTag(qname);
166     
167     if (tagInfo == null)
168       return null;
169     
170     String JavaDoc className = tagInfo.getTagClassName();
171
172     if (className != null)
173       return _tagFileManager.loadClass(className);
174     else
175       return null;
176   }
177
178   public synchronized Taglib addTaglib(QName qname)
179     throws JspParseException
180   {
181     String JavaDoc prefix = qname.getPrefix();
182
183     Taglib taglib = (Taglib) _taglibMap.get(prefix);
184     if (_taglibMap.get(prefix) != null)
185       return taglib;
186     
187     String JavaDoc uri = qname.getNamespace();
188
189     taglib = addTaglib(prefix, uri);
190     
191     _taglibMap.put(prefix, taglib);
192
193     return taglib;
194   }
195
196   /**
197    * Lookup and add a taglib based on a prefix and uri
198    */

199   public synchronized Taglib addTaglib(String JavaDoc prefix, String JavaDoc uri)
200     throws JspParseException
201   {
202     Taglib taglib = null;
203     
204     boolean hasTld = false;
205
206     if (uri == null)
207       return null;
208     else if (uri.startsWith("urn:jsptagdir:")) {
209       String JavaDoc tagDir = uri.substring("urn:jsptagdir:".length());
210
211       taglib = addTaglibDir(prefix, tagDir);
212       hasTld = true;
213
214       if (taglib == null) {
215         throw error(L.l("`{0}' has no matching tag. The taglib uri must match a <uri> element in a taglib.tld.", uri));
216       }
217     }
218     else {
219       if (uri.startsWith("urn:jsptld:")) {
220         hasTld = true;
221         uri = uri.substring("urn:jsptld:".length());
222       }
223
224       String JavaDoc location = uri;
225
226       taglib = _taglibManager.getTaglib(prefix, uri, location);
227
228       if (hasTld && taglib == null) {
229         throw error(L.l("`{0}' has no matching tag. The taglib uri must match a <uri> element in a taglib.tld.", uri));
230       }
231     }
232
233     return taglib;
234   }
235
236   /**
237    * Adds a taglib.
238    */

239   public synchronized Taglib addTaglibDir(String JavaDoc prefix, String JavaDoc dir)
240     throws JspParseException
241   {
242     return _taglibManager.getTaglibDir(prefix, dir);
243   }
244
245   /**
246    * Adds a taglib.
247    */

248   public synchronized Taglib addTaglib(String JavaDoc prefix,
249                        String JavaDoc uri,
250                        String JavaDoc location)
251     throws JspParseException
252   {
253     Taglib taglib = _taglibManager.getTaglib(prefix, uri, location);
254
255     return addTaglib(taglib);
256   }
257
258   private Taglib addTaglib(Taglib taglib)
259     throws JspParseException
260   {
261     taglib = taglib.copy();
262
263     for (Taglib oldTaglib : _taglibMap.values()) {
264       if (oldTaglib != null) {
265     oldTaglib.addTaglib(taglib);
266     taglib.addTaglib(oldTaglib);
267       }
268     }
269     
270     _taglibMap.put(taglib.getPrefixString(), taglib);
271
272     return taglib;
273   }
274   
275
276   public boolean hasTags()
277   {
278     return _taglibMap != null && _taglibMap.size() > 1;
279   }
280
281   public JspParseException error(String JavaDoc message)
282   {
283     return new JspParseException(message);
284   }
285 }
286
Popular Tags