Source Code Cross Referenced for BooleanArrayConverter.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » converters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils.converters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.beanutils.converters;
019:
020:        import java.util.List;
021:        import org.apache.commons.beanutils.ConversionException;
022:
023:        /**
024:         * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
025:         * String into a primitive array of boolean.  On a conversion failure, returns
026:         * a specified default value or throws a {@link ConversionException} depending
027:         * on how this instance is constructed.</p>
028:         *
029:         * <p>By default, the values to be converted are expected to be those
030:         * recognised by a default instance of BooleanConverter. A customised
031:         * BooleanConverter can be provided in order to recognise alternative values
032:         * as true/false. </p>
033:         *
034:         * @author Craig R. McClanahan
035:         * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
036:         * @since 1.4
037:         * @deprecated Replaced by the new {@link ArrayConverter} implementation
038:         */
039:
040:        public final class BooleanArrayConverter extends AbstractArrayConverter {
041:
042:            // ----------------------------------------------------------- Constructors
043:
044:            /**
045:             * Create a {@link org.apache.commons.beanutils.Converter} that will throw
046:             * a {@link ConversionException} if a conversion error occurs.
047:             *
048:             * <p>Conversion of strings to boolean values will be done via a default
049:             * instance of class BooleanConverter.</p>
050:             */
051:            public BooleanArrayConverter() {
052:
053:                super ();
054:                this .booleanConverter = DEFAULT_CONVERTER;
055:
056:            }
057:
058:            /**
059:             * Create a {@link org.apache.commons.beanutils.Converter} that will return
060:             * the specified default value if a conversion error occurs.
061:             *
062:             * <p>Conversion of strings to boolean values will be done via a default
063:             * instance of class BooleanConverter.</p>
064:             *
065:             * @param defaultValue The default value to be returned
066:             */
067:            public BooleanArrayConverter(Object defaultValue) {
068:
069:                super (defaultValue);
070:                this .booleanConverter = DEFAULT_CONVERTER;
071:
072:            }
073:
074:            /**
075:             * Create a {@link org.apache.commons.beanutils.Converter} that will return
076:             * the specified default value if a conversion error occurs.
077:             *
078:             * <p>Conversion of strings to boolean values will be done via the
079:             * specified converter.</p>
080:             *
081:             * @param converter is the converter object that will be used to
082:             *  convert each input string-value into a boolean.
083:             *
084:             * @param defaultValue is the default value to be returned by method
085:             * convert if conversion fails; null is a valid default value. See the
086:             * documentation for method "convert" for more information.
087:             * The value BooleanArrayConverter.NO_DEFAULT may be passed here to
088:             * specify that an exception should be thrown on conversion failure.
089:             *  
090:             */
091:            public BooleanArrayConverter(BooleanConverter converter,
092:                    Object defaultValue) {
093:
094:                super (defaultValue);
095:                this .booleanConverter = converter;
096:
097:            }
098:
099:            // ------------------------------------------------------- Static Variables
100:
101:            /**
102:             * Type which this class converts its input to. This value can be
103:             * used as a parameter to the ConvertUtils.register method.
104:             */
105:            public static final Class MODEL = new boolean[0].getClass();
106:
107:            /**
108:             * The converter that all instances of this class will use to
109:             * do individual string->boolean conversions, unless overridden
110:             * in the constructor.
111:             */
112:            private static final BooleanConverter DEFAULT_CONVERTER = new BooleanConverter();
113:
114:            // ---------------------------------------------------- Instance Variables
115:
116:            /**
117:             * This object is used to perform the conversion of individual strings
118:             * into Boolean/boolean values.
119:             */
120:            protected final BooleanConverter booleanConverter;
121:
122:            // --------------------------------------------------------- Public Methods
123:
124:            /**
125:             * Convert the specified input object into an output object of type
126:             * array-of-boolean.
127:             * 
128:             * <p>If the input value is null, then the default value specified in the
129:             * constructor is returned. If no such value was provided, then a
130:             * ConversionException is thrown instead.</p>
131:             *
132:             * <p>If the input value is of type String[] then the returned array shall
133:             * be of the same size as this array, with a true or false value in each
134:             * array element depending on the result of applying method
135:             * BooleanConverter.convert to each string.</p>
136:             *
137:             * <p>For all other types of value, the object's toString method is
138:             * expected to return a string containing a comma-separated list of
139:             * values, eg "true, false, true". See the documentation for
140:             * {@link AbstractArrayConverter#parseElements} for more information on
141:             * the exact formats supported.</p>
142:             *
143:             * <p>If the result of value.toString() cannot be split into separate
144:             * words, then the default value is also returned (or an exception thrown).
145:             * </p>
146:             *
147:             * <p>If any of the elements in the value array (or the elements resulting
148:             * from splitting up value.toString) are not recognised by the
149:             * BooleanConverter associated with this object, then what happens depends
150:             * on whether that BooleanConverter has a default value or not: if it does, 
151:             * then that unrecognised element is converted into the BooleanConverter's
152:             * default value. If the BooleanConverter does <i>not</i> have a default
153:             * value, then the default value for this object is returned as the
154:             * <i>complete</i> conversion result (not just for the element), or an 
155:             * exception is thrown if this object has no default value defined.</p>
156:             *
157:             * @param type is the type to which this value should be converted. In the
158:             *  case of this BooleanArrayConverter class, this value is ignored.
159:             *
160:             * @param value is the input value to be converted.
161:             *
162:             * @return an object of type boolean[], or the default value if there was
163:             *  any sort of error during conversion and the constructor
164:             *  was provided with a default value.
165:             *
166:             * @exception ConversionException if conversion cannot be performed
167:             *  successfully and the constructor was not provided with a default
168:             *  value to return on conversion failure.
169:             *
170:             * @exception NullPointerException if value is an array, and any of the
171:             * array elements are null.
172:             */
173:            public Object convert(Class type, Object value) {
174:
175:                // Deal with a null value
176:                if (value == null) {
177:                    if (useDefault) {
178:                        return (defaultValue);
179:                    } else {
180:                        throw new ConversionException("No value specified");
181:                    }
182:                }
183:
184:                // Deal with the no-conversion-needed case
185:                if (MODEL == value.getClass()) {
186:                    return (value);
187:                }
188:
189:                // Deal with input value as a String array
190:                //
191:                // TODO: use if (value.getClass().isArray() instead...
192:                //  this requires casting to Object[], then using values[i].toString()
193:                if (strings.getClass() == value.getClass()) {
194:                    try {
195:                        String[] values = (String[]) value;
196:                        boolean[] results = new boolean[values.length];
197:                        for (int i = 0; i < values.length; i++) {
198:                            String stringValue = values[i];
199:                            Object result = booleanConverter.convert(
200:                                    Boolean.class, stringValue);
201:                            results[i] = ((Boolean) result).booleanValue();
202:                        }
203:                        return (results);
204:                    } catch (Exception e) {
205:                        if (useDefault) {
206:                            return (defaultValue);
207:                        } else {
208:                            throw new ConversionException(value.toString(), e);
209:                        }
210:                    }
211:                }
212:
213:                // We only get here if the input value is not of type String[].
214:                // In this case, we assume value.toString() returns a comma-separated
215:                // sequence of values; see method AbstractArrayConverter.parseElements
216:                // for more information.
217:                try {
218:                    List list = parseElements(value.toString());
219:                    boolean[] results = new boolean[list.size()];
220:                    for (int i = 0; i < results.length; i++) {
221:                        String stringValue = (String) list.get(i);
222:                        Object result = booleanConverter.convert(Boolean.class,
223:                                stringValue);
224:                        results[i] = ((Boolean) result).booleanValue();
225:                    }
226:                    return (results);
227:                } catch (Exception e) {
228:                    if (useDefault) {
229:                        return (defaultValue);
230:                    } else {
231:                        throw new ConversionException(value.toString(), e);
232:                    }
233:                }
234:
235:            }
236:
237:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.