JcrToolbarItem.java
2.68 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
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
*/
package com.day.cq.wcm.api.components;
import com.day.cq.wcm.api.components.Toolbar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
public class JcrToolbarItem
implements Toolbar.Item {
private final Node node;
public JcrToolbarItem(Node node) throws RepositoryException {
this.node = node;
}
@Override
public void write(JSONWriter writer) throws JSONException {
try {
this.dump(writer, this.node);
}
catch (RepositoryException e) {
throw new JSONException("Error while writing JSON.", (Throwable)e);
}
}
private void dump(JSONWriter out, Node node) throws JSONException, RepositoryException {
out.object();
out.key("name");
out.value((Object)node.getName());
PropertyIterator pIter = node.getProperties();
while (pIter.hasNext()) {
Property p = pIter.nextProperty();
if (p.getType() == 2) {
out.key("*" + p.getName());
} else {
out.key(p.getName());
}
if (p.isMultiple()) {
Value[] vs;
out.array();
for (Value v : vs = p.getValues()) {
this.dump(out, v);
}
out.endArray();
continue;
}
this.dump(out, p.getValue());
}
NodeIterator nIter = node.getNodes();
while (nIter.hasNext()) {
Node child = nIter.nextNode();
out.key(child.getName());
this.dump(out, child);
}
out.endObject();
}
private void dump(JSONWriter out, Value v) throws RepositoryException, JSONException {
if (v.getType() == 2) {
out.value(0);
} else if (v.getType() == 5) {
SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US);
out.value((Object)fmt.format(v.getDate().getTime()));
} else {
out.value((Object)v.getString());
}
}
}