1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.xar.internal.property

File DateXarObjectPropertySerializer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

4
13
2
1
94
52
6
0.46
6.5
2
3

Classes

Class Line # Actions
DateXarObjectPropertySerializer 48 13 0% 6 6
0.6842105468.4%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 Date} based implementation of {@link XarObjectPropertySerializer}.
41    *
42    * @version $Id: 5e64680b58fa824a326de9b1a775807055afd7f1 $
43    * @since 5.4M1
44    */
45    @Component
46    @Named("Date")
47    @Singleton
 
48    public class DateXarObjectPropertySerializer implements XarObjectPropertySerializer
49    {
50    /**
51    * The default {@link String} format used to serialize and parse the date.
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   
 
58  5 toggle @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    // FIXME: The value of a date property should be serialized using the date timestamp or the date format
68    // specified in the XClass the date property belongs to.
69  4 SimpleDateFormat sdf = DEFAULT_FORMAT;
70  4 try {
71  4 return sdf.parse(value);
72    } catch (ParseException e) {
73    // I suppose this is a date format used a long time ago. DateProperty is using the above date format now.
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   
 
87  52 toggle @Override
88    public void write(XMLStreamWriter writer, Object value) throws XMLStreamException
89    {
90    // FIXME: The value of a date property should be serialized using the date timestamp or the date format
91    // specified in the XClass the date property belongs to.
92  52 writer.writeCharacters(value == null ? "" : DEFAULT_FORMAT.format(value));
93    }
94    }