jmcnally 01/06/18 16:35:48
Modified: src/java/org/apache/turbine/services/intake/model Field.java
src/java/org/apache/turbine/services/intake/validator
DefaultValidator.java IntegerValidator.java
NumberKeyValidator.java NumberValidator.java
Validator.java
Added: src/java/org/apache/turbine/services/intake/validator
InitableByConstraintMap.java
Log:
added accessor methods to the validators. the idea being that the validators
could be initialized using standard bean tools (though this is not implemented)
The initialization by a constraint map is moved to its own interface.
Some bug fixes such as not bypassing a minLength > 0 requirement when required=false
and setting the test value when a validator is not specified.
Revision Changes Path
1.16 +16 -4
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/Field.java
Index: Field.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/Field.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- Field.java 2001/06/11 19:41:34 1.15
+++ Field.java 2001/06/18 23:35:45 1.16
@@ -63,6 +63,7 @@
import org.apache.turbine.services.intake.xmlmodel.XmlField;
import org.apache.turbine.services.intake.xmlmodel.XmlGroup;
import org.apache.turbine.services.intake.validator.Validator;
+import org.apache.turbine.services.intake.validator.InitableByConstraintMap;
import org.apache.turbine.services.intake.validator.ValidationException;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.ParameterParser;
@@ -74,7 +75,7 @@
* Base class for Intake generated input processing classes.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: Field.java,v 1.15 2001/06/11 19:41:34 jon Exp $
+ * @version $Id: Field.java,v 1.16 2001/06/18 23:35:45 jmcnally Exp $
*/
public abstract class Field
{
@@ -154,7 +155,13 @@
if ( className != null )
{
validator = (Validator)Class.forName(className).newInstance();
- validator.init(field.getRuleMap());
+ // this should always be true for now
+ // (until bean property initialization is implemented)
+ if ( validator instanceof InitableByConstraintMap )
+ {
+ ((InitableByConstraintMap)validator).init(field.getRuleMap());
+ }
+
}
// field may have been declared as always required in the xml spec
@@ -274,7 +281,7 @@
set_flag = true;
validate(pp);
}
-
+
initialized = true;
return this;
}
@@ -522,6 +529,10 @@
setMessage(ve.getMessage());
}
}
+ else if ( set_flag )
+ {
+ doSetValue(pp);
+ }
}
/*
@@ -690,7 +701,8 @@
catch ( Exception e)
{
throw new TurbineException("An exception prevented the" +
- " mapping to " + obj, e);
+ " setting property "+name+" of " + obj + " to " +
+ valArray[0], e);
}
}
}
1.4 +155 -38
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/DefaultValidator.java
Index: DefaultValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/DefaultValidator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DefaultValidator.java 2001/06/13 12:40:08 1.3
+++ DefaultValidator.java 2001/06/18 23:35:46 1.4
@@ -82,10 +82,10 @@
* This validator can serve as the base class for more specific validators
*
* @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
- * @version $Id: DefaultValidator.java,v 1.3 2001/06/13 12:40:08 henning Exp $
+ * @version $Id: DefaultValidator.java,v 1.4 2001/06/18 23:35:46 jmcnally Exp $
*/
public class DefaultValidator
- implements Validator
+ implements Validator, InitableByConstraintMap
{
protected boolean required;
protected String requiredMessage;
@@ -108,31 +108,8 @@
{
}
+
- /* to be removed
- int requires_index = 0;
- for (int i=0; i<field.getRules().size(); i++)
- {
- Rule rule = (Rule)field.getRules().get(i);
- if ( rule.getRequiresProp()!=null )
- {
- requires_index++;
- }
- }
- requires = new String[requires_index];
- requiresMessage = new String[requires_index];
- requires_index = 0;
-
-
- if ( field.getGroup()!=null && rule.getRequiresProp()!=null )
- {
- requires[requires_index] = rule.getRequiresProp();
- requiresMessage[requires_index] = rule.getMessage();
- requires_index++;
- }
- */
-
-
/**
* Extract the relevant parameters from the constraints listed
* in <rule> tags within the intake.xml file.
@@ -155,14 +132,7 @@
if ( constraint != null )
{
String param = constraint.getValue();
- try
- {
- mask = new RE(param);
- }
- catch (org.apache.regexp.RESyntaxException e)
- {
- throw new TurbineException(e);
- }
+ setMask(param);
maskMessage = constraint.getMessage();
}
@@ -230,7 +200,8 @@
{
message = null;
- if ( !required && ( testValue == null || testValue.length() == 0) )
+ if ( (!required && minLength == 0)
+ && ( testValue == null || testValue.length() == 0) )
{
return;
}
@@ -284,13 +255,159 @@
{
}
+ // ************************************************************
+ // ** Bean accessor methods **
+ // ************************************************************
+ /**
+ * Get the value of required.
+ * @return value of required.
+ */
+ public boolean isRequired()
+ {
+ return required;
+ }
+
+ /**
+ * Set the value of required.
+ * @param v Value to assign to required.
+ */
+ public void setRequired(boolean v)
+ {
+ this.required = v;
+ }
+
+ /**
+ * Get the value of requiredMessage.
+ * @return value of requiredMessage.
+ */
+ public String getRequiredMessage()
+ {
+ return requiredMessage;
+ }
+
+ /**
+ * Set the value of requiredMessage.
+ * @param v Value to assign to requiredMessage.
+ */
+ public void setRequiredMessage(String v)
+ {
+ this.requiredMessage = v;
+ }
+
+ /**
+ * Get the value of mask.
+ * @return value of mask.
+ */
+ public String getMask()
+ {
+ return mask.toString();
+ }
+
+ /**
+ * Set the value of mask.
+ * @param v Value to assign to mask.
+ */
+ public void setMask(String v)
+ throws TurbineException
+ {
+ try
+ {
+ mask = new RE(v);
+ }
+ catch (org.apache.regexp.RESyntaxException e)
+ {
+ throw new TurbineException(e);
+ }
+ }
+
+ /**
+ * Get the value of maskMessage.
+ * @return value of maskMessage.
+ */
+ public String getMaskMessage()
+ {
+ return maskMessage;
+ }
+
+ /**
+ * Set the value of maskMessage.
+ * @param v Value to assign to maskMessage.
+ */
+ public void setMaskMessage(String v)
+ {
+ this.maskMessage = v;
+ }
+
+ /**
+ * Get the value of minLength.
+ * @return value of minLength.
+ */
+ public int getMinLength()
+ {
+ return minLength;
+ }
+
/**
- * This value can be used to construct a form input field so
- * lets make it available
+ * Set the value of minLength.
+ * @param v Value to assign to minLength.
*/
- public int getMaxLength()
+ public void setMinLength(int v)
{
+ this.minLength = v;
+ }
+
+ /**
+ * Get the value of minLengthMessage.
+ * @return value of minLengthMessage.
+ */
+ public String getMinLengthMessage()
+ {
+ return minLengthMessage;
+ }
+
+ /**
+ * Set the value of minLengthMessage.
+ * @param v Value to assign to minLengthMessage.
+ */
+ public void setMinLengthMessage(String v)
+ {
+ this.minLengthMessage = v;
+ }
+
+ /**
+ * Get the value of maxLength.
+ * @return value of maxLength.
+ */
+ public int getMaxLength()
+ {
return maxLength;
+ }
+
+ /**
+ * Set the value of maxLength.
+ * @param v Value to assign to maxLength.
+ */
+ public void setMaxLength(int v)
+ {
+ this.maxLength = v;
+ }
+
+ /**
+ * Get the value of maxLengthMessage.
+ * @return value of maxLengthMessage.
+ */
+ public String getMaxLengthMessage()
+ {
+ return maxLengthMessage;
+ }
+
+ /**
+ * Set the value of maxLengthMessage.
+ * @param v Value to assign to maxLengthMessage.
+ */
+ public void setMaxLengthMessage(String v)
+ {
+ this.maxLengthMessage = v;
}
}
1.2 +47 -3
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/IntegerValidator.java
Index: IntegerValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/IntegerValidator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- IntegerValidator.java 2001/05/18 19:35:55 1.1
+++ IntegerValidator.java 2001/06/18 23:35:46 1.2
@@ -74,7 +74,7 @@
* </table>
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: IntegerValidator.java,v 1.1 2001/05/18 19:35:55 jmcnally Exp $
+ * @version $Id: IntegerValidator.java,v 1.2 2001/06/18 23:35:46 jmcnally Exp $
*/
public class IntegerValidator
extends NumberValidator
@@ -87,11 +87,14 @@
public IntegerValidator(Map paramMap)
throws TurbineException
{
+ this();
init(paramMap);
}
public IntegerValidator()
- {
+ {
+ // sets the default invalid number message
+ super();
}
protected void doInit(Map paramMap)
@@ -116,7 +119,7 @@
}
}
- protected String getInvalidNumberMessage()
+ protected String getDefaultInvalidNumberMessage()
{
return INVALID_NUMBER;
}
@@ -153,5 +156,46 @@
message = maxValueMessage;
throw new ValidationException(maxValueMessage);
}
+ }
+
+
+ // ************************************************************
+ // ** Bean accessor methods **
+ // ************************************************************
+
+ /**
+ * Get the value of minValue.
+ * @return value of minValue.
+ */
+ public int getMinValue()
+ {
+ return minValue;
+ }
+
+ /**
+ * Set the value of minValue.
+ * @param v Value to assign to minValue.
+ */
+ public void setMinValue(int v)
+ {
+ this.minValue = v;
+ }
+
+ /**
+ * Get the value of maxValue.
+ * @return value of maxValue.
+ */
+ public int getMaxValue()
+ {
+ return maxValue;
+ }
+
+ /**
+ * Set the value of maxValue.
+ * @param v Value to assign to maxValue.
+ */
+ public void setMaxValue(int v)
+ {
+ this.maxValue = v;
}
}
1.2 +48 -3
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/NumberKeyValidator.java
Index: NumberKeyValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/NumberKeyValidator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NumberKeyValidator.java 2001/05/18 19:35:55 1.1
+++ NumberKeyValidator.java 2001/06/18 23:35:46 1.2
@@ -54,6 +54,7 @@
* <http://www.apache.org/>.
*/
+import java.math.BigDecimal;
import java.util.Map;
import org.apache.turbine.om.NumberKey;
@@ -75,7 +76,7 @@
* </table>
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: NumberKeyValidator.java,v 1.1 2001/05/18 19:35:55 jmcnally Exp $
+ * @version $Id: NumberKeyValidator.java,v 1.2 2001/06/18 23:35:46 jmcnally Exp $
*/
public class NumberKeyValidator
extends NumberValidator
@@ -88,11 +89,14 @@
public NumberKeyValidator(Map paramMap)
throws TurbineException
{
+ this();
init(paramMap);
}
public NumberKeyValidator()
- {
+ {
+ // sets the default invalid number message
+ super();
}
protected void doInit(Map paramMap)
@@ -117,7 +121,7 @@
}
}
- protected String getInvalidNumberMessage()
+ protected String getDefaultInvalidNumberMessage()
{
return INVALID_NUMBER;
}
@@ -153,5 +157,46 @@
message = maxValueMessage;
throw new ValidationException(maxValueMessage);
}
+ }
+
+
+ // ************************************************************
+ // ** Bean accessor methods **
+ // ************************************************************
+
+ /**
+ * Get the value of minValue.
+ * @return value of minValue.
+ */
+ public NumberKey getMinValue()
+ {
+ return minValue;
+ }
+
+ /**
+ * Set the value of minValue.
+ * @param v Value to assign to minValue.
+ */
+ public void setMinValue(NumberKey v)
+ {
+ this.minValue = v;
+ }
+
+ /**
+ * Get the value of maxValue.
+ * @return value of maxValue.
+ */
+ public NumberKey getMaxValue()
+ {
+ return maxValue;
+ }
+
+ /**
+ * Set the value of maxValue.
+ * @param v Value to assign to maxValue.
+ */
+ public void setMaxValue(NumberKey v)
+ {
+ this.maxValue = v;
}
}
1.2 +98 -10
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/NumberValidator.java
Index: NumberValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/NumberValidator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NumberValidator.java 2001/05/18 19:35:55 1.1
+++ NumberValidator.java 2001/06/18 23:35:47 1.2
@@ -76,7 +76,7 @@
* </table>
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: NumberValidator.java,v 1.1 2001/05/18 19:35:55 jmcnally Exp $
+ * @version $Id: NumberValidator.java,v 1.2 2001/06/18 23:35:47 jmcnally Exp $
*/
public class NumberValidator
extends DefaultValidator
@@ -92,11 +92,13 @@
public NumberValidator(Map paramMap)
throws TurbineException
{
+ this();
init(paramMap);
}
public NumberValidator()
{
+ invalidNumberMessage = getDefaultInvalidNumberMessage();
}
/**
@@ -129,15 +131,7 @@
{
invalidNumberMessage = constraint.getMessage();
}
- else
- {
- invalidNumberMessage = getInvalidNumberMessage();
- }
}
- else
- {
- invalidNumberMessage = getInvalidNumberMessage();
- }
}
protected void doInit(Map paramMap)
@@ -162,7 +156,7 @@
}
}
- protected String getInvalidNumberMessage()
+ protected String getDefaultInvalidNumberMessage()
{
return INVALID_NUMBER;
}
@@ -201,4 +195,98 @@
}
}
+ // ************************************************************
+ // ** Bean accessor methods **
+ // ************************************************************
+
+ /**
+ * Get the value of minValue.
+ * @return value of minValue.
+ */
+ public BigDecimal getMinValueAsBigDecimal()
+ {
+ return minValue;
+ }
+
+ /**
+ * Set the value of minValue.
+ * @param v Value to assign to minValue.
+ */
+ public void setMinValue(BigDecimal v)
+ {
+ this.minValue = v;
+ }
+
+ /**
+ * Get the value of minValueMessage.
+ * @return value of minValueMessage.
+ */
+ public String getMinValueMessage()
+ {
+ return minValueMessage;
+ }
+
+ /**
+ * Set the value of minValueMessage.
+ * @param v Value to assign to minValueMessage.
+ */
+ public void setMinValueMessage(String v)
+ {
+ this.minValueMessage = v;
+ }
+
+ /**
+ * Get the value of maxValue.
+ * @return value of maxValue.
+ */
+ public BigDecimal getMaxValueAsBigDecimal()
+ {
+ return maxValue;
+ }
+
+ /**
+ * Set the value of maxValue.
+ * @param v Value to assign to maxValue.
+ */
+ public void setMaxValue(BigDecimal v)
+ {
+ this.maxValue = v;
+ }
+
+ /**
+ * Get the value of maxValueMessage.
+ * @return value of maxValueMessage.
+ */
+ public String getMaxValueMessage()
+ {
+ return maxValueMessage;
+ }
+
+ /**
+ * Set the value of maxValueMessage.
+ * @param v Value to assign to maxValueMessage.
+ */
+ public void setMaxValueMessage(String v)
+ {
+ this.maxValueMessage = v;
+ }
+
+ /**
+ * Get the value of invalidNumberMessage.
+ * @return value of invalidNumberMessage.
+ */
+ public String getInvalidNumberMessage()
+ {
+ return invalidNumberMessage;
+ }
+
+ /**
+ * Set the value of invalidNumberMessage.
+ * @param v Value to assign to invalidNumberMessage.
+ */
+ public void setInvalidNumberMessage(String v)
+ {
+ this.invalidNumberMessage = v;
+ }
+
}
1.2 +1 -15
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/Validator.java
Index: Validator.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/Validator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Validator.java 2001/05/18 19:35:55 1.1
+++ Validator.java 2001/06/18 23:35:47 1.2
@@ -76,23 +76,11 @@
* Validator api.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: Validator.java,v 1.1 2001/05/18 19:35:55 jmcnally Exp $
+ * @version $Id: Validator.java,v 1.2 2001/06/18 23:35:47 jmcnally Exp $
*/
public interface Validator
{
/**
- * Extract the relevant parameters from the constraints listed
- * in <input-param> tags within the intake.xml file.
- *
- * @param inputParameters a <code>Map</code> of <code>InputParam</code>'s
- * containing constraints on the input.
- * @exception TurbineException if an error occurs
- */
- public void init(Map inputParameters)
- throws TurbineException;
-
-
- /**
* Determine whether a testValue meets the criteria specified
* in the constraints defined for this validator
*
@@ -118,6 +106,4 @@
* @return a <code>String</code> message, or the empty String "".
*/
public String getMessage();
-
-
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/InitableByConstraintMap.java
Index: InitableByConstraintMap.java
===================================================================
package org.apache.turbine.services.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 [EMAIL PROTECTED]
*
* 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.lang.reflect.Method;
import java.util.Vector;
import org.apache.regexp.RE;
import org.apache.turbine.om.Retrievable;
import org.apache.turbine.services.intake.TurbineIntake;
import org.apache.turbine.services.intake.xmlmodel.Rule;
import org.apache.turbine.services.intake.xmlmodel.XmlField;
import org.apache.turbine.services.intake.xmlmodel.XmlGroup;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.ValueParser;
*/
import java.util.Map;
import org.apache.turbine.util.TurbineException;
/**
* This interface marks a bean/class that can have its properties set
* by values in a Map.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
* @version $Id: InitableByConstraintMap.java,v 1.1 2001/06/18 23:35:46 jmcnally Exp
$
*/
public interface InitableByConstraintMap
{
/**
* Extract the relevant parameters from the constraints listed
* in <input-param> tags within the intake.xml file.
*
* @param inputParameters a <code>Map</code> of <code>Constraint</code>'s
* containing rules and error messages.
* @exception TurbineException if an error occurs
*/
public void init(Map inputParameters)
throws TurbineException;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]