[Jsptest-svn-commits] SF.net SVN: jsptest: [204] trunk/jsptest-jsp20/src
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-04-09 17:49:59
|
Revision: 204 http://jsptest.svn.sourceforge.net/jsptest/?rev=204&view=rev Author: lkoskela Date: 2008-04-09 10:49:49 -0700 (Wed, 09 Apr 2008) Log Message: ----------- Fixed the issue with locating TLD files from inside archives in the class path (reported by Mathias Broekelmann) Modified Paths: -------------- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/ClasspathTldLocator.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TldLocation.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/WebInfLibTldLocator.java trunk/jsptest-jsp20/src/test/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TestClasspathTldLocator.java Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/ClasspathTldLocator.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/ClasspathTldLocator.java 2008-04-09 17:47:50 UTC (rev 203) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/ClasspathTldLocator.java 2008-04-09 17:49:49 UTC (rev 204) @@ -22,14 +22,28 @@ public TldLocation find(String filename) { try { - String pathInsideArchive = "/META-INF/" + filename; - URL resource = getClass().getResource(pathInsideArchive); + URL resource = getClass().getResource( + "/META-INF/" + filename); if (resource != null) { - return TldLocation.foundFromClassPath(resource, - pathInsideArchive); + String filePath = extractFileUrlFrom(resource); + String resourcePath = null; + if ("jar".equals(resource.getProtocol())) { + resourcePath = "META-INF/" + filename; + } + return TldLocation.foundFromClassPath(filePath, + resourcePath); } } catch (Exception ignored) { + ignored.printStackTrace(); } return TldLocation.notFound(); } + + private String extractFileUrlFrom(URL resource) { + String path = resource.getPath(); + if (path.indexOf("!") > -1) { + path = path.substring(0, path.indexOf("!")); + } + return path; + } } \ No newline at end of file Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TldLocation.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TldLocation.java 2008-04-09 17:47:50 UTC (rev 203) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TldLocation.java 2008-04-09 17:49:49 UTC (rev 204) @@ -16,74 +16,83 @@ package net.sf.jsptest.compiler.jsp20.mock.taglibs; -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - +/** + * @author Lasse Koskela + */ public class TldLocation { - private final boolean wasFound; + private final boolean wasFound; - protected TldLocation(boolean wasFound) { - this.wasFound = wasFound; - } + /** + * @param wasFound + * <tt>true</tt> means the TLD location was found, + * <tt>false</tt> means it wasn't found. + */ + protected TldLocation(boolean wasFound) { + this.wasFound = wasFound; + } - /** - * Indicates whether the location was found or not. - * - * @return true if the TLD was found, false if it wasn't. - */ - public boolean wasFound() { - return wasFound; - } + /** + * Indicates whether the location was found or not. + * + * @return true if the TLD was found, false if it wasn't. + */ + public boolean wasFound() { + return wasFound; + } - /** - * <p> - * Formats the location of the TLD into the array format used by - * <tt>TldLocationsCache</tt>, i.e. an array in one of these formats: - * </p> - * <p> { "/WEB-INF/foo.tld", null } for exploded TLD files<br/> { - * "file://url/to/taglibs.jar", "META-INF/foo.tld } for TLD files found in - * archives - * </p> - */ - public String[] toArray() { - return null; - } + /** + * <p> + * Formats the location of the TLD into the array format used by + * <tt>TldLocationsCache</tt>, i.e. an array in one of these formats: + * </p> + * <p> { "/WEB-INF/foo.tld", null } for exploded TLD files<br/> { + * "file:/url/to/taglibs.jar", "META-INF/foo.tld } for TLD files found in + * archives + * </p> + */ + public String[] toArray() { + return null; + } - public static TldLocation notFound() { - return new TldLocation(false); - } + /** + * Creates a "not found" instance of TldLocation. + */ + public static TldLocation notFound() { + return new TldLocation(false); + } - public static TldLocation foundFromWebInf(final String filename) { - return new TldLocation(true) { - public String[] toArray() { - return new String[] { "/WEB-INF/" + filename, null }; - } - }; - } + /** + * Creates a <tt>TldLocation</tt> instance for a file located under + * WEB-INF. + * + * @param filename + * The base name of the TLD file, e.g. "c-rt.tld". + */ + public static TldLocation foundFromWebInf(final String filename) { + return new TldLocation(true) { + public String[] toArray() { + return new String[] { "/WEB-INF/" + filename, null }; + } + }; + } - public static TldLocation foundFromArchive(final File archive, - final String archiveEntry) { - return new TldLocation(true) { - public String[] toArray() { - try { - String pathToArchive = archive.toURL().toExternalForm(); - return new String[] { pathToArchive, archiveEntry }; - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - }; - } - - public static TldLocation foundFromClassPath(final URL archive, - final String pathInsideArchive) { - return new TldLocation(true) { - public String[] toArray() { - String pathToArchive = archive.toExternalForm(); - return new String[] { pathToArchive, pathInsideArchive }; - } - }; - } + /** + * Creates a <tt>TldLocation</tt> instance for a file located from the + * class path. + * + * @param pathToArchive + * URL to the archive containing the TLD file + * @param pathInsideArchive + * Path to the TLD file inside the archive + */ + public static TldLocation foundFromClassPath( + final String pathToArchive, final String pathInsideArchive) { + return new TldLocation(true) { + public String[] toArray() { + return new String[] { pathToArchive, + pathInsideArchive }; + } + }; + } } Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/WebInfLibTldLocator.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/WebInfLibTldLocator.java 2008-04-09 17:47:50 UTC (rev 203) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/WebInfLibTldLocator.java 2008-04-09 17:49:49 UTC (rev 204) @@ -24,42 +24,45 @@ public class WebInfLibTldLocator implements TldLocator { - private final String webRoot; + private final String webRoot; - public WebInfLibTldLocator(String webRoot) { - this.webRoot = webRoot; - } + public WebInfLibTldLocator(String webRoot) { + this.webRoot = webRoot; + } - public TldLocation find(String filename) { - File webInfLib = new File(new File(webRoot, "WEB-INF"), "lib"); - if (webInfLib.exists()) { - File[] archives = findArchivesFrom(webInfLib); - return findTldFromArchives(filename, archives); - } else { - return TldLocation.notFound(); - } - } + public TldLocation find(String filename) { + File webInfLib = new File(new File(webRoot, "WEB-INF"), "lib"); + if (webInfLib.exists()) { + File[] archives = findArchivesFrom(webInfLib); + return findTldFromArchives(filename, archives); + } else { + return TldLocation.notFound(); + } + } - private TldLocation findTldFromArchives(String filename, File[] archives) { - for (int i = 0; i < archives.length; i++) { - try { - JarFile archive = new JarFile(archives[i]); - JarEntry entry = archive.getJarEntry("META-INF/" + filename); - if (entry != null) { - return TldLocation.foundFromArchive(archives[i], - "META-INF/" + filename); - } - } catch (IOException ignored) { - } - } - return TldLocation.notFound(); - } + private TldLocation findTldFromArchives(String filename, + File[] archives) { + for (int i = 0; i < archives.length; i++) { + try { + JarFile archive = new JarFile(archives[i]); + JarEntry entry = archive.getJarEntry("META-INF/" + + filename); + if (entry != null) { + return TldLocation.foundFromClassPath(archives[i] + .toURL().toString(), "META-INF/" + + filename); + } + } catch (IOException ignored) { + } + } + return TldLocation.notFound(); + } - private File[] findArchivesFrom(File dir) { - return dir.listFiles(new FilenameFilter() { - public boolean accept(File directory, String name) { - return name.endsWith(".jar"); - } - }); - } + private File[] findArchivesFrom(File dir) { + return dir.listFiles(new FilenameFilter() { + public boolean accept(File directory, String name) { + return name.endsWith(".jar"); + } + }); + } } \ No newline at end of file Modified: trunk/jsptest-jsp20/src/test/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TestClasspathTldLocator.java =================================================================== --- trunk/jsptest-jsp20/src/test/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TestClasspathTldLocator.java 2008-04-09 17:47:50 UTC (rev 203) +++ trunk/jsptest-jsp20/src/test/java/net/sf/jsptest/compiler/jsp20/mock/taglibs/TestClasspathTldLocator.java 2008-04-09 17:49:49 UTC (rev 204) @@ -1,22 +1,41 @@ package net.sf.jsptest.compiler.jsp20.mock.taglibs; +import java.io.File; + import junit.framework.TestCase; public class TestClasspathTldLocator extends TestCase { - private ClasspathTldLocator locator; + private TldLocator locator; protected void setUp() throws Exception { super.setUp(); locator = new ClasspathTldLocator(); } + public void testMissingFilesAreReportedAsNotFound() + throws Exception { + assertFalse(locator.find("nosuchfile.tld").wasFound()); + } + public void testTldFilesAreFoundFromClasspath() throws Exception { - assertTrue(locator.find("exists.tld").wasFound()); + TldLocation location = locator.find("exists.tld"); + assertTrue(location.wasFound()); + assertTrue(new File(location.toArray()[0]).exists()); + assertNull(location.toArray()[1]); } - public void testMissingFilesAreReportedAsNotFound() + public void testValidLocationIfTldFilesAreFoundFromJarInClasspath() throws Exception { - assertFalse(locator.find("nosuchfile.tld").wasFound()); + TldLocation location = locator.find("c.tld"); + assertTrue(location.wasFound()); + String[] array = location.toArray(); + assertEquals(2, array.length); + assertFalse("location must not start with 'jar:' : " + + array[0], array[0].startsWith("jar:")); + assertFalse( + "location must not end with '.tld' : " + array[0], + array[0].endsWith(".tld")); + assertEquals(array[1], "META-INF/c.tld"); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |