SLING-5455 - Add helper class to construct valid paths
Move back PathBuilder to the api bundle, in a org.apache.sling.resource.path package.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/api@1728151 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/resource/path/PathBuilder.java b/src/main/java/org/apache/sling/resource/path/PathBuilder.java
new file mode 100644
index 0000000..bef3dfe
--- /dev/null
+++ b/src/main/java/org/apache/sling/resource/path/PathBuilder.java
@@ -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.resource.path;
+
+/**
+ * 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
diff --git a/src/main/java/org/apache/sling/resource/path/package-info.java b/src/main/java/org/apache/sling/resource/path/package-info.java
new file mode 100644
index 0000000..87079c2
--- /dev/null
+++ b/src/main/java/org/apache/sling/resource/path/package-info.java
@@ -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.
+ */
+@aQute.bnd.annotation.Version("1.0")
+package org.apache.sling.resource.path;
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resource/path/PathBuilderTest.java b/src/test/java/org/apache/sling/resource/path/PathBuilderTest.java
new file mode 100644
index 0000000..67ded9f
--- /dev/null
+++ b/src/test/java/org/apache/sling/resource/path/PathBuilderTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.resource.path;
+
+import static org.junit.Assert.assertThat;
+
+import org.apache.sling.resource.path.PathBuilder;
+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);
+ }
+}