in winegrower-core/src/main/java/org/apache/winegrower/deployer/BundleImpl.java [503:614]
private static List<HeaderClause> parse(final String header) {
final List<HeaderClause> clauses = new ArrayList<>();
if (header == null) {
return clauses;
}
HeaderClause clause = null;
String key = null;
Map targetMap = null;
int state = CLAUSE_START;
int currentPosition = 0;
int startPosition = 0;
int length = header.length();
boolean quoted = false;
boolean escaped = false;
char currentChar;
do {
currentChar = currentPosition >= length ? EOF : header.charAt(currentPosition);
switch (state) {
case CLAUSE_START:
clause = new HeaderClause(new ArrayList<>(), new HashMap<>(), new HashMap<>(), new HashMap<>());
clauses.add(clause);
// not needed to be called but "logically" needed: state = PARAMETER_START;
case PARAMETER_START:
startPosition = currentPosition;
state = KEY;
case KEY:
switch (currentChar) {
case ':':
case '=':
key = header.substring(startPosition, currentPosition).trim();
startPosition = currentPosition + 1;
targetMap = clause.attributes;
state = currentChar == ':' ? DIRECTIVE_OR_TYPEDATTRIBUTE : ARGUMENT;
break;
case EOF:
case ',':
case ';':
clause.paths.add(header.substring(startPosition, currentPosition).trim());
state = currentChar == ',' ? CLAUSE_START : PARAMETER_START;
break;
default:
break;
}
currentPosition++;
break;
case DIRECTIVE_OR_TYPEDATTRIBUTE:
if (currentChar == '=') {
if (startPosition != currentPosition) {
clause.types.put(key, header.substring(startPosition, currentPosition).trim());
} else {
targetMap = clause.directives;
}
state = ARGUMENT;
startPosition = currentPosition + 1;
}
currentPosition++;
break;
case ARGUMENT:
if (currentChar == '\"') {
quoted = true;
currentPosition++;
} else {
quoted = false;
}
if (!Character.isWhitespace(currentChar)) {
state = VALUE;
} else {
currentPosition++;
}
break;
case VALUE:
if (escaped) {
escaped = false;
} else {
if (currentChar == '\\') {
escaped = true;
} else if (quoted && currentChar == '\"') {
quoted = false;
} else if (!quoted) {
String value;
switch (currentChar) {
case EOF:
case ';':
case ',':
value = header.substring(startPosition, currentPosition).trim();
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
if (targetMap.put(key, value) != null) {
throw new IllegalArgumentException(
"Duplicate '" + key + "' in: " + header);
}
state = currentChar == ';' ? PARAMETER_START : CLAUSE_START;
break;
default:
break;
}
}
}
currentPosition++;
break;
default:
break;
}
} while (currentChar != EOF);
if (state > PARAMETER_START) {
throw new IllegalArgumentException("Unable to parse header: " + header);
}
return clauses;
}