]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java
5609cf7778ca65555824c500e48d3769be944f69
1 package org
.argeo
.cms
.acr
;
3 import java
.io
.PrintStream
;
4 import java
.util
.function
.BiConsumer
;
6 import javax
.xml
.namespace
.QName
;
8 import org
.argeo
.api
.acr
.Content
;
9 import org
.argeo
.api
.cms
.CmsSession
;
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);
17 public static void traverse(Content content
, BiConsumer
<Content
, Integer
> doIt
, Integer maxDepth
) {
18 doTraverse(content
, doIt
, 0, maxDepth
);
21 private static void doTraverse(Content content
, BiConsumer
<Content
, Integer
> doIt
, int currentDepth
,
23 doIt
.accept(content
, currentDepth
);
24 if (maxDepth
!= null && currentDepth
== maxDepth
)
26 int nextDepth
= currentDepth
+ 1;
27 for (Content child
: content
) {
28 doTraverse(child
, doIt
, nextDepth
, maxDepth
);
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
++) {
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
));
43 if (content
.hasText()) {
44 out
.println("<![CDATA[" + content
.getText().trim() + "]]>");
49 // public static <T> boolean isString(T t) {
50 // return t instanceof String;
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 '/').
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) };
66 private ContentUtils() {