JS.java
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.jcr.JsonItemWriter
*/
package com.day.cq.commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.jcr.JsonItemWriter;
public class JS {
public static String str(String text) {
try {
return JSONObject.valueToString((Object)text);
}
catch (JSONException e) {
return "null";
}
}
public static String array(String[] array) {
if (array == null) {
return "[]";
}
StringBuilder builder = new StringBuilder();
builder.append('[');
for (int i = 0; i < array.length; ++i) {
builder.append(JS.str(array[i]));
if (i >= array.length - 1) continue;
builder.append(", ");
}
builder.append(']');
return builder.toString();
}
public static String array(Collection<?> list) {
return new JSONArray(list).toString();
}
public static void writeNode(Writer out, Node node) throws IOException, RepositoryException, JSONException {
JS.writeNode(out, node, -1);
}
public static void writeNode(Writer out, Node node, int depth) throws IOException, RepositoryException, JSONException {
out.flush();
JsonItemWriter jsonWriter = new JsonItemWriter(null);
jsonWriter.dump(node, (Writer)new PrintWriter(out), depth);
out.flush();
}
public static String object(Node node) throws RepositoryException, JSONException {
return JS.object(node, -1);
}
public static String object(Node node, int depth) throws RepositoryException, JSONException {
JsonItemWriter jsonWriter = new JsonItemWriter(null);
StringWriter out = new StringWriter();
jsonWriter.dump(node, (Writer)out, depth);
return out.toString();
}
}