01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package javax.faces.convert;
17:
18: import javax.faces.component.UIComponent;
19: import javax.faces.context.FacesContext;
20:
21: /**
22: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
23: *
24: * @author Thomas Spiegl (latest modification by $Author: cagatay $)
25: * @version $Revision: 517624 $ $Date: 2007-03-13 10:55:20 +0100 (Di, 13 Mrz 2007) $
26: */
27: public class CharacterConverter implements Converter {
28: // FIELDS
29: public static final String CONVERTER_ID = "javax.faces.Character";
30: public static final String STRING_ID = "javax.faces.converter.STRING";
31: public static final String CHARACTER_ID = "javax.faces.converter.CharacterConverter.CHARACTER";
32:
33: // CONSTRUCTORS
34: public CharacterConverter() {
35: }
36:
37: // METHODS
38: public Object getAsObject(FacesContext facesContext,
39: UIComponent uiComponent, String value) {
40: if (facesContext == null)
41: throw new NullPointerException("facesContext");
42: if (uiComponent == null)
43: throw new NullPointerException("uiComponent");
44:
45: if (value != null) {
46: value = value.trim();
47: if (value.length() > 0) {
48: try {
49: return new Character(value.charAt(0));
50: } catch (Exception e) {
51: throw new ConverterException(_MessageUtils
52: .getErrorMessage(facesContext,
53: CHARACTER_ID, new Object[] {
54: value,
55: _MessageUtils.getLabel(
56: facesContext,
57: uiComponent) }), e);
58: }
59: }
60: }
61: return null;
62: }
63:
64: public String getAsString(FacesContext facesContext,
65: UIComponent uiComponent, Object value) {
66: if (facesContext == null)
67: throw new NullPointerException("facesContext");
68: if (uiComponent == null)
69: throw new NullPointerException("uiComponent");
70:
71: if (value == null) {
72: return "";
73: }
74: if (value instanceof String) {
75: return (String) value;
76: }
77: try {
78: return ((Character) value).toString();
79: } catch (Exception e) {
80: throw new ConverterException(_MessageUtils.getErrorMessage(
81: facesContext, STRING_ID, new Object[] {
82: value,
83: _MessageUtils.getLabel(facesContext,
84: uiComponent) }), e);
85: }
86: }
87:
88: }
|