📄 abstractcalendarvalidator.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.validator.routines;
import java.text.DateFormatSymbols;
import java.text.Format;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
/**
* <p>Abstract class for Date/Time/Calendar validation.</p>
*
* <p>This is a <i>base</i> class for building Date / Time
* Validators using format parsing.</p>
*
* @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
* @since Validator 1.3.0
*/
public abstract class AbstractCalendarValidator extends AbstractFormatValidator {
private int dateStyle = -1;
private int timeStyle = -1;
/**
* Construct an instance with the specified <i>strict</i>,
* <i>time</i> and <i>date</i> style parameters.
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
* @param dateStyle the date style to use for Locale validation.
* @param timeStyle the time style to use for Locale validation.
*/
public AbstractCalendarValidator(boolean strict, int dateStyle, int timeStyle) {
super(strict);
this.dateStyle = dateStyle;
this.timeStyle = timeStyle;
}
/**
* <p>Validate using the specified <code>Locale</code>.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @param locale The locale to use for the Format, defaults to the default
* @return <code>true</code> if the value is valid.
*/
public boolean isValid(String value, String pattern, Locale locale) {
Object parsedValue = parse(value, pattern, locale, (TimeZone)null);
return (parsedValue == null ? false : true);
}
/**
* <p>Format an object into a <code>String</code> using
* the default Locale.</p>
*
* @param value The value validation is being performed on.
* @param timeZone The Time Zone used to format the date,
* system default if null (unless value is a <code>Calendar</code>.
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, TimeZone timeZone) {
return format(value, (String)null, (Locale)null, timeZone);
}
/**
* <p>Format an object into a <code>String</code> using
* the specified pattern.</p>
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @param timeZone The Time Zone used to format the date,
* system default if null (unless value is a <code>Calendar</code>.
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, String pattern, TimeZone timeZone) {
return format(value, pattern, (Locale)null, timeZone);
}
/**
* <p>Format an object into a <code>String</code> using
* the specified Locale.</p>
*
* @param value The value validation is being performed on.
* @param locale The locale to use for the Format.
* @param timeZone The Time Zone used to format the date,
* system default if null (unless value is a <code>Calendar</code>.
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, Locale locale, TimeZone timeZone) {
return format(value, (String)null, locale, timeZone);
}
/**
* <p>Format an object using the specified pattern and/or
* <code>Locale</code>.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @param locale The locale to use for the Format.
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, String pattern, Locale locale) {
return format(value, pattern, locale, (TimeZone)null);
}
/**
* <p>Format an object using the specified pattern and/or
* <code>Locale</code>.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @param locale The locale to use for the Format.
* @param timeZone The Time Zone used to format the date,
* system default if null (unless value is a <code>Calendar</code>.
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, String pattern, Locale locale, TimeZone timeZone) {
DateFormat formatter = (DateFormat)getFormat(pattern, locale);
if (timeZone != null) {
formatter.setTimeZone(timeZone);
} else if (value instanceof Calendar) {
formatter.setTimeZone(((Calendar)value).getTimeZone());
}
return format(value, formatter);
}
/**
* <p>Format a value with the specified <code>DateFormat</code>.</p>
*
* @param value The value to be formatted.
* @param formatter The Format to use.
* @return The formatted value.
*/
protected String format(Object value, Format formatter) {
if (value == null) {
return null;
} else if (value instanceof Calendar) {
value = ((Calendar)value).getTime();
}
return formatter.format(value);
}
/**
* <p>Checks if the value is valid against a specified pattern.</p>
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to validate the value against, or the
* default for the <code>Locale</code> if <code>null</code>.
* @param locale The locale to use for the date format, system default if null.
* @param timeZone The Time Zone used to parse the date, system default if null.
* @return The parsed value if valid or <code>null</code> if invalid.
*/
protected Object parse(String value, String pattern, Locale locale, TimeZone timeZone) {
value = (value == null ? null : value.trim());
if (value == null || value.length() == 0) {
return null;
}
DateFormat formatter = (DateFormat)getFormat(pattern, locale);
if (timeZone != null) {
formatter.setTimeZone(timeZone);
}
return parse(value, formatter);
}
/**
* <p>Process the parsed value, performing any further validation
* and type conversion required.</p>
*
* @param value The parsed object created.
* @param formatter The Format used to parse the value with.
* @return The parsed value converted to the appropriate type
* if valid or <code>null</code> if invalid.
*/
protected abstract Object processParsedValue(Object value, Format formatter);
/**
* <p>Returns a <code>DateFormat</code> for the specified <i>pattern</i>
* and/or <code>Locale</code>.</p>
*
* @param pattern The pattern used to validate the value against or
* <code>null</code> to use the default for the <code>Locale</code>.
* @param locale The locale to use for the currency format, system default if null.
* @return The <code>DateFormat</code> to created.
*/
protected Format getFormat(String pattern, Locale locale) {
DateFormat formatter = null;
boolean usePattern = (pattern != null && pattern.length() > 0);
if (!usePattern) {
formatter = (DateFormat)getFormat(locale);
} else if (locale == null) {
formatter = new SimpleDateFormat(pattern);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -