All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.el.spel.SpelTemplateEngine Maven / Gradle / Ivy

/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.el.spel;

import io.gravitee.el.TemplateContext;
import io.gravitee.el.TemplateEngine;
import io.gravitee.el.exceptions.ExpressionEvaluationException;
import io.gravitee.el.spel.context.SpelTemplateContext;
import java.util.regex.Pattern;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
public class SpelTemplateEngine implements TemplateEngine {

    private static final String EXPRESSION_REGEX = "\\{( *[^#T\\( ])";
    private static final Pattern EXPRESSION_REGEX_PATTERN = Pattern.compile(EXPRESSION_REGEX);
    private static final String EXPRESSION_REGEX_SUBSTITUTE = "{'{'}$1";
    private static final ParserContext PARSER_CONTEXT = new TemplateParserContext();
    private static final SpelExpressionParser EXPRESSION_PARSER = new SpelExpressionParser(
        new SpelParserConfiguration(SpelCompilerMode.MIXED, null)
    );

    private final SpelTemplateContext templateContext = new SpelTemplateContext();

    @Override
    public String convert(String expression) {
        return getValue(expression, String.class);
    }

    private Expression parseExpression(String expression) {
        return EXPRESSION_PARSER.parseExpression(
            EXPRESSION_REGEX_PATTERN.matcher(expression).replaceAll(EXPRESSION_REGEX_SUBSTITUTE),
            PARSER_CONTEXT
        );
    }

    private  T getValue(Expression expression, Class clazz) {
        try {
            return expression.getValue(templateContext.getContext(), clazz);
        } catch (SpelEvaluationException spelEvaluationException) {
            throw new ExpressionEvaluationException(expression.getExpressionString(), spelEvaluationException);
        }
    }

    @Override
    public  T getValue(String expression, Class clazz) {
        return getValue(parseExpression(expression), clazz);
    }

    @Override
    public TemplateContext getTemplateContext() {
        return templateContext;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy