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  
18  package org.apache.commons.beanutils.converters;
19  
20  
21  import org.apache.commons.beanutils.ConversionException;
22  import org.apache.commons.beanutils.Converter;
23  
24  import java.net.URL;
25  import java.net.MalformedURLException;
26  
27  
28  
29  /**
30   * <p>Standard {@link Converter} implementation that converts an incoming
31   * String into a <code>java.net.URL</code> object, optionally using a
32   * default value or throwing a {@link ConversionException} if a conversion
33   * error occurs.</p>
34   *
35   * @author Henri Yandell
36   * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:34 $
37   * @since 1.3
38   */
39  
40  public final class URLConverter implements Converter {
41  
42  
43      // ----------------------------------------------------------- Constructors
44  
45  
46      /**
47       * Create a {@link Converter} that will throw a {@link ConversionException}
48       * if a conversion error occurs.
49       */
50      public URLConverter() {
51  
52          this.defaultValue = null;
53          this.useDefault = false;
54  
55      }
56  
57  
58      /**
59       * Create a {@link Converter} that will return the specified default value
60       * if a conversion error occurs.
61       *
62       * @param defaultValue The default value to be returned
63       */
64      public URLConverter(Object defaultValue) {
65  
66          this.defaultValue = defaultValue;
67          this.useDefault = true;
68  
69      }
70  
71  
72      // ----------------------------------------------------- Instance Variables
73  
74  
75      /**
76       * The default value specified to our Constructor, if any.
77       */
78      private Object defaultValue = null;
79  
80  
81      /**
82       * Should we return the default value on conversion errors?
83       */
84      private boolean useDefault = true;
85  
86  
87      // --------------------------------------------------------- Public Methods
88  
89  
90      /**
91       * Convert the specified input object into an output object of the
92       * specified type.
93       *
94       * @param type Data type to which this value should be converted
95       * @param value The input value to be converted
96       *
97       * @exception ConversionException if conversion cannot be performed
98       *  successfully
99       */
100     public Object convert(Class type, Object value) {
101 
102         if (value == null) {
103             if (useDefault) {
104                 return (defaultValue);
105             } else {
106                 throw new ConversionException("No value specified");
107             }
108         }
109 
110         if (value instanceof URL) {
111             return (value);
112         }
113 
114         try {
115             return new URL(value.toString());
116         } catch(MalformedURLException murle) {
117             if (useDefault) {
118                 return (defaultValue);
119             } else {
120                 throw new ConversionException(murle);
121             }
122         }
123 
124     }
125 
126 
127 }