You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2004/12/07 01:00:15 UTC

cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator Field.java

niallp      2004/12/06 16:00:15

  Modified:    validator/src/share/org/apache/commons/validator Field.java
  Log:
  Revert back to version 1.35
  
  Revision  Changes    Path
  1.37      +87 -94    jakarta-commons/validator/src/share/org/apache/commons/validator/Field.java
  
  Index: Field.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Field.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- Field.java	12 Nov 2004 16:02:52 -0000	1.36
  +++ Field.java	7 Dec 2004 00:00:15 -0000	1.37
  @@ -50,6 +50,13 @@
   public class Field implements Cloneable, Serializable {
   
       /**
  +     * This is the value that will be used as a key if the <code>Arg</code>
  +     * name field has no value.
  +     */
  +    private static final String DEFAULT_ARG =
  +            "org.apache.commons.validator.Field.DEFAULT";
  +
  +    /**
        * This indicates an indexed property is being referenced.
        */
       public static final String TOKEN_INDEXED = "[]";
  @@ -85,16 +92,12 @@
       protected FastHashMap hMsgs = new FastHashMap();
   
       /**
  -     * List containing the 'default' arguments
  -     * @since Validator 1.2.0
  -     */
  -    protected List defaultArgs;
  -
  -    /**
  -     * Map containing the 'overriden' arguments.
  -     * @since Validator 1.2.0
  +     * Holds Maps of arguments.  args[0] returns the Map for the first 
  +     * replacement argument.  Start with a 0 length array so that it will
  +     * only grow to the size of the highest argument position.
  +     * @since Validator 1.1
        */
  -    protected Map overridenArgs;
  +    protected Map[] args = new Map[0];
   
       /**
        * Gets the page value that the Field is associated with for
  @@ -190,14 +193,12 @@
   
           this.dependencyList.clear();
   
  -        if (this.depends != null) {
  -            StringTokenizer st = new StringTokenizer(depends, ",");
  -            while (st.hasMoreTokens()) {
  -                String depend = st.nextToken().trim();
  +        StringTokenizer st = new StringTokenizer(depends, ",");
  +        while (st.hasMoreTokens()) {
  +            String depend = st.nextToken().trim();
   
  -                if (depend != null && depend.length() > 0) {
  -                    this.dependencyList.add(depend);
  -                }
  +            if (depend != null && depend.length() > 0) {
  +                this.dependencyList.add(depend);
               }
           }
       }
  @@ -239,31 +240,38 @@
        * @since Validator 1.1
        */
       public void addArg(Arg arg) {
  +        // TODO this first if check can go away after arg0, etc. are removed from dtd
  +        if (arg == null || arg.getKey() == null || arg.getKey().length() == 0) {
  +            return;
  +        }
   
  -        // Determine if 'default' or 'overriden'
  -        boolean isDefaultArg = arg.getName() == null;
  -
  -        if (isDefaultArg) {
  +        this.ensureArgsCapacity(arg);
   
  -            if (defaultArgs == null) {
  -                defaultArgs = new ArrayList();
  -            }
  -            int pos = defaultArgs.size();
  -            arg.setPosition(pos);
  -            defaultArgs.add(arg);
  +        Map argMap = this.args[arg.getPosition()];
  +        if (argMap == null) {
  +            argMap = new HashMap();
  +            this.args[arg.getPosition()] = argMap;
  +        }
   
  +        if (arg.getName() == null) {
  +            argMap.put(DEFAULT_ARG, arg);
           } else {
  +            argMap.put(arg.getName(), arg);
  +        }
   
  -            if (overridenArgs == null) {
  -                overridenArgs = new HashMap();
  -            }
  -            int position = arg.getPosition();
  -            if (position < 0) {
  -                position = defaultArgs ==  null ? 0 : defaultArgs.size() - 1;
  -                arg.setPosition(position);
  -            }
  -            overridenArgs.put(arg.getName() + position, arg);
  +    }
   
  +    /**
  +     * Ensures that the args array can hold the given arg.  Resizes the array as
  +     * necessary.
  +     * @param arg Determine if the args array is long enough to store this arg's
  +     * position.
  +     */
  +    private void ensureArgsCapacity(Arg arg) {
  +        if (arg.getPosition() >= this.args.length) {
  +            Map[] newArgs = new Map[arg.getPosition() + 1];
  +            System.arraycopy(this.args, 0, newArgs, 0, this.args.length);
  +            this.args = newArgs;
           }
       }
   
  @@ -273,16 +281,7 @@
        * @since Validator 1.1
        */
       public Arg getArg(int position) {
  -
  -        if (position < 0) {
  -            return null;
  -        }
  -
  -        Arg arg = null; 
  -        if (defaultArgs != null && position < defaultArgs.size()) {
  -            arg = (Arg)defaultArgs.get(position);
  -        }
  -        return arg;
  +        return this.getArg(DEFAULT_ARG, position);
       }
   
       /**
  @@ -296,22 +295,19 @@
        * @since Validator 1.1
        */
       public Arg getArg(String key, int position) {
  -
  -        if (position < 0) {
  +        if ((position >= this.args.length) || (this.args[position] == null)) {
               return null;
           }
   
  -        Arg arg = null; 
  -        if (overridenArgs != null) {
  -            arg = (Arg)overridenArgs.get(key + position);
  -        }
  +        Arg arg = (Arg) args[position].get(key);
   
  -        if (arg == null) {
  -            arg = getArg(position);
  +        // Didn't find default arg so exit, otherwise we would get into 
  +        // infinite recursion
  +        if ((arg == null) && key.equals(DEFAULT_ARG)) {
  +            return null;
           }
   
  -        return arg;
  -
  +        return (arg == null) ? this.getArg(position) : arg;
       }
       
       /**
  @@ -322,15 +318,11 @@
        * @since Validator 1.1.1
        */
       public Arg[] getArgs(String key){
  -
  -        if (defaultArgs == null) {
  -           return new Arg[0];
  -        }
  -
  -        Arg[] args = new Arg[defaultArgs.size()];
  -        for (int i = 0; i < defaultArgs.size(); i++) {
  -            args[i] = getArg(key, i);
  -        }
  +        Arg[] args = new Arg[this.args.length];
  +        
  +        for (int i = 0; i < this.args.length; i++) {
  +		  args[i] = this.getArg(key, i);	
  +		}
           
           return args;
       }
  @@ -509,19 +501,21 @@
        * pairs passed in.
        */
       private void processArg(String key, String replaceValue) {
  +        for (int i = 0; i < this.args.length; i++) {
   
  -        if (defaultArgs != null) {
  -            for (int i = 0; i < defaultArgs.size(); i++) {
  -                Arg arg = getArg(i);
  -                arg.setKey(ValidatorUtils.replace(arg.getKey(), key, replaceValue));
  +            Map argMap = this.args[i];
  +            if (argMap == null) {
  +                continue;
               }
  -        }
   
  -        if (overridenArgs != null) {
  -            Iterator iter = overridenArgs.values().iterator();
  +            Iterator iter = argMap.values().iterator();
               while (iter.hasNext()) {
  -                Arg arg = (Arg)iter.next();
  -                arg.setKey(ValidatorUtils.replace(arg.getKey(), key, replaceValue));
  +                Arg arg = (Arg) iter.next();
  +
  +                if (arg != null) {
  +                    arg.setKey(
  +                            ValidatorUtils.replace(arg.getKey(), key, replaceValue));
  +                }
               }
           }
       }
  @@ -545,29 +539,27 @@
        * Creates and returns a copy of this object.
        */
       public Object clone() {
  +        Field field = null;
  +        try {
  +            field = (Field) super.clone();
  +        } catch(CloneNotSupportedException e) {
  +            throw new RuntimeException(e.toString());
  +        }
   
  -        Field field = new Field();
  -        field.setDepends(depends);
  -        field.setFieldOrder(fieldOrder);
  -        field.setIndexedListProperty(indexedListProperty);
  -        field.setIndexedProperty(indexedProperty);
  -        field.setKey(key);
  -        field.setPage(page);
  -        field.setProperty(property);
  -
  -        if (defaultArgs != null) {
  -            for (int i = 0; i < defaultArgs.size(); i++) {
  -                Arg arg = getArg(i);
  -                field.addArg((Arg)arg.clone());
  +        field.args = new Map[this.args.length];
  +        for (int i = 0; i < this.args.length; i++) {
  +            if (this.args[i] == null) {
  +                continue;
               }
  -        }
   
  -        if (overridenArgs != null) {
  -            Iterator iter = overridenArgs.values().iterator();
  +            Map argMap = new HashMap(this.args[i]);
  +            Iterator iter = argMap.keySet().iterator();
               while (iter.hasNext()) {
  -                Arg arg = (Arg)iter.next();
  -                field.addArg((Arg)arg.clone());
  +                String validatorName = (String) iter.next();
  +                Arg arg = (Arg) argMap.get(validatorName);
  +                argMap.put(validatorName, arg.clone());
               }
  +            field.args[i] = argMap;
           }
   
           field.hVars = ValidatorUtils.copyFastHashMap(hVars);
  @@ -769,3 +761,4 @@
       }
   
   }
  +
  
  
  

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