Top Description Inners Fields Constructors Methods
org.glassfish.hk2.utilities.general.internal

public Class MessageInterpolatorImpl

extends Object
implements MessageInterpolator
Class Inheritance
All Implemented Interfaces
jakarta.validation.MessageInterpolator
Imports
java.security.AccessController, .PrivilegedAction, java.util.Collections, .Enumeration, .Locale, .Map, .MissingResourceException, .ResourceBundle, .Set, .TreeSet, .WeakHashMap, java.util.regex.Matcher, .Pattern, jakarta.validation.MessageInterpolator, .Payload, jakarta.validation.metadata.ConstraintDescriptor

Custom MessageInterpolatorImpl for HK2

This message interpolator is different from the default one in the following ways:

  1. It uses a class specified in the "payload" argument to the annotation to find the class loader to find the resource bundle for messages. This allows classes that are in OSGi modules to specify a resource bundle for bean validation messages.
  2. The "LocalStrings" resource bundle within the same package as the payload class is used to search for messages. If a message is not found, the message is obtained from the default bean validation resource bundle.
This class borrows heavily from the RI implementation of the ResourcBundleMessageInterpolator authored by Emmanuel Bernard, Hardy Ferentschik, and Gunnar Morling.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MessageInterpolatorImpl.ContextResourceBundle

A resource bundle that takes strings from the annotation context.

private static class

Field Summary

Modifier and TypeField and Description
private final boolean
cacheMessages

Flag indicating whether this interpolator should chance some of the interpolation steps.

public static final String
DEFAULT_VALIDATION_MESSAGES

The name of the default message bundle.

private final Locale
defaultLocale

The default locale for the current user.

private static final Pattern
MESSAGE_PARAMETER_PATTERN

Regular expression used to do message interpolation.

private final Map<MessageInterpolatorImpl.LocalisedMessage, String>
resolvedMessages

Step 1-3 of message interpolation can be cached.

public static final String
USER_VALIDATION_MESSAGES

The name of the user-provided message bundle as defined in the specification.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private String
escapeMetaCharacters(String
The string in which to replace the meta characters '$' and '\'.
s
)

Returns: A string where meta characters relevant for Matcher#appendReplacement are escaped.

private boolean
hasReplacementTakenPlace(String origMessage, String newMessage)

public String
interpolate(String message, MessageInterpolator.Context context)

Implements jakarta.validation.MessageInterpolator.interpolate.

Interpolates the message template based on the constraint validation context.

public String

Returns:

the interpolated message.
interpolate
(String
the message to interpolate
message
,
MessageInterpolator.Context
the context of the annotation which holds the parameters for which to interpolate this message
context
,
Locale
the Locale to use for the resource bundle.
locale
)

Implements jakarta.validation.MessageInterpolator.interpolate.

Runs the message interpolation according to algorithm specified in JSR 303.

private String
private String
replaceAnnotationAttributes(String message, Map<String, Object> annotationParameters)

private String
replaceVariables(String message, ResourceBundle bundle, Locale locale, boolean recurse)

private String
resolveParameter(String parameterName, ResourceBundle bundle, Locale locale, boolean recurse)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheMessagesback to summary
private final boolean cacheMessages

Flag indicating whether this interpolator should chance some of the interpolation steps.

DEFAULT_VALIDATION_MESSAGESback to summary
public static final String DEFAULT_VALIDATION_MESSAGES

The name of the default message bundle.

defaultLocaleback to summary
private final Locale defaultLocale

The default locale for the current user.

MESSAGE_PARAMETER_PATTERNback to summary
private static final Pattern MESSAGE_PARAMETER_PATTERN

Regular expression used to do message interpolation.

resolvedMessagesback to summary
private final Map<MessageInterpolatorImpl.LocalisedMessage, String> resolvedMessages

Step 1-3 of message interpolation can be cached. We do this in this map.

USER_VALIDATION_MESSAGESback to summary
public static final String USER_VALIDATION_MESSAGES

The name of the user-provided message bundle as defined in the specification.

Constructor Detail

MessageInterpolatorImplback to summary
public MessageInterpolatorImpl()

Method Detail

escapeMetaCharactersback to summary
private String escapeMetaCharacters(String s)
Parameters
s:String

The string in which to replace the meta characters '$' and '\'.

Returns:String

A string where meta characters relevant for Matcher#appendReplacement are escaped.

hasReplacementTakenPlaceback to summary
private boolean hasReplacementTakenPlace(String origMessage, String newMessage)
interpolateback to summary
public String interpolate(String message, MessageInterpolator.Context context)

Implements jakarta.validation.MessageInterpolator.interpolate.

Doc from jakarta.validation.MessageInterpolator.interpolate.

Interpolates the message template based on the constraint validation context.

The locale is defaulted according to the MessageInterpolator implementation. See the implementation documentation for more detail.

Annotations
@Override
interpolateback to summary
public String interpolate(String message, MessageInterpolator.Context context, Locale locale)

Implements jakarta.validation.MessageInterpolator.interpolate.

Runs the message interpolation according to algorithm specified in JSR 303.
Note:
Look-ups in user bundles is recursive whereas look-ups in default bundle are not!

Parameters
message:String

the message to interpolate

context:MessageInterpolator.Context

the context of the annotation which holds the parameters for which to interpolate this message

locale:Locale

the Locale to use for the resource bundle.

Returns:String

the interpolated message.

Annotations
@Override
removeCurlyBraceback to summary
private String removeCurlyBrace(String parameter)
replaceAnnotationAttributesback to summary
private String replaceAnnotationAttributes(String message, Map<String, Object> annotationParameters)
replaceVariablesback to summary
private String replaceVariables(String message, ResourceBundle bundle, Locale locale, boolean recurse)
resolveParameterback to summary
private String resolveParameter(String parameterName, ResourceBundle bundle, Locale locale, boolean recurse)
org.glassfish.hk2.utilities.general.internal back to summary

private Class MessageInterpolatorImpl.ContextResourceBundle

extends ResourceBundle
Class Inheritance

A resource bundle that takes strings from the annotation context. This class looks for the "LocalStrings" resource bundle in the same package as the class that is specified in the payload of the annotation. If a String cannot be found there, then it looks in the user resource bundle as defined in JSR-303.

Field Summary

Modifier and TypeField and Description
pack-priv ResourceBundle
pack-priv ResourceBundle
Inherited from java.util.ResourceBundle:
parent

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Enumeration<String>
getKeys()

Implements abstract java.util.ResourceBundle.getKeys.

Returns an enumeration of the keys.

protected Object
handleGetObject(String key)

Implements abstract java.util.ResourceBundle.handleGetObject.

Gets an object for the given key from this resource bundle.

Inherited from java.util.ResourceBundle:
clearCacheclearCachecontainsKeygetBaseBundleNamegetBundlegetBundlegetBundlegetBundlegetBundlegetBundlegetBundlegetBundlegetLocalegetObjectgetStringgetStringArrayhandleKeySetkeySetsetParent

Field Detail

contextBundleback to summary
pack-priv ResourceBundle contextBundle
userBundleback to summary
pack-priv ResourceBundle userBundle

Constructor Detail

ContextResourceBundleback to summary
pack-priv ContextResourceBundle(MessageInterpolator.Context context, Locale locale)

Method Detail

getKeysback to summary
public Enumeration<String> getKeys()

Implements abstract java.util.ResourceBundle.getKeys.

Doc from java.util.ResourceBundle.getKeys.

Returns an enumeration of the keys.

Annotations
@Override
handleGetObjectback to summary
protected Object handleGetObject(String key)

Implements abstract java.util.ResourceBundle.handleGetObject.

Doc from java.util.ResourceBundle.handleGetObject.

Gets an object for the given key from this resource bundle. Returns null if this resource bundle does not contain an object for the given key.

Annotations
@Override
org.glassfish.hk2.utilities.general.internal back to summary

private Class MessageInterpolatorImpl.LocalisedMessage

extends Object
Class Inheritance
  • java.lang.Object
  • org.glassfish.hk2.utilities.general.internal.MessageInterpolatorImpl.LocalisedMessage

Field Summary

Modifier and TypeField and Description
private final Locale
private final String

Constructor Summary

AccessConstructor and Description
pack-priv
LocalisedMessage(String message, Locale locale)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

localeback to summary
private final Locale locale
messageback to summary
private final String message

Constructor Detail

LocalisedMessageback to summary
pack-priv LocalisedMessage(String message, Locale locale)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override