001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.binding.format.support;
017:
018: import java.text.ParseException;
019:
020: import org.springframework.binding.format.Formatter;
021: import org.springframework.binding.format.InvalidFormatException;
022: import org.springframework.util.Assert;
023: import org.springframework.util.StringUtils;
024:
025: /**
026: * Abstract base class for all formatters.
027: *
028: * @author Keith Donald
029: */
030: public abstract class AbstractFormatter implements Formatter {
031:
032: /**
033: * Does this formatter allow empty values?
034: */
035: private boolean allowEmpty = true;
036:
037: /**
038: * Constructs a formatter.
039: */
040: protected AbstractFormatter() {
041: }
042:
043: /**
044: * Constructs a formatter.
045: * @param allowEmpty allow formatting of empty (null or blank) values?
046: */
047: protected AbstractFormatter(boolean allowEmpty) {
048: this .allowEmpty = allowEmpty;
049: }
050:
051: /**
052: * Allow formatting of empty (null or blank) values?
053: */
054: public boolean isAllowEmpty() {
055: return allowEmpty;
056: }
057:
058: public final String formatValue(Object value) {
059: if (allowEmpty && isEmpty(value)) {
060: return getEmptyFormattedValue();
061: }
062: Assert.isTrue(!isEmpty(value),
063: "Object to format cannot be empty");
064: return doFormatValue(value);
065: }
066:
067: /**
068: * Template method subclasses should override to encapsulate formatting
069: * logic.
070: * @param value the value to format
071: * @return the formatted string representation
072: */
073: protected abstract String doFormatValue(Object value);
074:
075: /**
076: * Returns the formatted form of an empty value. Default implementation
077: * just returns the empty string.
078: */
079: protected String getEmptyFormattedValue() {
080: return "";
081: }
082:
083: public final Object parseValue(String formattedString,
084: Class targetClass) throws InvalidFormatException {
085: try {
086: if (allowEmpty && isEmpty(formattedString)) {
087: return getEmptyValue();
088: }
089: return doParseValue(formattedString, targetClass);
090: } catch (ParseException ex) {
091: throw new InvalidFormatException(formattedString,
092: getExpectedFormat(targetClass), ex);
093: }
094: }
095:
096: /**
097: * Template method subclasses should override to encapsulate parsing logic.
098: * @param formattedString the formatted string to parse
099: * @return the parsed value
100: * @throws InvalidFormatException an exception occured parsing
101: * @throws ParseException when parse exceptions occur
102: */
103: protected abstract Object doParseValue(String formattedString,
104: Class targetClass) throws InvalidFormatException,
105: ParseException;
106:
107: /**
108: * Returns the empty value (resulting from parsing an empty input string).
109: * This default implementation just returns null.
110: */
111: protected Object getEmptyValue() {
112: return null;
113: }
114:
115: /**
116: * Returns the expected string format for the given target class.
117: * The default implementation just returns null.
118: */
119: protected String getExpectedFormat(Class targetClass) {
120: return null;
121: }
122:
123: /**
124: * Is given object <i>empty</i> (null or empty string)?
125: */
126: protected boolean isEmpty(Object o) {
127: if (o == null) {
128: return true;
129: } else if (o instanceof String) {
130: return !StringUtils.hasText((String) o);
131: } else {
132: return false;
133: }
134: }
135: }
|