From c5886fb8dab1ee3b0bfcca3814d3aafca8a87c3e Mon Sep 17 00:00:00 2001 From: jensp Date: Tue, 29 Jan 2013 10:46:17 +0000 Subject: [PATCH] Methoden zum Laden eines Folders anhand seines Pfades (Ticket 1404). git-svn-id: https://svn.libreccm.org/ccm/trunk@2054 8810af33-2d31-482b-a856-94f89814c4df --- ccm-cms/src/com/arsdigita/cms/Folder.java | 66 +++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/ccm-cms/src/com/arsdigita/cms/Folder.java b/ccm-cms/src/com/arsdigita/cms/Folder.java index e57f57016..6ff55ee77 100755 --- a/ccm-cms/src/com/arsdigita/cms/Folder.java +++ b/ccm-cms/src/com/arsdigita/cms/Folder.java @@ -802,4 +802,70 @@ public class Folder extends ContentItem { } return folder; } + + /** + * Retrieves a folder by its path from a given content section. + * + * @param section The content section from which the folder should be retrieved. + * @param path The path of the folder, relative to the content section. + * @return The folder with the given path from the provided content section. If there is no such folder, + * {@code null} is returned. It is up to the caller to check the returned value for {@code null} and take + * appropriate actions. + */ + public static Folder retrieveFolder(final ContentSection section, final String path) { + if (section == null) { + throw new IllegalArgumentException("No content section provided."); + } + + if ((path == null) || path.isEmpty()) { + throw new IllegalArgumentException("No path provided."); + } + + if (path.charAt(0) != '/') { + throw new IllegalArgumentException("Provided path is not an absolute path (starting with '/')."); + } + + final String[] pathTokens = path.split("/"); + + final Folder rootFolder = section.getRootFolder(); + + Folder folder = rootFolder; + for(String token : pathTokens) { + if ((token == null) || token.isEmpty() || "/".equals(token)) { + continue; + } + + folder = getSubFolder(token, folder); + + if (folder == null) { + break; + } + } + + return folder; + } + + private static Folder getSubFolder(final String name, final Folder fromFolder) { + final ItemCollection items = fromFolder.getItems(); + items.addFolderFilter(true); + items.addNameFilter(name); + + if (items.next()) { + return (Folder) items.getDomainObject(); + } else { + return null; + } + } + + /** + * Retrieves a folder of the current content section by its path. The path is given in a UNIX like synatax and must + * be an absolute path starting with '/'. + * + * @param path The path of the folder to retrieve relative to the current content section. + * @return The folder with the given path from the content section. If there is no such folder, {@code null} is + * returned. It is up to the caller to check the returned value for {@code null} and take appropriate actions. + */ + public static Folder retrieveFolder(final String path) { + return retrieveFolder(CMS.getContext().getContentSection(), path); + } }