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