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