1 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 ; 38 import java.io.IOException ; 39 import java.util.HashMap ; 40 import java.util.logging.Logger ; 41 42 45 public class ParseTagManager { 46 static final L10N L = new L10N(ParseTagManager.class); 47 private static final Logger log = Log.open(ParseTagManager.class); 48 49 private JspResourceManager _resourceManager; 50 private TaglibManager _taglibManager; 51 private TagFileManager _tagFileManager; 52 53 private HashMap <QName,TagInfo > _tagMap = new HashMap <QName,TagInfo >(); 54 private HashMap <String ,Taglib> _taglibMap = new HashMap <String ,Taglib>(); 55 56 public ParseTagManager(JspResourceManager resourceManager, 57 TaglibManager taglibManager, 58 TagFileManager tagFileManager) 59 throws JspParseException, IOException 60 { 61 _resourceManager = resourceManager; 62 _taglibManager = taglibManager; 63 64 _tagFileManager = tagFileManager; 65 } 66 67 70 public synchronized AnalyzedTag analyzeTag(Class cl) 71 { 72 return _taglibManager.analyzeTag(cl); 73 } 74 75 78 public synchronized TagInfo getTag(QName qname) 79 throws JspParseException 80 { 81 TagInfo tag = getTagImpl(qname); 82 83 if (tag instanceof TagInfoImpl) 84 ((TagInfoImpl) tag).validate(); 85 86 return tag; 87 } 88 89 92 private TagInfo getTagImpl(QName qname) 93 throws JspParseException 94 { 95 TagInfo 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 name = qname.getName(); 113 String 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 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 162 public synchronized Class getTagClass(QName qname) 163 throws Exception 164 { 165 TagInfo tagInfo = getTag(qname); 166 167 if (tagInfo == null) 168 return null; 169 170 String 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 prefix = qname.getPrefix(); 182 183 Taglib taglib = (Taglib) _taglibMap.get(prefix); 184 if (_taglibMap.get(prefix) != null) 185 return taglib; 186 187 String uri = qname.getNamespace(); 188 189 taglib = addTaglib(prefix, uri); 190 191 _taglibMap.put(prefix, taglib); 192 193 return taglib; 194 } 195 196 199 public synchronized Taglib addTaglib(String prefix, String 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 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 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 239 public synchronized Taglib addTaglibDir(String prefix, String dir) 240 throws JspParseException 241 { 242 return _taglibManager.getTaglibDir(prefix, dir); 243 } 244 245 248 public synchronized Taglib addTaglib(String prefix, 249 String uri, 250 String 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 message) 282 { 283 return new JspParseException(message); 284 } 285 } 286 | Popular Tags |