01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * Resin Open Source is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License version 2
11: * as published by the Free Software Foundation.
12: *
13: * Resin Open Source is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
16: * of NON-INFRINGEMENT. See the GNU General Public License for more
17: * details.
18: *
19: * You should have received a copy of the GNU General Public License
20: * along with Resin Open Source; if not, write to the
21: *
22: * Free Software Foundation, Inc.
23: * 59 Temple Place, Suite 330
24: * Boston, MA 02111-1307 USA
25: *
26: * @author Scott Ferguson
27: */
28:
29: package javax.faces.convert;
30:
31: import javax.faces.application.*;
32: import javax.faces.context.*;
33: import javax.faces.component.*;
34:
35: public class IntegerConverter implements Converter {
36: public static final String CONVERTER_ID = "javax.faces.Integer";
37: public static final String INTEGER_ID = "javax.faces.converter.IntegerConverter.INTEGER";
38: public static final String STRING_ID = "javax.faces.converter.STRING";
39:
40: public Object getAsObject(FacesContext context,
41: UIComponent component, String value)
42: throws ConverterException {
43: // XXX: incorrect
44: if (value == null)
45: return null;
46:
47: value = value.trim();
48:
49: if (value.length() == 0)
50: return null;
51:
52: try {
53: return Integer.decode(value);
54: } catch (NumberFormatException e) {
55: String summary = Util.l10n(context, INTEGER_ID,
56: "{2}: \"{0}\" must be an integer number.", value,
57: getExample(), Util.getLabel(context, component));
58:
59: String detail = Util
60: .l10n(
61: context,
62: INTEGER_ID + "_detail",
63: "{2}: \"{0}\" must be a number between -2147483648 and 2147483647. Example: {1}.",
64: value, getExample(), Util.getLabel(context,
65: component));
66:
67: FacesMessage msg = new FacesMessage(summary, detail);
68:
69: throw new ConverterException(msg, e);
70: }
71: }
72:
73: public String getAsString(FacesContext context,
74: UIComponent component, Object value)
75: throws ConverterException {
76: // XXX: incorrect
77: if (value == null)
78: return "";
79: else if (value instanceof String)
80: return (String) value;
81: else
82: return value.toString();
83: }
84:
85: private String getExample() {
86: return "112";
87: }
88:
89: public String toString() {
90: return "IntegerConverter[]";
91: }
92: }
|