Author: rombert
Date: Tue Jan 26 14:59:04 2016
New Revision: 1726814

URL: http://svn.apache.org/viewvc?rev=1726814&view=rev
Log:
SLING-5455 - Add helper class to construct valid paths 

Add the PathBuilder class to org.apache.sling.api

Added:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/
    
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/PathBuilder.java
    
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/package-info.java
    sling/trunk/bundles/api/src/test/java/org/apache/sling/api/paths/
    
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/paths/PathBuilderTest.java
Modified:
    
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/CombinedResourceProvider.java

Added: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/PathBuilder.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/PathBuilder.java?rev=1726814&view=auto
==============================================================================
--- 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/PathBuilder.java
 (added)
+++ 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/PathBuilder.java
 Tue Jan 26 14:59:04 2016
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.sling.api.paths;
+
+/**
+ * The <tt>PathBuilder</tt> offers a convenient way of creating a valid path 
from multiple fragments
+ *
+ */
+public final class PathBuilder {
+    
+    private StringBuilder sb = new StringBuilder();
+    
+    /**
+     * Creates a new <tt>PathBuilder</tt> instance
+     * 
+     * @param path the initial path
+     */
+    public PathBuilder(String path) {
+        
+        if ( path == null || path.isEmpty() || path.charAt(0) != '/') {
+            throw new IllegalArgumentException("Path '" + path + "' is not 
absolute");
+        }
+        
+        sb.append(path);
+    }
+    
+    /**
+     * Appends a new path fragment
+     * 
+     * @param path the path fragment to append
+     * @return this instance
+     */
+    public PathBuilder append(String path) {
+        
+        if ( path == null || path.isEmpty() ) {
+            throw new IllegalArgumentException("Path '" + path + "' is null or 
empty");
+        }
+        
+        boolean trailingSlash = sb.charAt(sb.length() - 1) == '/';
+        boolean leadingSlash = path.charAt(0) == '/';
+        
+        if ( trailingSlash && leadingSlash) {
+            sb.append(path.substring(1));
+        } else if ( !trailingSlash && !leadingSlash ) {
+            sb.append('/').append(path);
+        } else {
+            sb.append(path);
+        }
+        
+        return this;
+    }
+    
+    /**
+     * Returns the path
+     * 
+     * @return the path
+     */
+    public String toString() {
+        return sb.toString();
+    }
+}
\ No newline at end of file

Added: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/package-info.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/package-info.java?rev=1726814&view=auto
==============================================================================
--- 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/package-info.java
 (added)
+++ 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/paths/package-info.java
 Tue Jan 26 14:59:04 2016
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
[email protected]("1.0")
+package org.apache.sling.api.paths;
\ No newline at end of file

Added: 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/paths/PathBuilderTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/paths/PathBuilderTest.java?rev=1726814&view=auto
==============================================================================
--- 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/paths/PathBuilderTest.java
 (added)
+++ 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/paths/PathBuilderTest.java
 Tue Jan 26 14:59:04 2016
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.sling.api.paths;
+
+import static org.junit.Assert.assertThat;
+
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+public class PathBuilderTest {
+
+    @Test
+    public void noChangeNeeded_root() {
+        
+        assertThat(new PathBuilder("/").append("path").toString(), 
Matchers.equalTo("/path"));
+    }
+
+    @Test
+    public void noChangeNeeded_intermediate() {
+        
+        assertThat(new PathBuilder("/parent").append("/child").toString(), 
Matchers.equalTo("/parent/child"));
+    }
+    
+    @Test
+    public void removeSlash_root() {
+        
+        assertThat(new PathBuilder("/").append("/path").toString(), 
Matchers.equalTo("/path"));
+    }
+
+    @Test
+    public void removeSlash_intermediate() {
+        
+        assertThat(new PathBuilder("/parent/").append("/child").toString(), 
Matchers.equalTo("/parent/child"));
+    }
+    
+    @Test
+    public void addSlash() {
+        
+        assertThat(new PathBuilder("/parent").append("child").toString(), 
Matchers.equalTo("/parent/child"));
+    }
+    
+    @Test(expected = IllegalArgumentException.class) 
+    public void relativeInitialPaths() {
+        new PathBuilder("relative");
+    }
+
+    @Test(expected = IllegalArgumentException.class) 
+    public void nullInitialPath() {
+        new PathBuilder(null);
+    }
+
+    @Test(expected = IllegalArgumentException.class) 
+    public void emptyInitialPath() {
+        new PathBuilder("");
+    }
+    
+    @Test(expected = IllegalArgumentException.class)
+    public void emptyAppendedPath() {
+        new PathBuilder("/parent").append("");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void nullAppendedPath() {
+        new PathBuilder("/parent").append(null);
+    }
+}

Modified: 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/CombinedResourceProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/CombinedResourceProvider.java?rev=1726814&r1=1726813&r2=1726814&view=diff
==============================================================================
--- 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/CombinedResourceProvider.java
 (original)
+++ 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/stateful/CombinedResourceProvider.java
 Tue Jan 26 14:59:04 2016
@@ -38,6 +38,7 @@ import javax.annotation.Nonnull;
 
 import org.apache.commons.collections.iterators.IteratorChain;
 import org.apache.commons.lang.ArrayUtils;
+import org.apache.sling.api.paths.PathBuilder;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
@@ -653,32 +654,4 @@ public class CombinedResourceProvider {
             return null;
         }
     }
-    
-    private static class PathBuilder {
-        private StringBuilder sb = new StringBuilder();
-        
-        public PathBuilder(String path) {
-            sb.append(path);
-        }
-        
-        PathBuilder append(String path) {
-            
-            boolean trailingSlash = sb.length() > 0 && sb.charAt(sb.length() - 
1) == '/';
-            boolean leadingSlash = path.length() > 0 && path.charAt(0) == '/';
-            
-            if ( trailingSlash && leadingSlash) {
-                sb.append(path.substring(1));
-            } else if ( !trailingSlash && !leadingSlash ) {
-                sb.append('/').append(path);
-            } else {
-                sb.append(path);
-            }
-            
-            return this;
-        }
-        
-        public String toString() {
-            return sb.toString();
-        }
-    }
 }


Reply via email to