]> git.argeo.org Git - lgpl/argeo-commons.git/blob - acr/ContentUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / acr / ContentUtils.java
1 package org.argeo.cms.acr;
2
3 import java.io.PrintStream;
4 import java.util.function.BiConsumer;
5
6 import javax.xml.namespace.QName;
7
8 import org.argeo.api.acr.Content;
9 import org.argeo.api.cms.CmsSession;
10
11 /** Utilities and routines around {@link Content}. */
12 public class ContentUtils {
13 public static void traverse(Content content, BiConsumer<Content, Integer> doIt) {
14 traverse(content, doIt, (Integer) null);
15 }
16
17 public static void traverse(Content content, BiConsumer<Content, Integer> doIt, Integer maxDepth) {
18 doTraverse(content, doIt, 0, maxDepth);
19 }
20
21 private static void doTraverse(Content content, BiConsumer<Content, Integer> doIt, int currentDepth,
22 Integer maxDepth) {
23 doIt.accept(content, currentDepth);
24 if (maxDepth != null && currentDepth == maxDepth)
25 return;
26 int nextDepth = currentDepth + 1;
27 for (Content child : content) {
28 doTraverse(child, doIt, nextDepth, maxDepth);
29 }
30 }
31
32 public static void print(Content content, PrintStream out, int depth, boolean printText) {
33 StringBuilder sb = new StringBuilder();
34 for (int i = 0; i < depth; i++) {
35 sb.append(" ");
36 }
37 String prefix = sb.toString();
38 out.println(prefix + content.getName());
39 for (QName key : content.keySet()) {
40 out.println(prefix + " " + key + "=" + content.get(key));
41 }
42 if (printText) {
43 if (content.hasText()) {
44 out.println("<![CDATA[" + content.getText().trim() + "]]>");
45 }
46 }
47 }
48
49 // public static <T> boolean isString(T t) {
50 // return t instanceof String;
51 // }
52
53 /**
54 * Split a path (with '/' separator) in an array of length 2, the first part
55 * being the parent path (which could be either absolute or relative), the
56 * second one being the last segment, (guaranteed to be with '/').
57 */
58 public static String[] getParentPath(String path) {
59 int parentIndex = path.lastIndexOf('/');
60 // TODO make it more robust
61 return new String[] { parentIndex != 0 ? path.substring(0, parentIndex) : "/",
62 path.substring(parentIndex + 1) };
63 }
64
65 /** Singleton. */
66 private ContentUtils() {
67
68 }
69
70 }