2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.validator.converter;
19
20 import java.util.Locale;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.convert.ConverterException;
24 import org.apache.commons.validator.routines.IntegerValidator;
25
26 /***
27 * <p>JavaServer Faces <code>Converter</code> for Integer (and int)
28 * instances. Parsing and formatting is controlled by the properties
29 * that are set on a Converter instance.</p>
30 */
31 public final class IntegerConverter extends AbstractConverter {
32
33
34
35
36
37 /***
38 * <p>The Apache Commons Validator instance we will use to perform
39 * our formatting and parsing.</p>
40 */
41 private static final IntegerValidator INSTANCE =
42 IntegerValidator.getInstance();
43
44
45
46
47
48
49 /***
50 * <p>The <code>Locale</code> to apply to this conversion, if any.</p>
51 */
52 private Locale locale = null;
53
54
55 /***
56 * <p>Return the <code>Locale</code> to apply to this conversion, or
57 * <code>null</code> to use the <code>Locale</code> for the current view.</p>
58 */
59 public Locale getLocale() {
60 return this.locale;
61 }
62
63
64 /***
65 * <p>Set the <code>Locale</code> to apply to this conversion, or
66 * <code>null</code> to use the <code>Locale</code> for the current view.</p>
67 *
68 * @param locale The new Locale
69 */
70 public void setLocale(Locale locale) {
71 this.locale = locale;
72 }
73
74
75 /***
76 * <p>The formatting pattern to apply to this conversion, if any.</p>
77 */
78 private String pattern = null;
79
80
81 /***
82 * <p>Return the <code>java.text.NumberFormat</code> pattern to apply
83 * to this conversion, or <code>null</code> for using no pattern.</p>
84 */
85 public String getPattern() {
86 return this.pattern;
87 }
88
89
90 /***
91 * <p>Set the <code>java.text.NumberFormat</code> pattern to apply to
92 * this conversion, or <code>null</code> for using no pattern.</p>
93 *
94 * @param pattern The new formatting pattern
95 */
96 public void setPattern(String pattern) {
97 this.pattern = null;
98 }
99
100
101
102
103
104 /*** {@inheritDoc} */
105 public Object getAsObject(FacesContext context, UIComponent component,
106 String value) {
107
108 if ((context == null) || (component == null)) {
109 throw new NullPointerException(message(context, "common.null"));
110 }
111
112 try {
113 if (locale == null) {
114 return INSTANCE.validate(value, pattern, context.getViewRoot().getLocale());
115 } else {
116 return INSTANCE.validate(value, pattern, locale);
117 }
118 } catch (Exception e) {
119 throw new ConverterException(e);
120 }
121
122 }
123
124
125 /*** {@inheritDoc} */
126 public String getAsString(FacesContext context, UIComponent component,
127 Object value) {
128
129 if ((context == null) || (component == null)) {
130 throw new NullPointerException(message(context, "common.null"));
131 }
132
133 try {
134 if (locale == null) {
135 return INSTANCE.format(value, pattern, context.getViewRoot().getLocale());
136 } else {
137 return INSTANCE.format(value, pattern, locale);
138 }
139 } catch (Exception e) {
140 throw new ConverterException(e);
141 }
142
143 }
144
145
146
147
148
149
150 /*** {@inheritDoc} */
151 public void restoreState(FacesContext context, Object state) {
152 Object[] values = (Object[]) state;
153 super.restoreState(context, values[0]);
154 this.locale = (Locale) values[1];
155 this.pattern = (String) values[2];
156 }
157
158
159 /*** {@inheritDoc} */
160 public Object saveState(FacesContext context) {
161 Object[] values = new Object[3];
162 values[0] = super.saveState(context);
163 values[1] = this.locale;
164 values[2] = this.pattern;
165 return values;
166 }
167
168
169 }