KickJava   Java API By Example, From Geeks To Geeks.

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


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: ShortConverter.java,v $
23  * Revision 1.9 2004/07/01 22:00:50 mwessendorf
24  * ASF switch
25  *
26  * Revision 1.8 2004/06/07 13:40:37 mwessendorf
27  * solved Feature Request #966892
28  *
29  * Revision 1.7 2004/03/26 12:08:42 manolito
30  * Exceptions in getAsString now catched and
31  * more relaxed Number casting in all number converters
32  *
33  * @author Thomas Spiegl (latest modification by $Author: mwessendorf $)
34  * @version $Revision: 1.9 $ $Date: 2004/07/01 22:00:50 $
35  */

36 public class ShortConverter
37         implements Converter
38 {
39     private static final String JavaDoc CONVERSION_MESSAGE_ID = "javax.faces.convert.ShortConverter.CONVERSION";
40
41     // FIELDS
42
public static final String JavaDoc CONVERTER_ID = "javax.faces.Short";
43
44     // CONSTRUCTORS
45
public ShortConverter()
46     {
47     }
48
49     // METHODS
50
public Object JavaDoc getAsObject(FacesContext facesContext, UIComponent uiComponent, String JavaDoc value)
51     {
52         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
53         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
54
55         if (value != null)
56         {
57             value = value.trim();
58             if (value.length() > 0)
59             {
60                 try
61                 {
62                     return Short.valueOf(value);
63                 }
64                 catch (NumberFormatException JavaDoc e)
65                 {
66                     throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
67                                                                                CONVERSION_MESSAGE_ID,
68                                                                                new Object JavaDoc[]{uiComponent.getId(),value}), e);
69                 }
70             }
71         }
72         return null;
73     }
74
75     public String JavaDoc getAsString(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
76     {
77         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
78         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
79
80         if (value == null)
81         {
82             return "";
83         }
84         if (value instanceof String JavaDoc)
85         {
86             return (String JavaDoc)value;
87         }
88         try
89         {
90             return Short.toString(((Number JavaDoc)value).shortValue());
91         }
92         catch (Exception JavaDoc e)
93         {
94             throw new ConverterException(e);
95         }
96     }
97 }
98
Popular Tags