Search in sources :

Example 1 with OptionsExtractor

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;
}
Also used : ParserException(com.mitchellbosecke.pebble.error.ParserException) RenderableNodeExpression(com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression) Expression(com.mitchellbosecke.pebble.node.expression.Expression) FilterExpression(com.mitchellbosecke.pebble.node.expression.FilterExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Token(com.mitchellbosecke.pebble.lexer.Token) OptionsExtractor(com.eden.orchid.api.options.OptionsExtractor)

Aggregations

OptionsExtractor (com.eden.orchid.api.options.OptionsExtractor)1 ParserException (com.mitchellbosecke.pebble.error.ParserException)1 Token (com.mitchellbosecke.pebble.lexer.Token)1 Expression (com.mitchellbosecke.pebble.node.expression.Expression)1 FilterExpression (com.mitchellbosecke.pebble.node.expression.FilterExpression)1 RenderableNodeExpression (com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1