Source Code Cross Referenced for StringConverter.java in  » J2EE » wicket » wicket » util » convert » 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 » J2EE » wicket » wicket.util.convert.converters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: StringConverter.java 457783 2005-10-02 10:06:33Z jcompagner $
003:         * $Revision: 457783 $ $Date: 2005-10-02 12:06:33 +0200 (Sun, 02 Oct 2005) $
004:         * 
005:         * ==============================================================================
006:         * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007:         * use this file except in compliance with the License. You may obtain a copy of
008:         * the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014:         * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015:         * License for the specific language governing permissions and limitations under
016:         * the License.
017:         */
018:        package wicket.util.convert.converters;
019:
020:        import java.sql.Timestamp;
021:        import java.util.Date;
022:        import java.util.HashMap;
023:        import java.util.Locale;
024:        import java.util.Map;
025:
026:        import wicket.util.convert.ITypeConverter;
027:
028:        /**
029:         * Converts objects to Strings. Since the formatting of Strings may vary
030:         * depending on the type of object being converted, you can register
031:         * ITypeConverters for each kind of formatting you want to support. For example,
032:         * the default StringConverter class registers a set of converters which convert
033:         * from various types to String, including DateToStringConverter, which converts
034:         * from Objects of type Date to Strings.
035:         * 
036:         * @author Eelco Hillenius
037:         * @author Jonathan Locke
038:         */
039:        public class StringConverter extends AbstractConverter {
040:            private static final long serialVersionUID = 1L;
041:
042:            /** Maps value Classes to specific StringConverters. */
043:            private final Map classToConverter = new HashMap();
044:            {
045:                DateToStringConverter dateConverter = new DateToStringConverter();
046:                NumberToStringConverter numberConverter = new NumberToStringConverter();
047:                set(Date.class, dateConverter);
048:                set(java.sql.Date.class, dateConverter);
049:                set(Timestamp.class, dateConverter);
050:                set(Float.class, numberConverter);
051:                set(Double.class, numberConverter);
052:            }
053:
054:            /**
055:             * Converter that is to be used when no registered converter is found.
056:             */
057:            private ITypeConverter defaultConverter = new ITypeConverter() {
058:                private static final long serialVersionUID = 1L;
059:
060:                /**
061:                 * @see wicket.util.convert.ITypeConverter#convert(java.lang.Object,java.util.Locale)
062:                 */
063:                public Object convert(final Object value, Locale locale) {
064:                    return value.toString();
065:                }
066:            };
067:
068:            /**
069:             * Construct.
070:             */
071:            public StringConverter() {
072:            }
073:
074:            /**
075:             * Removes all registered string converters.
076:             */
077:            public void clear() {
078:                classToConverter.clear();
079:            }
080:
081:            /**
082:             * @see wicket.util.convert.ITypeConverter#convert(java.lang.Object,java.util.Locale)
083:             */
084:            public Object convert(final Object value, Locale locale) {
085:                // Null is always converted to null
086:                if (value == null) {
087:                    return null;
088:                }
089:
090:                // Catch all cases where value is already the right type
091:                if (value instanceof  String) {
092:                    return value;
093:                }
094:
095:                // Get string converter for value's class
096:                final Class c = value.getClass();
097:                ITypeConverter converter = get(c);
098:                if (converter == null) {
099:                    return defaultConverter.convert(value, locale);
100:                }
101:
102:                // Use type converter to convert to value
103:                return converter.convert(value, locale);
104:            }
105:
106:            /**
107:             * Gets the type converter that is registered for class c.
108:             * 
109:             * @param c
110:             *            The class to get the type converter for
111:             * @return The type converter that is registered for class c or null if no
112:             *         type converter was registered for class c
113:             */
114:            public ITypeConverter get(final Class c) {
115:                return (ITypeConverter) classToConverter.get(c);
116:            }
117:
118:            /**
119:             * Gets the converter that is to be used when no registered converter is
120:             * found.
121:             * 
122:             * @return the converter that is to be used when no registered converter is
123:             *         found
124:             */
125:            public final ITypeConverter getDefaultConverter() {
126:                return defaultConverter;
127:            }
128:
129:            /**
130:             * Removes the type converter currently registered for class c.
131:             * 
132:             * @param c
133:             *            The class for which the converter registration should be
134:             *            removed
135:             * @return The converter that was registered for class c before removal or
136:             *         null if none was registered
137:             */
138:            public ITypeConverter remove(final Class c) {
139:                return (ITypeConverter) classToConverter.remove(c);
140:            }
141:
142:            /**
143:             * Registers a converter for use with class c.
144:             * 
145:             * @param converter
146:             *            The converter to add
147:             * @param c
148:             *            The class for which the converter should be used
149:             * @return The previous registered converter for class c or null if none was
150:             *         registered yet for class c
151:             */
152:            public ITypeConverter set(final Class c,
153:                    final ITypeConverter converter) {
154:                if (converter == null) {
155:                    throw new IllegalArgumentException(
156:                            "Converter cannot be null");
157:                }
158:                if (c == null) {
159:                    throw new IllegalArgumentException("Class cannot be null");
160:                }
161:                return (ITypeConverter) classToConverter.put(c, converter);
162:            }
163:
164:            /**
165:             * Sets the converter that is to be used when no registered converter is
166:             * found. This allows converter chaining.
167:             * 
168:             * @param defaultConverter
169:             *            The converter that is to be used when no registered converter
170:             *            is found
171:             */
172:            public final void setDefaultConverter(
173:                    final ITypeConverter defaultConverter) {
174:                this .defaultConverter = defaultConverter;
175:            }
176:
177:            /**
178:             * @see wicket.util.convert.converters.AbstractConverter#getTargetType()
179:             */
180:            protected Class getTargetType() {
181:                return String.class;
182:            }
183:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.