001/* 002 * Portions of this software was developed by employees of the National Institute 003 * of Standards and Technology (NIST), an agency of the Federal Government and is 004 * being made available as a public service. Pursuant to title 17 United States 005 * Code Section 105, works of NIST employees are not subject to copyright 006 * protection in the United States. This software may be subject to foreign 007 * copyright. Permission in the United States and in foreign countries, to the 008 * extent that NIST may hold copyright, to use, copy, modify, create derivative 009 * works, and distribute this software and its documentation without fee is hereby 010 * granted on a non-exclusive basis, provided that this notice and disclaimer 011 * of warranty appears in all copies. 012 * 013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER 014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY 015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF 016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM 017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE 018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT 019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, 020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, 021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, 022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR 023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT 024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. 025 */ 026 027package gov.nist.secauto.metaschema.core.datatype.adapter; 028 029import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; 030 031import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter; 032import gov.nist.secauto.metaschema.core.datatype.object.DateTime; 033import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; 034import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; 035import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; 036import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; 037import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; 038import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem; 039import gov.nist.secauto.metaschema.core.util.ObjectUtils; 040 041import java.time.LocalDateTime; 042import java.time.ZoneOffset; 043import java.time.ZonedDateTime; 044import java.time.format.DateTimeParseException; 045import java.util.List; 046 047import edu.umd.cs.findbugs.annotations.NonNull; 048 049public class DateTimeAdapter 050 extends AbstractCustomJavaDataTypeAdapter<DateTime, IDateTimeItem> { 051 @NonNull 052 private static final List<String> NAMES = ObjectUtils.notNull( 053 List.of( 054 "date-time", 055 // for backwards compatibility with original type name 056 "dateTime")); 057 058 DateTimeAdapter() { 059 super(DateTime.class); 060 } 061 062 @Override 063 public List<String> getNames() { 064 return NAMES; 065 } 066 067 @Override 068 public JsonFormatTypes getJsonRawType() { 069 return JsonFormatTypes.STRING; 070 } 071 072 @SuppressWarnings("null") 073 @Override 074 public DateTime parse(String value) { 075 try { 076 return new DateTime(ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value)), true); // NOPMD - readability 077 } catch (DateTimeParseException ex) { 078 try { 079 LocalDateTime dateTime = LocalDateTime.from(DateFormats.DATE_TIME_WITHOUT_TZ.parse(value)); 080 return new DateTime(ZonedDateTime.of(dateTime, ZoneOffset.UTC), false); 081 } catch (DateTimeParseException ex2) { 082 IllegalArgumentException newEx = new IllegalArgumentException(ex2.getLocalizedMessage(), ex2); 083 newEx.addSuppressed(ex); 084 throw newEx; // NOPMD - it's ok 085 } 086 } 087 } 088 089 @Override 090 public String asString(Object obj) { 091 DateTime value = (DateTime) obj; 092 String retval; 093 if (value.hasTimeZone()) { 094 @SuppressWarnings("null") 095 @NonNull String formatted = DateFormats.DATE_TIME_WITH_TZ.format(value.getValue()); 096 retval = formatted; 097 } else { 098 @SuppressWarnings("null") 099 @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}