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;
}
}