View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.datatype.adapter;
28  
29  import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
30  
31  import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter;
32  import gov.nist.secauto.metaschema.core.datatype.object.DateTime;
33  import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException;
34  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
35  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem;
36  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem;
37  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
38  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem;
39  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
40  
41  import java.time.LocalDateTime;
42  import java.time.ZoneOffset;
43  import java.time.ZonedDateTime;
44  import java.time.format.DateTimeParseException;
45  import java.util.List;
46  
47  import edu.umd.cs.findbugs.annotations.NonNull;
48  
49  public class DateTimeAdapter
50      extends AbstractCustomJavaDataTypeAdapter<DateTime, IDateTimeItem> {
51    @NonNull
52    private static final List<String> NAMES = ObjectUtils.notNull(
53        List.of(
54            "date-time",
55            // for backwards compatibility with original type name
56            "dateTime"));
57  
58    DateTimeAdapter() {
59      super(DateTime.class);
60    }
61  
62    @Override
63    public List<String> getNames() {
64      return NAMES;
65    }
66  
67    @Override
68    public JsonFormatTypes getJsonRawType() {
69      return JsonFormatTypes.STRING;
70    }
71  
72    @SuppressWarnings("null")
73    @Override
74    public DateTime parse(String value) {
75      try {
76        return new DateTime(ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value)), true); // NOPMD - readability
77      } catch (DateTimeParseException ex) {
78        try {
79          LocalDateTime dateTime = LocalDateTime.from(DateFormats.DATE_TIME_WITHOUT_TZ.parse(value));
80          return new DateTime(ZonedDateTime.of(dateTime, ZoneOffset.UTC), false);
81        } catch (DateTimeParseException ex2) {
82          IllegalArgumentException newEx = new IllegalArgumentException(ex2.getLocalizedMessage(), ex2);
83          newEx.addSuppressed(ex);
84          throw newEx; // NOPMD - it's ok
85        }
86      }
87    }
88  
89    @Override
90    public String asString(Object obj) {
91      DateTime value = (DateTime) obj;
92      String retval;
93      if (value.hasTimeZone()) {
94        @SuppressWarnings("null")
95        @NonNull String formatted = DateFormats.DATE_TIME_WITH_TZ.format(value.getValue());
96        retval = formatted;
97      } else {
98        @SuppressWarnings("null")
99        @NonNull String formatted = DateFormats.DATE_TIME_WITHOUT_TZ.format(value.getValue());
100       retval = formatted;
101     }
102     return retval;
103   }
104 
105   @Override
106   public Class<IDateTimeItem> getItemClass() {
107     return IDateTimeItem.class;
108   }
109 
110   @Override
111   public IDateTimeItem newItem(Object value) {
112     DateTime item = toValue(value);
113     return IDateTimeItem.valueOf(item);
114   }
115 
116   @Override
117   protected IDateTimeItem castInternal(@NonNull IAnyAtomicItem item) {
118     // TODO: bring up to spec
119     IDateTimeItem retval;
120     if (item instanceof IDateItem) {
121       retval = IDateTimeItem.valueOf(((IDateItem) item).asZonedDateTime());
122     } else if (item instanceof IStringItem || item instanceof IUntypedAtomicItem) {
123       retval = super.castInternal(item);
124     } else {
125       throw new InvalidValueForCastFunctionException(
126           String.format("unsupported item type '%s'", item.getClass().getName()));
127     }
128     return retval;
129   }
130 
131 }