Class DefaultValidationMessageHandler

  • All Implemented Interfaces:
    ValidationMessageHandler

    public class DefaultValidationMessageHandler
    extends Object
    implements ValidationMessageHandler

    A default implementation of the ValidationMessageHandler interface.

    This class performs the following steps for obtaining a ValidationMessage object for a given key:

    • It checks whether the passed in TransformerContext contains a property with the name of the given key. If this is the case, its value will be used as error message. This makes it possible to override validation messages directly in the builder script when the validator is defined.
    • Otherwise the error message will be loaded from the application's resources using the key as resource ID. For the resource group the following options exist:
      • With the setAlternativeResourceGroups(String) method a list of resource groups can be set, which will be searched for resources with message keys. So an application can define a custom resource group containing the error messages it wants to override and specify it here.
      • If the resource for the message key cannot be found in one of the alternative resource groups, the resource group returned by the getDefaultResourceGroup() method will be used. If not otherwise set, the name defined by the DEFAULT_RESOURCE_GROUP_NAME constant is used. This is a resource group shipped with the framework, which contains default validation messages in a couple of supported languages.

    DefaultValidationMessageHandler also determines the ValidationMessageLevel of the messages it returns. This is done by evaluating the errorLevel property of the passed in TransformerContext. If this property is set, its value is interpreted as an enumeration literal defined by the ValidationMessageLevel class. If the property is undefined, the default message level ValidationMessageLevel.ERROR is used. The advantage behind this concept is that the message level is completely transparent for validators. So they can produce messages of any level, provided that the TransformerContext is properly configured.

    The ValidationMessage objects returned by this object are thread-safe; they can be concurrently accessed without requiring further synchronization.

    Version:
    $Id: DefaultValidationMessageHandler.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Field Detail

      • DEFAULT_RESOURCE_GROUP_NAME

        public static final String DEFAULT_RESOURCE_GROUP_NAME
        Constant for the default resource group with validation messages. This group name will be used if no specific default resource group has been set.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DefaultValidationMessageHandler

        public DefaultValidationMessageHandler()
        Creates a new instance of DefaultValidationMessageHandler.
    • Method Detail

      • getDefaultResourceGroup

        public String getDefaultResourceGroup()
        Returns the name of the default resource group.
        Returns:
        the name of the default resource group
      • setDefaultResourceGroup

        public void setDefaultResourceGroup​(String defaultResourceGroup)
        Sets the name of the default resource group. This group will be searched for resources for validation messages if the search in the alternative resource groups yielded no results. If no default resource group was set, the group defined by the DEFAULT_RESOURCE_GROUP_NAME constant is used.
        Parameters:
        defaultResourceGroup - the new default resource group
        See Also:
        setAlternativeResourceGroups(String)
      • getAlternativeResourceGroups

        public String getAlternativeResourceGroups()
        Returns the alternative resource groups.
        Returns:
        the alternative resource groups
      • setAlternativeResourceGroups

        public void setAlternativeResourceGroups​(String alternativeResourceGroups)
        Sets the alternative resource groups. This method expects a comma separated string with names of resource groups to be searched for validation messages. If, for instance, the string "specialMessages, extendedMessages" is passed in, the text for a validation message will be searched first in the group "specialMessages", then in the group "extendedMessages", and finally in the default resource group. It is possible to set the alternative resource groups to null. Then only the default resource group will be searched.
        Parameters:
        alternativeResourceGroups - a comma separated list of alternative resource groups
      • getValidationMessage

        public ValidationMessage getValidationMessage​(TransformerContext context,
                                                      String key,
                                                      Object... params)
        Returns a validation message. This method implements the steps described in the class comment for obtaining a validation message for the given key.
        Specified by:
        getValidationMessage in interface ValidationMessageHandler
        Parameters:
        context - the transformer context (must not be null)
        key - the key of the message (must not be null)
        params - additional parameters to be integrated into the message
        Returns:
        the validation message object for this key
        Throws:
        IllegalArgumentException - if the transformer context or the key are null
      • getMessageLevel

        protected ValidationMessageLevel getMessageLevel​(TransformerContext context)
        Determines the ValidationMessageLevel for the validation messages produced by this object. This implementation tests whether in the TransformerContext the PROP_MESSAGE_LEVEL property is set. If so, its value is evaluated. Otherwise the default level ValidationMessageLevel.ERROR is returned.
        Parameters:
        context - the TransformerContext
        Returns:
        the ValidationMessageLevel