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/share/org/apache/commons/validator/util ValidatorUtils.java
Date Fri, 02 May 2003 23:39:31 GMT
dgraham     2003/05/02 16:39:31

  Modified:    validator/src/test/org/apache/commons/validator
                        ValidatorTest.java TestValidator.java
                        TestTypeValidator.java
               validator/src/share/org/apache/commons/validator
                        ValidatorUtil.java Field.java Validator.java
                        GenericValidator.java UrlValidator.java
  Added:       validator/src/share/org/apache/commons/validator/util
                        ValidatorUtils.java
  Log:
  Moved ValidatorUtil to new util package.
  
  Revision  Changes    Path
  1.9       +9 -7      jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java
  
  Index: ValidatorTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ValidatorTest.java	29 Apr 2003 00:59:54 -0000	1.8
  +++ ValidatorTest.java	2 May 2003 23:39:30 -0000	1.9
  @@ -71,7 +71,9 @@
   
   import junit.framework.Test;
   import junit.framework.TestCase;
  -import junit.framework.TestSuite;              
  +import junit.framework.TestSuite;
  +
  +import org.apache.commons.validator.util.ValidatorUtils;
   
                                                             
   /**                                                       
  @@ -243,7 +245,7 @@
       * Checks if the field is one upper case letter between 'A' and 'Z'.
      */
      public static boolean isCapLetter(Object bean, Field field, List l) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         if (value != null && value.length() == 1) {
            if (value.charAt(0) >= 'A' && value.charAt(0) <= 'Z') {
  @@ -264,7 +266,7 @@
       * as validation having failed.
       */
      public static Date formatDate(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
         Date date = null;
         
         try {
  
  
  
  1.7       +16 -14    jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java
  
  Index: TestValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestValidator.java	15 Mar 2003 22:28:20 -0000	1.6
  +++ TestValidator.java	2 May 2003 23:39:30 -0000	1.7
  @@ -61,6 +61,8 @@
   
   
   package org.apache.commons.validator;
  +
  +import org.apache.commons.validator.util.ValidatorUtils;
                                                             
   /**                                                       
    * <p>Contains validation methods for different 
  @@ -81,7 +83,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateRequired(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return !GenericValidator.isBlankOrNull(value);
      }
  @@ -95,7 +97,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateByte(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isByte(value);
      }
  @@ -109,7 +111,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateShort(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isShort(value);
      }
  @@ -123,7 +125,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateInt(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isInt(value);
      }
  @@ -137,7 +139,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateLong(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isLong(value);
      }
  @@ -151,7 +153,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateFloat(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isFloat(value);
      }
  @@ -165,7 +167,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateDouble(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isDouble(value);
      }
  @@ -179,7 +181,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static boolean validateEmail(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericValidator.isEmail(value);
      }
  @@ -197,7 +199,7 @@
       if (isString(bean)) {
         value = (String) bean;
       } else {
  -      value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      value = ValidatorUtils.getValueAsString(bean, field.getProperty());
       }
       int i = 0;
       String fieldJoin = "AND";
  @@ -223,7 +225,7 @@
             dependProp = ind + dependProp;
           }
         }
  -      dependVal = ValidatorUtil.getValueAsString(form, dependProp);
  +      dependVal = ValidatorUtils.getValueAsString(form, dependProp);
         if (dependTest.equals(FIELD_TEST_NULL)) {
           if ((dependVal != null ) && (dependVal.length() > 0)) {
             this_required =  false;
  
  
  
  1.3       +12 -10    jakarta-commons/validator/src/test/org/apache/commons/validator/TestTypeValidator.java
  
  Index: TestTypeValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestTypeValidator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestTypeValidator.java	15 Dec 2002 20:58:57 -0000	1.2
  +++ TestTypeValidator.java	2 May 2003 23:39:30 -0000	1.3
  @@ -61,6 +61,8 @@
   
   
   package org.apache.commons.validator;
  +
  +import org.apache.commons.validator.util.ValidatorUtils;
                                                             
   /**                                                       
    * <p>Contains validation methods for different 
  @@ -80,7 +82,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static Byte validateByte(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericTypeValidator.formatByte(value);
      }
  @@ -94,7 +96,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static Short validateShort(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericTypeValidator.formatShort(value);
      }
  @@ -108,7 +110,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static Integer validateInt(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericTypeValidator.formatInt(value);
      }
  @@ -122,7 +124,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static Long validateLong(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericTypeValidator.formatLong(value);
      }
  @@ -136,7 +138,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static Float validateFloat(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericTypeValidator.formatFloat(value);
      }
  @@ -150,7 +152,7 @@
       *                           Otherwise <code>false</code>.
      */
      public static Double validateDouble(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
   
         return GenericTypeValidator.formatDouble(value);
      }
  
  
  
  1.10      +13 -73    jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorUtil.java
  
  Index: ValidatorUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorUtil.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ValidatorUtil.java	29 Apr 2003 02:15:35 -0000	1.9
  +++ ValidatorUtil.java	2 May 2003 23:39:30 -0000	1.10
  @@ -61,10 +61,6 @@
   
   package org.apache.commons.validator;
   
  -import java.lang.reflect.InvocationTargetException;
  -import java.util.Iterator;
  -
  -import org.apache.commons.beanutils.PropertyUtils;
   import org.apache.commons.collections.FastHashMap;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -75,16 +71,20 @@
    * @author David Winterfeldt
    * @author David Graham
    * @version $Revision$ $Date$
  + * @deprecated This class has moved to the org.apache.commons.validator.util
  + * package.
   */
   public class ValidatorUtil {
   
   	/**
   	 * Delimiter to put around a regular expression following Perl 5 syntax.
  +     * @deprecated Use "/" directly.
   	 */
   	public final static String REGEXP_DELIMITER = "/";
   
   	/**
   	 * Logger.
  +     * @deprecated Subclasses should use their own logging instance.
   	 */
   	protected static Log log = LogFactory.getLog(ValidatorUtil.class);
   
  @@ -99,32 +99,8 @@
   		String value,
   		String key,
   		String replaceValue) {
  -		if (value == null || key == null || replaceValue == null) {
  -			return value;
  -		}
  -
  -		int pos = value.indexOf(key);
  -
  -		if (pos < 0) {
  -			return value;
  -		}
  -
  -		int length = value.length();
  -		int start = pos;
  -		int end = pos + key.length();
  -
  -		if (length == key.length()) {
  -			value = replaceValue;
  -		} else if (end == length) {
  -			value = value.substring(0, start) + replaceValue;
  -		} else {
  -			value =
  -				value.substring(0, start)
  -					+ replaceValue
  -					+ replace(value.substring(end), key, replaceValue);
  -		}
  -
  -		return value;
  +            
  +        return org.apache.commons.validator.util.ValidatorUtils.replace(value, key, replaceValue);
   	}
   
   	/**
  @@ -132,20 +108,7 @@
   	 * <code>String</code>.
   	 */
   	public static String getValueAsString(Object bean, String property) {
  -		Object value = null;
  -
  -		try {
  -			value = PropertyUtils.getProperty(bean, property);
  -
  -		} catch (IllegalAccessException e) {
  -			log.error(e.getMessage(), e);
  -		} catch (InvocationTargetException e) {
  -			log.error(e.getMessage(), e);
  -		} catch (NoSuchMethodException e) {
  -			log.error(e.getMessage(), e);
  -		}
  -
  -		return (value != null ? value.toString() : null);
  +		return org.apache.commons.validator.util.ValidatorUtils.getValueAsString(bean, property);
   	}
   
   	/**
  @@ -158,35 +121,12 @@
   	 * passed in.
   	 */
   	public static FastHashMap copyFastHashMap(FastHashMap map) {
  -		FastHashMap results = new FastHashMap();
  -
  -		Iterator i = map.keySet().iterator();
  -		while (i.hasNext()) {
  -			String key = (String) i.next();
  -			Object value = map.get(key);
  -
  -			// TODO I don't think Strings need to be copied and do we really need to cast
  -			// before cloning?
  -			if (value instanceof String) {
  -				results.put(key, new String((String) value));
  -			} else if (value instanceof Msg) {
  -				results.put(key, ((Msg) value).clone());
  -			} else if (value instanceof Arg) {
  -				results.put(key, ((Arg) value).clone());
  -			} else if (value instanceof Var) {
  -				results.put(key, ((Var) value).clone());
  -			} else {
  -				results.put(key, value);
  -			}
  -		}
  -
  -		results.setFast(true);
  -
  -		return results;
  +        return org.apache.commons.validator.util.ValidatorUtils.copyFastHashMap(map);
   	}
   
   	/**
   	 * Adds a '/' on either side of the regular expression.
  +     * @deprecated Use "/" directly.
   	 */
   	public static String getDelimitedRegExp(String regexp) {
   		return (REGEXP_DELIMITER + regexp + REGEXP_DELIMITER);
  
  
  
  1.9       +19 -17    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Field.java	29 Apr 2003 02:38:49 -0000	1.8
  +++ Field.java	2 May 2003 23:39:31 -0000	1.9
  @@ -68,7 +68,9 @@
   import java.util.Iterator;
   import java.util.Map;
   import java.util.StringTokenizer;
  -import org.apache.commons.collections.FastHashMap; 
  +
  +import org.apache.commons.collections.FastHashMap;
  +import org.apache.commons.validator.util.ValidatorUtils;
   
   
   /**
  @@ -460,7 +462,7 @@
       		String key2 = TOKEN_START + key + TOKEN_END;
       		String replaceValue = (String) constants.get(key);
       
  -    		property = ValidatorUtil.replace(property, key2, replaceValue);
  +    		property = ValidatorUtils.replace(property, key2, replaceValue);
       
       		processVars(key2, replaceValue);
       
  @@ -473,7 +475,7 @@
       		String key2 = TOKEN_START + key + TOKEN_END;
       		String replaceValue = (String) globalConstants.get(key);
       
  -    		property = ValidatorUtil.replace(property, key2, replaceValue);
  +    		property = ValidatorUtils.replace(property, key2, replaceValue);
       
       		processVars(key2, replaceValue);
       
  @@ -516,7 +518,7 @@
       		String varKey = (String) i.next();
       		Var var = (Var) hVars.get(varKey);
       
  -    		var.setValue(ValidatorUtil.replace(var.getValue(), key, replaceValue));
  +    		var.setValue(ValidatorUtils.replace(var.getValue(), key, replaceValue));
       	}
          
       }
  @@ -532,7 +534,7 @@
       			String msgKey = (String) i.next();
       			String value = (String) hMsgs.get(msgKey);
       
  -    			hMsgs.put(msgKey, ValidatorUtil.replace(value, key, replaceValue));
  +    			hMsgs.put(msgKey, ValidatorUtils.replace(value, key, replaceValue));
       		}
          }
          
  @@ -551,7 +553,7 @@
           	Arg arg = (Arg) i.next();
           
           	if (arg != null) {
  -        		arg.setKey(ValidatorUtil.replace(arg.getKey(), key, replaceValue));
  +        		arg.setKey(ValidatorUtils.replace(arg.getKey(), key, replaceValue));
           	}
           }
       }
  @@ -603,13 +605,13 @@
   
              // page & fieldOrder should be taken care of by clone method
              
  -           field.hDependencies = ValidatorUtil.copyFastHashMap(hDependencies);
  -           field.hVars = ValidatorUtil.copyFastHashMap(hVars);
  -           field.hMsgs = ValidatorUtil.copyFastHashMap(hMsgs);
  -           field.hArg0 = ValidatorUtil.copyFastHashMap(hArg0);
  -           field.hArg1 = ValidatorUtil.copyFastHashMap(hArg1);
  -           field.hArg2 = ValidatorUtil.copyFastHashMap(hArg2);
  -           field.hArg3 = ValidatorUtil.copyFastHashMap(hArg3);
  +           field.hDependencies = ValidatorUtils.copyFastHashMap(hDependencies);
  +           field.hVars = ValidatorUtils.copyFastHashMap(hVars);
  +           field.hMsgs = ValidatorUtils.copyFastHashMap(hMsgs);
  +           field.hArg0 = ValidatorUtils.copyFastHashMap(hArg0);
  +           field.hArg1 = ValidatorUtils.copyFastHashMap(hArg1);
  +           field.hArg2 = ValidatorUtils.copyFastHashMap(hArg2);
  +           field.hArg3 = ValidatorUtils.copyFastHashMap(hArg3);
       
              return field;
          } catch (CloneNotSupportedException e) {
  
  
  
  1.17      +6 -5      jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java
  
  Index: Validator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Validator.java	30 Apr 2003 21:51:05 -0000	1.16
  +++ Validator.java	2 May 2003 23:39:31 -0000	1.17
  @@ -75,6 +75,7 @@
   import org.apache.commons.beanutils.PropertyUtils;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.apache.commons.validator.util.ValidatorUtils;
   
   /**
    * <p>Validations are processed by the validate method.
  @@ -439,7 +440,7 @@
                   // the current field
                   Field indexedField = (Field) field.clone();
                   indexedField.setKey(
  -                    ValidatorUtil.replace(
  +                    ValidatorUtils.replace(
                           indexedField.getKey(),
                           Field.TOKEN_INDEXED,
                           "[" + pos + "]"));
  
  
  
  1.23      +12 -13    jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java
  
  Index: GenericValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- GenericValidator.java	1 May 2003 02:42:30 -0000	1.22
  +++ GenericValidator.java	2 May 2003 23:39:31 -0000	1.23
  @@ -80,7 +80,7 @@
   
      /**
       * Delimiter to put around a regular expression following Perl 5 syntax.
  -    * @deprecated Use ValidatorUtil.REGEXP_DELIMITER instead.
  +    * @deprecated Use ValidatorUtils.REGEXP_DELIMITER instead.
       */
      public final static String REGEXP_DELIM = ValidatorUtil.REGEXP_DELIMITER;
   
  @@ -106,13 +106,12 @@
       * @param regexp The regular expression.
       */
      public static boolean matchRegexp(String value, String regexp) {
  -
  -      if (regexp == null || regexp.length() <= 0) {
  -         return false;
  -      }
  -      
  -      Perl5Util matcher = new Perl5Util();
  -      return matcher.match(ValidatorUtil.getDelimitedRegExp(regexp), value);
  +        if (regexp == null || regexp.length() <= 0) {
  +        	return false;
  +        }
  +        
  +        Perl5Util matcher = new Perl5Util();
  +        return matcher.match("/" + regexp + "/", value);
      }
   
      /**
  @@ -321,7 +320,7 @@
   
      /**
       * Adds a '/' on either side of the regular expression.
  -    * @deprecated use ValidatorUtil.getDelimitedRegExp() instead.
  +    * @deprecated use ValidatorUtils.getDelimitedRegExp() instead.
       */
      protected static String getDelimittedRegexp(String regexp) {
         return ValidatorUtil.getDelimitedRegExp(regexp);
  
  
  
  1.6       +5 -5      jakarta-commons/validator/src/share/org/apache/commons/validator/UrlValidator.java
  
  Index: UrlValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/UrlValidator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- UrlValidator.java	2 May 2003 23:18:17 -0000	1.5
  +++ UrlValidator.java	2 May 2003 23:39:31 -0000	1.6
  @@ -136,7 +136,7 @@
       * Protocol (ie. http:, ftp:,https:).
       */
      private static final String schemePat = "/^[" + schemeChars + "]/";
  -   private static final String authorityPat = ValidatorUtil.getDelimitedRegExp("^([" +
authorityChars + "]*)(:\\d*)?(.*)?");
  +   private static final String authorityPat = "/^([" + authorityChars + "]*)(:\\d*)?(.*)?/";
      //                                                                            1    
                     2  3       4
      private static final int PARSE_AUTHORITY_HOST_IP = 1;
      private static final int PARSE_AUTHORITY_PORT = 2;
  
  
  
  1.1                  jakarta-commons/validator/src/share/org/apache/commons/validator/util/ValidatorUtils.java
  
  Index: ValidatorUtils.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/util/ValidatorUtils.java,v
1.1 2003/05/02 23:39:31 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/05/02 23:39:31 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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.util;
  
  import java.lang.reflect.InvocationTargetException;
  import java.util.Iterator;
  
  import org.apache.commons.beanutils.PropertyUtils;
  import org.apache.commons.collections.FastHashMap;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.commons.validator.Arg;
  import org.apache.commons.validator.Msg;
  import org.apache.commons.validator.Var;
  
  /**
   * <p>Basic utility methods.</p>
   *
   * @author David Winterfeldt
   * @author David Graham
   * @version $Revision: 1.1 $ $Date: 2003/05/02 23:39:31 $
  */
  public class ValidatorUtils {
  
  	/**
  	 * Logger.
  	 */
  	private static Log log = LogFactory.getLog(ValidatorUtils.class);
  
  	/**
  	 * <p>Replace part of a <code>String</code> with another value.</p>
  	 *
  	 * @param	value		<code>String</code> to perform the replacement on.
  	 * @param	key		The name of the constant.
  	 * @param	replaceValue	The value of the constant.
  	 */
  	public static String replace(
  		String value,
  		String key,
  		String replaceValue) {
  		if (value == null || key == null || replaceValue == null) {
  			return value;
  		}
  
  		int pos = value.indexOf(key);
  
  		if (pos < 0) {
  			return value;
  		}
  
  		int length = value.length();
  		int start = pos;
  		int end = pos + key.length();
  
  		if (length == key.length()) {
  			value = replaceValue;
  		} else if (end == length) {
  			value = value.substring(0, start) + replaceValue;
  		} else {
  			value =
  				value.substring(0, start)
  					+ replaceValue
  					+ replace(value.substring(end), key, replaceValue);
  		}
  
  		return value;
  	}
  
  	/**
  	 * Convenience method for getting a value from a bean property as a 
  	 * <code>String</code>.
  	 */
  	public static String getValueAsString(Object bean, String property) {
  		Object value = null;
  
  		try {
  			value = PropertyUtils.getProperty(bean, property);
  
  		} catch (IllegalAccessException e) {
  			log.error(e.getMessage(), e);
  		} catch (InvocationTargetException e) {
  			log.error(e.getMessage(), e);
  		} catch (NoSuchMethodException e) {
  			log.error(e.getMessage(), e);
  		}
  
  		return (value != null ? value.toString() : null);
  	}
  
  	/**
  	 * Makes a deep copy of a <code>FastHashMap</code> if the values 
  	 * are <code>String</code>, <code>Msg</code>, <code>Arg</code>,

  	 * or <code>Var</code>.  Otherwise it is a shallow copy.
  	 * 
  	 * @param map <code>FastHashMap</code> to copy.
  	 * @return FastHashMap A copy of the <code>FastHashMap</code> that was 
  	 * passed in.
  	 */
  	public static FastHashMap copyFastHashMap(FastHashMap map) {
  		FastHashMap results = new FastHashMap();
  
  		Iterator i = map.keySet().iterator();
  		while (i.hasNext()) {
  			String key = (String) i.next();
  			Object value = map.get(key);
  
  			// TODO I don't think Strings need to be copied and do we really need to cast
  			// before cloning?
  			if (value instanceof String) {
  				results.put(key, new String((String) value));
  			} else if (value instanceof Msg) {
  				results.put(key, ((Msg) value).clone());
  			} else if (value instanceof Arg) {
  				results.put(key, ((Arg) value).clone());
  			} else if (value instanceof Var) {
  				results.put(key, ((Var) value).clone());
  			} else {
  				results.put(key, value);
  			}
  		}
  
  		results.setFast(true);
  
  		return results;
  	}
  
  }
  
  
  

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