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