1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.xar.internal.property; |
21 |
|
|
22 |
|
import java.text.ParseException; |
23 |
|
import java.text.SimpleDateFormat; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.Locale; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
import javax.xml.stream.XMLStreamException; |
31 |
|
import javax.xml.stream.XMLStreamReader; |
32 |
|
import javax.xml.stream.XMLStreamWriter; |
33 |
|
|
34 |
|
import org.apache.commons.lang3.StringUtils; |
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.xar.internal.XarObjectPropertySerializer; |
38 |
|
|
39 |
|
|
40 |
|
@link@link |
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
@Component |
46 |
|
@Named("Date") |
47 |
|
@Singleton |
|
|
| 68.4% |
Uncovered Elements: 6 (19) |
Complexity: 6 |
Complexity Density: 0.46 |
|
48 |
|
public class DateXarObjectPropertySerializer implements XarObjectPropertySerializer |
49 |
|
{ |
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
public static final SimpleDateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); |
54 |
|
|
55 |
|
@Inject |
56 |
|
private Logger logger; |
57 |
|
|
|
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 4 |
Complexity Density: 0.33 |
|
58 |
5 |
@Override... |
59 |
|
public Object read(XMLStreamReader reader) throws XMLStreamException |
60 |
|
{ |
61 |
5 |
String value = reader.getElementText(); |
62 |
|
|
63 |
5 |
if (StringUtils.isEmpty(value)) { |
64 |
1 |
return null; |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
4 |
SimpleDateFormat sdf = DEFAULT_FORMAT; |
70 |
4 |
try { |
71 |
4 |
return sdf.parse(value); |
72 |
|
} catch (ParseException e) { |
73 |
|
|
74 |
0 |
SimpleDateFormat sdfOld = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US); |
75 |
0 |
this.logger.warn("Failed to parse date [{}] using format [{}]. Trying again with format [{}].", value, |
76 |
|
sdf.toPattern(), sdfOld.toPattern()); |
77 |
0 |
try { |
78 |
0 |
return sdfOld.parse(value); |
79 |
|
} catch (ParseException exception) { |
80 |
0 |
this.logger.warn("Failed to parse date [{}] using format [{}]. Defaulting to the current date.", value, |
81 |
|
sdfOld.toPattern()); |
82 |
0 |
return new Date(); |
83 |
|
} |
84 |
|
} |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
87 |
52 |
@Override... |
88 |
|
public void write(XMLStreamWriter writer, Object value) throws XMLStreamException |
89 |
|
{ |
90 |
|
|
91 |
|
|
92 |
52 |
writer.writeCharacters(value == null ? "" : DEFAULT_FORMAT.format(value)); |
93 |
|
} |
94 |
|
} |