final StringBuilder regExp = new StringBuilder();
int state = NAME_READ_START;
for (int i = braceIndex + 1; i < l; i++) {
final char c = pathTemplate.charAt(i);
if (c == '{') {
throw new IllegalPathException(pathForExcMess,
"A variable must not " + "contain an extra '{' in \""
+ pathTemplate + "\"");
} else if (c == ' ' || c == '\t') {
if (state == NAME_READ)
state = NAME_READ_READY;
else if (state == REGEXP_READ)
state = REGEXP_READ_READY;
continue;
} else if (c == ':') {
if (state == NAME_READ_START) {
throw new IllegalPathException(pathForExcMess,
"The variable name at position must not be null at "
+ braceIndex + " of \"" + pathTemplate
+ "\"");
}
if (state == NAME_READ || state == NAME_READ_READY) {
state = REGEXP_READ_START;
}
continue;
} else if (c == '}') {
if (state == NAME_READ_START) {
throw new IllegalPathException(pathForExcMess,
"The template variable name '{}' is not allowed in "
+ "\"" + pathTemplate + "\"");
} else if ((state == REGEXP_READ)
|| (state == REGEXP_READ_READY)) {
pathPattern.append(regExp);
if (!regExp.equals(DEFAULT_REG_EXP)) {
this.noNonDefaultRegExp++;
}
} else {
pathPattern.append(DEFAULT_REG_EXP);
}
pathPattern.append(')');
this.varNames.add(varName.toString());
return i;
}
if (state == NAME_READ_START) {
state = NAME_READ;
varName.append(c);
} else if (state == NAME_READ) {
varName.append(c);
} else if (state == REGEXP_READ_START) {
state = REGEXP_READ;
regExp.append(c);
} else if (state == REGEXP_READ) {
regExp.append(c);
} else {
throw new IllegalPathException(pathForExcMess,
"Invalid character found at position " + i + " of \""
+ pathTemplate + "\"");
}
}
throw new IllegalPathException(pathForExcMess,
"No '}' found after '{' " + "at position " + braceIndex
+ " of \"" + pathTemplate + "\"");
}