2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.FloatValidator;
25  
26  /***
27   * <p>JavaServer Faces <code>Converter</code> for Float (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 FloatConverter extends AbstractConverter {
32      
33  
34      // -------------------------------------------------------- Static Variables
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 FloatValidator INSTANCE =
42              FloatValidator.getInstance();
43  
44  
45  
46      // -------------------------------------------------------------- Properties
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     // ------------------------------------------------------- Converter Methods
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     // ----------------------------------------------------- StateHolder Methods
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 }