X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FContentUtils.java;h=cfd90f93c1d5e77aa86e900985646095204adf4c;hb=a3b108c64c2d33fd8c953288e638e28cb60c226a;hp=74e93fc0affa701c78233641e985d4e14f36a2cf;hpb=6aae2af9d483bef26ca8c5be37c9702f839443db;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java b/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java index 74e93fc0a..cfd90f93c 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java @@ -3,6 +3,7 @@ package org.argeo.cms.acr; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.StringJoiner; import java.util.function.BiConsumer; @@ -15,11 +16,11 @@ import org.argeo.api.acr.ContentRepository; import org.argeo.api.acr.ContentSession; import org.argeo.api.acr.DName; import org.argeo.api.cms.CmsAuth; -import org.argeo.cms.CmsUserManager; -import org.argeo.osgi.useradmin.UserDirectory; -import org.argeo.util.CurrentSubject; -import org.argeo.util.directory.Directory; -import org.argeo.util.directory.HierarchyUnit; +import org.argeo.api.cms.directory.CmsDirectory; +import org.argeo.api.cms.directory.CmsUserManager; +import org.argeo.api.cms.directory.HierarchyUnit; +import org.argeo.api.cms.directory.UserDirectory; +import org.argeo.cms.util.CurrentSubject; import org.osgi.service.useradmin.Role; /** Utilities and routines around {@link Content}. */ @@ -135,7 +136,7 @@ public class ContentUtils { } public static Content hierarchyUnitToContent(ContentSession contentSession, HierarchyUnit hierarchyUnit) { - Directory directory = hierarchyUnit.getDirectory(); + CmsDirectory directory = hierarchyUnit.getDirectory(); StringJoiner relativePath = new StringJoiner(SLASH_STRING); buildHierarchyUnitPath(hierarchyUnit, relativePath); String path = directoryPath(directory) + relativePath.toString(); @@ -143,8 +144,8 @@ public class ContentUtils { return content; } - /** The path to this {@link Directory}. Ends with a /. */ - private static String directoryPath(Directory directory) { + /** The path to this {@link CmsDirectory}. Ends with a /. */ + private static String directoryPath(CmsDirectory directory) { return CmsContentRepository.DIRECTORY_BASE + SLASH + directory.getName() + SLASH; } @@ -195,6 +196,23 @@ public class ContentUtils { } } + /** + * 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 + */ + public 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) == '/') + relativePath = relativePath.substring(1); + return relativePath; + } + /** Singleton. */ private ContentUtils() {