DateParser.java
4.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Value
* javax.jcr.ValueFactory
* org.apache.jackrabbit.util.ISO8601
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.jackrabbit.util.ISO8601;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateParser {
private static final Logger log = LoggerFactory.getLogger(DateParser.class);
private final List<CalendarParserSupport> formats = new LinkedList<CalendarParserSupport>();
public void register(String format) {
CalendarParserSupport parser = "ISO8601".equalsIgnoreCase(format) ? new Iso8601ParserSupport() : new SimpleDateFormatParserSupport(format);
this.formats.add(parser);
}
public synchronized Calendar parse(String source) {
for (CalendarParserSupport fmt : this.formats) {
try {
Calendar c = fmt.parse(source);
if (log.isDebugEnabled()) {
log.debug("Parsed " + source + " using " + fmt + " into " + c);
}
return c;
}
catch (ParseException e) {
if (!log.isDebugEnabled()) continue;
log.debug("Failed parsing " + source + " using " + fmt);
continue;
}
}
return null;
}
public synchronized Calendar[] parse(String[] sources) {
Calendar[] ret = new Calendar[sources.length];
for (int i = 0; i < sources.length; ++i) {
ret[i] = this.parse(sources[i]);
if (ret[i] != null) continue;
return null;
}
return ret;
}
public synchronized Value[] parse(String[] sources, ValueFactory factory) {
Value[] ret = new Value[sources.length];
for (int i = 0; i < sources.length; ++i) {
Calendar c = this.parse(sources[i]);
if (c == null) {
return null;
}
ret[i] = factory.createValue(c);
}
return ret;
}
private static class Iso8601ParserSupport
implements CalendarParserSupport {
static final String FORMAT_MARKER = "ISO8601";
private Iso8601ParserSupport() {
}
@Override
public Calendar parse(String dateTime) throws ParseException {
try {
Calendar c = ISO8601.parse((String)dateTime);
if (c == null) {
throw new ParseException(dateTime + " cannot be parsed as ISO8601 formatted date string", 0);
}
return c;
}
catch (Exception e) {
throw new ParseException(e.getMessage(), 0);
}
}
public String toString() {
return "ISO8601 Parser";
}
}
private static class SimpleDateFormatParserSupport
implements CalendarParserSupport {
private final SimpleDateFormat dateFormat;
SimpleDateFormatParserSupport(String format) {
this.dateFormat = new SimpleDateFormat(format, Locale.US);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Calendar parse(String dateTime) throws ParseException {
Date d;
SimpleDateFormat simpleDateFormat = this.dateFormat;
synchronized (simpleDateFormat) {
d = this.dateFormat.parse(dateTime);
}
Calendar c = Calendar.getInstance();
c.setTime(d);
return c;
}
public String toString() {
return "SimpleDateFormat:" + this.dateFormat.toPattern();
}
}
private static interface CalendarParserSupport {
public Calendar parse(String var1) throws ParseException;
}
}