ExtTreeJsonWriter.java
5.05 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.LabeledResource
* com.day.cq.commons.TidyJSONWriter
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.nodetype.NodeType
* org.apache.commons.collections.Predicate
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
*/
package com.day.cq.extwidget;
import com.day.cq.commons.LabeledResource;
import com.day.cq.commons.TidyJSONWriter;
import com.day.text.Text;
import org.apache.commons.collections.Predicate;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class ExtTreeJsonWriter {
private final ResourceResolver resolver;
private final Predicate predicate;
private final int depth;
private boolean tidy;
public ExtTreeJsonWriter(ResourceResolver resolver, Predicate predicate, int depth) {
this.resolver = resolver;
this.predicate = predicate;
this.depth = depth;
}
public boolean isTidy() {
return this.tidy;
}
public void setTidy(boolean tidy) {
this.tidy = tidy;
}
public void write(Writer out, String path) throws IOException {
this.write(out, this.resolver.getResource(path));
}
public void write(Writer out, Resource res) throws IOException {
try {
TidyJSONWriter jw = new TidyJSONWriter(out);
jw.setTidy(this.tidy);
if (res == null) {
jw.array();
jw.endArray();
return;
}
boolean isOrderable = this.getIsOrderable(res);
this.write((JSONWriter)jw, this.getChildren(res), 0, isOrderable);
}
catch (JSONException e) {
throw new IOException("Error while writing json " + (Object)e);
}
}
private void write(JSONWriter out, List<Resource> list, int level, boolean orderable) throws JSONException {
out.array();
List<Resource> oList = orderable ? list : this.orderList(list);
for (Resource resource : oList) {
String text;
out.object();
LabeledResource lr = (LabeledResource)resource.adaptTo(LabeledResource.class);
String name = Text.getName((String)resource.getPath());
out.key("name").value((Object)name);
if (lr == null) {
text = name;
} else {
String string = text = lr.getTitle() == null ? name : lr.getTitle();
if (lr.getDescription() != null) {
out.key("description").value((Object)lr.getDescription());
}
}
if (text != null) {
text = text.replaceAll("<", "<");
}
out.key("text").value((Object)text);
out.key("type").value((Object)resource.getResourceType());
List<Resource> children = this.getChildren(resource);
out.key("cls").value((Object)(children.isEmpty() ? "file" : "folder"));
if (children.isEmpty()) {
out.key("leaf").value(true);
} else if (level < this.depth) {
out.key("children");
boolean isOrderable = this.getIsOrderable(resource);
this.write(out, children, level + 1, isOrderable);
}
out.endObject();
}
out.endArray();
}
private List<Resource> getChildren(Resource res) {
LinkedList<Resource> children = new LinkedList<Resource>();
Iterator iter = this.resolver.listChildren(res);
while (iter.hasNext()) {
Resource child = (Resource)iter.next();
if (this.predicate != null && !this.predicate.evaluate((Object)child)) continue;
children.add(child);
}
return children;
}
private List<Resource> orderList(List<Resource> list) {
Collections.sort(list, new Comparator(){
public int compare(Object o1, Object o2) {
Resource r1 = (Resource)o1;
Resource r2 = (Resource)o2;
return Text.getName((String)r1.getPath()).compareToIgnoreCase(Text.getName((String)r2.getPath()));
}
});
return list;
}
private boolean getIsOrderable(Resource resource) {
Node node = (Node)resource.adaptTo(Node.class);
if (node != null) {
try {
return node.getPrimaryNodeType().hasOrderableChildNodes();
}
catch (RepositoryException re) {
// empty catch block
}
}
return false;
}
}