001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
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 de.odysseus.calyxo.forms.convert;
017:
018: import java.text.Format;
019:
020: import java.text.ParsePosition;
021: import java.text.ParseException;
022: import java.util.Locale;
023:
024: /**
025: * Format based converter.
026: *
027: * @author Christoph Beck
028: * @author Oliver Stuhr
029: */
030: public abstract class FormatBasedConverter extends SimpleConverter {
031: private Format format;
032: private String formattedNull;
033: private Object parsedNullOrEmpty;
034:
035: /**
036: * Default Constructor.
037: */
038: protected FormatBasedConverter() {
039: this (null, "");
040: }
041:
042: /**
043: * Constructor.
044: * @param parsedEmptyString empty string is converted into this value
045: * @param formattedNull null is formatted into this value
046: */
047: protected FormatBasedConverter(Object parsedEmptyString,
048: String formattedNull) {
049: setParsedNullOrEmpty(parsedEmptyString);
050: setFormattedNull(formattedNull);
051: }
052:
053: /**
054: * Cretate new format instamce
055: */
056: protected abstract Format createFormat(Locale locale);
057:
058: /**
059: * Get format instance.
060: * Lazily initialize format using {@link #createFormat(Locale)} (using default locale)
061: */
062: protected Format getFormat() {
063: if (format == null) {
064: setFormat(createFormat(Locale.getDefault()));
065: }
066: return format;
067: }
068:
069: protected String getFormattedNull() {
070: return formattedNull;
071: }
072:
073: protected void setFormattedNull(String formattedNull) {
074: this .formattedNull = formattedNull;
075: }
076:
077: protected Object getParsedNullOrEmpty() {
078: return parsedNullOrEmpty;
079: }
080:
081: protected void setParsedNullOrEmpty(Object parsedNullOrEmpty) {
082: this .parsedNullOrEmpty = parsedNullOrEmpty;
083: }
084:
085: protected void setFormat(Format format) {
086: this .format = format;
087: }
088:
089: /**
090: * Localize the receiver
091: */
092: public void localize(Locale locale) {
093: setFormat(createFormat(locale));
094: }
095:
096: /**
097: * Format method.
098: */
099: public String format(Object value) {
100: return value == null ? formattedNull : getFormat()
101: .format(value);
102: }
103:
104: /**
105: * Parse method.
106: */
107: public Object parse(String value) throws ParseException {
108: if (value == null || value.length() == 0)
109: return parsedNullOrEmpty;
110: else if (value.equals(formattedNull))
111: return null;
112: else {
113: ParsePosition pos = new ParsePosition(0);
114: Object result = getFormat().parseObject(value, pos);
115: if (pos.getIndex() < value.length())
116: throw new ParseException("Found garbage suffix!", pos
117: .getIndex());
118: return result;
119: }
120: }
121: }
|