X-Git-Url: http://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FCmsContent.java;fp=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FCmsContent.java;h=184b2ce35cb9280be2501ce4b4c1c37b017cf0ee;hp=0000000000000000000000000000000000000000;hb=c7b612ca9bd7df43b0bec37c8abcae846587c978;hpb=98f329e8641b792a07a4a0df62637565db0f738f 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 index 000000000..184b2ce35 --- /dev/null +++ b/org.argeo.cms/src/org/argeo/cms/acr/CmsContent.java @@ -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; + } + +}