public abstract class DateTransformerBase extends Object implements Transformer, Validator
An abstract base class for date transformer objects.
Date transformers know how to handle certain kinds of date formats. They can
java.util.Date
objectjava.util.Date
object into a string
representation.
This base class already implements the major part of the required
functionality. Concrete sub classes are responsible of creating an
appropriate DateFormat
object that is able to parse the
specific date format.
There are some properties for customizing the parsing of date strings. These
properties can be set either through the set methods provided by this class
or using a <properties>
section in the builder script
that declares the transformer. The following properties are supported:
Property | Description | Default |
---|---|---|
style | Defines the style of the date. This can be one of the style constants
declared by the java.text.DateFormat class like
SHORT or FULL . |
SHORT |
lenient | Specifies the lenient mode for parsing dates. The lenient flag has the
same meaning as described in the documentation of the
java.text.DateFormat class and controls how strict the parsing
process is. Note that lenient mode is turned off per default. |
false |
referenceDate | With this property a reference date can be specified that is used for
testing semantic correctness. For instance, if one of the after
or before flags described below are set, it can be tested
whether the entered date is after or before this reference date. The property
must be a string conforming to one of the formats supported by
java.sql.Timestamp , java.sql.Date , or
java.sql.Time . |
current date |
after | If this boolean flag is set, the entered date must be after the reference date. | false |
before | If this boolean flag is set, the entered date must be before the
reference date. Note that the properties before and
after are mutual exclusive. |
false |
equal | This flag is evaluated only if after or
before is true. In this case, it controls whether the
reference date is included in the comparison. So a comparison can be
specified whether the entered date is before or equal a reference date. |
false |
Depending on the performed validations this validator implementation can create a bunch of error messages. The following table lists all supported error messages:
Message key | Description | Parameters |
---|---|---|
|
The passed in string cannot be parsed to a date object. | {0} = the date string |
|
The entered date must be after the reference date. | {0} = the (formatted) reference date |
|
The entered date must be after or equal the reference date. | {0} = the (formatted) reference date |
|
The entered date must be before the reference date. | {0} = the (formatted) reference date |
|
The entered date must be before or equal the reference date. | {0} = the (formatted) reference date |
This class implements both the
and the
Transformer
interfaces. The Validator
Transformer
implementation can work in both directions: if a Date
object
is passed in, it will format the date to a string using the specified format.
Otherwise the passed in object is tried to be converted to a date.
Instances can be shared between multiple input components. It is especially possible to use an instance as both (read and write) transformer and validator for an input component at the same time (provided that the same properties are used). However the class is not thread-safe.
ValidationMessageConstants
Modifier and Type | Field and Description |
---|---|
protected static String |
PROP_AFTER
Constant for the after property.
|
protected static String |
PROP_BEFORE
Constant for the before property.
|
protected static String |
PROP_EQUAL
Constant for the equal property.
|
protected static String |
PROP_LENIENT
Constant for the lenient property.
|
protected static String |
PROP_REFERENCE_DATE
Constant for the referenceDate property.
|
protected static String |
PROP_STYLE
Constant for the style property.
|
Modifier | Constructor and Description |
---|---|
protected |
DateTransformerBase()
Creates a new instance of
DateTransformerBase . |
Modifier and Type | Method and Description |
---|---|
protected abstract DateFormat |
createFormat(Locale locale,
int style,
org.apache.commons.configuration.Configuration config)
Creates a
DateFormat object for parsing dates of the
supported format. |
protected ValidationResult |
errorResult(String errorKey,
TransformerContext ctx,
Object... params)
Creates a validation result if an error occurred.
|
protected Date |
getDefaultReferenceDate()
Creates a new default reference date.
|
String |
getReferenceDate()
Returns the reference date.
|
protected Date |
getReferenceDateProperty(org.apache.commons.configuration.Configuration config)
Returns the reference date to be used.
|
int |
getStyle()
Returns the style for the date to be parsed.
|
protected DateFormat |
initializeFormat(Locale locale,
org.apache.commons.configuration.Configuration config)
Returns an initialized format object.
|
boolean |
isAfter()
Returns the after flag.
|
boolean |
isBefore()
Returns the before flag.
|
protected ValidationResult |
isDateValid(Date date,
DateFormat fmt,
TransformerContext ctx,
org.apache.commons.configuration.Configuration config)
Checks the specified date.
|
boolean |
isEqual()
Returns the equal flag.
|
boolean |
isLenient()
Returns the lenient flag.
|
ValidationResult |
isValid(Object o,
TransformerContext ctx)
Validates the passed in object.
|
void |
setAfter(boolean after)
Sets the after flag.
|
void |
setBefore(boolean before)
Sets the before flag.
|
void |
setEqual(boolean equal)
Sets the equal flag.
|
void |
setLenient(boolean lenient)
Sets the lenient flag.
|
void |
setReferenceDate(String referenceDate)
Sets the reference date.
|
void |
setStyle(int dateStyle)
Sets the style for the date to be parsed.
|
Object |
transform(Object o,
TransformerContext ctx)
Transforms the specified object.
|
protected Date |
transformDate(String date,
DateFormat fmt)
Parses the specified date string.
|
protected Date |
transformSqlDate(String strDate)
Transforms a date in string form to a date object.
|
protected Object |
transformToDate(Object o,
TransformerContext ctx)
Performs a transformation to a date object.
|
protected Object |
transformToString(Date dt,
TransformerContext ctx)
Performs a transformation from a date to string.
|
static Date |
updateDatePart(Date dateTime,
Date datePart)
Writes the given date part into the specified date object.
|
static Date |
updateTimePart(Date dateTime,
Date timePart)
Writes the given time part into the specified date object.
|
protected static final String PROP_STYLE
protected static final String PROP_LENIENT
protected static final String PROP_REFERENCE_DATE
protected static final String PROP_BEFORE
protected static final String PROP_AFTER
protected static final String PROP_EQUAL
protected DateTransformerBase()
DateTransformerBase
.public int getStyle()
public void setStyle(int dateStyle)
DateFormat
class, e.g.
DateFormat.SHORT
or DateFormat.MEDIUM
.dateStyle
- the style for the datepublic boolean isLenient()
public void setLenient(boolean lenient)
lenient
- the lenient flagpublic String getReferenceDate()
public void setReferenceDate(String referenceDate)
java.sql
package.referenceDate
- the reference dateIllegalArgumentException
- if the date has not the expected formatpublic boolean isBefore()
public void setBefore(boolean before)
before
- the before flagpublic boolean isAfter()
public void setAfter(boolean after)
after
- the after flagpublic boolean isEqual()
public void setEqual(boolean equal)
before
or after
flags is set. In this case
the reference date is included into the comparison.equal
- the value of the equal flagpublic Object transform(Object o, TransformerContext ctx) throws Exception
java.util.Date
object. If the
date is invalid, an exception is thrown. The method does not perform any
additional validity checks. This means that any valid date will be
returned, even if it conflicts with a reference date.transform
in interface Transformer
o
- the object to be transformedctx
- the transformer contextException
- if an error occurspublic ValidationResult isValid(Object o, TransformerContext ctx)
before
or after
flags have been set, the
date will also be compared to a reference date. A null input will
be considered valid.public static Date updateDatePart(Date dateTime, Date datePart)
Date
object is used. For example, if the
dateTime
parameter has the value
2008-01-29 22:17:59
and datePart
is
2008-02-05
, the result will be
2008-02-05 22:17:59
.dateTime
- the combined date/time objectdatePart
- the date partIllegalArgumentException
- if one of the date parameters is nullpublic static Date updateTimePart(Date dateTime, Date timePart)
Date
object is used. For example, if the
dateTime
parameter has the value
2008-01-29 22:17:59
and timePart
is
10:22:05
, the result will be
2008-02-05 10:22:05
.dateTime
- the combined date/time objecttimePart
- the time partIllegalArgumentException
- if one of the date parameters is nullprotected Object transformToDate(Object o, TransformerContext ctx) throws Exception
o
- the object to be transformedctx
- the transformer contextException
- if an error occursprotected Object transformToString(Date dt, TransformerContext ctx) throws Exception
dt
- the date to be transformedctx
- the transformer contextException
- if an error occursprotected Date getReferenceDateProperty(org.apache.commons.configuration.Configuration config)
getDefaultReferenceDate()
method is called.config
- the configuration with the current propertiesIllegalArgumentException
- if the reference date is in an incorrect
formatprotected Date getDefaultReferenceDate()
Date
object for the
current date (only date, no time portion).protected Date transformSqlDate(String strDate)
java.sql
compatible format.strDate
- the date as a stringIllegalArgumentException
- if the date cannot be convertedprotected Date transformDate(String date, DateFormat fmt) throws ParseException
DateFormat
object for this purpose.date
- the date to be parsedfmt
- the DateFormat
to be usedParseException
- if the date cannot be parsedprotected DateFormat initializeFormat(Locale locale, org.apache.commons.configuration.Configuration config)
createFormat()
for obtaining a new format object. Then the
object is initialized based on the currently set properties.locale
- the localeconfig
- the properties associated with the current contextprotected ValidationResult isDateValid(Date date, DateFormat fmt, TransformerContext ctx, org.apache.commons.configuration.Configuration config)
isValid()
if the entered date is syntactically correct. It
checks for semantic correctness, e.g. whether the date is in correct
relation to the reference date.date
- the date to checkfmt
- the date format object to be usedctx
- the transformer contextconfig
- the configuration with the propertiesValidationResult
object with the result of the
validationprotected ValidationResult errorResult(String errorKey, TransformerContext ctx, Object... params)
errorKey
- the key of the error messagectx
- the transformer contextparams
- optional parameters for the error messageprotected abstract DateFormat createFormat(Locale locale, int style, org.apache.commons.configuration.Configuration config)
DateFormat
object for parsing dates of the
supported format. Concrete sub classes have to return an appropriate
instance of DateFormat
(an implementation will probably
call the correct getXXXInstance()
factory method of
DateFormat
).locale
- the localestyle
- the style to be usedconfig
- a configuration object for accessing the current propertiesDateFormat
object to be used for parsingCopyright © 2016 The JGUIraffe Team. All rights reserved.