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 FloatConverter implements Converter {
36: public static final String CONVERTER_ID = "javax.faces.Float";
37: public static final String FLOAT_ID = "javax.faces.converter.FloatConverter.FLOAT";
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 Float.parseFloat(value);
54: } catch (NumberFormatException e) {
55: String summary = Util.l10n(context, FLOAT_ID,
56: "{2}: \"{0}\" must be a floating-point number.",
57: value, getExample(), Util.getLabel(context,
58: component));
59:
60: String detail = Util
61: .l10n(
62: context,
63: FLOAT_ID + "_detail",
64: "{2}: \"{0}\" must be a number between 1.4E-45 and 3.4E38. Example: {1}.",
65: value, getExample(), Util.getLabel(context,
66: component));
67:
68: FacesMessage msg = new FacesMessage(summary, detail);
69:
70: throw new ConverterException(msg, e);
71: }
72: }
73:
74: public String getAsString(FacesContext context,
75: UIComponent component, Object value)
76: throws ConverterException {
77: // XXX: incorrect
78: if (value == null)
79: return "";
80: else if (value instanceof String)
81: return (String) value;
82: else
83: return value.toString();
84: }
85:
86: private String getExample() {
87: return "25.34";
88: }
89:
90: public String toString() {
91: return "FloatConverter[]";
92: }
93: }
|