case 'E':
case '-':
case '+':
if (ctx.aScience > 0) {
if (ch != '+' && ch != '-') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
} else {
ctx.aScience = -1;
}
} else if (ctx.aScience < 0) {
if (ch == 'E' || ch == 'e') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
}
} else if (ch == 'E' || ch == 'e') {
ctx.aScience = 1;
ctx.aNumberDot = false;
} else if (ctx.aNumberDot) {
if (ch == '0' || ch == '#') {
++ctx.scale;
}
} else if (ch == '.' && ctx.aScience < 1) {
ctx.aNumberDot = true;
}
appendChar(ctx, ch);
break;
default:
if (ch == '?') {
if (ctx.aScience > 0) {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
}
if (ctx.aSlash == 0) {
ctx.aSlash = 1;
}
appendChar(ctx, ch);
} else {
addToken(FormatToken.NUMBER, ctx.token.toString(), -1, ctx);
ctx.aScience = 0;
ctx.aNumberSpace = false;
ctx.aNumberDot = false;
dispatchState(format, ch, j, ctx);
}
break;
}
break;
case STATE_DATE:
switch (ch) {
case 'm':
case 'd':
case 'y':
appendChar(ctx, ch);
break;
default:
if (ch == 's') {
final StringBuffer tmp = new StringBuffer(32);
final int index = ctx.token.length();
int k = index - 1;
for (; k >= 0; --k) {
final char prech = ctx.token.charAt(k);
if (prech != 'm') {
break;
}
tmp.append('m');
}
final int mlength = tmp.length();
if (mlength > 0 && mlength < 3) { // s after m, change to
// minute
if (mlength < index) { // partial replace
final FormatToken fmtToken = new FormatToken(
FormatToken.DATE, ctx.token.substring(0,
k + 1).toString());
ctx.fmtSection.addToken(fmtToken);
}
ctx.token = tmp;
ctx.aM = 0;
ctx.state = STATE_TIME;
appendChar(ctx, ch);
break;
}
}
addToken(FormatToken.DATE, ctx.token.toString(), -1, ctx);
ctx.aM = 0;
dispatchState(format, ch, j, ctx);
break;
}
break;
case STATE_TIME:
switch (ch) {
case 'h':
case 's':
case 'a':
case 'A':
case 'p':
case 'P':
case 'm':
case 'M':
case '/':
if (ctx.anAm == 4) {
if (ch != 'm' && ch != 'M') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
} else {
ch = 'M';
ctx.anAm = 0;
}
} else if (ctx.anAm == 3) {
if (ch != 'p' && ch != 'P') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
} else {
ch = 'P';
ctx.anAm = 4;
}
} else if (ctx.anAm == 2) {
if (ch != '/') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
} else {
ctx.anAm = 3;
}
} else if (ctx.anAm == 1) {
if (ch != 'm' && ch != 'M') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
} else {
ch = 'M';
ctx.anAm = 2;
}
} else {
if (ch == 'A' || ch == 'a') {
ch = 'A';
ctx.anAm = 1;
} else if (ch == 'm') {
ctx.aTime = false;
++ctx.aM;
if (ctx.aM >= 3) { // mmm, must be a month
ctx.state = STATE_DATE;
}
}
}
appendChar(ctx, ch);
break;
default:
addToken(FormatToken.TIME, ctx.token.toString(), -1, ctx);
dispatchState(format, ch, j, ctx);
break;
}
break;
case STATE_MILLISECOND:
if (ch == '0') {
++ctx.milliSecond;
} else {
addToken(FormatToken.TIME_MILLISECOND, "" + ctx.milliSecond,
-1, ctx);
dispatchState(format, ch, j, ctx);
}
break;
case STATE_MISC_DROP:
//until ch == ']'
if (ch == ']') {
ctx.state = STATE_UNKNOWN;
}
break;
case STATE_MISC:
//try to locate [$-Xxx] pattern
if (ctx.misc_count == 1) {
if (ch == '$') {
ctx.misc_count = 2;
break;
} else {
ctx.misc_count = 0;
}
} else if (ctx.misc_count == 2) {
if (ch == '-') {
ctx.state = STATE_MISC_DROP;
ctx.misc_count = 0;
break;
} else {
appendChar(ctx, '$');
ctx.misc_count = 0;
}
}
switch (ch) {
case '<':
case '>':
case '=':
ctx.state = STATE_MISC_CONDITION;
appendChar(ctx, ch);
break;
case 'b':
case 'B':
case 'c':
case 'C':
case 'g':
case 'G':
case 'M':
case 'r':
case 'R':
case 'w':
case 'W':
case 'y':
case 'Y':
ctx.state = STATE_MISC_COLOR;
appendChar(ctx, ch);
break;
case 'h':
case 's':
ctx.state = STATE_MISC_TIME;
appendChar(ctx, ch);
break;
case 'm':
if (!ctx.aMagenta) {
ctx.aMagenta = true;
} else {
ctx.aMagenta = false;
ctx.state = STATE_MISC_TIME;
}
appendChar(ctx, ch);
break;
default:
if (ctx.aMagenta) {
ctx.aMagenta = false;
if (ch == ']') {
ctx.state = STATE_MISC_TIME;
parseChar(format, ch, j, ctx); // recursive
} else if (ch == 'a' || ch == 'A') {
ctx.state = STATE_MISC_COLOR;
appendChar(ctx, ch);
}
break;
}
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
}
break;
case STATE_MISC_CONDITION:
if (ch == ']') {
if (ctx.token.length() < 2) {
throw new ModelException("Incorrect condition format at "
+ j + " in \"" + format + "\"");
}
final char ch1 = ctx.token.charAt(0);
final char ch2 = ctx.token.charAt(1);
if (ch1 == '<' && ch2 == '>') { // <>
addToken(FormatToken.CONDITION_NE, ctx.token.substring(2)
.trim(), STATE_UNKNOWN, ctx);
} else if (ch1 == '=') { // =
addToken(FormatToken.CONDITION_EQ, ctx.token.substring(1)
.trim(), STATE_UNKNOWN, ctx);
} else if (ch1 == '<') { // <= or <
if (ch2 == '=') {
addToken(FormatToken.CONDITION_LE, ctx.token.substring(
2).trim(), STATE_UNKNOWN, ctx);
} else {
addToken(FormatToken.CONDITION_LT, ctx.token.substring(
1).trim(), STATE_UNKNOWN, ctx);
}
} else if (ch1 == '>') { // >= or >
if (ch2 == '=') {
addToken(FormatToken.CONDITION_GE, ctx.token.substring(
2).trim(), STATE_UNKNOWN, ctx);
} else {
addToken(FormatToken.CONDITION_GT, ctx.token.substring(
1).trim(), STATE_UNKNOWN, ctx);
}
} else {
throw new ModelException("Incorrect condition format at "
+ j + " in \"" + format + "\"");
}
} else {
appendChar(ctx, ch);
}
break;
case STATE_MISC_COLOR:
if (ch == ']') {
String data = ctx.token.toString().trim();
final String datau = data.toUpperCase();
if (datau.startsWith("COLOR")) {
final int colorIndex = Integer.parseInt(data.substring(5)
.trim());
if (colorIndex <= 0 || colorIndex >= 56) {
throw new ModelException("Incorrect color index at "
+ j + ", index: " + colorIndex + " in \""
+ format + "\"");
} else {
data = PALETTE[colorIndex - 1];
}
} else {
if (!NAME_PALETTE.containsKey(data.toUpperCase())) {
throw new ModelException("Incorrect color format at "
+ j + ", color: " + data + " in \"" + format
+ "\"");
} else {
data = (String) NAME_PALETTE.get(datau);
}
}
addToken(FormatToken.COLOR, "#" + data, STATE_UNKNOWN, ctx);
} else {
appendChar(ctx, ch);
}
break;
case STATE_MISC_TIME:
if (ctx.aTimePassed) { // allow only one aTimePassed
throw new ModelException(
"Only one [h], [m], or [s] is allowed. Incorrect format at "
+ j + ", charcter " + ch + " in \"" + format
+ "\"");
} else {
ctx.aTimePassed = true;
}
if (ch == ']') {
int type = -1;
final String data = ctx.token.toString();
final char fch = data.charAt(0);
switch (fch) {
case 'h':
type = FormatToken.TIME_HOUR_PASSED;
break;
case 'm':
type = FormatToken.TIME_MINUTE_PASSED;
break;
case 's':
type = FormatToken.TIME_SECOND_PASSED;
break;
default:
throw new ModelException("Unknown TIME_PASSED formate: "
+ fch);
}
addToken(type, data, STATE_UNKNOWN, ctx);
} else {
if (ch != 'h' && ch != 'm' && ch != 's') {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format + "\"");
} else {
final int index = ctx.token.length() - 1;
if (index >= 0) {
final char prech = ctx.token.charAt(index);
if (prech != ch) {
throw new ModelException("Incorrect format at " + j
+ ", charcter " + ch + " in \"" + format
+ "\"");
}
}
appendChar(ctx, ch);
}
}
break;
default:
throw new ModelException("Unknown State: " + ctx.state);
}
}