1 package org
.argeo
.cms
.acr
;
3 import java
.io
.PrintStream
;
4 import java
.util
.ArrayList
;
6 import java
.util
.StringJoiner
;
7 import java
.util
.function
.BiConsumer
;
9 import javax
.xml
.namespace
.QName
;
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
;
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);
23 public static void traverse(Content content
, BiConsumer
<Content
, Integer
> doIt
, Integer maxDepth
) {
24 doTraverse(content
, doIt
, 0, maxDepth
);
27 private static void doTraverse(Content content
, BiConsumer
<Content
, Integer
> doIt
, int currentDepth
,
29 doIt
.accept(content
, currentDepth
);
30 if (maxDepth
!= null && currentDepth
== maxDepth
)
32 int nextDepth
= currentDepth
+ 1;
33 for (Content child
: content
) {
34 doTraverse(child
, doIt
, nextDepth
, maxDepth
);
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
++) {
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
));
49 if (content
.hasText()) {
50 out
.println("<![CDATA[" + content
.getText().trim() + "]]>");
55 // public static <T> boolean isString(T t) {
56 // return t instanceof String;
59 public static final char SLASH
= '/';
60 public static final String ROOT_SLASH
= "" + SLASH
;
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 '/').
67 public static String
[] getParentPath(String path
) {
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
);
79 if (parentIndex
== -1) // no '/'
80 return new String
[] { "", path
};
82 return new String
[] { parentIndex
!= 0 ? path
.substring(0, parentIndex
) : "" + SLASH
,
83 path
.substring(parentIndex
+ 1) };
86 public static String
toPath(List
<String
> segments
) {
88 StringJoiner sj
= new StringJoiner("/");
89 segments
.forEach((s
) -> sj
.add(s
));
93 public static List
<String
> toPathSegments(String path
) {
94 List
<String
> res
= new ArrayList
<>();
95 if ("".equals(path
) || ROOT_SLASH
.equals(path
))
97 collectPathSegments(path
, res
);
101 private static void collectPathSegments(String path
, List
<String
> segments
) {
102 String
[] parent
= getParentPath(path
);
103 if ("".equals(parent
[1])) // root
105 segments
.add(0, parent
[1]);
106 if ("".equals(parent
[0])) // end
108 collectPathSegments(parent
[0], segments
);
111 public static void checkDoubleSlash(String path
) {
112 if (path
.contains(SLASH
+ "" + SLASH
))
113 throw new IllegalArgumentException("Path " + path
+ " contains //");
117 private ContentUtils() {
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
);