jmcnally 01/05/18 12:35:59
Modified: src/java/org/apache/turbine/services/intake/model
BigDecimalField.java BooleanField.java
ComboKeyField.java Field.java IntegerField.java
NumberKeyField.java StringField.java
src/java/org/apache/turbine/services/intake/xmlmodel
Rule.java XmlField.java
Added: src/java/org/apache/turbine/services/intake/validator
Constraint.java DefaultValidator.java
IntegerValidator.java NumberKeyValidator.java
NumberValidator.java ValidationException.java
Validator.java
Log:
refactored the validation in intake into classes that implement the Validator
interface and are initialized with Constraint objects which are
name/value pairs with an error message, if the constraint is violated.
A hook exists in the Fields where the validator may be specified in the
xml file.
Revision Changes Path
1.9 +14 -149
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/BigDecimalField.java
Index: BigDecimalField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/BigDecimalField.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- BigDecimalField.java 2001/05/05 15:26:57 1.8
+++ BigDecimalField.java 2001/05/18 19:35:41 1.9
@@ -64,63 +64,26 @@
/** */
public class BigDecimalField extends Field
{
- protected final BigDecimal onError;
-
- protected final BigDecimal minValue;
- protected final String minValueMessage;
- protected final boolean minValueForce;
- protected final BigDecimal maxValue;
- protected final String maxValueMessage;
- protected final boolean maxValueForce;
-
public BigDecimalField(XmlField field, Group group)
throws Exception
{
super(field, group);
-
- BigDecimal tmpOnError = null;
- if ( field.getOnError() != null )
- {
- tmpOnError = new BigDecimal(field.getOnError());
- }
- onError = tmpOnError;
-
- BigDecimal tmpMinValue = null;
- String tmpMinValueMessage = null;
- boolean tmpMinValueForce = false;
- BigDecimal tmpMaxValue = null;
- String tmpMaxValueMessage = null;
- boolean tmpMaxValueForce = false;
- for (int i=0; i<field.getRules().size(); i++)
- {
- Rule rule = (Rule)field.getRules().get(i);
-
- if ( rule.getMinValue() != null )
- {
- tmpMinValue = new BigDecimal(rule.getMinValue());
- tmpMinValueMessage = rule.getMessage();
- tmpMinValueForce = "force".equals(rule.getAction());
- }
-
- if ( rule.getMaxValue() != null )
- {
- tmpMaxValue = new BigDecimal(rule.getMaxValue());
- tmpMaxValueMessage = rule.getMessage();
- tmpMaxValueForce = "force".equals(rule.getAction());
- }
- }
- minValue = tmpMinValue;
- minValueMessage = tmpMinValueMessage;
- minValueForce = tmpMinValueForce;
- maxValue = tmpMaxValue;
- maxValueMessage = tmpMaxValueMessage;
- maxValueForce = tmpMaxValueForce;
}
+ /**
+ * A suitable validator.
+ *
+ * @return "NumberValidator"
+ */
+ protected String getDefaultValidator()
+ {
+ return "org.apache.turbine.services.intake.validator.NumberValidator";
+ }
+
/**
- * Compares request data with constraints and sets the valid flag.
+ * converts the parameter to the correct Object.
*/
- protected void doValidate(ParameterParser pp)
+ protected void doSetValue(ParameterParser pp)
{
if ( isMultiValued )
{
@@ -128,111 +91,13 @@
BigDecimal[] ival = new BigDecimal[ss.length];
for (int i=0; i<ss.length; i++)
{
- ival[i] = checkString(ss[i]);
+ ival[i] = new BigDecimal(ss[i]);
}
setTestValue(ival);
}
else
{
- setTestValue( checkString(pp.getString(getKey())) );
+ setTestValue( new BigDecimal(pp.getString(getKey())) );
}
}
-
- /**
- * Compares request data with constraints and sets the valid flag.
- */
- private BigDecimal checkString(String val)
- {
- BigDecimal ival = onError;
- try
- {
- ival = new BigDecimal(val);
- }
- catch (RuntimeException e)
- {
- valid_flag = false;
- ival = onError;
- }
-
- boolean b;
- if ( maxLength > 0 )
- {
- b = val.length() > maxLength;
- if ( maxLengthForce && b )
- {
- val = val.substring(0, maxLength);
- try
- {
- ival = new BigDecimal(val);
- }
- catch (NumberFormatException nfe)
- {
- valid_flag = false;
- ival = onError;
- }
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxLengthMessage != null && b)
- {
- message = maxLengthMessage;
- }
- }
-
- if ( minValue != null )
- {
- b = ival.compareTo(minValue) < 0;
- if ( minValueForce && b )
- {
- ival = minValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (minValueForce && b)
- {
- message = minValueMessage;
- }
- }
-
- if ( maxValue != null )
- {
- b = ival.compareTo(maxValue) > 0;
- if ( maxValueForce && b )
- {
- ival = maxValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxValueForce && b)
- {
- message = maxValueMessage;
- }
- }
- return ival;
- }
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1.6 +13 -3
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/BooleanField.java
Index: BooleanField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/BooleanField.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BooleanField.java 2001/05/05 15:26:57 1.5
+++ BooleanField.java 2001/05/18 19:35:42 1.6
@@ -63,7 +63,7 @@
* Base class for Intake generated input processing classes.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: BooleanField.java,v 1.5 2001/05/05 15:26:57 jvanzyl Exp $
+ * @version $Id: BooleanField.java,v 1.6 2001/05/18 19:35:42 jmcnally Exp $
*/
public class BooleanField extends Field
{
@@ -74,9 +74,19 @@
}
/**
- * Compares request data with constraints and sets the valid flag.
+ * A suitable validator.
+ *
+ * @return null
*/
- protected void doValidate(ParameterParser pp)
+ protected String getDefaultValidator()
+ {
+ return null;
+ }
+
+ /**
+ * converts the parameter to the correct Object.
+ */
+ protected void doSetValue(ParameterParser pp)
{
setTestValue( pp.getBool(getKey()) );
}
1.4 +14 -142
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/ComboKeyField.java
Index: ComboKeyField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/ComboKeyField.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ComboKeyField.java 2001/05/05 15:26:57 1.3
+++ ComboKeyField.java 2001/05/18 19:35:42 1.4
@@ -61,172 +61,44 @@
import org.apache.turbine.services.intake.xmlmodel.XmlField;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.ValueParser;
+import org.apache.turbine.util.TurbineException;
+import org.apache.turbine.util.Log;
/** */
public class ComboKeyField extends Field
{
- protected final ComboKey minValue;
- protected final String minValueMessage;
- protected final boolean minValueForce;
- protected final ComboKey maxValue;
- protected final String maxValueMessage;
- protected final boolean maxValueForce;
-
public ComboKeyField(XmlField field, Group group)
throws Exception
{
super(field, group);
-
- onError = new ComboKey();
-
- ComboKey tmpMinValue = null;
- String tmpMinValueMessage = null;
- boolean tmpMinValueForce = false;
- ComboKey tmpMaxValue = null;
- String tmpMaxValueMessage = null;
- boolean tmpMaxValueForce = false;
- for (int i=0; i<field.getRules().size(); i++)
- {
- Rule rule = (Rule)field.getRules().get(i);
-
- if ( rule.getMinValue() != null )
- {
- tmpMinValue = new ComboKey(rule.getMinValue());
- tmpMinValueMessage = rule.getMessage();
- tmpMinValueForce = "force".equals(rule.getAction());
- }
-
- if ( rule.getMaxValue() != null )
- {
- tmpMaxValue = new ComboKey(rule.getMaxValue());
- tmpMaxValueMessage = rule.getMessage();
- tmpMaxValueForce = "force".equals(rule.getAction());
- }
- }
- minValue = tmpMinValue;
- minValueMessage = tmpMinValueMessage;
- minValueForce = tmpMinValueForce;
- maxValue = tmpMaxValue;
- maxValueMessage = tmpMaxValueMessage;
- maxValueForce = tmpMaxValueForce;
}
- /**
- * Compares request data with constraints and sets the valid flag.
- */
- protected void doValidate(ParameterParser pp)
- {
- if ( isMultiValued )
- {
- String[] ss = pp.getStrings(getKey());
- ComboKey[] ival = new ComboKey[ss.length];
- for (int i=0; i<ss.length; i++)
- {
- ival[i] = checkString(ss[i]);
- }
- setTestValue(ival);
- }
- else
- {
- setTestValue( checkString(pp.getString(getKey())) );
- }
- }
-
/**
- * Compares request data with constraints and sets the valid flag.
+ * converts the parameter to the correct Object.
*/
- private ComboKey checkString(String val)
+ protected void doSetValue(ParameterParser pp)
{
- ComboKey ival = (ComboKey)onError;
try
{
- ival = new ComboKey(val);
- }
- catch (Exception e)
- {
- valid_flag = false;
- ival = (ComboKey)onError;
- }
-
- boolean b;
- if ( maxLength > 0 )
- {
- b = val.length() > maxLength;
- if ( maxLengthForce && b )
+ if ( isMultiValued )
{
- val = val.substring(0, maxLength);
- try
- {
- ival = new ComboKey(val);
- }
- catch (Exception e)
+ String[] ss = pp.getStrings(getKey());
+ ComboKey[] ival = new ComboKey[ss.length];
+ for (int i=0; i<ss.length; i++)
{
- valid_flag = false;
- ival = (ComboKey)onError;
+ ival[i] = new ComboKey(ss[i]);
}
+ setTestValue(ival);
}
else
- {
- valid_flag &= !b;
- }
- if (maxLengthMessage != null && b)
{
- message = maxLengthMessage;
+ setTestValue( new ComboKey(pp.getString(getKey())) );
}
}
-
- if ( minValue != null )
+ catch (TurbineException e)
{
- b = ival.compareTo(minValue) < 0;
- if ( minValueForce && b )
- {
- ival = minValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (minValueForce && b)
- {
- message = minValueMessage;
- }
- }
-
- if ( maxValue != null )
- {
- b = ival.compareTo(maxValue) > 0;
- if ( maxValueForce && b )
- {
- ival = maxValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxValueForce && b)
- {
- message = maxValueMessage;
- }
+ valid_flag = false;
+ Log.error(e);
}
- return ival;
}
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1.12 +96 -63
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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Field.java 2001/05/05 15:26:57 1.11
+++ Field.java 2001/05/18 19:35:43 1.12
@@ -54,14 +54,16 @@
* <http://www.apache.org/>.
*/
+import java.util.Map;
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.services.intake.validator.Validator;
+import org.apache.turbine.services.intake.validator.ValidationException;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.RunData;
@@ -72,18 +74,27 @@
* Base class for Intake generated input processing classes.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: Field.java,v 1.11 2001/05/05 15:26:57 jvanzyl Exp $
+ * @version $Id: Field.java,v 1.12 2001/05/18 19:35:43 jmcnally Exp $
*/
public abstract class Field
{
private static final String EMPTY = "";
private static final String VALUE_IF_ABSENT_KEY = "_vifa_";
+
+ // the following are set from the xml file and are permanent (final)
protected final String name;
protected final String key;
protected final String mapToObject;
+ protected Validator validator;
protected final Method getter;
protected final Method setter;
+ protected final String ifRequiredMessage;
+ protected final boolean isMultiValued;
+ protected final Group group;
+ protected boolean alwaysRequired;
+ protected Object onError;
+ /* to be removed
protected final RE regexp;
protected final String regexpMessage;
protected final int minLength;
@@ -92,12 +103,10 @@
protected final String maxLengthMessage;
protected final boolean maxLengthForce;
protected final String[] requires;
- protected final String[] requiresMessage;
- protected final String ifRequiredMessage;
- protected final boolean isMultiValued;
- protected Object onError;
+ protected final String[] requiresMessage;
+ */
+
- protected final Group group;
protected boolean set_flag;
protected boolean valid_flag;
@@ -109,7 +118,7 @@
private Object validValue;
private Object testValue;
private String propertyName;
- private Object[] valArray;
+ private Object[] valArray; // for reflection
/** The object containing the request data */
protected RunData data;
@@ -129,7 +138,46 @@
key = field.getKey();
name = field.getName();
isMultiValued = field.isMultiValued();
+ String className = field.getValidator();
+ if ( className == null && field.getRules().size() > 0 )
+ {
+ className = getDefaultValidator();
+ }
+ else if ( className != null && className.indexOf('.') == -1 )
+ {
+ className = "org.apache.turbine.services.intake.validator."
+ + className;
+ }
+
+ if ( className != null )
+ {
+ validator = (Validator)Class.forName(className).newInstance();
+ validator.init(field.getRuleMap());
+ }
+ // field may have been declared as always required in the xml spec
+ String required = (String)field.getRuleMap().get("required");
+ if ( required != null )
+ {
+ alwaysRequired = new Boolean(required).booleanValue();
+ }
+
+ mapToObject = field.getMapToObject();
+ String propName = field.getMapToProperty();
+ Method tmpGetter = null;
+ Method tmpSetter = null;
+ if ( mapToObject != null && mapToObject.length() != 0)
+ {
+ tmpGetter = TurbineIntake.getFieldGetter(mapToObject, propName);
+ tmpSetter = TurbineIntake.getFieldSetter(mapToObject, propName);
+ }
+ getter = tmpGetter;
+ setter = tmpSetter;
+ ifRequiredMessage = field.getIfRequiredMessage();
+
+ valArray = new Object[1];
+
+ /* to be removed
int requires_index = 0;
for (int i=0; i<field.getRules().size(); i++)
{
@@ -187,24 +235,11 @@
maxLength = tmpMaxLength;
maxLengthMessage = tmpMaxLengthMessage;
maxLengthForce = tmpMaxLengthForce;
+ */
- mapToObject = field.getMapToObject();
- String propName = field.getMapToProperty();
- Method tmpGetter = null;
- Method tmpSetter = null;
- if ( mapToObject != null && mapToObject.length() != 0)
- {
- tmpGetter = TurbineIntake.getFieldGetter(mapToObject, propName);
- tmpSetter = TurbineIntake.getFieldSetter(mapToObject, propName);
- }
- getter = tmpGetter;
- setter = tmpSetter;
- ifRequiredMessage = field.getIfRequiredMessage();
-
- valArray = new Object[1];
}
-
+
/**
* Method called when this field (the group it belongs to) is
* pulled from the pool. The request data is searched to determine
@@ -261,14 +296,19 @@
return this;
}
+
+ protected String getDefaultValidator()
+ {
+ return "org.apache.turbine.services.intake.validator.DefaultValidator";
+ }
/**
- * Flag to determine whether the field is required to be set.
+ * Flag to determine whether the field has been declared as required.
* @return value of required.
*/
public boolean isRequired()
{
- return required;
+ return alwaysRequired || required;
}
/**
@@ -401,7 +441,7 @@
* Compares request data with constraints and sets the valid flag.
*/
protected boolean validate(ParameterParser pp)
- throws TurbineException
+ // throws TurbineException
{
if ( isMultiValued )
{
@@ -411,13 +451,20 @@
if ( ss.length == 0 || (ss.length == 1 && ss[0].length() == 0) )
{
set_flag = false;
- return true;
}
- else
- {
- for (int i=0; i<ss.length; i++)
+
+ for (int i=0; i<ss.length; i++)
+ {
+ if ( validator != null )
{
- checkString(ss[i]);
+ try
+ {
+ validator.assertValidity(ss[i]);
+ }
+ catch (ValidationException ve)
+ {
+ setMessage(ve.getMessage());
+ }
}
}
}
@@ -427,14 +474,22 @@
if ( s.length() == 0 )
{
set_flag = false;
- return true;
}
- else
+
+ if ( validator != null )
{
- checkString(s);
+ try
+ {
+ validator.assertValidity(s);
+ }
+ catch (ValidationException ve)
+ {
+ setMessage(ve.getMessage());
+ }
}
}
+ /*
if ( group != null && requires.length > 0 )
{
for (int i=0; i<requires.length; i++)
@@ -448,8 +503,13 @@
}
}
}
+ */
- doValidate(pp);
+ if ( set_flag )
+ {
+ doSetValue(pp);
+ }
+
return valid_flag;
}
@@ -457,35 +517,8 @@
* Compares request data with constraints and sets the valid flag.
* To be implemented in subclasses
*/
- protected abstract void doValidate(ParameterParser pp);
+ protected abstract void doSetValue(ParameterParser pp);
- /**
- * Compares request data with constraints and sets the valid flag.
- */
- private void checkString(String val)
- {
- boolean b;
- if ( regexp != null )
- {
- b = regexp.match(val);
- valid_flag &= b;
- if (regexpMessage != null && !b)
- {
- message = regexpMessage;
- }
- }
-
- if ( minLength > 0 )
- {
- b = val.length() < minLength;
- valid_flag &= !b;
- if (minLengthMessage != null && b)
- {
- message = minLengthMessage;
- }
- }
-
- }
/**
1.7 +14 -123
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/IntegerField.java
Index: IntegerField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/IntegerField.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- IntegerField.java 2001/05/05 15:26:58 1.6
+++ IntegerField.java 2001/05/18 19:35:44 1.7
@@ -63,57 +63,27 @@
/** */
public class IntegerField extends Field
{
- protected final int onError;
- protected final int minValue;
- protected final String minValueMessage;
- protected final boolean minValueForce;
- protected final int maxValue;
- protected final String maxValueMessage;
- protected final boolean maxValueForce;
-
public IntegerField(XmlField field, Group group)
throws Exception
{
super(field, group);
- onError = Integer.parseInt(field.getOnError());
-
- int tmpMinValue = Integer.MIN_VALUE;
- String tmpMinValueMessage = null;
- boolean tmpMinValueForce = false;
- int tmpMaxValue = Integer.MAX_VALUE;
- String tmpMaxValueMessage = null;
- boolean tmpMaxValueForce = false;
- for (int i=0; i<field.getRules().size(); i++)
- {
- Rule rule = (Rule)field.getRules().get(i);
-
- if ( rule.getMinValue() != null )
- {
- tmpMinValue = Integer.parseInt(rule.getMinValue());
- tmpMinValueMessage = rule.getMessage();
- tmpMinValueForce = "force".equals(rule.getAction());
- }
-
- if ( rule.getMaxValue() != null )
- {
- tmpMaxValue = Integer.parseInt(rule.getMaxValue());
- tmpMaxValueMessage = rule.getMessage();
- tmpMaxValueForce = "force".equals(rule.getAction());
- }
- }
- minValue = tmpMinValue;
- minValueMessage = tmpMinValueMessage;
- minValueForce = tmpMinValueForce;
- maxValue = tmpMaxValue;
- maxValueMessage = tmpMaxValueMessage;
- maxValueForce = tmpMaxValueForce;
}
+ /**
+ * A suitable validator.
+ *
+ * @return "IntegerValidator"
+ */
+ protected String getDefaultValidator()
+ {
+ return "org.apache.turbine.services.intake.validator.IntegerValidator";
+ }
+
/**
- * Compares request data with constraints and sets the valid flag.
+ * converts the parameter to the correct Object.
*/
- protected void doValidate(ParameterParser pp)
+ protected void doSetValue(ParameterParser pp)
{
if ( isMultiValued )
{
@@ -121,93 +91,14 @@
int[] ival = new int[ss.length];
for (int i=0; i<ss.length; i++)
{
- ival[i] = checkString(ss[i]);
+ ival[i] = Integer.parseInt(ss[i]);
}
setTestValue(ival);
}
else
- {
- setTestValue(new Integer(checkString(pp.getString(getKey()))));
- }
- }
-
- /**
- * Compares request data with constraints and sets the valid flag.
- */
- private int checkString(String val)
- {
- int ival = onError;
- try
- {
- ival = Integer.parseInt(val);
- }
- catch (Exception e)
{
- valid_flag = false;
- ival = onError;
- }
-
- boolean b;
- if ( maxLength > 0 )
- {
- b = val.length() > maxLength;
- if ( maxLengthForce && b )
- {
- val = val.substring(0, maxLength);
- try
- {
- ival = Integer.parseInt(val);
- }
- catch (NumberFormatException nfe)
- {
- valid_flag = false;
- ival = onError;
- }
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxLengthMessage != null && b)
- {
- message = maxLengthMessage;
- }
- }
-
- if ( minValue != Integer.MIN_VALUE )
- {
- b = ival < minValue;
- if ( minValueForce && b )
- {
- ival = minValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (minValueForce && b)
- {
- message = minValueMessage;
- }
- }
-
- if ( maxValue != Integer.MAX_VALUE )
- {
- b = ival > maxValue;
- if ( maxValueForce && b )
- {
- ival = maxValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxValueForce && b)
- {
- message = maxValueMessage;
- }
+ setTestValue(new Integer(pp.getString(getKey())));
}
- return ival;
}
}
1.5 +14 -121
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/NumberKeyField.java
Index: NumberKeyField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/NumberKeyField.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- NumberKeyField.java 2001/05/05 15:26:58 1.4
+++ NumberKeyField.java 2001/05/18 19:35:44 1.5
@@ -65,58 +65,29 @@
/** */
public class NumberKeyField extends Field
{
- // protected final NumberKey onError;
-
- protected final NumberKey minValue;
- protected final String minValueMessage;
- protected final boolean minValueForce;
- protected final NumberKey maxValue;
- protected final String maxValueMessage;
- protected final boolean maxValueForce;
-
public NumberKeyField(XmlField field, Group group)
throws Exception
{
super(field, group);
-
- onError = new NumberKey();
- NumberKey tmpMinValue = null;
- String tmpMinValueMessage = null;
- boolean tmpMinValueForce = false;
- NumberKey tmpMaxValue = null;
- String tmpMaxValueMessage = null;
- boolean tmpMaxValueForce = false;
- for (int i=0; i<field.getRules().size(); i++)
- {
- Rule rule = (Rule)field.getRules().get(i);
+ }
- if ( rule.getMinValue() != null )
- {
- tmpMinValue = new NumberKey(rule.getMinValue());
- tmpMinValueMessage = rule.getMessage();
- tmpMinValueForce = "force".equals(rule.getAction());
- }
- if ( rule.getMaxValue() != null )
- {
- tmpMaxValue = new NumberKey(rule.getMaxValue());
- tmpMaxValueMessage = rule.getMessage();
- tmpMaxValueForce = "force".equals(rule.getAction());
- }
- }
- minValue = tmpMinValue;
- minValueMessage = tmpMinValueMessage;
- minValueForce = tmpMinValueForce;
- maxValue = tmpMaxValue;
- maxValueMessage = tmpMaxValueMessage;
- maxValueForce = tmpMaxValueForce;
+ /**
+ * A suitable validator.
+ *
+ * @return "NumberKeyValidator"
+ */
+ protected String getDefaultValidator()
+ {
+ return
+ "org.apache.turbine.services.intake.validator.NumberKeyValidator";
}
/**
- * Compares request data with constraints and sets the valid flag.
+ * converts the parameter to the correct Object.
*/
- protected void doValidate(ParameterParser pp)
+ protected void doSetValue(ParameterParser pp)
{
if ( isMultiValued )
{
@@ -124,94 +95,16 @@
NumberKey[] ival = new NumberKey[ss.length];
for (int i=0; i<ss.length; i++)
{
- ival[i] = checkString(ss[i]);
+ ival[i] = new NumberKey(ss[i]);
}
setTestValue(ival);
}
else
{
- setTestValue( checkString(pp.getString(getKey())) );
+ setTestValue( new NumberKey(pp.getString(getKey())) );
}
}
- /**
- * Compares request data with constraints and sets the valid flag.
- */
- private NumberKey checkString(String val)
- {
- NumberKey ival = (NumberKey)onError;
- try
- {
- ival = new NumberKey(val);
- }
- catch (RuntimeException e)
- {
- valid_flag = false;
- ival = (NumberKey)onError;
- }
-
- boolean b;
- if ( maxLength > 0 )
- {
- b = val.length() > maxLength;
- if ( maxLengthForce && b )
- {
- val = val.substring(0, maxLength);
- try
- {
- ival = new NumberKey(val);
- }
- catch (RuntimeException e)
- {
- valid_flag = false;
- ival = (NumberKey)onError;
- }
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxLengthMessage != null && b)
- {
- message = maxLengthMessage;
- }
- }
-
- if ( minValue != null )
- {
- b = ival.compareTo(minValue) < 0;
- if ( minValueForce && b )
- {
- ival = minValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (minValueForce && b)
- {
- message = minValueMessage;
- }
- }
-
- if ( maxValue != null )
- {
- b = ival.compareTo(maxValue) > 0;
- if ( maxValueForce && b )
- {
- ival = maxValue;
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxValueForce && b)
- {
- message = maxValueMessage;
- }
- }
- return ival;
- }
}
1.7 +5 -36
jakarta-turbine/src/java/org/apache/turbine/services/intake/model/StringField.java
Index: StringField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/StringField.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- StringField.java 2001/05/05 15:26:58 1.6
+++ StringField.java 2001/05/18 19:35:45 1.7
@@ -62,7 +62,7 @@
* Base class for Intake generated input processing classes.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: StringField.java,v 1.6 2001/05/05 15:26:58 jvanzyl Exp $
+ * @version $Id: StringField.java,v 1.7 2001/05/18 19:35:45 jmcnally Exp $
*/
public class StringField extends Field
{
@@ -73,23 +73,18 @@
}
/**
- * Compares request data with constraints and sets the valid flag.
+ * converts the parameter to the correct Object.
*/
- protected void doValidate(ParameterParser pp)
+ protected void doSetValue(ParameterParser pp)
{
if ( isMultiValued )
{
String[] ss = pp.getStrings(getKey());
- String[] sval = new String[ss.length];
- for (int i=0; i<ss.length; i++)
- {
- sval[i] = checkString(ss[i]);
- }
- setTestValue(sval);
+ setTestValue(ss);
}
else
{
- setTestValue(checkString(pp.getString(getKey())));
+ setTestValue(pp.getString(getKey()));
}
}
@@ -106,32 +101,6 @@
this.message = message;
}
}
-
- /**
- * Compares request data with constraints and sets the valid flag.
- */
- private String checkString(String val)
- {
- boolean b;
- if ( maxLength > 0 )
- {
- b = val.length() > maxLength;
- if ( maxLengthForce && b )
- {
- val = val.substring(0, maxLength);
- }
- else
- {
- valid_flag &= !b;
- }
- if (maxLengthMessage != null && b)
- {
- message = maxLengthMessage;
- }
- }
- return val;
- }
-
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/Constraint.java
Index: Constraint.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/>.
*/
/**
* A constraint has a name and a value and an optional message.
* The name/value pair will have meaning to a Validator and the
* message will serve as an error message in the event the Validator
* determines the constraint is violated.
* example:
* name="maxLength"
* value="255"
* message="Value cannot be longer than 255 characters."
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
* @version $Id: Constraint.java,v 1.1 2001/05/18 19:35:54 jmcnally Exp $
*/
public interface Constraint
{
/**
* Get the name of the constraint.
*/
public String getName();
/**
* Get the value of the constraint.
*/
public String getValue();
/**
* Get the error message.
*/
public String getMessage();
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/DefaultValidator.java
Index: DefaultValidator.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.Map;
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.TurbineException;
import org.apache.turbine.util.ValueParser;
/**
* A validator that will compare a testValue against the following
* constraints:
* <table>
* <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
* <tr><td>required</td><td>true|false</td><td>false</td></tr>
* <tr><td>mask</td><td>regexp</td><td> </td></tr>
* <tr><td>minLength</td><td>integer</td><td>0</td></tr>
* <tr><td>maxLength</td><td>integer</td><td> </td></tr>
* </table>
*
* 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.1 2001/05/18 19:35:54 jmcnally Exp $
*/
public class DefaultValidator
implements Validator
{
protected boolean required;
protected String requiredMessage;
protected RE mask;
protected String maskMessage;
protected int minLength;
protected String minLengthMessage;
protected int maxLength;
protected String maxLengthMessage;
protected String message;
public DefaultValidator(Map paramMap)
throws TurbineException
{
init(paramMap);
}
public DefaultValidator()
{
}
/* 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.
*
* @param paramMap a <code>Map</code> of <code>Rule</code>'s
* containing constraints on the input.
* @exception TurbineException if an error occurs
*/
public void init(Map paramMap)
throws TurbineException
{
mask = null;
maskMessage = null;
minLength = 0;
minLengthMessage = null;
maxLength = 0;
maxLengthMessage = null;
Constraint constraint = (Constraint)paramMap.get("mask");
if ( constraint != null )
{
String param = constraint.getValue();
try
{
mask = new RE(param);
}
catch (org.apache.regexp.RESyntaxException e)
{
throw new TurbineException(e);
}
maskMessage = constraint.getMessage();
}
constraint = (Constraint)paramMap.get("minLength");
if ( constraint != null )
{
String param = constraint.getValue();
minLength = Integer.parseInt(param);
minLengthMessage = constraint.getMessage();
}
constraint = (Constraint)paramMap.get("maxLength");
if ( constraint != null )
{
String param = constraint.getValue();
maxLength = Integer.parseInt(param);
maxLengthMessage = constraint.getMessage();
}
constraint = (Constraint)paramMap.get("required");
if ( constraint == null )
{
required = false;
}
else
{
String param = constraint.getValue();
required = new Boolean(param).booleanValue();
requiredMessage = constraint.getMessage();
}
}
/**
* Determine whether a testValue meets the criteria specified
* in the constraints defined for this validator
*
* @param testValue a <code>String</code> to be tested
* @return true if valid, false otherwise
*/
public boolean isValid(String testValue)
{
boolean valid = false;
try
{
assertValidity(testValue);
valid = true;
}
catch (ValidationException ve)
{
valid = false;
}
return valid;
}
/**
* 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.
*/
public void assertValidity(String testValue)
throws ValidationException
{
message = null;
if ( !required && ( testValue == null || testValue.length() == 0) )
{
return;
}
else if ( required
&& ( testValue == null || testValue.length() == 0))
{
message = requiredMessage;
throw new ValidationException(requiredMessage);
}
// allow subclasses first chance at validation
doAssertValidity(testValue);
if ( mask != null && !mask.match(testValue) )
{
message = maskMessage;
throw new ValidationException(maskMessage);
}
if ( minLength > 0 && testValue.length() < minLength )
{
message = minLengthMessage;
throw new ValidationException(minLengthMessage);
}
if ( maxLength > 0 && testValue.length() > maxLength )
{
message = maxLengthMessage;
throw new ValidationException(maxLengthMessage);
}
}
/**
* Get the last error message resulting from invalid input.
*
* @return a <code>String</code> message, or the empty String "".
*/
public String getMessage()
{
if ( message == null )
{
return "";
}
return message;
}
/**
* Method to allow subclasses to add additional validation
*/
protected void doAssertValidity(String testValue)
throws ValidationException
{
}
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/IntegerValidator.java
Index: IntegerValidator.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.util.Map;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;
/**
* 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>minLength</td><td>greater than Integer.MIN_VALUE</td>
* <td> </td></tr>
* <tr><td>maxLength</td><td>less than Integer.MAX_VALUE</td>
* <td> </td></tr>
* <tr><td>notANumberMessage</td><td>Some text</td>
* <td>Entry was not a valid number</td></tr>
* </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 $
*/
public class IntegerValidator
extends NumberValidator
{
private static String INVALID_NUMBER = "Entry was not a valid integer";
private int minValue;
private int maxValue;
public IntegerValidator(Map paramMap)
throws TurbineException
{
init(paramMap);
}
public IntegerValidator()
{
}
protected void doInit(Map paramMap)
{
minValue = Integer.MIN_VALUE;
maxValue = Integer.MAX_VALUE;
Constraint constraint = (Constraint)paramMap.get("minValue");
if ( constraint != null )
{
String param = constraint.getValue();
minValue = Integer.parseInt(param);
minValueMessage = constraint.getMessage();
}
constraint = (Constraint)paramMap.get("maxValue");
if ( constraint != null )
{
String param = constraint.getValue();
maxValue = Integer.parseInt(param);
maxValueMessage = constraint.getMessage();
}
}
protected String getInvalidNumberMessage()
{
return INVALID_NUMBER;
}
/**
* 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
{
int i = 0;
try
{
i = Integer.parseInt(testValue);
}
catch (RuntimeException e)
{
message = invalidNumberMessage;
throw new ValidationException(invalidNumberMessage);
}
if ( i < minValue )
{
message = minValueMessage;
throw new ValidationException(minValueMessage);
}
if ( i > maxValue )
{
message = maxValueMessage;
throw new ValidationException(maxValueMessage);
}
}
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/NumberKeyValidator.java
Index: NumberKeyValidator.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.util.Map;
import org.apache.turbine.om.NumberKey;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;
/**
* 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>minLength</td><td>greater than Integer.MIN_VALUE</td>
* <td> </td></tr>
* <tr><td>maxLength</td><td>less than BigDecimal.MAX_VALUE</td>
* <td> </td></tr>
* <tr><td>notANumberMessage</td><td>Some text</td>
* <td>Entry was not a valid number</td></tr>
* </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 $
*/
public class NumberKeyValidator
extends NumberValidator
{
private static String INVALID_NUMBER = "Entry was not valid.";
private NumberKey minValue;
private NumberKey maxValue;
public NumberKeyValidator(Map paramMap)
throws TurbineException
{
init(paramMap);
}
public NumberKeyValidator()
{
}
protected void doInit(Map paramMap)
{
minValue = null;
maxValue = null;
Constraint constraint = (Constraint)paramMap.get("minValue");
if ( constraint != null )
{
String param = constraint.getValue();
minValue = new NumberKey(param);
minValueMessage = constraint.getMessage();
}
constraint = (Constraint)paramMap.get("maxValue");
if ( constraint != null )
{
String param = constraint.getValue();
maxValue = new NumberKey(param);
maxValueMessage = constraint.getMessage();
}
}
protected String getInvalidNumberMessage()
{
return INVALID_NUMBER;
}
/**
* 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
{
NumberKey nk = null;
try
{
nk = new NumberKey(testValue);
}
catch (RuntimeException e)
{
message = invalidNumberMessage;
throw new ValidationException(invalidNumberMessage);
}
if ( minValue != null && nk.compareTo(minValue) < 0 )
{
message = minValueMessage;
throw new ValidationException(minValueMessage);
}
if ( maxValue != null && nk.compareTo(maxValue) > 0 )
{
message = maxValueMessage;
throw new ValidationException(maxValueMessage);
}
}
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/NumberValidator.java
Index: NumberValidator.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.math.BigDecimal;
import java.util.Map;
import org.apache.turbine.om.NumberKey;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;
/**
* 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>minLength</td><td>greater than BigDecimal.MIN_VALUE</td>
* <td> </td></tr>
* <tr><td>maxLength</td><td>less than BigDecimal.MAX_VALUE</td>
* <td> </td></tr>
* <tr><td>notANumberMessage</td><td>Some text</td>
* <td>Entry was not a valid number</td></tr>
* </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 $
*/
public class NumberValidator
extends DefaultValidator
{
private static String INVALID_NUMBER = "Entry was not a valid number";
private BigDecimal minValue;
protected String minValueMessage;
private BigDecimal maxValue;
protected String maxValueMessage;
protected String invalidNumberMessage;
public NumberValidator(Map paramMap)
throws TurbineException
{
init(paramMap);
}
public NumberValidator()
{
}
/**
* 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 paramMap)
throws TurbineException
{
super.init(paramMap);
minValueMessage = null;
maxValueMessage = null;
doInit(paramMap);
Constraint constraint = (Constraint)paramMap.get("notANumberMessage");
if ( constraint != null )
{
String param = constraint.getValue();
if ( param != null && param.length() != 0 )
{
invalidNumberMessage = param;
}
else if ( constraint.getMessage().length() != 0 )
{
invalidNumberMessage = constraint.getMessage();
}
else
{
invalidNumberMessage = getInvalidNumberMessage();
}
}
else
{
invalidNumberMessage = getInvalidNumberMessage();
}
}
protected void doInit(Map paramMap)
{
minValue = null;
maxValue = null;
Constraint constraint = (Constraint)paramMap.get("minValue");
if ( constraint != null )
{
String param = constraint.getValue();
minValue = new BigDecimal(param);
minValueMessage = constraint.getMessage();
}
constraint = (Constraint)paramMap.get("maxValue");
if ( constraint != null )
{
String param = constraint.getValue();
maxValue = new BigDecimal(param);
maxValueMessage = constraint.getMessage();
}
}
protected String getInvalidNumberMessage()
{
return INVALID_NUMBER;
}
/**
* 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
{
BigDecimal bd = null;
try
{
bd = new BigDecimal(testValue);
}
catch (RuntimeException e)
{
message = invalidNumberMessage;
throw new ValidationException(invalidNumberMessage);
}
if ( minValue != null && bd.compareTo(minValue) < 0 )
{
message = minValueMessage;
throw new ValidationException(minValueMessage);
}
if ( maxValue != null && bd.compareTo(maxValue) > 0 )
{
message = maxValueMessage;
throw new ValidationException(maxValueMessage);
}
}
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/ValidationException.java
Index: ValidationException.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/>.
*/
/**
* An Exception to mark a failed validation
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
* @version $Id: ValidationException.java,v 1.1 2001/05/18 19:35:55 jmcnally Exp $
*/
public class ValidationException
extends Exception
{
private String message;
/**
* Creates a new <code>ValidationException</code> instance.
*
* @param message describing the reason validation failed.
*/
public ValidationException(String message)
{
setMessage(message);
}
/**
* Get the value of message.
* @return value of message.
*/
public String getMessage()
{
return message;
}
/**
* Set the value of message.
* @param v Value to assign to message.
*/
public void setMessage(String v)
{
this.message = v;
}
}
1.1
jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/Validator.java
Index: Validator.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;
/**
* 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 $
*/
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
*
* @param testValue a <code>String</code> to be tested
* @return true if valid, false otherwise
*/
public boolean isValid(String testValue);
/**
* 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.
*/
public void assertValidity(String testValue)
throws ValidationException;
/**
* Get the last error message resulting from invalid input.
*
* @return a <code>String</code> message, or the empty String "".
*/
public String getMessage();
}
1.5 +30 -149
jakarta-turbine/src/java/org/apache/turbine/services/intake/xmlmodel/Rule.java
Index: Rule.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/xmlmodel/Rule.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Rule.java 2001/05/17 00:58:46 1.4
+++ Rule.java 2001/05/18 19:35:57 1.5
@@ -56,24 +56,20 @@
import org.apache.turbine.util.StringUtils;
import org.xml.sax.Attributes;
+import org.apache.turbine.services.intake.validator.Constraint;
/**
* A Class for holding data about a constraint on a property.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: Rule.java,v 1.4 2001/05/17 00:58:46 jmcnally Exp $
+ * @version $Id: Rule.java,v 1.5 2001/05/18 19:35:57 jmcnally Exp $
*/
public class Rule
- implements java.io.Serializable
+ implements Constraint, java.io.Serializable
{
- private String mask;
- private String minLength;
- private String maxLength;
- private String minValue;
- private String maxValue;
- private String requireProp;
+ private String name;
+ private String value;
private String message;
- private String action;
private XmlField parent;
/**
@@ -88,115 +84,41 @@
*/
public void loadFromXML (Attributes attrib)
{
- setMask(attrib.getValue("mask"));
- setMinLength(attrib.getValue("minLength"));
- setMaxLength(attrib.getValue("maxLength"));
- setMinValue(attrib.getValue("minValue"));
- setMaxValue(attrib.getValue("maxValue"));
- setRequiresProp(attrib.getValue("requires"));
- setAction(attrib.getValue("action"));
+ setName(attrib.getValue("name"));
+ setValue(attrib.getValue("value"));
}
-
- /**
- * Set the regexp used to verify parameter
- */
- public void setMask(String newMask)
- {
- mask = newMask;
- }
- /**
- * Get regexp used to verify a parameter
- */
- public String getMask()
- {
- return mask;
- }
-
/**
- * Set the name of the property
+ * Set the name of the parameter
*/
- public void setRequiresProp(String newRequireProp)
+ public void setName(String newName)
{
- requireProp = newRequireProp;
+ name = newName;
}
-
/**
- * Get the name of the property
+ * Get the name of the parameter
*/
- public String getRequiresProp()
+ public String getName()
{
- if ( requireProp == null )
- {
- return null;
- }
- else
- {
- return StringUtils.removeUnderScores(requireProp);
- }
+ return name;
}
/**
- * Set the minimum length of the parameter value
+ * Set the value of the parameter
*/
- public void setMinLength(String length)
+ public void setValue(String newValue)
{
- minLength = length;
+ value = newValue;
}
/**
- * Get minimum length of the parameter value
+ * Get the value of the parameter
*/
- public String getMinLength()
+ public String getValue()
{
- return minLength;
+ return value;
}
/**
- * Set the maximum length of the parameter value
- */
- public void setMaxLength(String length)
- {
- maxLength = length;
- }
- /**
- * Get maximum length of the parameter value
- */
- public String getMaxLength()
- {
- return maxLength;
- }
-
- /**
- * Set the minimum value of the parameter
- */
- public void setMinValue(String value)
- {
- minValue = value;
- }
- /**
- * Get minimum value of the parameter
- */
- public String getMinValue()
- {
- return minValue;
- }
-
- /**
- * Set the maximum value of the parameter
- */
- public void setMaxValue(String value)
- {
- maxValue = value;
- }
- /**
- * Get maximum value of the parameter
- */
- public String getMaxValue()
- {
- return maxValue;
- }
-
- /**
* Set the error message
*/
public void setMessage(String newMessage)
@@ -211,25 +133,6 @@
{
return message;
}
- /**
- * Set the action to take on failure
- */
- public void setAction(String newAction)
- {
- action = newAction;
- }
-
- /**
- * Get the action to take on failure
- */
- public String getAction()
- {
- if ( action == null)
- {
- action = "invalidate";
- }
- return action;
- }
/**
* Set the parent Field of the rule
@@ -253,45 +156,23 @@
*/
public String toString()
{
- StringBuffer result = new StringBuffer();
+ StringBuffer result = new StringBuffer(100);
- if (mask != null)
- {
- result.append(" mask=\""+mask+"\"");
- }
- if (minLength != null)
- {
- result.append(" minLength=\""+minLength+"\"");
- }
- if (maxLength != null)
- {
- result.append(" maxLength=\""+maxLength+"\"");
- }
- if (minValue != null)
- {
- result.append(" minValue=\""+minValue+"\"");
- }
- if (maxValue != null)
- {
- result.append(" maxValue=\""+maxValue+"\"");
- }
- if (requireProp != null)
- {
- result.append(" requires=\""+requireProp+"\"");
- }
- if (action != null)
- {
- result.append(" action=\""+action+"\"");
- }
+ result.append("<rule name=\""+name+"\"")
+ .append(" value=\""+value+"\"");
- result.append(">\n");
- if (message != null)
+ if (message == null)
+ {
+ result.append(" />\n");
+ }
+ else
{
- result.append(message);
+ result.append(">")
+ .append(message)
+ .append("</rule>\n");
}
- result.append("</rule>\n");
return result.toString();
}
1.10 +70 -22
jakarta-turbine/src/java/org/apache/turbine/services/intake/xmlmodel/XmlField.java
Index: XmlField.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/xmlmodel/XmlField.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- XmlField.java 2001/05/17 00:58:46 1.9
+++ XmlField.java 2001/05/18 19:35:58 1.10
@@ -54,9 +54,11 @@
* <http://www.apache.org/>.
*/
-import java.util.Enumeration;
import java.util.HashMap;
-import java.util.Vector;
+import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
import org.apache.turbine.util.StringUtils;
import org.xml.sax.Attributes;
@@ -64,11 +66,14 @@
* A Class for holding data about a property used in an Application.
*
* @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
- * @version $Id: XmlField.java,v 1.9 2001/05/17 00:58:46 jmcnally Exp $
+ * @version $Id: XmlField.java,v 1.10 2001/05/18 19:35:58 jmcnally Exp $
*/
public class XmlField
implements java.io.Serializable
{
+ private static final String DEFAULT_VALIDATOR =
+ "org.apache.turbine.services.intake.validatore.DefaultValidator";
+
private String baseClass;
private String name;
private String key;
@@ -76,10 +81,12 @@
private String onError;
private String multiValued;
private XmlGroup parent;
- private Vector rules;
+ private List rules;
+ private Map ruleMap;
private String ifRequiredMessage;
private String mapToObject;
private String mapToProperty;
+ private String validator;
private static HashMap defaultOnErrors;
private static HashMap convertHash;
@@ -133,12 +140,14 @@
convertArrayHash.put("BigDecimal[]", "new BigDecimal(stringValue[i])");
// convertHash.put("BigInteger", "new BigInteger(stringValue)");
}
+
/**
* Default Constructor
*/
public XmlField()
{
- rules = new Vector();
+ rules = new ArrayList();
+ ruleMap = new HashMap();
}
@@ -148,7 +157,8 @@
public XmlField(String name)
{
this.name = name;
- rules = new Vector();
+ rules = new ArrayList();
+ ruleMap = new HashMap();
}
/**
@@ -160,7 +170,7 @@
setName(attrib.getValue("name"));
key = attrib.getValue("key");
type = attrib.getValue("type");
- setOnError(attrib.getValue("onError"));
+ //setOnError(attrib.getValue("onError"));
setMultiValued(attrib.getValue("multiValued"));
String mapObj = attrib.getValue("mapToObject");
@@ -174,6 +184,7 @@
{
setMapToProperty(mapProp);
}
+ setValidator(attrib.getValue("validator"));
}
@@ -254,17 +265,17 @@
return baseClass;
}
- /**
+ /* *
* Set the value of the property, if a conversion error occurs.
- */
+ * /
public void setOnError(String newOnError)
{
onError = newOnError;
}
- /**
+ /* *
* Get the value of the property, if a conversion error occurs.
- */
+ * /
public String getOnError()
{
if ( onError == null && defaultOnErrors.containsKey(getType()) )
@@ -273,6 +284,7 @@
}
return onError;
}
+ */
/**
* Set whether this class can have multiple values
@@ -332,6 +344,22 @@
return mapToProperty;
}
}
+
+ /**
+ * Set the class name of the validator
+ */
+ public void setValidator(String prop)
+ {
+ validator = prop;
+ }
+
+ /**
+ * Get the className of the validator
+ */
+ public String getValidator()
+ {
+ return validator;
+ }
/**
* The name of the field making sure the first letter is lowercase.
@@ -403,7 +431,7 @@
}
/**
- * A utility function to create a new rule
+ * A utility function to create a new input parameter
* from attrib and add it to this property.
*/
public Rule addRule(Attributes attrib)
@@ -416,26 +444,38 @@
}
/**
- * Adds a new rule to the rules vector and set the
- * parent property of the rule to this property
+ * Adds a new rule to the parameter Map and set the
+ * parent property of the Rule to this property
*/
public void addRule(Rule rule)
{
rule.setField(this);
- rules.addElement(rule);
+ rules.add(rule);
+ ruleMap.put(rule.getName(), rule);
}
/**
* The collection of rules for this field.
*
- * @return a <code>Vector</code> value
+ * @return a <code>List</code> value
*/
- public Vector getRules()
+ public List getRules()
{
return rules;
}
/**
+ * The collection of rules for this field keyed by
+ * parameter name.
+ *
+ * @return a <code>Map</code> value
+ */
+ public Map getRuleMap()
+ {
+ return ruleMap;
+ }
+
+ /**
* String representation of the column. This
* is an xml representation.
*/
@@ -458,18 +498,26 @@
{
result.append(" mapToProperty=\""+mapToProperty+"\"");
}
+ if (validator != null)
+ {
+ result.append(" validator=\""+validator+"\"");
+ }
- result.append(">\n");
- if (rules != null)
+ if ( rules.size() == 0 )
+ {
+ result.append(" />\n");
+ }
+ else
{
- for (Enumeration e = rules.elements() ; e.hasMoreElements() ;)
+ result.append(">\n");
+ for (Iterator i = rules.iterator() ; i.hasNext() ;)
{
- result.append(e.nextElement());
+ result.append(i.next());
}
+ result.append("</field>\n");
}
- result.append("</field>\n");
return result.toString();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]