]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ContentUtils.java
5ea79662a387341f5d902c58f0fdc5c95de74f3b
[lgpl/argeo-commons.git] / ContentUtils.java
1 package org.argeo.cms.acr;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.StringJoiner;
7 import java.util.function.BiConsumer;
8
9 import javax.xml.namespace.QName;
10
11 import org.argeo.api.acr.Content;
12 import org.argeo.api.acr.ContentSession;
13 import org.argeo.cms.CmsUserManager;
14 import org.argeo.osgi.useradmin.UserDirectory;
15 import org.osgi.service.useradmin.Role;
16
17 /** Utilities and routines around {@link Content}. */
18 public class ContentUtils {
19 public static void traverse(Content content, BiConsumer<Content, Integer> doIt) {
20 traverse(content, doIt, (Integer) null);
21 }
22
23 public static void traverse(Content content, BiConsumer<Content, Integer> doIt, Integer maxDepth) {
24 doTraverse(content, doIt, 0, maxDepth);
25 }
26
27 private static void doTraverse(Content content, BiConsumer<Content, Integer> doIt, int currentDepth,
28 Integer maxDepth) {
29 doIt.accept(content, currentDepth);
30 if (maxDepth != null && currentDepth == maxDepth)
31 return;
32 int nextDepth = currentDepth + 1;
33 for (Content child : content) {
34 doTraverse(child, doIt, nextDepth, maxDepth);
35 }
36 }
37
38 public static void print(Content content, PrintStream out, int depth, boolean printText) {
39 StringBuilder sb = new StringBuilder();
40 for (int i = 0; i < depth; i++) {
41 sb.append(" ");
42 }
43 String prefix = sb.toString();
44 out.println(prefix + content.getName());
45 for (QName key : content.keySet()) {
46 out.println(prefix + " " + key + "=" + content.get(key));
47 }
48 if (printText) {
49 if (content.hasText()) {
50 out.println("<![CDATA[" + content.getText().trim() + "]]>");
51 }
52 }
53 }
54
55 // public static <T> boolean isString(T t) {
56 // return t instanceof String;
57 // }
58
59 public static final char SLASH = '/';
60 public static final String ROOT_SLASH = "" + SLASH;
61
62 /**
63 * Split a path (with '/' separator) in an array of length 2, the first part
64 * being the parent path (which could be either absolute or relative), the
65 * second one being the last segment, (guaranteed to be without a '/').
66 */
67 public static String[] getParentPath(String path) {
68 if (path == null)
69 throw new IllegalArgumentException("Path cannot be null");
70 if (path.length() == 0)
71 throw new IllegalArgumentException("Path cannot be empty");
72 checkDoubleSlash(path);
73 int parentIndex = path.lastIndexOf(SLASH);
74 if (parentIndex == path.length() - 1) {// trailing '/'
75 path = path.substring(0, path.length() - 1);
76 parentIndex = path.lastIndexOf(SLASH);
77 }
78
79 if (parentIndex == -1) // no '/'
80 return new String[] { "", path };
81
82 return new String[] { parentIndex != 0 ? path.substring(0, parentIndex) : "" + SLASH,
83 path.substring(parentIndex + 1) };
84 }
85
86 public static String toPath(List<String> segments) {
87 // TODO checks
88 StringJoiner sj = new StringJoiner("/");
89 segments.forEach((s) -> sj.add(s));
90 return sj.toString();
91 }
92
93 public static List<String> toPathSegments(String path) {
94 List<String> res = new ArrayList<>();
95 if ("".equals(path) || ROOT_SLASH.equals(path))
96 return res;
97 collectPathSegments(path, res);
98 return res;
99 }
100
101 private static void collectPathSegments(String path, List<String> segments) {
102 String[] parent = getParentPath(path);
103 if ("".equals(parent[1])) // root
104 return;
105 segments.add(0, parent[1]);
106 if ("".equals(parent[0])) // end
107 return;
108 collectPathSegments(parent[0], segments);
109 }
110
111 public static void checkDoubleSlash(String path) {
112 if (path.contains(SLASH + "" + SLASH))
113 throw new IllegalArgumentException("Path " + path + " contains //");
114 }
115
116 /** Singleton. */
117 private ContentUtils() {
118
119 }
120
121 public static Content roleToContent(CmsUserManager userManager, ContentSession contentSession, Role role) {
122 UserDirectory userDirectory = userManager.getDirectory(role);
123 String path = CmsContentRepository.DIRECTORY_BASE + SLASH + userDirectory.getName() + SLASH
124 + userDirectory.getRolePath(role);
125 Content content = contentSession.get(path);
126 return content;
127 }
128
129 }