| 1 | package fr.sii.ogham.spring.template.thymeleaf; | |
| 2 | ||
| 3 | import java.util.HashMap; | |
| 4 | import java.util.Map; | |
| 5 | ||
| 6 | import org.springframework.expression.EvaluationContext; | |
| 7 | import org.thymeleaf.context.IContext; | |
| 8 | ||
| 9 | import fr.sii.ogham.core.exception.template.ContextException; | |
| 10 | import fr.sii.ogham.template.thymeleaf.common.ThymeleafContextConverter; | |
| 11 | ||
| 12 | /** | |
| 13 | * Specific context converter for Spring that registers static variables and | |
| 14 | * {@link EvaluationContext} for SpEL expressions. | |
| 15 | * | |
| 16 | * The aim is to provide the almost same support as if user was using Spring in | |
| 17 | * web context (access to Spring beans from templates, be able to use static | |
| 18 | * variables, ...). | |
| 19 | * | |
| 20 | * @author Aurélien Baudet | |
| 21 | * | |
| 22 | */ | |
| 23 | public class SpringStandaloneThymeleafContextConverter implements ThymeleafContextConverter { | |
| 24 | private final ThymeleafContextConverter delegate; | |
| 25 | private final String evaluationContextVariableName; | |
| 26 | private final StaticVariablesProvider staticVariablesProvider; | |
| 27 | private final ThymeleafEvaluationContextProvider thymeleafEvaluationContextProvider; | |
| 28 | private final ContextMerger contextMerger; | |
| 29 | ||
| 30 | public SpringStandaloneThymeleafContextConverter(ThymeleafContextConverter delegate, String evaluationContextVariableName, StaticVariablesProvider staticVariablesProvider, | |
| 31 | ThymeleafEvaluationContextProvider thymeleafEvaluationContextProvider, ContextMerger contextMerger) { | |
| 32 | super(); | |
| 33 | this.delegate = delegate; | |
| 34 | this.evaluationContextVariableName = evaluationContextVariableName; | |
| 35 | this.staticVariablesProvider = staticVariablesProvider; | |
| 36 | this.thymeleafEvaluationContextProvider = thymeleafEvaluationContextProvider; | |
| 37 | this.contextMerger = contextMerger; | |
| 38 | } | |
| 39 | ||
| 40 | @Override | |
| 41 | public IContext convert(fr.sii.ogham.core.template.context.Context context) throws ContextException { | |
| 42 | IContext base = delegate.convert(context); | |
| 43 | ||
| 44 | // partially borrowed from org.thymeleaf.spring4.view.ThymeleafView | |
| 45 | final Map<String, Object> springModel = new HashMap<>(30); | |
| 46 | ||
| 47 | final Map<String, Object> templateStaticVariables = staticVariablesProvider.getStaticVariables(context); | |
| 48 |
1
1. convert : negated conditional → TIMED_OUT |
if (templateStaticVariables != null) { |
| 49 |
1
1. convert : removed call to java/util/Map::putAll → NO_COVERAGE |
springModel.putAll(templateStaticVariables); |
| 50 | } | |
| 51 | ||
| 52 | final EvaluationContext evaluationContext = thymeleafEvaluationContextProvider.getEvaluationContext(context); | |
| 53 | springModel.put(evaluationContextVariableName, evaluationContext); | |
| 54 | ||
| 55 |
1
1. convert : replaced return value with null for fr/sii/ogham/spring/template/thymeleaf/SpringStandaloneThymeleafContextConverter::convert → TIMED_OUT |
return contextMerger.mergeVariables(base, springModel); |
| 56 | } | |
| 57 | ||
| 58 | } | |
Mutations | ||
| 48 |
1.1 |
|
| 49 |
1.1 |
|
| 55 |
1.1 |