KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > converters > BooleanArrayConverter


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 java.util.List JavaDoc;
22 import org.apache.commons.beanutils.ConversionException;
23 import org.apache.commons.beanutils.Converter;
24
25
26 /**
27  * <p>Standard {@link Converter} implementation that converts an incoming
28  * String into a primitive array of boolean. On a conversion failure, returns
29  * a specified default value or throws a {@link ConversionException} depending
30  * on how this instance is constructed.</p>
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.7 $ $Date: 2004/02/28 13:18:34 $
34  * @since 1.4
35  */

36
37 public final class BooleanArrayConverter extends AbstractArrayConverter {
38
39
40     // ----------------------------------------------------------- Constructors
41

42
43     /**
44      * Create a {@link Converter} that will throw a {@link ConversionException}
45      * if a conversion error occurs.
46      */

47     public BooleanArrayConverter() {
48
49         this.defaultValue = null;
50         this.useDefault = false;
51
52     }
53
54
55     /**
56      * Create a {@link Converter} that will return the specified default value
57      * if a conversion error occurs.
58      *
59      * @param defaultValue The default value to be returned
60      */

61     public BooleanArrayConverter(Object JavaDoc defaultValue) {
62
63         this.defaultValue = defaultValue;
64         this.useDefault = true;
65
66     }
67
68
69     // ------------------------------------------------------- Static Variables
70

71
72     /**
73      * <p>Model object for type comparisons.</p>
74      */

75     private static boolean model[] = new boolean[0];
76
77
78     // --------------------------------------------------------- Public Methods
79

80
81     /**
82      * Convert the specified input object into an output object of the
83      * specified type.
84      *
85      * @param type Data type to which this value should be converted
86      * @param value The input value to be converted
87      *
88      * @exception ConversionException if conversion cannot be performed
89      * successfully
90      */

91     public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
92
93         // Deal with a null value
94
if (value == null) {
95             if (useDefault) {
96                 return (defaultValue);
97             } else {
98                 throw new ConversionException("No value specified");
99             }
100         }
101
102         // Deal with the no-conversion-needed case
103
if (model.getClass() == value.getClass()) {
104             return (value);
105         }
106
107         // Deal with input value as a String array
108
if (strings.getClass() == value.getClass()) {
109             try {
110                 String JavaDoc values[] = (String JavaDoc[]) value;
111                 boolean results[] = new boolean[values.length];
112                 for (int i = 0; i < values.length; i++) {
113                     String JavaDoc stringValue = values[i];
114                     if (stringValue.equalsIgnoreCase("yes") ||
115                         stringValue.equalsIgnoreCase("y") ||
116                         stringValue.equalsIgnoreCase("true") ||
117                         stringValue.equalsIgnoreCase("on") ||
118                         stringValue.equalsIgnoreCase("1")) {
119                         results[i] = true;
120                     } else if (stringValue.equalsIgnoreCase("no") ||
121                                stringValue.equalsIgnoreCase("n") ||
122                                stringValue.equalsIgnoreCase("false") ||
123                                stringValue.equalsIgnoreCase("off") ||
124                                stringValue.equalsIgnoreCase("0")) {
125                         results[i] = false;
126                     } else {
127                         if (useDefault) {
128                             return (defaultValue);
129                         } else {
130                             throw new ConversionException(value.toString());
131                         }
132                     }
133                 }
134                 return (results);
135             } catch (Exception JavaDoc e) {
136                 if (useDefault) {
137                     return (defaultValue);
138                 } else {
139                     throw new ConversionException(value.toString(), e);
140                 }
141             }
142         }
143
144         // Parse the input value as a String into elements
145
// and convert to the appropriate type
146
try {
147             List JavaDoc list = parseElements(value.toString());
148             boolean results[] = new boolean[list.size()];
149             for (int i = 0; i < results.length; i++) {
150                 String JavaDoc stringValue = (String JavaDoc) list.get(i);
151                 if (stringValue.equalsIgnoreCase("yes") ||
152                     stringValue.equalsIgnoreCase("y") ||
153                     stringValue.equalsIgnoreCase("true") ||
154                     stringValue.equalsIgnoreCase("on") ||
155                     stringValue.equalsIgnoreCase("1")) {
156                     results[i] = true;
157                 } else if (stringValue.equalsIgnoreCase("no") ||
158                            stringValue.equalsIgnoreCase("n") ||
159                            stringValue.equalsIgnoreCase("false") ||
160                            stringValue.equalsIgnoreCase("off") ||
161                            stringValue.equalsIgnoreCase("0")) {
162                     results[i] = false;
163                 } else {
164                     if (useDefault) {
165                         return (defaultValue);
166                     } else {
167                         throw new ConversionException(value.toString());
168                     }
169                 }
170             }
171             return (results);
172         } catch (Exception JavaDoc e) {
173             if (useDefault) {
174                 return (defaultValue);
175             } else {
176                 throw new ConversionException(value.toString(), e);
177             }
178         }
179
180     }
181
182
183 }
184
Popular Tags