Package ch.pollet.jzic.parser

Source Code of ch.pollet.jzic.parser.RuleParser

package ch.pollet.jzic.parser;

import ch.pollet.jzic.timezone.Rule;
import ch.pollet.jzic.timezone.Database;
import ch.pollet.jzic.tokenizer.Tokenizer;

/**
*
* @author Christophe Pollet
*/
class RuleParser {
    private Database db;
   
    public RuleParser(Database db) {
        this.db = db;
    }

    public void parse(Tokenizer st) throws ParserException {
        Rule r = new Rule();
        r.setName(st.nextToken());
        r.setFromYear(parseFromYear(st));
        r.setToYear(parseToYear(st, r.getFromYear()));
        r.setType(parseType(st));
        r.setAt(Parser.parseDateTime(st));
        r.setOffset(Parser.parseOffset(st.nextToken()));
        r.setFormat(parseFormat(st));

        db.addRule(r);
    }

    private int parseFromYear(Tokenizer st) {
        String str = st.nextToken();
        if (str.equals("min") || str.equals("minimum")) {
            return Integer.MIN_VALUE;
        } else if (str.equals("max") || str.equals("maximum")) {
            return Integer.MAX_VALUE;
        } else {
            return Integer.parseInt(str);
        }
    }

    private int parseToYear(Tokenizer st, int fromYear) {
        String str = st.nextToken();
        if (str.equals("min") || str.equals("minimum")) {
            return Integer.MIN_VALUE;
        } else if (str.equals("max") || str.equals("maximum")) {
            return Integer.MAX_VALUE;
        } else if (str.equals("only")) {
            return fromYear;
        } else {
            return Integer.parseInt(str);
        }
    }

    private String parseType(Tokenizer st) {
        String str = st.nextToken();
        return str.equals("-") ? null : str;
    }

    private String parseFormat(Tokenizer st) {
        String str = st.nextToken();

        return str.equals("-") ? "" : str;
    }
}
TOP

Related Classes of ch.pollet.jzic.parser.RuleParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.