KickJava   Java API By Example, From Geeks To Geeks.

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


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 an array of String. 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 StringArrayConverter 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 StringArrayConverter() {
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 StringArrayConverter(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 String JavaDoc model[] = new String JavaDoc[0];
76     
77     /**
78      * <p> Model object for int arrays.</p>
79      */

80     private static int ints[] = new int[0];
81
82
83
84     // --------------------------------------------------------- Public Methods
85

86
87     /**
88      * Convert the specified input object into an output object of the
89      * specified type.
90      *
91      * @param type Data type to which this value should be converted
92      * @param value The input value to be converted
93      *
94      * @exception ConversionException if conversion cannot be performed
95      * successfully
96      */

97     public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
98
99         // Deal with a null value
100
if (value == null) {
101             if (useDefault) {
102                 return (defaultValue);
103             } else {
104                 throw new ConversionException("No value specified");
105             }
106         }
107
108         // Deal with the no-conversion-needed case
109
if (model.getClass() == value.getClass()) {
110             return (value);
111         }
112
113         // Deal with the input value as an int array
114
if (ints.getClass() == value.getClass())
115         {
116             int[] values = (int[]) value;
117             String JavaDoc[] results = new String JavaDoc[values.length];
118             for (int i = 0; i < values.length; i++)
119             {
120                 results[i] = Integer.toString(values[i]);
121             }
122
123             return (results);
124         }
125
126         // Parse the input value as a String into elements
127
// and convert to the appropriate type
128
try {
129             List JavaDoc list = parseElements(value.toString());
130             String JavaDoc results[] = new String JavaDoc[list.size()];
131             for (int i = 0; i < results.length; i++) {
132                 results[i] = (String JavaDoc) list.get(i);
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
145
146 }
147
Popular Tags