[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tapestry-dev
Subject:    svn commit: r480658 - in /tapestry/tapestry5/tapestry-ioc/trunk/src:
From:       hlship () apache ! org
Date:       2006-11-29 17:54:19
Message-ID: 20061129175420.581281A9846 () eris ! apache ! org
[Download RAW message or body]

Author: hlship
Date: Wed Nov 29 09:54:18 2006
New Revision: 480658

URL: http://svn.apache.org/viewvc?view=rev&rev=480658
Log:
Add more methods to the Resource interface, and split out most of the methods into an \
abstract base class.

Added:
    tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java
  tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/parent-folder.txt
  tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/same-folder.txt
  tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/sub/
  tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/sub/sub-folder.txt
 Modified:
    tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java
  tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
  tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java
  tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java


Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java?view=diff&rev=480658&r1=480657&r2=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java \
                (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java \
Wed Nov 29 09:54:18 2006 @@ -28,19 +28,38 @@
 public interface Resource
 {
     /** Returns the URL for the resource, or null if it does not exist. */
-    URL getResourceURL();
+    URL toURL();
 
     /** Returns a localized version of the resource. */
-    Resource getLocalization(Locale locale);
+    Resource forLocale(Locale locale);
+
+    /**
+     * Returns a Resource based on a relative path, relative to the folder \
containing the resource. +     * Understands the "." (current folder) and ".." \
(parent folder) conventions, and treats +     * multiple sequential slashes as a \
single slash. +     */
+    Resource forFile(String relativePath);
 
     /**
      * Returns a new Resource with the extension changed (or, if the resource does \
                not have an
-     * extension, the extension is added). The new Resource may not exist (that is,
-     * {@link #getResourceURL()} may return null.
+     * extension, the extension is added). The new Resource may not exist (that is, \
{@link #toURL()} +     * may return null.
      * 
      * @param extension
      *            to apply to the resource
      * @return the new resource
      */
     Resource withExtension(String extension);
+
+    /**
+     * Returns the portion of the path up to the last forward slash; this is the \
directory or folder +     * portion of the Resource.
+     */
+    String getFolder();
+
+    /**
+     * Returns the file portion of the Resource path, everything that follows the \
final forward +     * slash.
+     */
+    String getFile();
 }

Added: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java?view=auto&rev=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java \
                (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java \
Wed Nov 29 09:54:18 2006 @@ -0,0 +1,138 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.ioc.internal.util;
+
+import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
+import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
+
+import java.net.URL;
+import java.util.Locale;
+
+import org.apache.tapestry.ioc.Resource;
+
+/**
+ * Abstract implementation of {@link Resource}. Subclasses must implement the \
abstract methods + * {@link Resource#toURL()} and {@link #newResource(String)} as \
well as toString(), hashCode() and + * equals().
+ */
+public abstract class AbstractResource implements Resource
+{
+    private final String _path;
+
+    AbstractResource(String path)
+    {
+        notNull(path, "path");
+        _path = path;
+
+    }
+
+    protected final String getPath()
+    {
+        return _path;
+    }
+
+    public final String getFile()
+    {
+        int slashx = _path.lastIndexOf('/');
+
+        return _path.substring(slashx + 1);
+    }
+
+    public final String getFolder()
+    {
+        int slashx = _path.lastIndexOf('/');
+
+        // TODO: Incorrect for file in root folder
+
+        return _path.substring(0, slashx);
+    }
+
+    public final Resource forFile(String relativePath)
+    {
+        Defense.notNull(relativePath, "relativePath");
+
+        StringBuilder builder = new StringBuilder(getFolder());
+
+        for (String term : relativePath.split("/"))
+        {
+            // This will occur if the relative path contains sequential slashes
+
+            if (term.equals("")) continue;
+
+            if (term.equals(".")) continue;
+
+            if (term.equals(".."))
+            {
+                int slashx = builder.lastIndexOf("/");
+
+                // TODO: slashx < 0 (i.e., no slash)
+
+                // Trim path to content before the slash
+
+                builder.setLength(slashx);
+                continue;
+            }
+
+            // TODO: term blank or otherwise invalid?
+            // TODO: final term should not be "." or "..", or for that matter, the
+            // name of a folder, since a Resource should be a file within
+            // a folder.
+
+            builder.append("/");
+            builder.append(term);
+        }
+
+        return createResource(builder.toString());
+    }
+
+    public final Resource forLocale(Locale locale)
+    {
+        for (String path : new LocalizedNameGenerator(_path, locale))
+        {
+            Resource potential = createResource(path);
+
+            URL url = potential.toURL();
+
+            if (url != null) return potential;
+        }
+
+        return null;
+    }
+
+    public final Resource withExtension(String extension)
+    {
+        notBlank(extension, "extension");
+
+        int dotx = _path.lastIndexOf('.');
+
+        if (dotx < 0) return createResource(_path + "." + extension);
+
+        return createResource(_path.substring(0, dotx + 1) + extension);
+    }
+
+    /**
+     * Creates a new resource, unless the path matches the current Resource's path \
(in which case, +     * this resource is returned).
+     */
+    private Resource createResource(String path)
+    {
+        if (_path.equals(path)) return this;
+
+        return newResource(path);
+    }
+
+    /** Factory method provided by subclasses. */
+    protected abstract Resource newResource(String path);
+}

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/ \
org/apache/tapestry/ioc/internal/util/ClasspathResource.java?view=diff&rev=480658&r1=480657&r2=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java \
                (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java \
Wed Nov 29 09:54:18 2006 @@ -14,20 +14,21 @@
 
 package org.apache.tapestry.ioc.internal.util;
 
+import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
+
 import java.net.URL;
 import java.util.Locale;
 
 import org.apache.tapestry.ioc.Resource;
 
 /**
- * Bare-bones, temporary implementation.
+ * Implementation of {@link Resource} for files on the classpath (as defined by a
+ * {@link ClassLoader}).
  */
-public class ClasspathResource implements Resource
+public final class ClasspathResource extends AbstractResource
 {
     private final ClassLoader _classLoader;
 
-    private final String _path;
-
     private URL _url;
 
     public ClasspathResource(String path)
@@ -37,19 +38,22 @@
 
     public ClasspathResource(ClassLoader classLoader, String path)
     {
+        super(path);
+
+        notNull(classLoader, "classLoader");
+
         _classLoader = classLoader;
-        _path = path;
     }
 
-    private ClasspathResource(ClassLoader classLoader, String path, URL url)
+    @Override
+    protected Resource newResource(String path)
     {
-        this(classLoader, path);
-        _url = url;
+        return new ClasspathResource(_classLoader, path);
     }
 
-    public synchronized URL getResourceURL()
+    public synchronized URL toURL()
     {
-        if (_url == null) _url = _classLoader.getResource(_path);
+        if (_url == null) _url = _classLoader.getResource(getPath());
 
         return _url;
     }
@@ -65,48 +69,19 @@
 
         ClasspathResource other = (ClasspathResource) obj;
 
-        return other._classLoader == _classLoader && other._path.equals(_path);
+        return other._classLoader == _classLoader && \
other.getPath().equals(getPath());  }
 
     @Override
     public int hashCode()
     {
-        return 227 ^ _path.hashCode();
-    }
-
-    public Resource getLocalization(Locale locale)
-    {
-        for (String path : new LocalizedNameGenerator(_path, locale))
-        {
-            URL url = _classLoader.getResource(path);
-
-            if (url == null) continue;
-
-            if (_path.equals(path)) return this;
-
-            return new ClasspathResource(_classLoader, path, url);
-        }
-
-        return null;
-    }
-
-    public Resource withExtension(String extension)
-    {
-        int dotx = _path.lastIndexOf(".");
-
-        if (dotx < 0) return new ClasspathResource(_classLoader, _path + "." + \
                extension);
-
-        String existing = _path.substring(dotx + 1);
-
-        if (existing.equals(extension)) return this;
-
-        return new ClasspathResource(_classLoader, _path.substring(0, dotx + 1) + \
extension); +        return 227 ^ getPath().hashCode();
     }
 
     @Override
     public String toString()
     {
-        return "classpath:" + _path;
+        return "classpath:" + getPath();
     }
 
 }

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/ \
org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java?view=diff&rev=480658&r1=480657&r2=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java \
                (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java \
Wed Nov 29 09:54:18 2006 @@ -27,38 +27,108 @@
 
 public class ClasspathResourceTest extends Assert
 {
-    private static final String PATH = "org/apache/tapestry/ioc/internal/util/";
+    private static final String FOLDER = "org/apache/tapestry/ioc/internal/util";
+
+    private static final String PATH = FOLDER + "/resource.txt";
 
     @Test
     public void get_resource_URL() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
 
         assertEquals(content(r), "content from resource.txt");
     }
 
     @Test
+    public void path_and_file()
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        assertEquals(r.getFolder(), FOLDER);
+        assertEquals(r.getFile(), "resource.txt");
+
+    }
+
+    @Test
+    public void for_file_in_same_folder() throws Exception
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        Resource n = r.forFile("same-folder.txt");
+
+        assertEquals(content(n), "content from same-folder resource");
+    }
+
+    @Test
+    public void for_file_single_dot() throws Exception
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        Resource n = r.forFile("./same-folder.txt");
+
+        assertEquals(content(n), "content from same-folder resource");
+    }
+
+    @Test
+    public void multiple_slashes_treated_as_single_slash() throws Exception
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        Resource n = r.forFile("././/.///same-folder.txt");
+
+        assertEquals(content(n), "content from same-folder resource");
+    }
+
+    @Test
+    public void for_file_in_subfolder() throws Exception
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        Resource n = r.forFile("sub/sub-folder.txt");
+
+        assertEquals(content(n), "content from sub-folder resource");
+    }
+
+    @Test
+    public void for_file_same_resource() throws Exception
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        assertSame(r.forFile("../util/resource.txt"), r);
+    }
+
+    @Test
+    public void for_file_in_parent_folder() throws Exception
+    {
+        Resource r = new ClasspathResource(PATH);
+
+        Resource n = r.forFile("../parent-folder.txt");
+
+        assertEquals(content(n), "content from parent-folder resource");
+    }
+
+    @Test
     public void to_string() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
 
-        assertEquals(r.toString(), "classpath:" + PATH + "resource.txt");
+        assertEquals(r.toString(), "classpath:" + PATH);
     }
 
     @Test
     public void get_URL_for_missing_resource() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "missing-resource.txt");
+        Resource r = new ClasspathResource(FOLDER + "/missing-resource.txt");
 
-        assertNull(r.getResourceURL());
+        assertNull(r.toURL());
     }
 
     @Test
     public void localization_of_resource() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
 
-        Resource l = r.getLocalization(Locale.FRENCH);
+        Resource l = r.forLocale(Locale.FRENCH);
 
         assertEquals(content(l), "french content");
     }
@@ -66,9 +136,9 @@
     @Test
     public void localization_to_closest_match() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
 
-        Resource l = r.getLocalization(Locale.CANADA_FRENCH);
+        Resource l = r.forLocale(Locale.CANADA_FRENCH);
 
         assertEquals(content(l), "french content");
     }
@@ -76,9 +146,9 @@
     @Test
     public void localization_to_base_resource() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
 
-        Resource l = r.getLocalization(Locale.JAPANESE);
+        Resource l = r.forLocale(Locale.JAPANESE);
 
         assertSame(l, r);
     }
@@ -86,7 +156,7 @@
     @Test
     public void with_extension_same_extension()
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
 
         assertSame(r.withExtension("txt"), r);
     }
@@ -94,7 +164,7 @@
     @Test
     public void with_extension() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
         Resource e = r.withExtension("ext");
 
         assertEquals(content(e), "ext content");
@@ -103,7 +173,7 @@
     @Test
     public void with_extension_adds_extension() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "resource");
+        Resource r = new ClasspathResource(FOLDER + "/resource");
         Resource e = r.withExtension("ext");
 
         assertEquals(content(e), "ext content");
@@ -112,23 +182,23 @@
     @Test
     public void with_extension_missing_resource_is_null()
     {
-        Resource r = new ClasspathResource(PATH + "resource.txt");
+        Resource r = new ClasspathResource(PATH);
         Resource e = r.withExtension("does-not-exist");
 
-        assertNull(e.getResourceURL());
+        assertNull(e.toURL());
     }
 
     @Test
     public void localization_of_missing_resource() throws Exception
     {
-        Resource r = new ClasspathResource(PATH + "missing-resource.txt");
+        Resource r = new ClasspathResource(FOLDER + "/missing-resource.txt");
 
-        assertNull(r.getLocalization(Locale.FRENCH));
+        assertNull(r.forLocale(Locale.FRENCH));
     }
 
     private String content(Resource resource) throws Exception
     {
-        return content(resource.getResourceURL());
+        return content(resource.toURL());
     }
 
     private String content(URL url) throws Exception

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/ \
org/apache/tapestry/ioc/internal/util/LocationImplTest.java?view=diff&rev=480658&r1=480657&r2=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java \
                (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java \
Wed Nov 29 09:54:18 2006 @@ -14,8 +14,6 @@
 
 package org.apache.tapestry.ioc.internal.util;
 
-import java.net.URL;
-import java.util.Locale;
 import java.util.Random;
 
 import org.apache.tapestry.ioc.Location;
@@ -27,75 +25,48 @@
 {
     private final Random _random = new Random();
 
-    private static class ResourceFixture implements Resource
-    {
-        public URL getResourceURL()
-        {
-            return null;
-        }
-
-        public Resource getLocalization(Locale locale)
-        {
-            return null;
-        }
-
-        @Override
-        public String toString()
-        {
-            return "<Resource>";
-        }
-
-        public Resource withExtension(String extension)
-        {
-            throw new UnsupportedOperationException("withExtension");
-        }
-
-    }
+    private final Resource _resource = new ClasspathResource("/foo/Bar.xml");
 
     @Test
     public void all_three_parameters()
     {
-        Resource r = new ResourceFixture();
 
         int line = _random.nextInt();
         int column = _random.nextInt();
 
-        Location l = new LocationImpl(r, line, column);
+        Location l = new LocationImpl(_resource, line, column);
 
-        assertSame(l.getResource(), r);
+        assertSame(l.getResource(), _resource);
         assertEquals(l.getLine(), line);
         assertEquals(l.getColumn(), column);
 
-        assertEquals(l.toString(), String.format("<Resource>, line %d, column %d", \
line, column)); +        assertEquals(l.toString(), String.format("%s, line %d, \
column %d", _resource, line, column));  }
 
     @Test
     public void unknown_column()
     {
-        Resource r = new ResourceFixture();
-
         int line = _random.nextInt();
 
-        Location l = new LocationImpl(r, line);
+        Location l = new LocationImpl(_resource, line);
 
-        assertSame(l.getResource(), r);
+        assertSame(l.getResource(), _resource);
         assertEquals(l.getLine(), line);
         assertEquals(l.getColumn(), -1);
 
-        assertEquals(l.toString(), String.format("<Resource>, line %d", line));
+        assertEquals(l.toString(), String.format("%s, line %d", _resource, line));
     }
 
     @Test
     public void unknown_line_and_column()
     {
-        Resource r = new ResourceFixture();
 
-        Location l = new LocationImpl(r);
+        Location l = new LocationImpl(_resource);
 
-        assertSame(l.getResource(), r);
+        assertSame(l.getResource(), _resource);
         assertEquals(l.getLine(), -1);
         assertEquals(l.getColumn(), -1);
 
-        assertEquals(l.toString(), "<Resource>");
+        assertEquals(l.toString(), _resource.toString());
     }
 }

Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/parent-folder.txt
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/parent-folder.txt?view=auto&rev=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/parent-folder.txt \
                (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/parent-folder.txt \
Wed Nov 29 09:54:18 2006 @@ -0,0 +1 @@
+content from parent-folder resource

Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/same-folder.txt
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/same-folder.txt?view=auto&rev=480658
 ==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/same-folder.txt \
                (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/same-folder.txt \
Wed Nov 29 09:54:18 2006 @@ -0,0 +1 @@
+content from same-folder resource

Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/sub/sub-folder.txt
                
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/resou \
rces/org/apache/tapestry/ioc/internal/util/sub/sub-folder.txt?view=auto&rev=480658 \
                ==============================================================================
                
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/sub/sub-folder.txt \
                (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/sub/sub-folder.txt \
Wed Nov 29 09:54:18 2006 @@ -0,0 +1 @@
+content from sub-folder resource


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic