Source Code Cross Referenced for StringArrayConverter.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:         * Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
025:         * String into an array of String objects. On a conversion failure, returns
026:         * a specified default value or throws a {@link ConversionException} depending
027:         * on how this instance is constructed.
028:         * <p>
029:         * There is also some special handling where the input is of type int[].
030:         * See method convert for more details.  
031:         *
032:         * @author Craig R. McClanahan
033:         * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
034:         * @since 1.4
035:         * @deprecated Replaced by the new {@link ArrayConverter} implementation
036:         */
037:
038:        public final class StringArrayConverter extends AbstractArrayConverter {
039:
040:            // ----------------------------------------------------------- Constructors
041:
042:            /**
043:             * Create a {@link org.apache.commons.beanutils.Converter} that will throw
044:             * a {@link ConversionException} if a conversion error occurs.
045:             */
046:            public StringArrayConverter() {
047:
048:                this .defaultValue = null;
049:                this .useDefault = false;
050:
051:            }
052:
053:            /**
054:             * Create a {@link org.apache.commons.beanutils.Converter} that will return
055:             * the specified default value if a conversion error occurs.
056:             *
057:             * @param defaultValue The default value to be returned
058:             */
059:            public StringArrayConverter(Object defaultValue) {
060:
061:                this .defaultValue = defaultValue;
062:                this .useDefault = true;
063:
064:            }
065:
066:            // ------------------------------------------------------- Static Variables
067:
068:            /**
069:             * <p>Model object for type comparisons.</p>
070:             */
071:            private static final String[] MODEL = new String[0];
072:
073:            /**
074:             * <p> Model object for int arrays.</p>
075:             */
076:            private static final int[] INT_MODEL = new int[0];
077:
078:            // --------------------------------------------------------- Public Methods
079:
080:            /**
081:             * Convert the specified input object into an output object of the
082:             * specified type.
083:             * <p>
084:             * If the value is already of type String[] then it is simply returned
085:             * unaltered.
086:             * <p>
087:             * If the value is of type int[], then a String[] is returned where each
088:             * element in the string array is the result of calling Integer.toString
089:             * on the corresponding element of the int array. This was added as a
090:             * result of bugzilla request #18297 though there is not complete
091:             * agreement that this feature should have been added. 
092:             * <p>
093:             * In all other cases, this method calls toString on the input object, then
094:             * assumes the result is a comma-separated list of values. The values are 
095:             * split apart into the individual items and returned as the elements of an
096:             * array. See class AbstractArrayConverter for the exact input formats
097:             * supported.
098:             * 
099:             * @param type is the data type to which this value should be converted.
100:             * It is expected to be the class for type String[] (though this parameter
101:             * is actually ignored by this method).
102:             * 
103:             * @param value is the input value to be converted. If null then the
104:             * default value is returned or an exception thrown if no default value
105:             * exists.
106:             * @return the converted value
107:             *
108:             * @exception ConversionException if conversion cannot be performed
109:             * successfully, or the input is null and there is no default value set
110:             * for this object.
111:             */
112:            public Object convert(Class type, Object value) {
113:
114:                // Deal with a null value
115:                if (value == null) {
116:                    if (useDefault) {
117:                        return (defaultValue);
118:                    } else {
119:                        throw new ConversionException("No value specified");
120:                    }
121:                }
122:
123:                // Deal with the no-conversion-needed case
124:                if (MODEL.getClass() == value.getClass()) {
125:                    return (value);
126:                }
127:
128:                // Deal with the input value as an int array
129:                if (INT_MODEL.getClass() == value.getClass()) {
130:                    int[] values = (int[]) value;
131:                    String[] results = new String[values.length];
132:                    for (int i = 0; i < values.length; i++) {
133:                        results[i] = Integer.toString(values[i]);
134:                    }
135:
136:                    return (results);
137:                }
138:
139:                // Parse the input value as a String into elements
140:                // and convert to the appropriate type
141:                try {
142:                    List list = parseElements(value.toString());
143:                    String[] results = new String[list.size()];
144:                    for (int i = 0; i < results.length; i++) {
145:                        results[i] = (String) list.get(i);
146:                    }
147:                    return (results);
148:                } catch (Exception e) {
149:                    if (useDefault) {
150:                        return (defaultValue);
151:                    } else {
152:                        throw new ConversionException(value.toString(), e);
153:                    }
154:                }
155:            }
156:
157:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.