Clarify ACR API
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / CmsContent.java
diff --git a/org.argeo.cms/src/org/argeo/cms/acr/CmsContent.java b/org.argeo.cms/src/org/argeo/cms/acr/CmsContent.java
new file mode 100644 (file)
index 0000000..184b2ce
--- /dev/null
@@ -0,0 +1,53 @@
+package org.argeo.cms.acr;
+
+import java.util.Objects;
+
+import org.argeo.api.acr.Content;
+import org.argeo.api.acr.spi.ProvidedContent;
+
+/** A content within a CMS system. */
+public interface CmsContent extends ProvidedContent {
+
+       /**
+        * Split a path (with {@link Content#PATH_SEPARATOR} separator) in an array of
+        * length 2, the first part being the parent path (which could be either
+        * absolute or relative), the second one being the last segment, (guaranteed to
+        * be without a '/').
+        */
+       static String[] getParentPath(String path) {
+               if (path == null)
+                       throw new IllegalArgumentException("Path cannot be null");
+               if (path.length() == 0)
+                       throw new IllegalArgumentException("Path cannot be empty");
+               ContentUtils.checkDoubleSlash(path);
+               int parentIndex = path.lastIndexOf(PATH_SEPARATOR);
+               if (parentIndex == path.length() - 1) {// trailing '/'
+                       path = path.substring(0, path.length() - 1);
+                       parentIndex = path.lastIndexOf(PATH_SEPARATOR);
+               }
+
+               if (parentIndex == -1) // no '/'
+                       return new String[] { "", path };
+
+               return new String[] { parentIndex != 0 ? path.substring(0, parentIndex) : ContentUtils.PATH_SEPARATOR_STRING,
+                               path.substring(parentIndex + 1) };
+       }
+
+       /**
+        * Constructs a relative path between a base path and a given path.
+        * 
+        * @throws IllegalArgumentException if the base path is not an ancestor of the
+        *                                  path
+        */
+       static String relativize(String basePath, String path) throws IllegalArgumentException {
+               Objects.requireNonNull(basePath);
+               Objects.requireNonNull(path);
+               if (!path.startsWith(basePath))
+                       throw new IllegalArgumentException(basePath + " is not an ancestor of " + path);
+               String relativePath = path.substring(basePath.length());
+               if (relativePath.length() > 0 && relativePath.charAt(0) == PATH_SEPARATOR)
+                       relativePath = relativePath.substring(1);
+               return relativePath;
+       }
+
+}