View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL 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
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.event.xml;
19  
20  import java.text.DateFormat;
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.TimeZone;
25  import org.w3c.dom.Element;
26  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEvent;
27  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
28  import at.ac.tuwien.infosys.sm4all.copal.api.util.InvalidFieldValueException;
29  
30  /**
31   * The COPAL element for the event's time-stamp,
32   * 
33   * @author sanjin
34   */
35  public class TimeStamp extends ConcreteElement<Date> {
36  
37      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
38  
39      @Override
40      protected Date retrieve(final Element element) throws FailedUnmarshallingException {
41          if (element == null)
42              throw new NullPointerException("XML DOM element cannot be null.");
43  
44          final DateFormat xmlDateTimeFormat = createXMLDateTimeFormat();
45          final Date result;
46  
47          try {
48              final String textContent = element.getTextContent().trim();
49              if (textContent.isEmpty())
50                  throw new InvalidFieldValueException(
51                          "Time stamp is empty or blank.");
52              result = xmlDateTimeFormat.parse(textContent);
53          } catch (final ParseException ex) {
54              throw new InvalidFieldValueException(
55                      "Time stamp is not a valid date.");
56          }
57  
58          return result;
59      }
60  
61      @Override
62      public void update(final Element element, final ContextEvent event) {
63          if (element == null)
64              throw new NullPointerException("XML DOM element cannot be null.");
65          if (event == null)
66              throw new NullPointerException("Context event cannot be null.");
67  
68          final DateFormat xmlDateTimeFormat = createXMLDateTimeFormat();
69  
70          element.setTextContent(xmlDateTimeFormat.format(event.getTimeStamp()));
71      }
72  
73      private DateFormat createXMLDateTimeFormat() {
74          final DateFormat result = new SimpleDateFormat(
75                  "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
76  
77          // when formatting dates convert them from/to UTC time zone
78          result.setTimeZone(UTC_TIME_ZONE);
79          result.setLenient(false);
80  
81          return result;
82      }
83  
84      /**
85       * Singleton instance.
86       */
87      public static final TimeStamp INSTANCE = new TimeStamp();
88  
89      private TimeStamp() {
90          super(String.class);
91      }
92  }