Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove joda-time library and use Java 8 time instead #355

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions project.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*/
group = "com.github.java-json-tools";
version = "2.2.14";
sourceCompatibility = JavaVersion.VERSION_1_7;
targetCompatibility = JavaVersion.VERSION_1_7; // defaults to sourceCompatibility
sourceCompatibility = JavaVersion.VERSION_1_8;
targetCompatibility = JavaVersion.VERSION_1_8; // defaults to sourceCompatibility

project.ext {
description = "A Java implementation of the JSON Schema specification";
Expand All @@ -38,7 +38,6 @@ dependencies {
compile(group: "com.github.java-json-tools", name: "json-schema-core", version: "1.2.14");
// FIXME: 1.6.4 exists, but has different license (EDL 1.0, EPL 2.0). Can update?
compile(group: "com.sun.mail", name: "mailapi", version: "1.6.2");
compile(group: "joda-time", name: "joda-time", version: "2.10.5");
compile(group: "com.googlecode.libphonenumber", name: "libphonenumber", version: "8.11.1");
compile(group: "com.google.code.findbugs", name: "jsr305", version: "3.0.2");
compile(group: "net.sf.jopt-simple", name: "jopt-simple", version: "5.0.4");
Expand All @@ -57,9 +56,9 @@ javadoc {
def currentJavaVersion = org.gradle.api.JavaVersion.current()
// FIXME: https://github.com/gradle/gradle/issues/11182
if (currentJavaVersion.compareTo(org.gradle.api.JavaVersion.VERSION_1_9) >= 0) {
addStringOption("-release", "7");
addStringOption("-release", "8");
}
links("https://docs.oracle.com/javase/7/docs/api/");
links("https://docs.oracle.com/javase/8/docs/api/");
links("https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/3.0.2/");
links("https://fasterxml.github.io/jackson-databind/javadoc/2.11/");
links("https://fasterxml.github.io/jackson-core/javadoc/2.11/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.google.common.collect.ImmutableList;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;

import static org.joda.time.DateTimeFieldType.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;

/**
* Validator for the {@code date-time} format attribute
Expand All @@ -40,30 +40,29 @@ public final class DateTimeAttribute
extends AbstractFormatAttribute
{
private static final ImmutableList<String> FORMATS = ImmutableList.of(
"yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z"
"yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,9}Z"
);
private static final DateTimeFormatter FORMATTER;

static {
final DateTimeParser secFracsParser = new DateTimeFormatterBuilder()
.appendLiteral('.').appendFractionOfSecond(1,12)
.toParser();
final DateTimeFormatter secFracsParser = new DateTimeFormatterBuilder()
.appendFraction(ChronoField.OFFSET_SECONDS, 1, 9, true)
.toFormatter();

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

builder = builder.appendFixedDecimal(year(), 4)
.appendLiteral('-')
.appendFixedDecimal(monthOfYear(), 2)
.appendLiteral('-')
.appendFixedDecimal(dayOfMonth(), 2)
.appendLiteral('T')
.appendFixedDecimal(hourOfDay(), 2)
.appendLiteral(':')
.appendFixedDecimal(minuteOfHour(), 2)
.appendLiteral(':')
.appendFixedDecimal(secondOfMinute(), 2)
.appendOptional(secFracsParser)
.appendTimeZoneOffset("Z", false, 2, 2);
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('T')
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.appendOptional(secFracsParser)
.appendZoneOrOffsetId();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be the cause of (overly?) strict offset validation, causing +0100 to be rejected.


FORMATTER = builder.toFormatter();
}
Expand All @@ -88,8 +87,8 @@ public void validate(final ProcessingReport report,
final String value = data.getInstance().getNode().textValue();

try {
FORMATTER.parseDateTime(value);
} catch (IllegalArgumentException ignored) {
FORMATTER.parse(value);
} catch (DateTimeParseException ignored) {
report.error(newMsg(data, bundle, "err.format.invalidDate")
.putArgument("value", value).putArgument("expected", FORMATS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.library.DraftV4Library;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;

import com.github.fge.jackson.NodeType;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
Expand All @@ -15,6 +12,11 @@
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.google.common.collect.ImmutableList;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;

/**
* A {@link DateTimeFormatter} for date and time format defined in RFC3339.
*
Expand All @@ -30,20 +32,20 @@
public class RFC3339DateTimeAttribute extends AbstractFormatAttribute {

private static final ImmutableList<String> RFC3339_FORMATS = ImmutableList.of(
"yyyy-MM-dd'T'HH:mm:ss((+|-)HH:mm|Z)", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}((+|-)HH:mm|Z)"
"yyyy-MM-dd'T'HH:mm:ss((+|-)HH:mm|Z)", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,9}((+|-)HH:mm|Z)"
);

private static final DateTimeFormatter FORMATTER;

static {
final DateTimeParser secFracsParser = new DateTimeFormatterBuilder()
.appendLiteral('.').appendFractionOfSecond(1,12)
.toParser();
final DateTimeFormatter secFracsParser = new DateTimeFormatterBuilder()
.appendFraction(ChronoField.OFFSET_SECONDS, 1, 9, true)
.toFormatter();

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:mm:ss")
.appendOptional(secFracsParser)
.appendTimeZoneOffset("Z", true, 2, 2);
.appendZoneOrOffsetId();

FORMATTER = builder.toFormatter();
}
Expand All @@ -69,7 +71,7 @@ public void validate(final ProcessingReport report,

try
{
FORMATTER.parseDateTime(value);
FORMATTER.parse(value);

final String secFracsAndOffset = value.substring("yyyy-MM-ddTHH:mm:ss".length());
final String offset;
Expand All @@ -88,7 +90,7 @@ public void validate(final ProcessingReport report,
throw new IllegalArgumentException();
}

} catch (IllegalArgumentException ignored) {
} catch (DateTimeParseException | IllegalArgumentException ignored) {
report.error(newMsg(data, bundle, "err.format.invalidDate")
.putArgument("value", value).putArgument("expected", RFC3339_FORMATS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.format.helpers.AbstractDateFormatAttribute;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import static org.joda.time.DateTimeFieldType.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public final class DateAttribute
extends AbstractDateFormatAttribute
Expand All @@ -44,13 +44,12 @@ public static FormatAttribute getInstance()
@Override
protected DateTimeFormatter getFormatter()
{
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

builder = builder.appendFixedDecimal(year(), 4)
.appendLiteral('-')
.appendFixedDecimal(monthOfYear(), 2)
.appendLiteral('-')
.appendFixedDecimal(dayOfMonth(), 2);
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2);

return builder.toFormatter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.format.helpers.AbstractDateFormatAttribute;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import static org.joda.time.DateTimeFieldType.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public final class TimeAttribute
extends AbstractDateFormatAttribute
Expand All @@ -44,13 +44,12 @@ public static FormatAttribute getInstance()
@Override
protected DateTimeFormatter getFormatter()
{
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

builder = builder.appendFixedDecimal(hourOfDay(), 2)
.appendLiteral(':')
.appendFixedDecimal(minuteOfHour(), 2)
.appendLiteral(':')
.appendFixedDecimal(secondOfMinute(), 2);
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2);

return builder.toFormatter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@
import com.github.fge.jsonschema.format.AbstractFormatAttribute;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Abstract class for date/time related format attributes
*
* <p><a href="http://joda-time.sourceforge.net/">Joda Time</a> is used for
* date and time parsing: it can handle all defined formats, and catches more
* errors than the standard JDK's {@link SimpleDateFormat} does.</p>
*
* <p>Furthermore (and more importantly), unlike {@link SimpleDateFormat}, Joda
* Time's {@link DateTimeFormatter} is thread-safe!</p>
*/
public abstract class AbstractDateFormatAttribute
extends AbstractFormatAttribute
Expand All @@ -61,8 +54,8 @@ public final void validate(final ProcessingReport report,
final String value = data.getInstance().getNode().textValue();

try {
formatter.parseLocalDate(value);
} catch (IllegalArgumentException ignored) {
formatter.parse(value);
} catch (DateTimeParseException ignored) {
report.error(newMsg(data, bundle, "err.format.invalidDate")
.putArgument("value", value).putArgument("expected", format));
}
Expand Down
16 changes: 2 additions & 14 deletions src/test/resources/format/common/date-time.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"message": "err.format.invalidDate",
"msgData": {
"value": "2012-02-30T00:00:00+0000",
"expected": [ "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z" ]
"expected": [ "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,9}Z" ]
},
"msgParams": [ "value", "expected" ]
},
Expand All @@ -35,7 +35,7 @@
"message": "err.format.invalidDate",
"msgData": {
"value": "201202030",
"expected": [ "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z" ]
"expected": [ "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,9}Z" ]
},
"msgParams": [ "value", "expected" ]
},
Expand Down Expand Up @@ -66,17 +66,5 @@
{
"data": "2012-08-07T20:42:32.123456789Z",
"valid": true
},
{
"data": "2012-08-07T20:42:32.1234567890Z",
"valid": true
},
{
"data": "2012-08-07T20:42:32.12345678901Z",
"valid": true
},
{
"data": "2012-08-07T20:42:32.123456789012Z",
"valid": true
}
]
Loading