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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp;
031:
032: import com.caucho.jsp.java.TagTaglib;
033: import com.caucho.loader.SimpleLoader;
034: import com.caucho.util.L10N;
035: import com.caucho.util.Log;
036: import com.caucho.vfs.Path;
037:
038: import javax.servlet.jsp.tagext.TagInfo;
039: import javax.servlet.jsp.tagext.TagLibraryInfo;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: /**
044: * Stores the information for the .tags
045: */
046: public class TagFileManager {
047: static final L10N L = new L10N(TagFileManager.class);
048: private static final Logger log = Log.open(TagFileManager.class);
049:
050: private JspCompiler _jspCompiler;
051:
052: public TagFileManager(JspCompiler compiler) {
053: _jspCompiler = compiler;
054: }
055:
056: public TagInfo getTag(String prefix, String shortName,
057: String location) throws JspParseException {
058: if (location == null)
059: return null;
060:
061: try {
062: String originalLocation = location;
063:
064: if (location.startsWith("urn:jsptagdir:"))
065: location = location
066: .substring("urn:jsptagdir:".length());
067:
068: TagLibraryInfo taglib = new TagTaglib(prefix,
069: originalLocation);
070:
071: String uri = location;
072:
073: TagInfo tag = null;
074:
075: try {
076: tag = getTag(uri, taglib);
077: if (tag != null)
078: return tag;
079: } catch (Exception e) {
080: log.log(Level.FINEST, e.toString(), e);
081: }
082:
083: if (!location.endsWith("/"))
084: location = location + "/";
085:
086: uri = location + shortName + ".tag";
087:
088: tag = getTag(uri, taglib);
089: if (tag != null)
090: return tag;
091:
092: uri = location + shortName + ".tagx";
093: return getTag(uri, taglib);
094: } catch (Exception e) {
095: throw JspParseException.create(e);
096: }
097: }
098:
099: public TagInfo getTag(String prefix, String location)
100: throws JspParseException {
101: TagLibraryInfo taglib = new TagTaglib(prefix, location);
102:
103: return getTag(location, taglib);
104: }
105:
106: public TagInfo getTag(String location, TagLibraryInfo taglib)
107: throws JspParseException {
108: JspResourceManager resourceManager = _jspCompiler
109: .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 getTag(Path path, String prefix, String location)
119: throws JspParseException {
120: TagLibraryInfo taglib = new TagTaglib(prefix, location);
121:
122: return getTag(path, location, taglib);
123: }
124:
125: public TagInfo getTag(Path path, String location,
126: TagLibraryInfo taglib) throws JspParseException {
127: if (path == null || !path.canRead())
128: return null;
129:
130: try {
131: if (location.endsWith(".tag")) {
132: JspCompilerInstance tagCompiler;
133:
134: tagCompiler = _jspCompiler.getCompilerInstance(path,
135: location);
136: tagCompiler.setXML(false);
137:
138: return tagCompiler.compileTag(taglib);
139: } else if (location.endsWith(".tagx")) {
140: JspCompilerInstance tagCompiler;
141:
142: tagCompiler = _jspCompiler.getCompilerInstance(path,
143: location);
144: tagCompiler.setXML(true);
145:
146: return tagCompiler.compileTag(taglib);
147: } else
148: throw new JspParseException(L.l(
149: "tag file '{0}' must end with .tag or .tagx",
150: location));
151: } catch (Exception e) {
152: throw JspParseException.create(e);
153: }
154: }
155:
156: public Class loadClass(String className) throws Exception {
157: Path classDir = _jspCompiler.getClassDir();
158:
159: ClassLoader parentLoader = Thread.currentThread()
160: .getContextClassLoader();
161: ClassLoader jspLoader = SimpleLoader.create(parentLoader,
162: classDir, null);
163:
164: return Class.forName(className, false, jspLoader);
165: }
166: }
|