commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/example/org/apache/commons/validator/example ValidateExample.java
Date Thu, 22 May 2003 03:12:19 GMT
dgraham     2003/05/21 20:12:18

  Modified:    validator/src/test/org/apache/commons/validator
                        MultipleTests.java TypeTest.java DoubleTest.java
                        ByteTest.java LongTest.java FloatTest.java
                        RequiredNameTest.java IntegerTest.java
                        LocaleTest.java ShortTest.java RequiredIfTest.java
                        MultipleConfigFilesTest.java EmailTest.java
               validator/src/share/org/apache/commons/validator
                        ValidatorResources.java
                        ValidatorResourcesInitializer.java
               validator/src/example/org/apache/commons/validator/example
                        ValidateExample.java
  Log:
  The ValidatorResourcesInitializer code wasn't pulling its weight being in 
  its own class.  ValidatorResources knows how to initialize itself from 
  InputStreams and ValidatorResourcesInitializer is deprecated.
  
  Revision  Changes    Path
  1.5       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleTests.java
  
  Index: MultipleTests.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleTests.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MultipleTests.java	22 May 2003 02:29:46 -0000	1.4
  +++ MultipleTests.java	22 May 2003 03:12:18 -0000	1.5
  @@ -131,11 +131,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
   
         try {
            in = this.getClass().getResourceAsStream("validator-multipletest.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.7       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/TypeTest.java
  
  Index: TypeTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TypeTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TypeTest.java	22 May 2003 02:29:46 -0000	1.6
  +++ TypeTest.java	22 May 2003 03:12:18 -0000	1.7
  @@ -134,11 +134,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-type.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.6       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/DoubleTest.java
  
  Index: DoubleTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/DoubleTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DoubleTest.java	22 May 2003 02:29:46 -0000	1.5
  +++ DoubleTest.java	22 May 2003 03:12:18 -0000	1.6
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-numeric.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.6       +6 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/ByteTest.java
  
  Index: ByteTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ByteTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ByteTest.java	22 May 2003 02:29:46 -0000	1.5
  +++ ByteTest.java	22 May 2003 03:12:18 -0000	1.6
  @@ -132,11 +132,11 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
  +      
         
         try {
            in = this.getClass().getResourceAsStream("validator-numeric.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.6       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/LongTest.java
  
  Index: LongTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/LongTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- LongTest.java	22 May 2003 02:29:46 -0000	1.5
  +++ LongTest.java	22 May 2003 03:12:18 -0000	1.6
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-numeric.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.6       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/FloatTest.java
  
  Index: FloatTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/FloatTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- FloatTest.java	22 May 2003 02:29:46 -0000	1.5
  +++ FloatTest.java	22 May 2003 03:12:18 -0000	1.6
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-numeric.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.8       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredNameTest.java
  
  Index: RequiredNameTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredNameTest.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- RequiredNameTest.java	22 May 2003 02:29:46 -0000	1.7
  +++ RequiredNameTest.java	22 May 2003 03:12:18 -0000	1.8
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-name-required.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.6       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/IntegerTest.java
  
  Index: IntegerTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/IntegerTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- IntegerTest.java	22 May 2003 02:29:46 -0000	1.5
  +++ IntegerTest.java	22 May 2003 03:12:18 -0000	1.6
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-numeric.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.4       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/LocaleTest.java
  
  Index: LocaleTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/LocaleTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LocaleTest.java	22 May 2003 02:29:46 -0000	1.3
  +++ LocaleTest.java	22 May 2003 03:12:18 -0000	1.4
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-locale.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.6       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/ShortTest.java
  
  Index: ShortTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ShortTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ShortTest.java	22 May 2003 02:29:46 -0000	1.5
  +++ ShortTest.java	22 May 2003 03:12:18 -0000	1.6
  @@ -132,11 +132,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-numeric.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.5       +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredIfTest.java
  
  Index: RequiredIfTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredIfTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RequiredIfTest.java	22 May 2003 02:29:46 -0000	1.4
  +++ RequiredIfTest.java	22 May 2003 03:12:18 -0000	1.5
  @@ -131,11 +131,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-requiredif.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.3       +10 -10    jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleConfigFilesTest.java
  
  Index: MultipleConfigFilesTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleConfigFilesTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MultipleConfigFilesTest.java	22 May 2003 02:52:47 -0000	1.2
  +++ MultipleConfigFilesTest.java	22 May 2003 03:12:18 -0000	1.3
  @@ -89,8 +89,6 @@
        */
       private static final String ACTION = "required";
   
  -
  -
       /**
        * Constructor for MultipleConfigFilesTest.
        * @param name
  @@ -103,16 +101,18 @@
        * Load <code>ValidatorResources</code> from multiple xml files.
        */
       protected void setUp() throws IOException {
  -        InputStream in = null;
  -        this.resources = new ValidatorResources();
  +        InputStream[] streams =
  +            new InputStream[] {
  +                this.getClass().getResourceAsStream(
  +                    "validator-multiple-config-1.xml"),
  +                this.getClass().getResourceAsStream(
  +                    "validator-multiple-config-2.xml")};
   
  -        in = this.getClass().getResourceAsStream("validator-multiple-config-1.xml");
  -        ValidatorResourcesInitializer.initialize(resources, in);
  -        in.close();
  +        this.resources = new ValidatorResources(streams);
   
  -        in = this.getClass().getResourceAsStream("validator-multiple-config-2.xml");
  -        ValidatorResourcesInitializer.initialize(resources, in);
  -        in.close();
  +        for (int i = 0; i < streams.length; i++) {
  +            streams[i].close();
  +        }
       }
   
       /**
  
  
  
  1.11      +5 -6      jakarta-commons/validator/src/test/org/apache/commons/validator/EmailTest.java
  
  Index: EmailTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/EmailTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- EmailTest.java	22 May 2003 02:29:46 -0000	1.10
  +++ EmailTest.java	22 May 2003 03:12:18 -0000	1.11
  @@ -131,11 +131,10 @@
      protected void setUp() throws IOException {
         // Load resources
         InputStream in = null;
  -      resources = new ValidatorResources();
         
         try {
            in = this.getClass().getResourceAsStream("validator-regexp.xml");
  -         ValidatorResourcesInitializer.initialize(resources, in);
  +         resources = new ValidatorResources(in);
         } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
  
  
  
  1.16      +79 -4     jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResources.java
  
  Index: ValidatorResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResources.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ValidatorResources.java	21 May 2003 02:53:41 -0000	1.15
  +++ ValidatorResources.java	22 May 2003 03:12:18 -0000	1.16
  @@ -61,7 +61,10 @@
   
   package org.apache.commons.validator;
   
  +import java.io.IOException;
  +import java.io.InputStream;
   import java.io.Serializable;
  +import java.net.URL;
   import java.util.ArrayList;
   import java.util.Collections;
   import java.util.Iterator;
  @@ -70,8 +73,11 @@
   import java.util.Map;
   
   import org.apache.commons.collections.FastHashMap;
  +import org.apache.commons.digester.Digester;
  +import org.apache.commons.digester.xmlrules.DigesterLoader;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.xml.sax.SAXException;
   
   /**
    * <p>General purpose class for storing <code>FormSet</code> objects
based 
  @@ -88,6 +94,20 @@
   public class ValidatorResources implements Serializable {
   
       /**
  +     * The set of public identifiers, and corresponding resource names, for
  +     * the versions of the configuration file DTDs that we know about.  There
  +     * <strong>MUST</strong> be an even number of Strings in this list!
  +     */
  +    private static final String registrations[] = {
  +        "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN",
  +        "/org/apache/commons/validator/resources/validator_1_0.dtd",
  +        "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN",
  +        "/org/apache/commons/validator/resources/validator_1_0_1.dtd",
  +        "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN",
  +        "/org/apache/commons/validator/resources/validator_1_1.dtd"
  +   };
  +
  +    /**
        * Logger.
        */
       protected static Log log = LogFactory.getLog(ValidatorResources.class);
  @@ -114,6 +134,61 @@
        * The default locale on our server.
        */
       protected static Locale defaultLocale = Locale.getDefault();
  +
  +    /**
  +     * Create an empty ValidatorResources object.
  +     */
  +    public ValidatorResources() {
  +        super();
  +    }
  +    
  +    /**
  +     * Create a ValidatorResources object from an InputStream.
  +     * 
  +     * @param in InputStream to a validation.xml configuration file.  It's the client's

  +     * responsibility to close this stream.
  +     */
  +    public ValidatorResources(InputStream in) throws IOException {
  +        this(new InputStream[] { in });
  +    }
  +    
  +    /**
  +     * Create a ValidatorResources object from an InputStream.
  +     * 
  +     * @param streams An array of InputStreams to several validation.xml 
  +     * configuration files that will be read in order and merged into this object.  
  +     * It's the client's responsibility to close these streams.
  +     */
  +    public ValidatorResources(InputStream[] streams) throws IOException {
  +        super();
  +        
  +        URL rulesUrl = this.getClass().getResource("digester-rules.xml");
  +        Digester digester = DigesterLoader.createDigester(rulesUrl);
  +        digester.setNamespaceAware(true);
  +        digester.setValidating(false);
  +        digester.setUseContextClassLoader(true);
  +
  +        // register DTDs
  +        for (int i = 0; i < registrations.length; i += 2) {
  +            URL url = this.getClass().getResource(registrations[i + 1]);
  +            if (url != null) {
  +                digester.register(registrations[i], url.toString());
  +            }
  +        }
  +
  +        for (int i = 0; i < streams.length; i++) {
  +            digester.push(this);
  +
  +            try {
  +                digester.parse(streams[i]);
  +
  +            } catch (SAXException e) {
  +                log.error(e.getMessage(), e);
  +            }
  +        }
  +        
  +        this.process();
  +    }
   
       /**
        * Add a <code>FormSet</code> to this <code>ValidatorResources</code>
  
  
  
  1.17      +5 -4      jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResourcesInitializer.java
  
  Index: ValidatorResourcesInitializer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResourcesInitializer.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- ValidatorResourcesInitializer.java	22 May 2003 00:26:52 -0000	1.16
  +++ ValidatorResourcesInitializer.java	22 May 2003 03:12:18 -0000	1.17
  @@ -79,6 +79,7 @@
    * @author David Winterfeldt
    * @author Dave Derry
    * @version $Revision$ $Date$
  + * @deprecated ValidatorResources knows how to initialize itself now.
    */
   public class ValidatorResourcesInitializer {
   
  
  
  
  1.7       +47 -51    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ValidateExample.java	22 May 2003 02:29:47 -0000	1.6
  +++ ValidateExample.java	22 May 2003 03:12:18 -0000	1.7
  @@ -75,7 +75,6 @@
   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;
   import org.apache.commons.validator.ValidatorResults;
   
  @@ -109,65 +108,62 @@
        *
        */
       public static void main(String[] args) throws IOException, ValidatorException {
  -
           InputStream in = null;
  -
  +        ValidatorResources resources = 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.addParameter(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);
  -
  +            resources = new ValidatorResources(in);
  +            
           } finally {
  -            // Make sure we close the input stream if it was left open.
  +            // Make sure we close the input stream.
               if (in != null) {
                   in.close();
               }
           }
  -
  +        
  +        // 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.addParameter(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);
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message