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