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 DoubleConverter implements Converter {
28: // API FIELDS
29: public static final String CONVERTER_ID = "javax.faces.Double";
30: public static final String STRING_ID = "javax.faces.converter.STRING";
31: public static final String DOUBLE_ID = "javax.faces.converter.DoubleConverter.DOUBLE";
32:
33: // CONSTRUCTORS
34: public DoubleConverter() {
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 Double.valueOf(value);
50: } catch (NumberFormatException e) {
51: throw new ConverterException(_MessageUtils
52: .getErrorMessage(facesContext, DOUBLE_ID,
53: new Object[] {
54: value,
55: "4214",
56: _MessageUtils.getLabel(
57: facesContext,
58: uiComponent) }), e);
59: }
60: }
61: }
62: return null;
63: }
64:
65: public String getAsString(FacesContext facesContext,
66: UIComponent uiComponent, Object value) {
67: if (facesContext == null)
68: throw new NullPointerException("facesContext");
69: if (uiComponent == null)
70: throw new NullPointerException("uiComponent");
71:
72: if (value == null) {
73: return "";
74: }
75: if (value instanceof String) {
76: return (String) value;
77: }
78: try {
79: return Double.toString(((Number) value).doubleValue());
80: } catch (Exception e) {
81: throw new ConverterException(_MessageUtils.getErrorMessage(
82: facesContext, STRING_ID, new Object[] {
83: value,
84: _MessageUtils.getLabel(facesContext,
85: uiComponent) }), e);
86: }
87: }
88: }
|