com.relogiclabs.json.schema.internal.function.DateTimeAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of relogiclabs-json-schema Show documentation
Show all versions of relogiclabs-json-schema Show documentation
The New JSON Schema prioritizes simplicity, conciseness, and readability, making
it user-friendly and accessible without the need for extensive prior knowledge.
It offers efficient read-write facilities, precise JSON document definition
through various data types and functions, and extensibility to meet modern web
service diverse requirements.
package com.relogiclabs.json.schema.internal.function;
import com.relogiclabs.json.schema.exception.DateTimeLexerException;
import com.relogiclabs.json.schema.exception.InvalidDateTimeException;
import com.relogiclabs.json.schema.exception.JsonSchemaException;
import com.relogiclabs.json.schema.internal.time.DateTimeParser;
import com.relogiclabs.json.schema.message.ActualDetail;
import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.json.schema.message.ExpectedDetail;
import com.relogiclabs.json.schema.time.DateTimeType;
import com.relogiclabs.json.schema.time.JsonDateTime;
import com.relogiclabs.json.schema.type.JFunction;
import com.relogiclabs.json.schema.type.JString;
public class DateTimeAgent {
private final String pattern;
private final DateTimeType type;
private DateTimeParser parser;
public DateTimeAgent(String pattern, DateTimeType type) {
this.pattern = pattern;
this.type = type;
}
public DateTimeAgent(DateTimeParser parser) {
this.pattern = parser.getPattern();
this.type = parser.getType();
this.parser = parser;
}
public JsonDateTime parse(JFunction function, JString dateTime) {
var exceptions = function.getRuntime().getExceptions();
try {
if(parser == null) parser = new DateTimeParser(pattern, type);
return parser.parse(dateTime.getValue());
} catch(DateTimeLexerException ex) {
exceptions.failWith(new JsonSchemaException(
new ErrorDetail(ex.getCode(), ex.getMessage()),
new ExpectedDetail(function, "a valid ", type, " pattern"),
new ActualDetail(dateTime, "found ", pattern, " that is invalid"),
ex));
} catch(InvalidDateTimeException ex) {
exceptions.failWith(new JsonSchemaException(
new ErrorDetail(ex.getCode(), ex.getMessage()),
new ExpectedDetail(function, "a valid ", type, " formatted as ", pattern),
new ActualDetail(dateTime, "found ", dateTime, " that is invalid or malformatted"),
ex));
}
return null;
}
}