jstrachan    2003/04/05 02:16:55

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        ConvertUtilsBean.java
  Added:       beanutils/src/java/org/apache/commons/beanutils/converters
                        FileConverter.java
  Log:
  Added a File converter and registered the File and URL converters by default
  
  Revision  Changes    Path
  1.1                  
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FileConverter.java
  
  Index: FileConverter.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FileConverter.java,v
 1.1 2003/04/05 10:16:55 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/05 10:16:55 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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.beanutils.converters;
  
  import java.io.File;
  
  import org.apache.commons.beanutils.ConversionException;
  import org.apache.commons.beanutils.Converter;
  
  /**
   * <p>Standard [EMAIL PROTECTED] Converter} implementation that converts an incoming
   * String into a <code>java.io.FileL</code> object, optionally using a
   * default value or throwing a [EMAIL PROTECTED] ConversionException} if a conversion
   * error occurs.</p>
   *
   * @author James Strachan
   * @version $Revision: 1.1 $ $Date: 2003/04/05 10:16:55 $
   * @since 1.6
   */
  public final class FileConverter implements Converter {
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The default value specified to our Constructor, if any.
       */
      private Object defaultValue = null;
  
  
      /**
       * Should we return the default value on conversion errors?
       */
      private boolean useDefault = true;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL PROTECTED] 
ConversionException}
       * if a conversion error occurs.
       */
      public FileConverter() {
  
          this.defaultValue = null;
          this.useDefault = false;
  
      }
  
  
      /**
       * Create a [EMAIL PROTECTED] Converter} that will return the specified default 
value
       * if a conversion error occurs.
       *
       * @param defaultValue The default value to be returned
       */
      public FileConverter(Object defaultValue) {
  
          this.defaultValue = defaultValue;
          this.useDefault = true;
  
      }
  
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Convert the specified input object into an output object of the
       * specified type.
       *
       * @param type Data type to which this value should be converted
       * @param value The input value to be converted
       *
       * @exception ConversionException if conversion cannot be performed
       *  successfully
       */
      public Object convert(Class type, Object value) {
  
          if (value == null) {
              if (useDefault) {
                  return (defaultValue);
              } else {
                  throw new ConversionException("No value specified");
              }
          }
  
          if (value instanceof File) {
              return (value);
          }
  
          return new File(value.toString());
      }
  }
  
  
  
  1.4       +13 -4     
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java
  
  Index: ConvertUtilsBean.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ConvertUtilsBean.java     20 Mar 2003 20:12:24 -0000      1.3
  +++ ConvertUtilsBean.java     5 Apr 2003 10:16:55 -0000       1.4
  @@ -63,9 +63,11 @@
   package org.apache.commons.beanutils;
   
   
  +import java.io.File;
   import java.lang.reflect.Array;
   import java.math.BigDecimal;
   import java.math.BigInteger;
  +import java.net.URL;
   import java.sql.Date;
   import java.sql.Time;
   import java.sql.Timestamp;
  @@ -80,6 +82,7 @@
   import org.apache.commons.beanutils.converters.ClassConverter;
   import org.apache.commons.beanutils.converters.DoubleConverter;
   import org.apache.commons.beanutils.converters.DoubleArrayConverter;
  +import org.apache.commons.beanutils.converters.FileConverter;
   import org.apache.commons.beanutils.converters.FloatConverter;
   import org.apache.commons.beanutils.converters.FloatArrayConverter;
   import org.apache.commons.beanutils.converters.IntegerConverter;
  @@ -93,6 +96,7 @@
   import org.apache.commons.beanutils.converters.SqlTimestampConverter;
   import org.apache.commons.beanutils.converters.StringConverter;
   import org.apache.commons.beanutils.converters.StringArrayConverter;
  +import org.apache.commons.beanutils.converters.URLConverter;
   import org.apache.commons.collections.FastHashMap;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -118,6 +122,8 @@
    * <li>long and java.lang.Long</li>
    * <li>short and java.lang.Short</li>
    * <li>java.lang.String</li>
  + * <li>java.io.File</li>
  + * <li>java.net.URL</li>
    * <li>java.sql.Date</li>
    * <li>java.sql.Time</li>
    * <li>java.sql.Timestamp</li>
  @@ -141,6 +147,7 @@
    * @author Craig R. McClanahan
    * @author Ralph Schaer
    * @author Chris Audley
  + * @author James Strachan
    * @version $Revision$ $Date$
    * @since 1.7
    */
  @@ -570,6 +577,8 @@
           converters.put(Date.class, new SqlDateConverter());
           converters.put(Time.class, new SqlTimeConverter());
           converters.put(Timestamp.class, new SqlTimestampConverter());
  +        converters.put(File.class, new FileConverter());
  +        converters.put(URL.class, new URLConverter());
   
       }
   
  
  
  

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

Reply via email to