XPath.java
3.78 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.jackrabbit.util.ISO9075
* org.apache.jackrabbit.util.Text
*/
package com.day.cq.search.eval;
import org.apache.jackrabbit.util.ISO9075;
import org.apache.jackrabbit.util.Text;
public abstract class XPath {
public static final String SEARCH_ALL = "//*";
public static final String NOT = "not";
public static final String AND = " and ";
public static final String OR = " or ";
public static final String ORDER_BY = " order by ";
public static final String DESC = " descending";
public static final String OPENING_BRACKET = "(";
public static final String CLOSING_BRACKET = ")";
public static final String PREDICATE_OPENING_BRACKET = "[";
public static final String PREDICATE_CLOSING_BRACKET = "]";
public static final String JCR_ROOT = "/jcr:root";
public static final String JCR_LIKE = "jcr:like";
public static final String JCR_CONTAINS = "jcr:contains";
public static final String JCR_LIKE_WILDCARD = "%";
public static final char JCR_LIKE_ANY_WILDCARD = '%';
public static final char JCR_LIKE_SINGLE_WILDCARD = '_';
public static final String REP_EXCERPT = "/rep:excerpt(.)";
public static final String FN_LOWER_CASE = "fn:lower-case";
public static String getPropertyPath(String property) {
String name;
if (property.charAt(0) == '@') {
property = property.substring(1);
}
name = (name = Text.getName((String)property)).charAt(0) != '@' ? "" + '@' + ISO9075.encode((String)name) : "" + '@' + ISO9075.encode((String)name.substring(1));
String parent = Text.getRelativeParent((String)property, (int)1);
if (parent.length() > 0) {
return ISO9075.encodePath((String)parent) + "/" + name;
}
return name;
}
public static String getStringLiteral(String value) {
return "'" + value.replaceAll("'", "''") + "'";
}
private static int count(String s, char c) {
if (s == null || s.length() == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) != c) continue;
++count;
}
return count;
}
public static String getFulltextStringLiteral(String value) {
int doubleQuotes = XPath.count(value, '\"');
if (doubleQuotes % 2 == 1) {
int last = value.lastIndexOf(34);
value = value.substring(0, last) + "\\" + value.substring(last);
}
value = Text.escapeIllegalXpathSearchChars((String)value);
return "'" + value.replaceAll("'", "''") + "'";
}
public static String getEqualsExpression(String property, String value) {
return XPath.getPropertyPath(property) + " = " + XPath.getStringLiteral(value);
}
public static String getUnequalsExpression(String property, String value) {
return XPath.getPropertyPath(property) + " != " + XPath.getStringLiteral(value);
}
public static String getJcrLikeExpression(String property, String value) {
return "jcr:like(" + XPath.getPropertyPath(property) + ", " + XPath.getStringLiteral(value) + ")";
}
public static String getNotExpression(String property) {
return "not(" + XPath.getPropertyPath(property) + ")";
}
public static String getXPathOrderBy(String property, boolean ascending) {
return XPath.getXPathOrderBy(property, ascending, false);
}
public static String getXPathOrderBy(String property, boolean ascending, boolean ignoreCase) {
if (ignoreCase) {
return "fn:lower-case(" + XPath.getPropertyPath(property) + ")" + (ascending ? "" : " descending");
}
return XPath.getPropertyPath(property) + (ascending ? "" : " descending");
}
}