commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jke...@apache.org
Subject cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli/validator StringValueValidatorTest.java
Date Mon, 09 Jun 2003 22:39:51 GMT
jkeyes      2003/06/09 15:39:51

  Modified:    cli/src/java/org/apache/commons/cli Tag: cli_1_x
                        ArgumentBuilder.java Argument.java Options.java
                        ArgumentImpl.java AnonymousArgumentImpl.java
  Added:       cli/src/java/org/apache/commons/cli Tag: cli_1_x
                        ValueValidator.java InvalidValueException.java
               cli/src/java/org/apache/commons/cli/validator Tag: cli_1_x
                        StringValueValidator.java
               cli/src/test/org/apache/commons/cli/validator Tag: cli_1_x
                        StringValueValidatorTest.java
  Log:
  
o  initial validator impl
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.6   +15 -4     jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/ArgumentBuilder.java
  
  Index: ArgumentBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/ArgumentBuilder.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- ArgumentBuilder.java	8 Jun 2003 18:16:01 -0000	1.1.2.5
  +++ ArgumentBuilder.java	9 Jun 2003 22:39:50 -0000	1.1.2.6
  @@ -104,6 +104,9 @@
   
       /** specifies whether the values can be omitted, by default false */
       private boolean hasOptionalValues;
  +    
  +    /** TODO: javadoc */
  +    private ValueValidator valueValidator;
   
       /**
        * Creates ArgumentBuilder instances.
  @@ -131,7 +134,8 @@
                   this.valueSeparator,
                   this.valueName,
                   this.hasOptionalValues,
  -                this.options);
  +                this.options,
  +                this.valueValidator);
           } finally {
               // reset the state of the builder
               reset();
  @@ -248,6 +252,12 @@
           return this;
       }
   
  +    /** TODO: javadoc */
  +    public ArgumentBuilder withValueValidator(final ValueValidator validator) {
  +        this.valueValidator = validator;
  +        return this;
  +    }
  +    
       /**
        * Sets the number of values the next Argument created will have.
        * 
  @@ -299,6 +309,7 @@
           this.options = null;
           this.valueSeparator = (char) 0;
           this.valueName = null;
  +        this.valueValidator = null;
           this.size = 1;
           this.type = null;
           this.hasOptionalValues = false;
  
  
  
  1.1.2.4   +6 -3      jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/Argument.java
  
  Index: Argument.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/Argument.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- Argument.java	24 May 2003 21:54:15 -0000	1.1.2.3
  +++ Argument.java	9 Jun 2003 22:39:50 -0000	1.1.2.4
  @@ -111,4 +111,7 @@
        * @return whether the values of this argument are optional
        */
       boolean hasOptionalValues();
  +    
  +    /** TODO: javadoc */
  +    ValueValidator getValueValidator();
   }
  
  
  
  1.19.2.6  +48 -0     jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java
  
  Index: Options.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java,v
  retrieving revision 1.19.2.5
  retrieving revision 1.19.2.6
  diff -u -r1.19.2.5 -r1.19.2.6
  --- Options.java	6 Jun 2003 22:17:08 -0000	1.19.2.5
  +++ Options.java	9 Jun 2003 22:39:50 -0000	1.19.2.6
  @@ -63,6 +63,7 @@
   import java.util.Collection;
   import java.util.HashSet;
   import java.util.Iterator;
  +import java.util.List;
   import java.util.Set;
   
   /**
  @@ -210,6 +211,8 @@
           if (!options.isEmpty()) {
               for (Iterator iter = options.iterator(); iter.hasNext(); ) {
                   Option option = (Option)iter.next();
  +                
  +                // validate the presence
                   if (option.isRequired()) {
                       if (option.getName() != null) {
                           if (!line.hasOption("-" + option.getName())) {
  @@ -222,6 +225,51 @@
                           }
                       }
   
  +                }
  +                
  +                // validate the values
  +                if (option instanceof Argument) {
  +                    Argument argument = (Argument) option;
  +                    
  +                    ValueValidator validator = argument.getValueValidator();
  +                    if (validator != null) {
  +                        String id = "-" + option.getName();
  +                        if (id == null) {
  +                            id = "--" + option.getLongName();
  +                        }
  +                        if (option.getName() != null) {
  +                            List vals = line.getValues(id);
  +                            for (Iterator viter = vals.iterator(); viter.hasNext();) {
  +                                String value = viter.next().toString();
  +                                if (!validator.validate(value)) {
  +                                    StringBuffer buff = new StringBuffer();
  +                                    
  +                                    buff.append("The value {");
  +                                    buff.append(value);
  +                                    buff.append("} is not permitted for the argument '");
  +                                    buff.append(id); 
  +                                    buff.append("'.\nThe permitted values are:");
  +                                    
  +                                    String[] strings = validator.getValidValues();
  +                                    for (int i = 0; i < strings.length; i++) {
  +                                        if (i+1 == strings.length) {
  +                                            buff.append(" or ");
  +                                            buff.append(strings[i]);
  +                                        }
  +                                        else if (i+2 == strings.length) {
  +                                            buff.append(strings[i]);
  +                                        }
  +                                        else {
  +                                            buff.append(strings[i]);
  +                                            buff.append(", ");
  +                                        }
  +                                    }
  +                                    
  +                                    throw new InvalidValueException(buff.toString());
  +                                }
  +                            }
  +                        }
  +                    }
                   }
               }
           }
  
  
  
  1.1.2.5   +16 -4     jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/ArgumentImpl.java
  
  Index: ArgumentImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/ArgumentImpl.java,v
  retrieving revision 1.1.2.4
  retrieving revision 1.1.2.5
  diff -u -r1.1.2.4 -r1.1.2.5
  --- ArgumentImpl.java	8 Jun 2003 18:16:01 -0000	1.1.2.4
  +++ ArgumentImpl.java	9 Jun 2003 22:39:51 -0000	1.1.2.5
  @@ -81,6 +81,9 @@
   
       /** whether this argument has optional values */
       private final boolean hasOptionalValues;
  +    
  +    /** TODO: javadoc */
  +    private final ValueValidator validator;
   
       /**
        * Creates an Argument with the specified name, long name
  @@ -126,7 +129,8 @@
           final char valueSeparator,
           final String valueName,
           final boolean hasOptionalValues,
  -        final Options options) {
  +        final Options options,
  +        final ValueValidator validator) {
   
           super(name, longName, description, required, options);
   
  @@ -135,6 +139,7 @@
           this.valueSeparator = valueSeparator;
           this.valueName = valueName;
           this.hasOptionalValues = hasOptionalValues;
  +        this.validator = validator;
       }
   
       /**
  @@ -170,5 +175,12 @@
        */
       public boolean hasOptionalValues() {
           return this.hasOptionalValues;
  +    }
  +
  +    /**
  +     * @see org.apache.commons.cli.Argument#getValueValidator()
  +     */
  +    public ValueValidator getValueValidator() {
  +        return this.validator;
       }
   }
  
  
  
  1.1.2.7   +13 -3     jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/AnonymousArgumentImpl.java
  
  Index: AnonymousArgumentImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/AnonymousArgumentImpl.java,v
  retrieving revision 1.1.2.6
  retrieving revision 1.1.2.7
  diff -u -r1.1.2.6 -r1.1.2.7
  --- AnonymousArgumentImpl.java	8 Jun 2003 18:16:01 -0000	1.1.2.6
  +++ AnonymousArgumentImpl.java	9 Jun 2003 22:39:51 -0000	1.1.2.7
  @@ -80,6 +80,9 @@
   
       /** the child options */
       private Options options;
  +    
  +    /** TODO: javadoc */
  +    private ValueValidator validator;
   
       /**
        * Creates an anonymous Argument with the specified name.
  @@ -203,5 +206,12 @@
        */
       public boolean isRequired() {
           return false;
  +    }
  +
  +    /** 
  +     * @see org.apache.commons.cli.Argument#getValueValidator()
  +     */
  +    public ValueValidator getValueValidator() {
  +        return this.validator;
       }
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +76 -0     jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/ValueValidator.java
  
  
  
  
  1.1.2.1   +84 -0     jakarta-commons/cli/src/java/org/apache/commons/cli/Attic/InvalidValueException.java
  
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +97 -0     jakarta-commons/cli/src/java/org/apache/commons/cli/validator/Attic/StringValueValidator.java
  
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +116 -0    jakarta-commons/cli/src/test/org/apache/commons/cli/validator/Attic/StringValueValidatorTest.java
  
  
  
  

---------------------------------------------------------------------
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