package org.hibernate.validator.messageinterpolation;
import static org.hibernate.validator.internal.util.ConcurrentReferenceHashMap.ReferenceType.SOFT;
import java.lang.invoke.MethodHandles;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.MessageInterpolator;
import javax.validation.ValidationException;
import org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTermType;
import org.hibernate.validator.internal.engine.messageinterpolation.LocalizedMessage;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.MessageDescriptorFormatException;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.Token;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.TokenCollector;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.TokenIterator;
import org.hibernate.validator.internal.util.ConcurrentReferenceHashMap;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
public abstract class AbstractMessageInterpolator implements MessageInterpolator {
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
private static final int DEFAULT_INITIAL_CAPACITY = 100;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
private static final String DEFAULT_VALIDATION_MESSAGES = "org.hibernate.validator.ValidationMessages";
public static final String USER_VALIDATION_MESSAGES = "ValidationMessages";
public static final String CONTRIBUTOR_VALIDATION_MESSAGES = "ContributorValidationMessages";
private final Locale defaultLocale;
private final ResourceBundleLocator userResourceBundleLocator;
private final ResourceBundleLocator defaultResourceBundleLocator;
private final ResourceBundleLocator contributorResourceBundleLocator;
private final ConcurrentReferenceHashMap<LocalizedMessage, String> resolvedMessages;
private final ConcurrentReferenceHashMap<String, List<Token>> tokenizedParameterMessages;
private final ConcurrentReferenceHashMap<String, List<Token>> tokenizedELMessages;
private final boolean cachingEnabled;
private static final Pattern LEFT_BRACE = Pattern.compile( "\\{", Pattern.LITERAL );
private static final Pattern RIGHT_BRACE = Pattern.compile( "\\}", Pattern.LITERAL );
private static final Pattern SLASH = Pattern.compile( "\\\\", Pattern.LITERAL );
private static final Pattern DOLLAR = Pattern.compile( "\\$", Pattern.LITERAL );
public AbstractMessageInterpolator() {
this( null );
}
public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocator) {
this( userResourceBundleLocator, null );
}
public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocator,
ResourceBundleLocator contributorResourceBundleLocator) {
this( userResourceBundleLocator, contributorResourceBundleLocator, true );
}
public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocator,
ResourceBundleLocator contributorResourceBundleLocator,
boolean cacheMessages) {
defaultLocale = Locale.getDefault();
if ( userResourceBundleLocator == null ) {
this.userResourceBundleLocator = new PlatformResourceBundleLocator( USER_VALIDATION_MESSAGES );
}
else {
this.userResourceBundleLocator = userResourceBundleLocator;
}
if ( contributorResourceBundleLocator == null ) {
this.contributorResourceBundleLocator = new PlatformResourceBundleLocator(
CONTRIBUTOR_VALIDATION_MESSAGES,
null,
true
);
}
else {
this.contributorResourceBundleLocator = contributorResourceBundleLocator;
}
this.defaultResourceBundleLocator = new PlatformResourceBundleLocator( DEFAULT_VALIDATION_MESSAGES );
this.cachingEnabled = cacheMessages;
if ( cachingEnabled ) {
this.resolvedMessages = new ConcurrentReferenceHashMap<LocalizedMessage, String>(
DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR,
DEFAULT_CONCURRENCY_LEVEL,
SOFT,
SOFT,
EnumSet.noneOf( ConcurrentReferenceHashMap.Option.class )
);
this.tokenizedParameterMessages = new ConcurrentReferenceHashMap<String, List<Token>>(
DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR,
DEFAULT_CONCURRENCY_LEVEL,
SOFT,
SOFT,
EnumSet.noneOf( ConcurrentReferenceHashMap.Option.class )
);
this.tokenizedELMessages = new ConcurrentReferenceHashMap<String, List<Token>>(
DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR,
DEFAULT_CONCURRENCY_LEVEL,
SOFT,
SOFT,
EnumSet.noneOf( ConcurrentReferenceHashMap.Option.class )
);
}
else {
resolvedMessages = null;
tokenizedParameterMessages = null;
tokenizedELMessages = null;
}
}
@Override
public String interpolate(String message, Context context) {
String interpolatedMessage = message;
try {
interpolatedMessage = interpolateMessage( message, context, defaultLocale );
}
catch (MessageDescriptorFormatException e) {
LOG.warn( e.getMessage() );
}
return interpolatedMessage;
}
@Override
public String interpolate(String message, Context context, Locale locale) {
String interpolatedMessage = message;
try {
interpolatedMessage = interpolateMessage( message, context, locale );
}
catch (ValidationException e) {
LOG.warn( e.getMessage() );
}
return interpolatedMessage;
}
private String interpolateMessage(String message, Context context, Locale locale) throws MessageDescriptorFormatException {
if ( message.indexOf( '{' ) < 0 ) {
return replaceEscapedLiterals( message );
}
String resolvedMessage = null;
if ( cachingEnabled ) {
resolvedMessage = resolvedMessages.computeIfAbsent( new LocalizedMessage( message, locale ), lm -> resolveMessage( message, locale ) );
}
else {
resolvedMessage = resolveMessage( message, locale );
}
if ( resolvedMessage.indexOf( '{' ) > -1 ) {
resolvedMessage = interpolateExpression(
new TokenIterator( getParameterTokens( resolvedMessage, tokenizedParameterMessages, InterpolationTermType.PARAMETER ) ),
context,
locale
);
resolvedMessage = interpolateExpression(
new TokenIterator( getParameterTokens( resolvedMessage, tokenizedELMessages, InterpolationTermType.EL ) ),
context,
locale
);
}
resolvedMessage = replaceEscapedLiterals( resolvedMessage );
return resolvedMessage;
}
private List<Token> getParameterTokens(String resolvedMessage, ConcurrentReferenceHashMap<String, List<Token>> cache, InterpolationTermType termType) {
if ( cachingEnabled ) {
return cache.computeIfAbsent(
resolvedMessage,
rm -> new TokenCollector( resolvedMessage, termType ).getTokenList()
);
}
else {
return new TokenCollector( resolvedMessage, termType ).getTokenList();
}
}
private String resolveMessage(String message, Locale locale) {
String resolvedMessage = message;
ResourceBundle userResourceBundle = userResourceBundleLocator
.getResourceBundle( locale );
ResourceBundle constraintContributorResourceBundle = contributorResourceBundleLocator
.getResourceBundle( locale );
ResourceBundle defaultResourceBundle = defaultResourceBundleLocator
.getResourceBundle( locale );
String userBundleResolvedMessage;
boolean evaluatedDefaultBundleOnce = false;
do {
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, userResourceBundle, locale, true
);
if ( !hasReplacementTakenPlace( userBundleResolvedMessage, resolvedMessage ) ) {
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, constraintContributorResourceBundle, locale, true
);
}
if ( evaluatedDefaultBundleOnce && !hasReplacementTakenPlace( userBundleResolvedMessage, resolvedMessage ) ) {
break;
}
resolvedMessage = interpolateBundleMessage(
userBundleResolvedMessage,
defaultResourceBundle,
locale,
false
);
evaluatedDefaultBundleOnce = true;
} while ( true );
return resolvedMessage;
}
private String replaceEscapedLiterals(String resolvedMessage) {
if ( resolvedMessage.indexOf( '\\' ) > -1 ) {
resolvedMessage = LEFT_BRACE.matcher( resolvedMessage ).replaceAll( "{" );
resolvedMessage = RIGHT_BRACE.matcher( resolvedMessage ).replaceAll( "}" );
resolvedMessage = SLASH.matcher( resolvedMessage ).replaceAll( Matcher.quoteReplacement( "\\" ) );
resolvedMessage = DOLLAR.matcher( resolvedMessage ).replaceAll( Matcher.quoteReplacement( "$" ) );
}
return resolvedMessage;
}
private boolean hasReplacementTakenPlace(String origMessage, String newMessage) {
return !origMessage.equals( newMessage );
}
private String interpolateBundleMessage(String message, ResourceBundle bundle, Locale locale, boolean recursive)
throws MessageDescriptorFormatException {
TokenCollector tokenCollector = new TokenCollector( message, InterpolationTermType.PARAMETER );
TokenIterator tokenIterator = new TokenIterator( tokenCollector.getTokenList() );
while ( tokenIterator.hasMoreInterpolationTerms() ) {
String term = tokenIterator.nextInterpolationTerm();
String resolvedParameterValue = resolveParameter(
term, bundle, locale, recursive
);
tokenIterator.replaceCurrentInterpolationTerm( resolvedParameterValue );
}
return tokenIterator.getInterpolatedMessage();
}
private String interpolateExpression(TokenIterator tokenIterator, Context context, Locale locale)
throws MessageDescriptorFormatException {
while ( tokenIterator.hasMoreInterpolationTerms() ) {
String term = tokenIterator.nextInterpolationTerm();
String resolvedExpression = interpolate( context, locale, term );
tokenIterator.replaceCurrentInterpolationTerm( resolvedExpression );
}
return tokenIterator.getInterpolatedMessage();
}
public abstract String interpolate(Context context, Locale locale, String term);
private String resolveParameter(String parameterName, ResourceBundle bundle, Locale locale, boolean recursive)
throws MessageDescriptorFormatException {
String parameterValue;
try {
if ( bundle != null ) {
parameterValue = bundle.getString( removeCurlyBraces( parameterName ) );
if ( recursive ) {
parameterValue = interpolateBundleMessage( parameterValue, bundle, locale, recursive );
}
}
else {
parameterValue = parameterName;
}
}
catch (MissingResourceException e) {
parameterValue = parameterName;
}
return parameterValue;
}
private String removeCurlyBraces(String parameter) {
return parameter.substring( 1, parameter.length() - 1 );
}
}