KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > format > support > AbstractFormatter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.format.support;
17
18 import java.text.ParseException JavaDoc;
19
20 import org.springframework.binding.format.Formatter;
21 import org.springframework.binding.format.InvalidFormatException;
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24
25 /**
26  * Base template class for all formatters (also implements type converter for
27  * those who need general type conversion.)
28  * @author Keith Donald
29  */

30 public abstract class AbstractFormatter implements Formatter {
31
32     /**
33      * Does this formatter allow empty values?
34      */

35     private boolean allowEmpty = true;
36
37     /**
38      * Constructs a formatter.
39      */

40     protected AbstractFormatter() {
41     }
42
43     /**
44      * Constructs a formatter.
45      * @param allowEmpty allow formatting of empty (null or blank) values?
46      */

47     protected AbstractFormatter(boolean allowEmpty) {
48         this.allowEmpty = allowEmpty;
49     }
50
51     public final String JavaDoc formatValue(Object JavaDoc value) {
52         if (allowEmpty && isEmpty(value)) {
53             return getEmptyFormattedValue();
54         }
55         Assert.isTrue(!isEmpty(value), "Object to format cannot be empty");
56         return doFormatValue(value);
57     }
58
59     /**
60      * Template method subclasses should override to encapsulate formatting
61      * logic.
62      * @param value the value to format
63      * @return the formatted string representation
64      */

65     protected abstract String JavaDoc doFormatValue(Object JavaDoc value);
66
67     protected String JavaDoc getEmptyFormattedValue() {
68         return "";
69     }
70
71     /**
72      * Template method subclasses should override to encapsulate parsing logic.
73      * @param formattedString the formatted string to parse
74      * @return the parsed value
75      * @throws InvalidFormatException an exception occured parsing
76      */

77     public final Object JavaDoc parseValue(String JavaDoc formattedString, Class JavaDoc targetClass) throws InvalidFormatException {
78         try {
79             if (allowEmpty && isEmpty(formattedString)) {
80                 return getEmptyValue();
81             }
82             return doParseValue(formattedString, targetClass);
83         }
84         catch (ParseException JavaDoc ex) {
85             throw new InvalidFormatException(formattedString, getExpectedFormat(targetClass), ex);
86         }
87     }
88
89     protected Object JavaDoc getEmptyValue() {
90         return null;
91     }
92
93     protected String JavaDoc getExpectedFormat(Class JavaDoc targetClass) {
94         return null;
95     }
96
97     protected abstract Object JavaDoc doParseValue(String JavaDoc formattedString, Class JavaDoc targetClass) throws InvalidFormatException,
98             ParseException JavaDoc;
99
100     protected boolean isEmpty(Object JavaDoc o) {
101         if (o == null) {
102             return true;
103         }
104         else if (o instanceof String JavaDoc) {
105             return !StringUtils.hasText((String JavaDoc)o);
106         }
107         else {
108             return false;
109         }
110     }
111
112     public boolean isAllowEmpty() {
113         return allowEmpty;
114     }
115 }
Popular Tags