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: }
|