Source Code Cross Referenced for AbstractArrayConverter.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.io.IOException;
021:        import java.io.StreamTokenizer;
022:        import java.io.StringReader;
023:        import java.util.ArrayList;
024:        import java.util.List;
025:        import org.apache.commons.beanutils.ConversionException;
026:        import org.apache.commons.beanutils.Converter;
027:
028:        /**
029:         * <p>Convenience base class for converters that translate the String
030:         * representation of an array into a corresponding array of primitives
031:         * object.  This class encapsulates the functionality required to parse
032:         * the String into a list of String elements that can later be
033:         * individually converted to the appropriate primitive type.</p>
034:         *
035:         * <p>The input syntax accepted by the <code>parseElements()</code> method
036:         * is designed to be compatible with the syntax used to initialize arrays
037:         * in a Java source program, except that only String literal values are
038:         * supported.  For maximum flexibility, the surrounding '{' and '}'
039:         * characters are optional, and individual elements may be separated by
040:         * any combination of whitespace and comma characters.</p>
041:         *
042:         * @author Craig R. McClanahan
043:         * @version $Revision: 557808 $ $Date: 2007-07-20 00:05:03 +0100 (Fri, 20 Jul 2007) $
044:         * @since 1.4
045:         * @deprecated Replaced by the new {@link ArrayConverter} implementation
046:         */
047:
048:        public abstract class AbstractArrayConverter implements  Converter {
049:
050:            // ----------------------------------------------------------- Constructors
051:
052:            /**
053:             * Create a {@link Converter} that will throw a {@link ConversionException}
054:             * if a conversion error occurs.
055:             */
056:            public AbstractArrayConverter() {
057:
058:                this .defaultValue = null;
059:                this .useDefault = false;
060:
061:            }
062:
063:            /**
064:             * Create a {@link Converter} that will return the specified default value
065:             * if a conversion error occurs.
066:             *
067:             * @param defaultValue The default value to be returned
068:             */
069:            public AbstractArrayConverter(Object defaultValue) {
070:
071:                if (defaultValue == NO_DEFAULT) {
072:                    this .useDefault = false;
073:                } else {
074:                    this .defaultValue = defaultValue;
075:                    this .useDefault = true;
076:                }
077:
078:            }
079:
080:            // ------------------------------------------------------- Static Variables
081:
082:            /**
083:             * This is a special reference that can be passed as the "default object"
084:             * to the constructor to indicate that no default is desired. Note that
085:             * the value 'null' cannot be used for this purpose, as the caller may
086:             * want a null to be returned as the default.
087:             */
088:            public static final Object NO_DEFAULT = new Object();
089:
090:            // ----------------------------------------------------- Instance Variables
091:
092:            /**
093:             * <p>Model object for string arrays.</p>
094:             */
095:            protected static String[] strings = new String[0];
096:
097:            /**
098:             * The default value specified to our Constructor, if any.
099:             */
100:            protected Object defaultValue = null;
101:
102:            /**
103:             * Should we return the default value on conversion errors?
104:             */
105:            protected boolean useDefault = true;
106:
107:            // --------------------------------------------------------- Public Methods
108:
109:            /**
110:             * Convert the specified input object into an output object of the
111:             * specified type.  This method must be implemented by a concrete
112:             * subclass.
113:             *
114:             * @param type Data type to which this value should be converted
115:             * @param value The input value to be converted
116:             * @return The converted value
117:             *
118:             * @exception ConversionException if conversion cannot be performed
119:             *  successfully
120:             */
121:            public abstract Object convert(Class type, Object value);
122:
123:            // ------------------------------------------------------ Protected Methods
124:
125:            /**
126:             * <p>Parse an incoming String of the form similar to an array initializer
127:             * in the Java language into a <code>List</code> individual Strings
128:             * for each element, according to the following rules.</p>
129:             * <ul>
130:             * <li>The string is expected to be a comma-separated list of values.</li>
131:             * <li>The string may optionally have matching '{' and '}' delimiters
132:             *   around the list.</li>
133:             * <li>Whitespace before and after each element is stripped.</li>
134:             * <li>Elements in the list may be delimited by single or double quotes.
135:             *  Within a quoted elements, the normal Java escape sequences are valid.</li>
136:             * </ul>
137:             *
138:             * @param svalue String value to be parsed
139:             * @return The parsed list of String values
140:             *
141:             * @exception ConversionException if the syntax of <code>svalue</code>
142:             *  is not syntactically valid
143:             * @exception NullPointerException if <code>svalue</code>
144:             *  is <code>null</code>
145:             */
146:            protected List parseElements(String svalue) {
147:
148:                // Validate the passed argument
149:                if (svalue == null) {
150:                    throw new NullPointerException();
151:                }
152:
153:                // Trim any matching '{' and '}' delimiters
154:                svalue = svalue.trim();
155:                if (svalue.startsWith("{") && svalue.endsWith("}")) {
156:                    svalue = svalue.substring(1, svalue.length() - 1);
157:                }
158:
159:                try {
160:
161:                    // Set up a StreamTokenizer on the characters in this String
162:                    StreamTokenizer st = new StreamTokenizer(new StringReader(
163:                            svalue));
164:                    st.whitespaceChars(',', ','); // Commas are delimiters
165:                    st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
166:                    st.ordinaryChars('.', '.');
167:                    st.ordinaryChars('-', '-');
168:                    st.wordChars('0', '9'); // Needed to make part of tokens
169:                    st.wordChars('.', '.');
170:                    st.wordChars('-', '-');
171:
172:                    // Split comma-delimited tokens into a List
173:                    ArrayList list = new ArrayList();
174:                    while (true) {
175:                        int ttype = st.nextToken();
176:                        if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
177:                            list.add(st.sval);
178:                        } else if (ttype == StreamTokenizer.TT_EOF) {
179:                            break;
180:                        } else {
181:                            throw new ConversionException(
182:                                    "Encountered token of type " + ttype);
183:                        }
184:                    }
185:
186:                    // Return the completed list
187:                    return (list);
188:
189:                } catch (IOException e) {
190:
191:                    throw new ConversionException(e);
192:
193:                }
194:
195:            }
196:
197:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.