RootEvaluator.java
5.28 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
152
153
154
155
/*
* Decompiled with CFR 0_118.
*/
package com.day.cq.search.impl.builder;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.eval.EvaluationContext;
import com.day.cq.search.eval.PathPredicateEvaluator;
import com.day.cq.search.eval.PredicateGroupEvaluator;
import com.day.cq.search.eval.TypePredicateEvaluator;
import com.day.cq.search.eval.XPath;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RootEvaluator
extends PredicateGroupEvaluator {
private Predicate pathPredicate;
private Predicate typePredicate;
private List<OrderBySpec> orderBySpecs = new ArrayList<OrderBySpec>();
@Override
protected String getOpeningBracket() {
return "[";
}
@Override
protected String getClosingBracket() {
return "]";
}
public static boolean isExcerpt(Predicate rootPredicate) {
return rootPredicate.getBool("excerpt");
}
public static void setExcerpt(Predicate rootPredicate, boolean excerpt) {
rootPredicate.set("excerpt", excerpt ? "true" : "false");
}
public void addOrderBySpec(String property, boolean ascending, boolean ignoreCase) {
this.orderBySpecs.add(new OrderBySpec(property, ascending, ignoreCase));
}
@Override
public String getXPathExpression(Predicate predicate, EvaluationContext context) {
String type;
PredicateGroup pg;
Predicate p;
Iterator<Predicate> i$;
StringBuffer xpath = new StringBuffer();
if (!(predicate instanceof PredicateGroup)) {
return "";
}
PredicateGroup group = (PredicateGroup)predicate;
if (group.size() == 1 || group.allRequired()) {
i$ = group.iterator();
while (i$.hasNext()) {
p = i$.next();
if (p instanceof PredicateGroup && (pg = (PredicateGroup)p).size() == 1) {
p = pg.get(0);
}
if (!"path".equals(p.getType()) || !p.hasNonEmptyValue("path") || !p.get("path").startsWith("/") || p.getBool("self")) continue;
this.pathPredicate = p;
this.pathPredicate.setIgnored(true);
break;
}
}
if (group.size() == 1 || group.allRequired()) {
i$ = group.iterator();
while (i$.hasNext()) {
p = i$.next();
if (p instanceof PredicateGroup && (pg = (PredicateGroup)p).size() == 1) {
p = pg.get(0);
}
if (!"type".equals(p.getType()) || !p.hasNonEmptyValue("type")) continue;
this.typePredicate = p;
this.typePredicate.setIgnored(true);
break;
}
}
if (this.pathPredicate != null) {
xpath.append("/jcr:root").append(PathPredicateEvaluator.encodePath(this.pathPredicate));
if (!this.pathPredicate.getBool("exact")) {
if (this.pathPredicate.getBool("flat")) {
if (xpath.charAt(xpath.length() - 1) == '/') {
xpath.append("*");
} else {
xpath.append("/*");
}
} else if (xpath.charAt(xpath.length() - 1) == '/') {
xpath.append("/*");
} else {
xpath.append("//*");
}
}
} else {
xpath.append("//*");
}
if (this.typePredicate != null && (type = TypePredicateEvaluator.getType(this.typePredicate, context)) != null) {
String typeConstraint = "element(*, " + type + ")";
int end = xpath.length() - 1;
if (xpath.charAt(end) == '*') {
xpath.replace(end, end + 1, typeConstraint);
} else if (xpath.charAt(end) != '/') {
xpath.append("/").append(typeConstraint);
} else {
xpath.append(typeConstraint);
}
}
xpath.append(super.getXPathExpression(predicate, context));
if (RootEvaluator.isExcerpt(group)) {
xpath.append("/rep:excerpt(.)");
}
if (this.orderBySpecs.size() > 0) {
xpath.append(" order by ");
int count = 0;
for (OrderBySpec obs : this.orderBySpecs) {
if (count > 0) {
xpath.append(", ");
}
xpath.append(obs.getOrderByForXPath());
++count;
}
}
return xpath.toString();
}
public void cleanup() {
if (this.pathPredicate != null) {
this.pathPredicate.setIgnored(false);
}
if (this.typePredicate != null) {
this.typePredicate.setIgnored(false);
}
}
private static class OrderBySpec {
public String property;
public boolean ascending;
public boolean ignoreCase;
public OrderBySpec(String property, boolean ascending, boolean ignoreCase) {
this.property = property;
this.ascending = ascending;
this.ignoreCase = ignoreCase;
}
public String getOrderByForXPath() {
return XPath.getXPathOrderBy(this.property, this.ascending, this.ignoreCase);
}
}
}