1 /*
2 * $Id: BooleanConverter.java,v 1.20 2007/04/27 22:00:07 ofung Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.convert;
42
43
44 import javax.faces.component.UIComponent;
45 import javax.faces.context.FacesContext;
46
47
48 /**
49 * <p>{@link Converter} implementation for <code>java.lang.Boolean</code>
50 * (and boolean primitive) values.</p>
51 */
52
53 public class BooleanConverter implements Converter {
54
55 // ------------------------------------------------------ Manifest Constants
56
57
58 /**
59 * <p>The standard converter id for this converter.</p>
60 */
61 public static final String CONVERTER_ID = "javax.faces.Boolean";
62
63 /**
64 * <p>The message identifier of the {@link javax.faces.application.FacesMessage} to be created if
65 * the conversion to <code>Boolean</code> fails. The message format
66 * string for this message may optionally include the following
67 * placeholders:
68 * <ul>
69 * <li><code>{0}</code> replaced by the unconverted value.</li>
70 * <li><code>{1}</code> replaced by a <code>String</code> whose value
71 * is the label of the input component that produced this message.</li>
72 * </ul></p>
73 */
74 public static final String BOOLEAN_ID =
75 "javax.faces.converter.BooleanConverter.BOOLEAN";
76
77 /**
78 * <p>The message identifier of the {@link javax.faces.application.FacesMessage} to be created if
79 * the conversion of the <code>Boolean</code> value to
80 * <code>String</code> fails. The message format string for this message
81 * may optionally include the following placeholders:
82 * <ul>
83 * <li><code>{0}</code> relaced by the unconverted value.</li>
84 * <li><code>{1}</code> replaced by a <code>String</code> whose value
85 * is the label of the input component that produced this message.</li>
86 * </ul></p>
87 */
88 public static final String STRING_ID =
89 "javax.faces.converter.STRING";
90
91 // ------------------------------------------------------- Converter Methods
92
93 /**
94 * @throws ConverterException {@inheritDoc}
95 * @throws NullPointerException {@inheritDoc}
96 */
97 public Object getAsObject(FacesContext context, UIComponent component,
98 String value) {
99
100 if (context == null || component == null) {
101 throw new NullPointerException();
102 }
103
104 // If the specified value is null or zero-length, return null
105 if (value == null) {
106 return (null);
107 }
108 value = value.trim();
109 if (value.length() < 1) {
110 return (null);
111 }
112
113 // Let them know that the value being converted is not specifically
114 // "true" or "false".
115 try {
116 return (Boolean.valueOf(value));
117 } catch (Exception e) {
118 throw new ConverterException(
119 MessageFactory.getMessage(context,
120 BOOLEAN_ID,
121 value,
122 MessageFactory.getLabel(context,
123 component)),
124 e);
125 }
126 }
127
128 /**
129 * @throws ConverterException {@inheritDoc}
130 * @throws NullPointerException {@inheritDoc}
131 */
132 public String getAsString(FacesContext context, UIComponent component,
133 Object value) {
134
135 if (context == null || component == null) {
136 throw new NullPointerException();
137 }
138
139 // If the specified value is null, return a zero-length String
140 if (value == null) {
141 return "";
142 }
143
144 try {
145 return value.toString();
146 } catch (Exception e) {
147 throw new ConverterException(
148 MessageFactory.getMessage(context,
149 STRING_ID,
150 value,
151 MessageFactory.getLabel(context,
152 component)),
153 e);
154 }
155 }
156 }