View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.beanutils.converters;
18  
19  import java.io.File;
20  
21  import org.apache.commons.beanutils.ConversionException;
22  import org.apache.commons.beanutils.Converter;
23  
24  /**
25   * <p>Standard {@link Converter} implementation that converts an incoming
26   * String into a <code>java.io.FileL</code> object, optionally using a
27   * default value or throwing a {@link ConversionException} if a conversion
28   * error occurs.</p>
29   *
30   * @author James Strachan
31   * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:34 $
32   * @since 1.6
33   */
34  public final class FileConverter implements Converter {
35  
36      // ----------------------------------------------------- Instance Variables
37  
38  
39      /**
40       * The default value specified to our Constructor, if any.
41       */
42      private Object defaultValue = null;
43  
44  
45      /**
46       * Should we return the default value on conversion errors?
47       */
48      private boolean useDefault = true;
49  
50  
51      // ----------------------------------------------------------- Constructors
52  
53  
54      /**
55       * Create a {@link Converter} that will throw a {@link ConversionException}
56       * if a conversion error occurs.
57       */
58      public FileConverter() {
59  
60          this.defaultValue = null;
61          this.useDefault = false;
62  
63      }
64  
65  
66      /**
67       * Create a {@link Converter} that will return the specified default value
68       * if a conversion error occurs.
69       *
70       * @param defaultValue The default value to be returned
71       */
72      public FileConverter(Object defaultValue) {
73  
74          this.defaultValue = defaultValue;
75          this.useDefault = true;
76  
77      }
78  
79  
80  
81      // --------------------------------------------------------- Public Methods
82  
83  
84      /**
85       * Convert the specified input object into an output object of the
86       * specified type.
87       *
88       * @param type Data type to which this value should be converted
89       * @param value The input value to be converted
90       *
91       * @exception ConversionException if conversion cannot be performed
92       *  successfully
93       */
94      public Object convert(Class type, Object value) {
95  
96          if (value == null) {
97              if (useDefault) {
98                  return (defaultValue);
99              } else {
100                 throw new ConversionException("No value specified");
101             }
102         }
103 
104         if (value instanceof File) {
105             return (value);
106         }
107 
108         return new File(value.toString());
109     }
110 }