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: import java.math.BigDecimal;
21:
22: /**
23: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
24: *
25: * @author Thomas Spiegl (latest modification by $Author: cagatay $)
26: * @version $Revision: 517624 $ $Date: 2007-03-13 10:55:20 +0100 (Di, 13 Mrz 2007) $
27: */
28: public class BigDecimalConverter implements Converter {
29: // FIELDS
30: public static final String CONVERTER_ID = "javax.faces.BigDecimal";
31: public static final String STRING_ID = "javax.faces.converter.STRING";
32: public static final String DECIMAL_ID = "javax.faces.converter.BigDecimalConverter.DECIMAL";
33:
34: // CONSTRUCTORS
35: public BigDecimalConverter() {
36: }
37:
38: // METHODS
39: public Object getAsObject(FacesContext facesContext,
40: UIComponent uiComponent, String value) {
41: if (facesContext == null)
42: throw new NullPointerException("facesContext");
43: if (uiComponent == null)
44: throw new NullPointerException("uiComponent");
45:
46: if (value != null) {
47: {
48: value = value.trim();
49: if (value.length() > 0) {
50: try {
51: return new BigDecimal(value.trim());
52: } catch (NumberFormatException e) {
53: throw new ConverterException(
54: _MessageUtils.getErrorMessage(
55: facesContext, DECIMAL_ID,
56: new Object[] {
57: value,
58: "4815,16",
59: _MessageUtils.getLabel(
60: facesContext,
61: uiComponent) }),
62: e);
63: }
64: }
65: }
66: }
67: return null;
68: }
69:
70: public String getAsString(FacesContext facesContext,
71: UIComponent uiComponent, Object value) {
72: if (facesContext == null)
73: throw new NullPointerException("facesContext");
74: if (uiComponent == null)
75: throw new NullPointerException("uiComponent");
76:
77: if (value == null) {
78: return "";
79: }
80: if (value instanceof String) {
81: return (String) value;
82: }
83: try {
84: return ((BigDecimal) value).toString();
85: } catch (Exception e) {
86: throw new ConverterException(_MessageUtils.getErrorMessage(
87: facesContext, STRING_ID, new Object[] {
88: value,
89: _MessageUtils.getLabel(facesContext,
90: uiComponent) }), e);
91: }
92: }
93: }
|