dgraham     2003/03/15 16:29:21

  Modified:    validator/src/example/org/apache/commons/validator/example
                        ValidateExample.java
  Log:
  Format changes and cleaned up exception handling.
  
  Revision  Changes    Path
  1.4       +125 -123  
jakarta-commons/validator/src/example/org/apache/commons/validator/example/ValidateExample.java
  
  Index: ValidateExample.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/validator/src/example/org/apache/commons/validator/example/ValidateExample.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ValidateExample.java      13 Mar 2003 02:11:39 -0000      1.3
  +++ ValidateExample.java      16 Mar 2003 00:29:21 -0000      1.4
  @@ -59,9 +59,9 @@
    *
    */
   
  -
   package org.apache.commons.validator.example;
   
  +import java.io.IOException;
   import java.io.InputStream;
   import java.text.MessageFormat;
   import java.util.Iterator;
  @@ -73,6 +73,7 @@
   import org.apache.commons.validator.Form;
   import org.apache.commons.validator.Validator;
   import org.apache.commons.validator.ValidatorAction;
  +import org.apache.commons.validator.ValidatorException;
   import org.apache.commons.validator.ValidatorResources;
   import org.apache.commons.validator.ValidatorResourcesInitializer;
   import org.apache.commons.validator.ValidatorResult;
  @@ -91,148 +92,149 @@
    * 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(
  +    private static ResourceBundle apps =
  +        ResourceBundle.getBundle(
               "org.apache.commons.validator.example.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) throws IOException, ValidatorException {
   
  -    public static void main (String[] args) {
  +        InputStream in = null;
   
  -     InputStream in = null;
  +        try {
   
  -     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) {}      
  -         }
  -     }
  +            // 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);
  +
  +        } finally {
  +            // Make sure we close the input stream if it was left open.
  +            if (in != null) {
  +                in.close();
  +            }
  +        }
   
       }
   
  -
       /**
  -     *
  -     * Method which dumps out the Bean in question and the results of validating it.
  -     *
  +     * 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();
  +    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();
  +                String actName = (String) keys.next();
   
  -             // Get the Action for that name.
  -             ValidatorAction action = resources.getValidatorAction(actName);
  +                // 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");
  -     }
  +                // 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");
  +        }
   
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to