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