RelativeDateRangePredicateEvaluator.java
3.29 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
*/
package com.day.cq.search.eval;
import com.day.cq.search.Predicate;
import com.day.cq.search.eval.DateRangePredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;
import java.util.GregorianCalendar;
import java.util.Scanner;
import org.apache.felix.scr.annotations.Component;
@Component(metatype=0, factory="com.day.cq.search.eval.PredicateEvaluator/relativedaterange")
public class RelativeDateRangePredicateEvaluator
extends DateRangePredicateEvaluator {
@Override
public String getXPathExpression(Predicate p, EvaluationContext context) {
Predicate modifiedPredicate = p.clone();
long now = new GregorianCalendar().getTimeInMillis();
long upperBound = 0;
long lowerBound = 0;
try {
upperBound = this.parseDateRange(p.get("upperBound"));
modifiedPredicate.set("upperBound", "" + (upperBound += now));
}
catch (Exception nfe) {
modifiedPredicate.set("upperBound", null);
}
try {
lowerBound = this.parseDateRange(p.get("lowerBound"));
modifiedPredicate.set("lowerBound", "" + (lowerBound += now));
}
catch (Exception nfe) {
modifiedPredicate.set("lowerBound", null);
}
return super.getXPathExpression(modifiedPredicate, context);
}
public long parseDateRange(String daterange) throws NumberFormatException {
if (daterange == null || daterange.length() == 0) {
throw new NumberFormatException("cannot parse empty string");
}
if (daterange.matches("^(-)?\\d+$")) {
return Long.parseLong(daterange);
}
String cleandaterange = daterange.replaceAll("[^dsmhdwMy\\d-]", "");
boolean negative = cleandaterange.matches("^-.*");
if (!(cleandaterange = cleandaterange.replaceAll("-", "")).matches("^(\\d+\\w)+$")) {
throw new NumberFormatException("only s, m, h, d, w, M, y are allowed modifiers");
}
cleandaterange = cleandaterange.replaceAll("(\\d+)(\\w)", "$1 $2 ");
Scanner scanner = new Scanner(cleandaterange);
long value = 0;
while (scanner.hasNext()) {
long number = scanner.nextLong();
String unit = scanner.next();
if ("s".equals(unit)) {
value += number * 1000;
continue;
}
if ("m".equals(unit)) {
value += number * 60 * 1000;
continue;
}
if ("h".equals(unit)) {
value += number * 60 * 60 * 1000;
continue;
}
if ("d".equals(unit)) {
value += number * 24 * 60 * 60 * 1000;
continue;
}
if ("w".equals(unit)) {
value += number * 7 * 24 * 60 * 60 * 1000;
continue;
}
if ("M".equals(unit)) {
value += number * 30 * 24 * 60 * 60 * 1000;
continue;
}
if (!"y".equals(unit)) continue;
value += number * 365 * 24 * 60 * 60 * 1000;
}
if (negative) {
value *= -1;
}
return value;
}
}