XPathTreeQuery.java
3.48 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:
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.core.servlets;
import com.day.cq.mcm.core.servlets.JSONBuilderVisitor;
import com.day.cq.mcm.core.servlets.MyNode;
import com.day.cq.mcm.core.servlets.MyTree;
import com.day.cq.mcm.core.servlets.TreeVisitor;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class XPathTreeQuery {
private final Logger log;
protected SlingHttpServletRequest request;
protected SlingHttpServletResponse response;
private Map<String, List<String>> filter;
public XPathTreeQuery(SlingHttpServletRequest request, SlingHttpServletResponse response) {
this.log = LoggerFactory.getLogger(this.getClass());
this.request = request;
this.response = response;
}
public Iterator<Resource> findResources(String xpathQuery) {
Iterator found = this.request.getResourceResolver().findResources(xpathQuery, "xpath");
return found;
}
public JSONArray createJSONTreeFromRequest() {
String xpathQuery = this.request.getParameter("xpathQuery");
return this.createJSONTreeFromQuery(xpathQuery, false);
}
public JSONArray createJSONTreeFromQuery(String query, boolean includeSystemRes) {
JSONArray json = null;
try {
Iterator<Resource> iterator = this.findResources(query);
HashSet touchpointTypeFilter = null;
if (this.filter != null && this.filter.containsKey("touchpointType")) {
touchpointTypeFilter = new HashSet(this.filter.get("touchpointType"));
}
MyTree tree = new MyTree();
while (iterator.hasNext()) {
Resource current = iterator.next();
if (!includeSystemRes && current.getPath().startsWith("/jcr:system")) continue;
MyNode newNode = new MyNode();
newNode.initFrom(current);
if (touchpointTypeFilter != null && !touchpointTypeFilter.contains(newNode.touchpointType)) continue;
tree.addNode(newNode);
}
JSONBuilderVisitor visitor = new JSONBuilderVisitor();
tree.depthFirstTraversal(visitor);
if (visitor.rootArray.length() > 1) {
throw new Exception("More than 1 root: " + visitor.rootArray.toString());
}
json = visitor.rootArray.length() != 0 ? visitor.rootArray.getJSONObject(0).getJSONArray("children") : new JSONArray();
}
catch (Exception e) {
this.log.error("Exception while finding.", (Throwable)e);
json = new JSONArray();
}
return json;
}
public void setFilter(Map<String, List<String>> filterMap) {
this.filter = filterMap;
}
}