KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.java.TagTaglib;
33 import com.caucho.loader.SimpleLoader;
34 import com.caucho.util.L10N;
35 import com.caucho.util.Log;
36 import com.caucho.vfs.Path;
37
38 import javax.servlet.jsp.tagext.TagInfo JavaDoc;
39 import javax.servlet.jsp.tagext.TagLibraryInfo JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42 /**
43  * Stores the information for the .tags
44  */

45 public class TagFileManager {
46   static final L10N L = new L10N(TagFileManager.class);
47   private static final Logger JavaDoc log = Log.open(TagFileManager.class);
48   
49   private JspCompiler _jspCompiler;
50
51   public TagFileManager(JspCompiler compiler)
52   {
53     _jspCompiler = compiler;
54   }
55
56   public TagInfo JavaDoc getTag(String JavaDoc prefix, String JavaDoc shortName, String JavaDoc location)
57     throws JspParseException
58   {
59     if (location == null)
60       return null;
61     
62     try {
63       String JavaDoc originalLocation = location;
64       
65       if (location.startsWith("urn:jsptagdir:"))
66     location = location.substring("urn:jsptagdir:".length());
67
68       TagLibraryInfo JavaDoc taglib = new TagTaglib(prefix, originalLocation);
69       
70       String JavaDoc uri = location;
71       
72       TagInfo JavaDoc tag = null;
73
74       try {
75     tag = getTag(uri, taglib);
76     if (tag != null)
77       return tag;
78       } catch (Exception JavaDoc e) {
79     log.log(Level.FINEST, e.toString(), e);
80       }
81       
82       if (! location.endsWith("/"))
83     location = location + "/";
84       
85       uri = location + shortName + ".tag";
86       
87       tag = getTag(uri, taglib);
88       if (tag != null)
89     return tag;
90
91       uri = location + shortName + ".tagx";
92       return getTag(uri, taglib);
93     } catch (Exception JavaDoc e) {
94       throw JspParseException.create(e);
95     }
96   }
97
98   public TagInfo JavaDoc getTag(String JavaDoc prefix, String JavaDoc location)
99     throws JspParseException
100   {
101     TagLibraryInfo JavaDoc taglib = new TagTaglib(prefix, location);
102     
103     return getTag(location, taglib);
104   }
105
106   public TagInfo JavaDoc getTag(String JavaDoc location, TagLibraryInfo JavaDoc taglib)
107     throws JspParseException
108   {
109     JspResourceManager resourceManager = _jspCompiler.getResourceManager();
110     if (resourceManager == null)
111       return null;
112     
113     Path path = resourceManager.resolvePath(location);
114     
115     return getTag(path, location, taglib);
116   }
117
118   public TagInfo JavaDoc getTag(Path path, String JavaDoc prefix, String JavaDoc location)
119     throws JspParseException
120   {
121     TagLibraryInfo JavaDoc taglib = new TagTaglib(prefix, location);
122     
123     return getTag(path, location, taglib);
124   }
125
126   public TagInfo JavaDoc getTag(Path path, String JavaDoc location, TagLibraryInfo JavaDoc taglib)
127     throws JspParseException
128   {
129     if (path == null || ! path.canRead())
130       return null;
131
132     try {
133       if (location.endsWith(".tag")) {
134     JspCompilerInstance tagCompiler;
135
136     tagCompiler = _jspCompiler.getCompilerInstance(path, location);
137     tagCompiler.setXML(false);
138
139     return tagCompiler.compileTag(taglib);
140       }
141       else if (location.endsWith(".tagx")) {
142     JspCompilerInstance tagCompiler;
143
144     tagCompiler = _jspCompiler.getCompilerInstance(path, location);
145     tagCompiler.setXML(true);
146
147     return tagCompiler.compileTag(taglib);
148       }
149       else
150     throw new JspParseException(L.l("tag file '{0}' must end with .tag or .tagx",
151                     location));
152     } catch (Exception JavaDoc e) {
153       throw JspParseException.create(e);
154     }
155   }
156
157   public Class JavaDoc loadClass(String JavaDoc className)
158     throws Exception JavaDoc
159   {
160     Path classDir = _jspCompiler.getClassDir();
161
162     ClassLoader JavaDoc parentLoader = Thread.currentThread().getContextClassLoader();
163     ClassLoader JavaDoc jspLoader = SimpleLoader.create(parentLoader,
164                                                 classDir,
165                                                 null);
166     
167     return Class.forName(className, false, jspLoader);
168   }
169 }
170
Popular Tags