KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > convert > CharacterConverter


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  * $Log: CharacterConverter.java,v $
23  * Revision 1.7 2004/07/01 22:00:51 mwessendorf
24  * ASF switch
25  *
26  * Revision 1.6 2004/03/26 12:08:41 manolito
27  * Exceptions in getAsString now catched and
28  * more relaxed Number casting in all number converters
29  *
30  * @author Thomas Spiegl (latest modification by $Author: mwessendorf $)
31  * @version $Revision: 1.7 $ $Date: 2004/07/01 22:00:51 $
32  */

33 public class CharacterConverter
34         implements Converter
35 {
36     // FIELDS
37
public static final String JavaDoc CONVERTER_ID = "javax.faces.Character";
38
39     // CONSTRUCTORS
40
public CharacterConverter()
41     {
42     }
43
44     // METHODS
45
public Object JavaDoc getAsObject(FacesContext facesContext, UIComponent uiComponent, String JavaDoc value)
46     {
47         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
48         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
49
50         if (value != null)
51         {
52             value = value.trim();
53             if (value.length() > 0)
54             {
55                 return new Character JavaDoc(value.charAt(0));
56             }
57         }
58         return null;
59     }
60
61     public String JavaDoc getAsString(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
62     {
63         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
64         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
65
66         if (value == null)
67         {
68             return "";
69         }
70         if (value instanceof String JavaDoc)
71         {
72             return (String JavaDoc)value;
73         }
74         try
75         {
76             return ((Character JavaDoc)value).toString();
77         }
78         catch (Exception JavaDoc e)
79         {
80             throw new ConverterException(e);
81         }
82     }
83
84 }
85
Popular Tags