View Javadoc

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