01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 de.odysseus.calyxo.forms;
17:
18: import java.text.ParseException;
19: import java.util.Locale;
20:
21: import javax.servlet.http.HttpServletRequest;
22:
23: /**
24: * Converter interface.
25: * A converter can parse a string to an object value and format it back
26: * to a string.
27: * Implementors must have an empty constructor. Additionally to the
28: * methods required for a validation engine they have to implement the
29: * {@link #parse(HttpServletRequest, String)} and
30: * {@link #format(HttpServletRequest, Object)} methods.
31: *
32: * @author Christoph Beck
33: * @author Oliver Stuhr
34: */
35: public interface Converter extends ValidatorBase {
36: /**
37: * Static "do-nothing" converter instance
38: */
39: public static final Converter IDENTITY = new Converter() {
40: public String format(HttpServletRequest request, Object value) {
41: return value == null ? null : value.toString();
42: }
43:
44: public Object parse(HttpServletRequest request, String value) {
45: return value;
46: }
47:
48: public void localize(Locale locale) {
49: }
50:
51: public boolean isSharable() {
52: return true;
53: }
54: };
55:
56: /**
57: * Formats the given value to a string.
58: * @return formatted string
59: */
60: public String format(HttpServletRequest request, Object value);
61:
62: /**
63: * Parses the given string to an object.
64: * @return converted value
65: */
66: public Object parse(HttpServletRequest request, String value)
67: throws ParseException;
68: }
|