From 63135b555490255df5ffa89ae7297cfbf29ff32a Mon Sep 17 00:00:00 2001 From: "fabio.sinibaldi" Date: Fri, 29 Nov 2019 17:57:49 +0100 Subject: [PATCH] Date parsing with java.time --- .../data/publishing/ckan2zenodo/Fixer.java | 123 ++++++++++-------- .../java/org/gcube/tests/ParsingTests.java | 80 ++++++++++-- .../java/org/gcube/tests/ZenodoTests.java | 2 +- .../src/test/resources/logback.xml | 25 ++++ 4 files changed, 164 insertions(+), 66 deletions(-) create mode 100644 ckan2zenodo-library/src/test/resources/logback.xml diff --git a/ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/Fixer.java b/ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/Fixer.java index f753938..6015e7d 100644 --- a/ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/Fixer.java +++ b/ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/Fixer.java @@ -1,8 +1,13 @@ package org.gcube.data.publishing.ckan2zenodo; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; import java.util.List; import org.gcube.data.publishing.ckan2zenodo.model.zenodo.Commons; @@ -16,22 +21,30 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class Fixer { + - private static final ArrayList INCOMING_DATE_FORMATS=new ArrayList(); - private static final SimpleDateFormat INTERNAL_DATE_FORMAT=new SimpleDateFormat(Commons.ISO_DATE_PATTERN); + + private static DateTimeFormatter INCOMING_FORMATTER=null; + private static DateTimeFormatter INTERNAL_FORMATTER=null; + + + + private static Configuration PATH_CONFIGURATION=null; private static Configuration ALWAYS_LIST_CONFIG=null; static { - INCOMING_DATE_FORMATS.add(new SimpleDateFormat("YYYY-MM-DD")); - INCOMING_DATE_FORMATS.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX")); + INCOMING_FORMATTER=DateTimeFormatter.ofPattern("[yyyy-MM-dd['T'HH:mm:ss[.SSSSSS[z][XXX]]]]"); + INTERNAL_FORMATTER=DateTimeFormatter.ofPattern(Commons.ISO_DATE_PATTERN); - - PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST,Option.SUPPRESS_EXCEPTIONS).build(); - ALWAYS_LIST_CONFIG= Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS).build(); + + + + PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build(); + ALWAYS_LIST_CONFIG= Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build(); } - - - + + + public static final String fixIncoming(String toFix) { DocumentContext ctx=JsonPath.using(ALWAYS_LIST_CONFIG).parse(toFix); DocumentContext pathCtx=JsonPath.using(PATH_CONFIGURATION).parse(toFix); @@ -40,76 +53,78 @@ public class Fixer { ctx=fixIncomingDate(ctx,pathCtx, "$.modified"); ctx=fixIncomingDate(ctx,pathCtx, "$..publication_date"); ctx=fixIncomingDate(ctx,pathCtx, "$..embargo_date"); - + return ctx.jsonString(); } - - + + public static String fixSending(String toFix) { DocumentContext ctx=JsonPath.using(ALWAYS_LIST_CONFIG).parse(toFix); DocumentContext pathCtx=JsonPath.using(PATH_CONFIGURATION).parse(toFix); - + ctx=fixOutgoingDate(ctx,pathCtx, "$.created"); ctx=fixOutgoingDate(ctx,pathCtx, "$.modified"); ctx=fixOutgoingDate(ctx,pathCtx, "$..publication_date"); ctx=fixOutgoingDate(ctx,pathCtx, "$..embargo_date"); - - + + return ctx.jsonString(); } - + //*** INCOMINGs private static final DocumentContext fixIncomingDate(DocumentContext valuesCtx,DocumentContext pathCtx,String toFixPath) { try { List values=valuesCtx.read(toFixPath); List paths=pathCtx.read(toFixPath); - - for(int i=0;i values=valuesCtx.read(toFixPath); List paths=pathCtx.read(toFixPath); - - for(int i=0;i + + + + + %d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n + + + + + logFile.log + true + + %d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n + + + + + + + + + + + \ No newline at end of file