All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.relogiclabs.json.schema.internal.time.DateTimeParser Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.12.1
Show newest version
package com.relogiclabs.json.schema.internal.time;

import com.relogiclabs.json.schema.exception.InvalidDateTimeException;
import com.relogiclabs.json.schema.internal.antlr.DateTimeLexer;
import com.relogiclabs.json.schema.internal.util.DebugUtilities;
import com.relogiclabs.json.schema.internal.util.LexerErrorListener;
import com.relogiclabs.json.schema.time.DateTimeType;
import com.relogiclabs.json.schema.time.JsonDateTime;
import com.relogiclabs.json.schema.util.Reference;
import lombok.Getter;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Token;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.CLOCK_AM_PM;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.DAY_NUMBER;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.DAY_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.ERA;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER1;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER3;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER4;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER5;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.FRACTION_NUMBER6;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.HOUR_NUMBER;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.HOUR_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.MINUTE_NUMBER;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.MINUTE_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.MONTH_NAME;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.MONTH_NUMBER;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.MONTH_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.MONTH_SHORT_NAME;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.SECOND_NUMBER;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.SECOND_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.SYMBOL;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.TEXT;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.UTC_OFFSET_HOUR;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.UTC_OFFSET_TIME1;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.UTC_OFFSET_TIME2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.WEEKDAY_NAME;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.WEEKDAY_SHORT_NAME;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.WHITESPACE;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.YEAR_NUMBER2;
import static com.relogiclabs.json.schema.internal.antlr.DateTimeLexer.YEAR_NUMBER4;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.ClockAmPm;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.DayNumber;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.DayNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.Era;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber1;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber3;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber4;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber5;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.FractionNumber6;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.HourNumber;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.HourNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.MinuteNumber;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.MinuteNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.MonthName;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.MonthNumber;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.MonthNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.MonthShortName;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.SecondNumber;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.SecondNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.Symbol;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.Text;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.UtcOffsetHour;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.UtcOffsetTime1;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.UtcOffsetTime2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.WeekdayName;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.WeekdayShortName;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.Whitespace;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.YearNumber2;
import static com.relogiclabs.json.schema.internal.time.SegmentProcessor.YearNumber4;
import static com.relogiclabs.json.schema.internal.util.StringHelper.concat;
import static com.relogiclabs.json.schema.message.ErrorCode.DINV02;

public final class DateTimeParser {
    private static final Map PROCESSORS;
    private final DateTimeLexer dateTimeLexer;
    private final List lexerTokens;

    @Getter private final String pattern;
    @Getter private final DateTimeType type;

    static {
        PROCESSORS = new HashMap<>(50);
        addProcessor(TEXT, Text);
        addProcessor(SYMBOL, Symbol);
        addProcessor(WHITESPACE, Whitespace);
        addProcessor(ERA, Era);
        addProcessor(YEAR_NUMBER4, YearNumber4);
        addProcessor(YEAR_NUMBER2, YearNumber2);
        addProcessor(MONTH_NAME, MonthName);
        addProcessor(MONTH_SHORT_NAME, MonthShortName);
        addProcessor(MONTH_NUMBER2, MonthNumber2);
        addProcessor(MONTH_NUMBER, MonthNumber);
        addProcessor(WEEKDAY_NAME, WeekdayName);
        addProcessor(WEEKDAY_SHORT_NAME, WeekdayShortName);
        addProcessor(DAY_NUMBER2, DayNumber2);
        addProcessor(DAY_NUMBER, DayNumber);
        addProcessor(CLOCK_AM_PM, ClockAmPm);
        addProcessor(HOUR_NUMBER2, HourNumber2);
        addProcessor(HOUR_NUMBER, HourNumber);
        addProcessor(MINUTE_NUMBER2, MinuteNumber2);
        addProcessor(MINUTE_NUMBER, MinuteNumber);
        addProcessor(SECOND_NUMBER2, SecondNumber2);
        addProcessor(SECOND_NUMBER, SecondNumber);
        addProcessor(FRACTION_NUMBER, FractionNumber);
        addProcessor(FRACTION_NUMBER1, FractionNumber1);
        addProcessor(FRACTION_NUMBER2, FractionNumber2);
        addProcessor(FRACTION_NUMBER3, FractionNumber3);
        addProcessor(FRACTION_NUMBER4, FractionNumber4);
        addProcessor(FRACTION_NUMBER5, FractionNumber5);
        addProcessor(FRACTION_NUMBER6, FractionNumber6);
        addProcessor(UTC_OFFSET_HOUR, UtcOffsetHour);
        addProcessor(UTC_OFFSET_TIME1, UtcOffsetTime1);
        addProcessor(UTC_OFFSET_TIME2, UtcOffsetTime2);
    }

    private static void addProcessor(int index, SegmentProcessor processor) {
        PROCESSORS.put(DateTimeLexer.ruleNames[index - 1], processor);
    }

    @SuppressWarnings("unchecked")
    public DateTimeParser(String pattern, DateTimeType type) {
        this.pattern = pattern;
        this.type = type;
        this.dateTimeLexer = new DateTimeLexer(CharStreams.fromString(pattern));
        this.dateTimeLexer.removeErrorListeners();
        this.dateTimeLexer.addErrorListener(LexerErrorListener.DATE_TIME);
        this.lexerTokens = (List) dateTimeLexer.getAllTokens();
    }

    private JsonDateTime parse(String input, DateTimeContext context) {
        for(var token : lexerTokens) {
            var processor = PROCESSORS.get(dateTimeLexer.getVocabulary().getSymbolicName(token.getType()));
            input = processor.process(input, token, context);
        }
        if(input.length() != 0) throw new InvalidDateTimeException(DINV02,
                concat("Invalid ", context.getType(), " input format"));

        var dateTime = context.validate();
        DebugUtilities.print(context);
        return dateTime;
    }

    public JsonDateTime parse(String input) {
        return parse(input, new DateTimeContext(type));
    }

    public JsonDateTime tryParse(String input, Reference error) {
        try {
            return parse(input);
        } catch(InvalidDateTimeException e) {
            DebugUtilities.print(e);
            error.setValue(e.getMessage());
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy