You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by tu...@apache.org on 2002/10/11 01:44:32 UTC
cvs commit: jakarta-commons/validator/src/example/org/apache/commons/validator ValidateBean.java ValidateExample.java applicationResources.properties validator-example.xml
turner 2002/10/10 16:44:32
Added: validator/src/example/org/apache/commons/validator
ValidateBean.java ValidateExample.java
applicationResources.properties
validator-example.xml
Log:
Revision Changes Path
1.1 jakarta-commons/validator/src/example/org/apache/commons/validator/ValidateBean.java
Index: ValidateBean.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/example/org/apache/commons/validator/ValidateBean.java,v 1.1 2002/10/10 23:44:32 turner Exp $
* $Revision: 1.1 $
* $Date: 2002/10/10 23:44:32 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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/>.
*
*/
package org.apache.commons.validator;
/**
* <p>A simple bean to use with the Validator Example.</p>
*
* @author James Turner
* @version $Revision: 1.1 $ $Date: 2002/10/10 23:44:32 $
*/
public class ValidateBean extends Object {
String lastName, firstName, street1, street2, city, state, postalCode, age;
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setStreet1 (String street1) {
this.street1 = street1;
}
public void setStreet2(String street2) {
this.street2 = street2;
}
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public void setAge (String age) {
this.age = age;
}
public String getLastName() {
return this.lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getStreet1 () {
return this.street1;
}
public String getStreet2() {
return this.street2;
}
public String getCity() {
return this.city;
}
public String getState() {
return this.state;
}
public String getPostalCode() {
return this.postalCode;
}
public String getAge () {
return this.age;
}
public String toString() {
return "{lastname=" + this.lastName + ", firstname=" + this.firstName +
", street1=" + this.street1 + ",\n street2=" + this.street2 + ", " +
"city=" + this.city + ", state=" + this.state +
",\n postalcode=" + this.postalCode + ", age=" + this.age + "}";
}
}
1.1 jakarta-commons/validator/src/example/org/apache/commons/validator/ValidateExample.java
Index: ValidateExample.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/example/org/apache/commons/validator/ValidateExample.java,v 1.1 2002/10/10 23:44:32 turner Exp $
* $Revision: 1.1 $
* $Date: 2002/10/10 23:44:32 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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/>.
*
*/
package org.apache.commons.validator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* <p>A simple example of setting up and using the Validator.</p>
*
* @author James Turner
* @version $Revision: 1.1 $ $Date: 2002/10/10 23:44:32 $
*
* This simple example shows all the steps needed to set up and use
* the Validator. Note that in most cases, some kind of framework
* would be wrapped around the Validator, such as is the case with
* the Struts Validator Framework. However, should you wish to use
* the Validator against raw Beans in a pure Java application, you
* can see everything you need to know to get it working here.
*
*/
public class ValidateExample extends Object {
/**
* We need a resource bundle to get our field names and errors messages from. Note that this is not strictly
* required to make the Validator work, but is a good coding practice.
*
*/
private static ResourceBundle apps = ResourceBundle.getBundle("org.apache.commons.validator.applicationResources");
/**
* This is the main method that will be called to initialize the Validator, create some sample beans, and
* run the Validator against them.
*
*/
public static void main (String[] args) {
InputStream in = null;
try {
// Create a new instance of a ValidatorResource, then get a stream
// handle on the XML file with the actions in it, and initialize the
// resources from it. This would normally be done by a servlet
// run during JSP initialization or some other application-startup
// routine.
ValidatorResources resources = new ValidatorResources();
in = ValidateExample.class.getResourceAsStream("validator-example.xml");
ValidatorResourcesInitializer.initialize(resources, in);
// Create a test bean to validate against.
ValidateBean bean = new ValidateBean();
// Create a validator with the ValidateBean actions for the bean
// we're interested in.
Validator validator = new Validator(resources, "ValidateBean");
// Tell the validator which bean to validate against.
validator.addResource(Validator.BEAN_KEY, bean);
ValidatorResults results = null;
// Run the validation actions against the bean. Since all of the properties
// are null, we expect them all to error out except for street2, which has
// no validations (it's an optional property)
results = validator.validate();
printResults(bean, results, resources);
// Now set all the required properties, but make the age a non-integer.
// You'll notice that age will pass the required test, but fail the int
// test.
bean.setLastName("Tester");
bean.setFirstName("John");
bean.setStreet1("1 Test Street");
bean.setCity("Testville");
bean.setState("TE");
bean.setPostalCode("12345");
bean.setAge("Too Old");
results = validator.validate();
printResults(bean, results, resources);
// Now everything should pass.
bean.setAge("123");
results = validator.validate();
printResults(bean, results, resources);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Make sure we close the input stream if it was left open.
if (in != null) {
try { in.close(); } catch (Exception e) {}
}
}
}
/**
*
* Method which dumps out the Bean in question and the results of validating it.
*
*/
public static void printResults(ValidateBean bean, ValidatorResults results, ValidatorResources resources) {
boolean success = true;
// Start by getting the form for the current locale and Bean.
Form form = resources.get(Locale.getDefault(), "ValidateBean");
System.out.println("\n\nValidating:");
System.out.println(bean);
// Iterate over each of the properties of the Bean which had messages.
Iterator propertyNames = results.get();
while (propertyNames.hasNext()) {
String propertyName = (String) propertyNames.next();
// Get the Field associated with that property in the Form
Field field = (Field) form.getFieldMap().get(propertyName);
// Look up the formatted name of the field from the Field arg0
String prettyFieldName = apps.getString(field.getArg0().getKey());
// Get the result of validating the property.
ValidatorResult result = results.getValidatorResult(propertyName);
// Get all the actions run against the property, and iterate over their names.
Map actionMap = result.getActionMap();
Iterator keys = actionMap.keySet().iterator();
while (keys.hasNext()) {
String actName = (String) keys.next();
// Get the Action for that name.
ValidatorAction action = resources.getValidatorAction(actName);
// If the result is valid, print PASSED, otherwise print FAILED
System.out.println(propertyName + "[" + actName + "] (" +
(result.isValid(actName)?"PASSED":"FAILED") + ")");
//If the result failed, format the Action's message against the formatted field name
if (!result.isValid(actName)) {
success = false;
String message = apps.getString(action.getMsg());
Object[] args = { prettyFieldName };
System.out.println(" Error message will be: " + MessageFormat.format(message, args));
}
}
}
if (success) {
System.out.println("FORM VALIDATION PASSED");
} else {
System.out.println("FORM VALIDATION FAILED");
}
}
}
1.1 jakarta-commons/validator/src/example/org/apache/commons/validator/applicationResources.properties
Index: applicationResources.properties
===================================================================
# The error messages for the Validation Actions
errors.required=The {0} field is required.
errors.int=The {0} field is not an integer.
# The formatted names of the properties
nameForm.age.displayname=Age
nameForm.lastname.displayname=Last Name
nameForm.firstname.displayname=First Name
nameForm.city.displayname=City
nameForm.state.displayname=State
nameForm.postalCode.displayname=Postal Code
nameForm.street1.displayname=Street Address
1.1 jakarta-commons/validator/src/example/org/apache/commons/validator/validator-example.xml
Index: validator-example.xml
===================================================================
<form-validation>
<global>
<validator name="int"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateInt"
methodParams="java.lang.Object,org.apache.commons.validator.Field"
msg="errors.int"/>
<validator name="required"
classname="org.apache.commons.validator.TestValidator"
method="validateRequired"
methodParams="java.lang.Object,org.apache.commons.validator.Field"
msg="errors.required"/>
</global>
<formset>
<form name="ValidateBean">
<field property="firstName" depends="required">
<arg0 key="nameForm.firstname.displayname"/>
</field>
<field property="lastName" depends="required">
<arg0 key="nameForm.lastname.displayname"/>
</field>
<field property="street1" depends="required">
<arg0 key="nameForm.street1.displayname"/>
</field>
<field property="state" depends="required">
<arg0 key="nameForm.city.displayname"/>
</field>
<field property="state" depends="required">
<arg0 key="nameForm.state.displayname"/>
</field>
<field property="postalCode" depends="required">
<arg0 key="nameForm.postalCode.displayname"/>
</field>
<field property="age" depends="required,int">
<arg0 key="nameForm.age.displayname"/>
</field>
</form>
</formset>
</form-validation>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>