-
Notifications
You must be signed in to change notification settings - Fork 565
/
ElTermResolver.java
114 lines (99 loc) · 4.09 KB
/
ElTermResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.internal.engine.messageinterpolation;
import java.util.Locale;
import java.util.Map;
import javax.el.ELException;
import javax.el.ExpressionFactory;
import javax.el.PropertyNotFoundException;
import javax.el.ValueExpression;
import javax.validation.MessageInterpolator;
import org.hibernate.validator.internal.engine.MessageInterpolatorContext;
import org.hibernate.validator.internal.engine.messageinterpolation.el.RootResolver;
import org.hibernate.validator.internal.engine.messageinterpolation.el.SimpleELContext;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
/**
* Resolver for the el expressions.
*
* @author Hardy Ferentschik
* @author Adam Stawicki
*/
public class ElTermResolver implements TermResolver {
private static final Log log = LoggerFactory.make();
/**
* Name under which the currently validate value is bound to the EL context.
*/
private static final String VALIDATED_VALUE_NAME = "validatedValue";
/**
* The locale for which to interpolate the expression.
*/
private final Locale locale;
/**
* Factory for creating EL expressions
*/
private final ExpressionFactory expressionFactory;
/**
* Construct the resolver. The expression factory has to be passed in to ensure that it is
* set up early and to allow for application control.
* @param locale the locale.
* @param expressionFactory the expression factory.
*/
public ElTermResolver(Locale locale, ExpressionFactory expressionFactory) {
this.locale = locale;
this.expressionFactory = expressionFactory;
}
@Override
public String interpolate(MessageInterpolator.Context context, String expression) {
String resolvedExpression = expression;
SimpleELContext elContext = new SimpleELContext( expressionFactory );
try {
ValueExpression valueExpression = bindContextValues( expression, context, elContext );
resolvedExpression = (String) valueExpression.getValue( elContext );
}
catch (PropertyNotFoundException pnfe) {
log.unknownPropertyInExpressionLanguage( expression, pnfe );
}
catch (ELException e) {
log.errorInExpressionLanguage( expression, e );
}
catch (Exception e) {
log.evaluatingExpressionLanguageExpressionCausedException( expression, e );
}
return resolvedExpression;
}
private ValueExpression bindContextValues(String messageTemplate, MessageInterpolator.Context messageInterpolatorContext, SimpleELContext elContext) {
// bind the validated value
ValueExpression valueExpression = expressionFactory.createValueExpression(
messageInterpolatorContext.getValidatedValue(),
Object.class
);
elContext.setVariable( VALIDATED_VALUE_NAME, valueExpression );
// bind a formatter instantiated with proper locale
valueExpression = expressionFactory.createValueExpression(
new FormatterWrapper( locale ),
FormatterWrapper.class
);
elContext.setVariable( RootResolver.FORMATTER, valueExpression );
// map the annotation values
for ( Map.Entry<String, Object> entry : messageInterpolatorContext.getConstraintDescriptor()
.getAttributes()
.entrySet() ) {
valueExpression = expressionFactory.createValueExpression( entry.getValue(), Object.class );
elContext.setVariable( entry.getKey(), valueExpression );
}
// check for custom parameters provided by HibernateConstraintValidatorContext
if ( messageInterpolatorContext instanceof MessageInterpolatorContext ) {
MessageInterpolatorContext internalContext = (MessageInterpolatorContext) messageInterpolatorContext;
for ( Map.Entry<String, Object> entry : internalContext.getMessageParameters().entrySet() ) {
valueExpression = expressionFactory.createValueExpression( entry.getValue(), Object.class );
elContext.setVariable( entry.getKey(), valueExpression );
}
}
return expressionFactory.createValueExpression( elContext, messageTemplate, String.class );
}
}