You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jm...@apache.org on 2001/09/21 20:59:47 UTC
cvs commit: jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/intake/validator DateStringValidator.java
jmcnally 01/09/21 11:59:47
Modified: src/services/java/org/apache/fulcrum/intake/model
FieldFactory.java
Added: src/services/java/org/apache/fulcrum/intake/model
DateStringField.java
src/services/java/org/apache/fulcrum/intake/validator
DateStringValidator.java
Log:
added a date validator.
Revision Changes Path
1.2 +10 -1 jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/intake/model/FieldFactory.java
Index: FieldFactory.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/intake/model/FieldFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FieldFactory.java 2001/08/23 07:20:21 1.1
+++ FieldFactory.java 2001/09/21 18:59:46 1.2
@@ -63,7 +63,7 @@
* Creates Field objects.
*
* @author <a href="mailto:jmcnally@collab.net>John McNally</a>
- * @version $Id: FieldFactory.java,v 1.1 2001/08/23 07:20:21 jmcnally Exp $
+ * @version $Id: FieldFactory.java,v 1.2 2001/09/21 18:59:46 jmcnally Exp $
*/
public abstract class FieldFactory
{
@@ -133,6 +133,15 @@
throws Exception
{
return new FileItemField(f, g);
+ }
+ }
+ );
+ fieldCtors.put("DateString", new FieldFactory.FieldCtor()
+ {
+ public Field getInstance(XmlField f, Group g)
+ throws Exception
+ {
+ return new DateStringField(f, g);
}
}
);
1.1 jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/intake/model/DateStringField.java
Index: DateStringField.java
===================================================================
package org.apache.fulcrum.intake.model;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import org.apache.fulcrum.intake.validator.DateStringValidator;
import org.apache.fulcrum.intake.xmlmodel.Rule;
import org.apache.fulcrum.intake.xmlmodel.XmlField;
import org.apache.fulcrum.util.parser.ValueParser;
/**
* Field for date inputs as free form text. The parsing of date strings
* is dependent on any rules that are defined, so this field will expect that
* any validator will be (or extend) DateStringValidator.
*/
public class DateStringField extends Field
{
private DateFormat df = null;
public DateStringField(XmlField field, Group group)
throws Exception
{
super(field, group);
if ( validator == null )
{
df = DateFormat.getInstance();
df.setLenient(true);
}
}
/**
* A suitable validator.
*
* @return "DateStringValidator"
*/
protected String getDefaultValidator()
{
return "org.apache.fulcrum.intake.validator.DateStringValidator";
}
/**
* converts the parameter to the correct Object.
*/
protected void doSetValue(ValueParser pp)
{
if ( isMultiValued )
{
String[] ss = pp.getStrings(getKey());
Date[] dates = new Date[ss.length];
for (int i=0; i<ss.length; i++)
{
dates[i] = getDate(ss[i]);
}
setTestValue(dates);
}
else
{
setTestValue( getDate(pp.getString(getKey())) );
}
}
private Date getDate(String dateString)
{
Date date = null;
try
{
if ( df == null ) // guarantees validator != null
{
date = ((DateStringValidator)validator).parse(dateString);
}
else
{
date = df.parse(dateString);
}
}
catch (ParseException e)
{
//ignore, return null
}
return date;
}
}
1.1 jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/intake/validator/DateStringValidator.java
Index: DateStringValidator.java
===================================================================
package org.apache.fulcrum.intake.validator;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.Date;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import org.apache.fulcrum.ServiceException;
/**
* Validates numbers with the following constraints in addition to those
* listed in DefaultValidator.
*
* <table>
* <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
* <tr><td>format</td><td>see SimpleDateFormat javadoc</td>
* <td> </td></tr>
* <tr><td>formatx</td><td>see SimpleDateFormat javadoc</td>
* <td> </td></tr>
* <tr><td colspan=3>where x is >= 0 to specify multiple date
* formats. Only one format rule should have a message</td></tr>
* <tr><td>flexible</td><td>true, as long as DateFormat can parse the date,
* allow it, and false</td>
* <td>false</td></tr>
* </table>
*
* @author <a href="mailto:jmcnally@collab.net>John McNally</a>
* @version $Id: DateStringValidator.java,v 1.1 2001/09/21 18:59:47 jmcnally Exp $
*/
public class DateStringValidator
extends DefaultValidator
{
private static final String DEFAULT_DATE_MESSAGE =
"Date could not be parsed";
private List dateFormats;
private String dateFormatMessage;
private boolean flexible;
private DateFormat df;
private SimpleDateFormat sdf;
public DateStringValidator(Map paramMap)
throws ServiceException
{
this();
init(paramMap);
}
public DateStringValidator()
{
super();
}
protected void doInit(Map paramMap)
{
dateFormats = new ArrayList(5);
Constraint constraint = (Constraint)paramMap.get("format");
if ( constraint != null )
{
dateFormats.add(constraint.getValue());
setDateFormatMessage(constraint.getMessage());
}
int i = 1;
constraint = (Constraint)paramMap.get("format" + i);
while ( constraint != null )
{
dateFormats.add(constraint.getValue());
setDateFormatMessage(constraint.getMessage());
constraint = (Constraint)paramMap.get("format" + (++i));
}
if ( dateFormatMessage == null || dateFormatMessage.equals("") )
{
dateFormatMessage = DEFAULT_DATE_MESSAGE;
}
constraint = (Constraint)paramMap.get("flexible");
if ( constraint != null )
{
flexible = Boolean.valueOf(constraint.getValue()).booleanValue();
}
if ( dateFormats.size() == 0 || flexible )
{
df = DateFormat.getInstance();
df.setLenient(true);
}
if (dateFormats.size() != 0)
{
sdf = new SimpleDateFormat();
}
}
/**
* Determine whether a testValue meets the criteria specified
* in the constraints defined for this validator
*
* @param testValue a <code>String</code> to be tested
* @exception ValidationException containing an error message if the
* testValue did not pass the validation tests.
*/
protected void doAssertValidity(String testValue)
throws ValidationException
{
try
{
parse(testValue);
}
catch (ParseException e)
{
message = dateFormatMessage;
throw new ValidationException(dateFormatMessage);
}
}
/**
* Parses the String s according to the rules/formats for this
* validator.
*/
public Date parse(String s)
throws ParseException
{
Date date = null;
if ( s == null )
{
throw new ParseException("Input string was null", -1);
}
for ( int i=0; i<dateFormats.size(); i++)
{
sdf.applyPattern((String)dateFormats.get(i));
try
{
date = sdf.parse(s);
}
catch (ParseException e)
{
// ignore
}
if ( date != null )
{
break;
}
}
if ( date == null && df != null )
{
date = df.parse(s);
}
return date;
}
// ************************************************************
// ** Bean accessor methods **
// ************************************************************
/**
* Get the value of minLengthMessage.
* @return value of minLengthMessage.
*/
public String getDateFormatMessage()
{
return dateFormatMessage;
}
/**
* Only sets the message if the new message has some information.
* So the last setMessage call with valid data wins. But later calls
* with null or empty string will not affect a previous valid setting.
*
* @param v Value to assign to minLengthMessage.
*/
public void setDateFormatMessage(String v)
{
if ( v != null && !v.equals("") )
{
dateFormatMessage = v;
}
}
/**
* Get the value of dateFormats.
* @return value of dateFormats.
*/
public List getDateFormats()
{
return dateFormats;
}
/**
* Set the value of dateFormats.
* @param v Value to assign to dateFormats.
*/
public void setDateFormats(List v)
{
this.dateFormats = v;
}
/**
* Get the value of flexible.
* @return value of flexible.
*/
public boolean isFlexible()
{
return flexible;
}
/**
* Set the value of flexible.
* @param v Value to assign to flexible.
*/
public void setFlexible(boolean v)
{
this.flexible = v;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org