You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jm...@apache.org on 2001/06/24 20:06:28 UTC
cvs commit: jakarta-turbine/src/java/org/apache/turbine/services/intake/validator FileValidator.java
jmcnally 01/06/24 11:06:28
Modified: src/java/org/apache/turbine/services/intake/model Field.java
FieldFactory.java
Added: src/java/org/apache/turbine/services/intake/model
FileItemField.java
src/java/org/apache/turbine/services/intake/validator
FileValidator.java
Log:
added a field type and validator for file uploads.
bug fix for multivalued fields
Revision Changes Path
1.18 +10 -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.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- Field.java 2001/06/22 01:51:31 1.17
+++ Field.java 2001/06/24 18:06:27 1.18
@@ -75,7 +75,7 @@
* Base class for Intake generated input processing classes.
*
* @author <a href="mailto:jmcnally@collab.net>John McNally</a>
- * @version $Id: Field.java,v 1.17 2001/06/22 01:51:31 jmcnally Exp $
+ * @version $Id: Field.java,v 1.18 2001/06/24 18:06:27 jmcnally Exp $
*/
public abstract class Field
{
@@ -415,10 +415,10 @@
{
set_flag = false;
}
-
- for (int i=0; i<ss.length; i++)
+
+ if ( validator != null )
{
- if ( validator != null )
+ for (int i=0; i<ss.length; i++)
{
try
{
@@ -430,6 +430,12 @@
}
}
}
+
+ if ( set_flag && valid_flag )
+ {
+ doSetValue(pp);
+ }
+
}
else
{
1.7 +10 -1 jakarta-turbine/src/java/org/apache/turbine/services/intake/model/FieldFactory.java
Index: FieldFactory.java
===================================================================
RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/intake/model/FieldFactory.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- FieldFactory.java 2001/05/05 15:26:58 1.6
+++ FieldFactory.java 2001/06/24 18:06:27 1.7
@@ -63,7 +63,7 @@
* Creates Field objects.
*
* @author <a href="mailto:jmcnally@collab.net>John McNally</a>
- * @version $Id: FieldFactory.java,v 1.6 2001/05/05 15:26:58 jvanzyl Exp $
+ * @version $Id: FieldFactory.java,v 1.7 2001/06/24 18:06:27 jmcnally Exp $
*/
public abstract class FieldFactory
{
@@ -124,6 +124,15 @@
throws Exception
{
return new ComboKeyField(f, g);
+ }
+ }
+ );
+ fieldCtors.put("FileItem", new FieldFactory.FieldCtor()
+ {
+ public Field getInstance(XmlField f, Group g)
+ throws Exception
+ {
+ return new FileItemField(f, g);
}
}
);
1.1 jakarta-turbine/src/java/org/apache/turbine/services/intake/model/FileItemField.java
Index: FileItemField.java
===================================================================
package org.apache.turbine.services.intake.model;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.Vector;
import org.apache.turbine.services.intake.xmlmodel.Rule;
import org.apache.turbine.services.intake.xmlmodel.XmlField;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.services.intake.validator.Validator;
import org.apache.turbine.services.intake.validator.FileValidator;
// import org.apache.turbine.services.intake.validator.InitableByConstraintMap;
import org.apache.turbine.services.intake.validator.ValidationException;
import org.apache.turbine.services.intake.validator.Constraint;
import org.apache.turbine.util.upload.FileItem;
/** */
public class FileItemField extends Field
{
public FileItemField(XmlField field, Group group)
throws Exception
{
super(field, group);
}
/**
* A suitable validator.
*
* @return "FileValidator"
*/
protected String getDefaultValidator()
{
return "org.apache.turbine.services.intake.validator.FileValidator";
}
/**
* Method called when this field (the group it belongs to) is
* pulled from the pool. The request data is searched to determine
* if a value has been supplied for this field. if so, the value
* is validated.
*
* @param data a <code>RunData</code> value
* @return a <code>Field</code> value
* @exception TurbineException if an error occurs
*/
public Field init(RunData data)
throws TurbineException
{
this.data = data;
valid_flag = true;
ParameterParser pp = data.getParameters();
if ( pp.containsKey(getKey()) )
{
set_flag = true;
validate(pp);
}
initialized = true;
return this;
}
/**
* Compares request data with constraints and sets the valid flag.
*/
protected boolean validate(ParameterParser pp)
// throws TurbineException
{
if ( isMultiValued )
{
FileItem[] ss = pp.getFileItems(getKey());
// this definition of not set might need refined. But
// not sure the situation will arise.
if ( ss.length == 0 )
{
set_flag = false;
}
if ( validator != null )
{
for (int i=0; i<ss.length; i++)
{
try
{
((FileValidator)validator).assertValidity(ss[i]);
}
catch (ValidationException ve)
{
setMessage(ve.getMessage());
}
}
}
if ( set_flag && valid_flag )
{
doSetValue(pp);
}
}
else
{
FileItem s = pp.getFileItem(getKey());
if ( s.getSize() == 0 )
{
set_flag = false;
}
if ( validator != null )
{
try
{
((FileValidator)validator).assertValidity(s);
if ( set_flag )
{
doSetValue(pp);
}
}
catch (ValidationException ve)
{
setMessage(ve.getMessage());
}
}
else if ( set_flag )
{
doSetValue(pp);
}
}
return valid_flag;
}
/**
* converts the parameter to the correct Object.
*/
protected void doSetValue(ParameterParser pp)
{
if ( isMultiValued )
{
setTestValue(pp.getFileItems(getKey()));
}
else
{
setTestValue(pp.getFileItem(getKey()));
}
}
}
1.1 jakarta-turbine/src/java/org/apache/turbine/services/intake/validator/FileValidator.java
Index: FileValidator.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 apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.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.upload.FileItem;
/**
* 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:jmcnally@collab.net">John McNally</a>
* @version $Id: FileValidator.java,v 1.1 2001/06/24 18:06:28 jmcnally Exp $
*/
public class FileValidator
implements Validator, InitableByConstraintMap
{
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 FileValidator(Map paramMap)
throws TurbineException
{
init(paramMap);
}
public FileValidator()
{
}
/**
* 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
{
minLength = 0;
minLengthMessage = null;
maxLength = 0;
maxLengthMessage = null;
Constraint 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;
}
/**
* This method is not implemented, but is provided for the Validator
* interface.
*
* @param testValue a <code>String</code> to be tested
* @exception ValidationException always thrown.
*/
public void assertValidity(String testValue)
throws ValidationException
{
throw new ValidationException("this validation is not implemented");
}
/**
* 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(FileItem testValue)
throws ValidationException
{
message = null;
if ( (!required && minLength == 0)
&& ( testValue == null || testValue.getSize() == 0) )
{
return;
}
else if ( required
&& ( testValue == null || testValue.getSize() == 0))
{
message = requiredMessage;
throw new ValidationException(requiredMessage);
}
// allow subclasses first chance at validation
doAssertValidity(testValue);
if ( minLength > 0 && testValue.getSize() < minLength )
{
message = minLengthMessage;
throw new ValidationException(minLengthMessage);
}
if ( maxLength > 0 && testValue.getSize() > 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(FileItem testValue)
throws ValidationException
{
}
// ************************************************************
// ** 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 minLength.
* @return value of minLength.
*/
public int getMinLength()
{
return minLength;
}
/**
* Set the value of minLength.
* @param v Value to assign to minLength.
*/
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;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org