Class DefaultValidationMessageHandler
- java.lang.Object
-
- net.sf.jguiraffe.transform.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 theDEFAULT_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.
- With the
DefaultValidationMessageHandler
also determines theValidationMessageLevel
of the messages it returns. This is done by evaluating theerrorLevel
property of the passed inTransformerContext
. If this property is set, its value is interpreted as an enumeration literal defined by theValidationMessageLevel
class. If the property is undefined, the default message levelValidationMessageLevel.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 theTransformerContext
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 Summary
Fields Modifier and Type Field Description static String
DEFAULT_RESOURCE_GROUP_NAME
Constant for the default resource group with validation messages.static String
PROP_MESSAGE_LEVEL
Constant for the name of the property that defines theValidationMessageLevel
of the validation messages produced by this object.
-
Constructor Summary
Constructors Constructor Description DefaultValidationMessageHandler()
Creates a new instance ofDefaultValidationMessageHandler
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getAlternativeResourceGroups()
Returns the alternative resource groups.String
getDefaultResourceGroup()
Returns the name of the default resource group.protected ValidationMessageLevel
getMessageLevel(TransformerContext context)
Determines theValidationMessageLevel
for the validation messages produced by this object.ValidationMessage
getValidationMessage(TransformerContext context, String key, Object... params)
Returns a validation message.void
setAlternativeResourceGroups(String alternativeResourceGroups)
Sets the alternative resource groups.void
setDefaultResourceGroup(String defaultResourceGroup)
Sets the name of the default resource group.
-
-
-
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
-
PROP_MESSAGE_LEVEL
public static final String PROP_MESSAGE_LEVEL
Constant for the name of the property that defines theValidationMessageLevel
of the validation messages produced by this object. This property can be set in the properties of theTransformerContext
.- See Also:
- Constant Field Values
-
-
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
constant is used.DEFAULT_RESOURCE_GROUP_NAME
- 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 interfaceValidationMessageHandler
- 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 theValidationMessageLevel
for the validation messages produced by this object. This implementation tests whether in theTransformerContext
thePROP_MESSAGE_LEVEL
property is set. If so, its value is evaluated. Otherwise the default levelValidationMessageLevel.ERROR
is returned.- Parameters:
context
- theTransformerContext
- Returns:
- the
ValidationMessageLevel
-
-