struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jer...@apache.org
Subject svn commit: r672982 - in /struts/struts2/trunk/plugins/codebehind/src: main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java
Date Tue, 01 Jul 2008 03:48:04 GMT
Author: jeromy
Date: Mon Jun 30 20:48:03 2008
New Revision: 672982

URL: http://svn.apache.org/viewvc?rev=672982&view=rev
Log:
WW-2701 Improved CodebehindUnknownHandler use of classpath leading slash & path concatenation


Modified:
    struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
    struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java

Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java?rev=672982&r1=672981&r2=672982&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
(original)
+++ struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
Mon Jun 30 20:48:03 2008
@@ -179,22 +179,73 @@
         }
         return sb.toString();
     }
-    
-    protected String determinePath(String prefix, String ns) {
-        if (ns == null || "/".equals(ns)) {
-            ns = "";
+
+    protected String joinPaths(boolean leadingSlash, boolean trailingSlash, String... parts)
{
+        StringBuilder sb = new StringBuilder();
+        if (leadingSlash) {
+            sb.append("/");
         }
-        if (ns.length() > 0) {
-            if (ns.charAt(0) == '/') {
-                ns = ns.substring(1);
+        for (String part : parts) {
+            if (sb.length() > 0 && sb.charAt(sb.length()-1) != '/') {
+                sb.append("/");
             }
-            if (ns.charAt(ns.length() - 1) != '/') {
-                ns += "/";
+            sb.append(stripSlashes(part));
+        }
+        if (trailingSlash) {
+            if (sb.length() > 0 && sb.charAt(sb.length()-1) != '/') {
+                sb.append("/");
             }
         }
-        return prefix + ns;
+        return sb.toString();
+    }
+
+    protected String determinePath(String prefix, String ns) {        
+        return joinPaths(true, true, prefix, ns);
+    }
+
+    protected String stripLeadingSlash(String path) {
+        String result;
+        if (path != null) {
+            if (path.length() > 0) {
+                if (path.charAt(0) == '/') {
+                    result = path.substring(1);
+                } else {
+                    result = path;
+                }
+            } else {
+                result = path;
+            }
+        } else {
+            result = "";
+        }
+
+        return result;
     }
-    
+
+    protected String stripTrailingSlash(String path) {
+        String result;
+
+        if (path != null) {
+            if (path.length() > 0) {
+                if (path.charAt(path.length() - 1) == '/') {
+                    result = path.substring(0, path.length()-1);
+                } else {
+                    result = path;
+                }
+            } else {
+                result = path;
+            }
+        } else {
+            result = "";
+        }
+
+        return result;
+    }
+
+    protected String stripSlashes(String path) {
+        return stripLeadingSlash(stripTrailingSlash(path));
+    }
+
     URL locateTemplate(String path) throws MalformedURLException {
         URL template = servletContext.getResource(path);
         if (template != null) {
@@ -202,9 +253,9 @@
                 LOG.debug("Loaded template '" + path + "' from servlet context.");
             }
         } else {
-            template = ClassLoaderUtils.getResource(path, getClass());
+            template = ClassLoaderUtils.getResource(stripLeadingSlash(path), getClass());
             if (template != null && LOG.isDebugEnabled()) {
-                LOG.debug("Loaded template '" + path + "' from class path.");           
    
+                LOG.debug("Loaded template '" + stripLeadingSlash(path) + "' from class path.");
             }
         }
         return template;

Modified: struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java?rev=672982&r1=672981&r2=672982&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java
(original)
+++ struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java
Mon Jun 30 20:48:03 2008
@@ -76,6 +76,9 @@
         assertEquals("/foo/", handler.determinePath("/", "/foo"));
         assertEquals("/foo/", handler.determinePath("/", "/foo/"));
         assertEquals("/foo/", handler.determinePath("/", "foo"));
+        assertEquals("/", handler.determinePath("", ""));
+        assertEquals("/foo/", handler.determinePath("", "foo"));
+        assertEquals("/foo/", handler.determinePath("", "/foo/"));
     }
     
     public void testLocateTemplate() throws MalformedURLException {



Mime
View raw message