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 BooleanConverter implements Converter {
36: public static final String CONVERTER_ID = "javax.faces.Boolean";
37: public static final String BOOLEAN_ID = "javax.faces.converter.BooleanConverter.BOOLEAN";
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: if (context == null || component == null)
44: throw new NullPointerException();
45:
46: if (value == null)
47: return null;
48:
49: value = value.trim();
50:
51: if (value.length() == 0)
52: return null;
53:
54: try {
55: return Boolean.valueOf(value);
56: } catch (Exception e) {
57: String summary = Util.l10n(context, BOOLEAN_ID,
58: "{1}: \"{0}\" must be 'true' or 'false'.", value,
59: Util.getLabel(context, component));
60:
61: String detail = Util
62: .l10n(
63: context,
64: BOOLEAN_ID + "_detail",
65: "{1}: \"{0}\" must be 'true' or 'false'. Any value other than 'true' will evaluate to 'false'.",
66: value, Util.getLabel(context, 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: if (context == null || component == null)
78: throw new NullPointerException();
79:
80: if (value == null)
81: return "";
82: else
83: return value.toString();
84: }
85:
86: public String toString() {
87: return "BooleanConverter[]";
88: }
89: }
|