use of com.eden.orchid.api.options.OptionsExtractor in project Orchid by JavaEden.
the class BaseTagParser method parseParams.
protected Map<String, Expression<?>> parseParams(String[] parameters, boolean enforceParamMap, Class<? extends OptionsHolder> paramsClass, TokenStream stream, Parser parser) throws ParserException {
// Get list of available parameter names
OptionsExtractor extractor = contextProvider.get().resolve(OptionsExtractor.class);
List<String> remainingParameters = new ArrayList<>(extractor.getOptionNames(paramsClass));
// parameter expressions will be added here
Map<String, Expression<?>> paramExpressionMap = new HashMap<>();
if (!isParameterEnd(stream)) {
if (!EdenUtils.isEmpty(parameters) && !stream.current().test(Token.Type.NAME)) {
// parse the parameters sequentially
int i = 0;
while (i < parameters.length && !isParameterEnd(stream) && !stream.current().test(Token.Type.NAME)) {
Expression<?> parsedExpression = parser.getExpressionParser().parseExpression();
paramExpressionMap.put(parameters[i], parsedExpression);
i++;
}
}
if (stream.current().test(Token.Type.NAME)) {
// parse parameters as map of key=value pairs
while (!isParameterEnd(stream)) {
Token paramNameToken = stream.expect(Token.Type.NAME);
final Optional<String> foundKey;
if (enforceParamMap) {
foundKey = remainingParameters.stream().filter(key -> key.equalsIgnoreCase(paramNameToken.getValue())).findAny();
} else {
foundKey = Optional.of(paramNameToken.getValue());
}
if (foundKey.isPresent()) {
String paramKey = foundKey.get();
if (enforceParamMap) {
remainingParameters.remove(paramKey);
}
stream.expect(Token.Type.PUNCTUATION, "=");
Expression<?> parsedExpression = parser.getExpressionParser().parseExpression();
paramExpressionMap.put(paramKey, parsedExpression);
} else {
throw new ParserException(null, Clog.format("Could not parse parameter {}.", paramNameToken.getValue()), stream.current().getLineNumber(), "");
}
}
}
}
return paramExpressionMap;
}
Aggregations