View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.taglib.core;
20  
21  
22  import org.apache.myfaces.shared_impl.taglib.UIComponentELTagUtils;
23  import org.apache.myfaces.shared_impl.util.LocaleUtils;
24  
25  import javax.el.ELContext;
26  import javax.el.ValueExpression;
27  import javax.faces.context.FacesContext;
28  import javax.faces.convert.Converter;
29  import javax.faces.convert.NumberConverter;
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.PageContext;
32  import java.util.Locale;
33  
34  
35  /**
36   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
37   * @version $Revision: 1178543 $ $Date: 2011-10-03 14:52:41 -0500 (Mon, 03 Oct 2011) $
38   */
39  public class ConvertNumberTag
40          extends ConverterTag
41  {
42      private static final long serialVersionUID = -8365745569697171573L;
43      private ValueExpression _currencyCode = null;
44      private ValueExpression _currencySymbol = null;
45      private ValueExpression _groupingUsed = null;
46      private ValueExpression _integerOnly = null;
47      private ValueExpression _locale = null;
48      private ValueExpression _maxFractionDigits = null;
49      private ValueExpression _maxIntegerDigits = null;
50      private ValueExpression _minFractionDigits = null;
51      private ValueExpression _minIntegerDigits = null;
52      private ValueExpression _pattern = null;
53      private ValueExpression _type = null;
54  
55      public ConvertNumberTag()
56      {
57          setConverterIdString(NumberConverter.CONVERTER_ID);
58      }
59  
60      public void setCurrencyCode(ValueExpression currencyCode)
61      {
62          _currencyCode = currencyCode;
63      }
64  
65      public void setCurrencySymbol(ValueExpression currencySymbol)
66      {
67          _currencySymbol = currencySymbol;
68      }
69  
70      public void setGroupingUsed(ValueExpression groupingUsed)
71      {
72          _groupingUsed = groupingUsed;
73      }
74  
75      public void setIntegerOnly(ValueExpression integerOnly)
76      {
77          _integerOnly = integerOnly;
78      }
79  
80      public void setLocale(ValueExpression locale)
81      {
82          _locale = locale;
83      }
84  
85      public void setMaxFractionDigits(ValueExpression maxFractionDigits)
86      {
87          _maxFractionDigits = maxFractionDigits;
88      }
89  
90      public void setMaxIntegerDigits(ValueExpression maxIntegerDigits)
91      {
92          _maxIntegerDigits = maxIntegerDigits;
93      }
94  
95      public void setMinFractionDigits(ValueExpression minFractionDigits)
96      {
97          _minFractionDigits = minFractionDigits;
98      }
99  
100     public void setMinIntegerDigits(ValueExpression minIntegerDigits)
101     {
102         _minIntegerDigits = minIntegerDigits;
103     }
104 
105     public void setPattern(ValueExpression pattern)
106     {
107         _pattern = pattern;
108     }
109 
110     public void setType(ValueExpression type)
111     {
112         _type = type;
113     }
114 
115     public void setPageContext(PageContext context)
116     {
117         super.setPageContext(context);
118         setConverterIdString(NumberConverter.CONVERTER_ID);
119     }
120 
121     protected Converter createConverter() throws JspException
122     {
123         NumberConverter converter = (NumberConverter) super.createConverter();
124         ELContext elContext = FacesContext.getCurrentInstance().getELContext();
125         if (null != _currencyCode)
126         {
127             converter.setCurrencyCode(
128                     (String) UIComponentELTagUtils.evaluateValueExpression(elContext, _currencyCode));
129         }
130         if (null != _currencySymbol)
131         {
132             converter.setCurrencySymbol(
133                     (String) UIComponentELTagUtils.evaluateValueExpression(elContext, _currencySymbol));
134         }
135         if (null != _groupingUsed)
136         {
137             converter.setGroupingUsed(UIComponentELTagUtils.getBooleanValue(elContext, _groupingUsed));
138         } else
139         {
140             converter.setGroupingUsed(true);
141         }
142         if (null != _integerOnly)
143         {
144             converter.setIntegerOnly(UIComponentELTagUtils.getBooleanValue(elContext, _integerOnly));
145         } else
146         {
147             converter.setIntegerOnly(false);
148         }
149         if (null != _locale)
150         {
151             Locale locale;
152             if (_locale.isLiteralText())
153             {
154                 locale = LocaleUtils.toLocale(_locale.getExpressionString());
155             }
156             else
157             {
158                 Object localeValue = _locale.getValue(elContext);
159                 if (localeValue instanceof Locale)
160                 {
161                     locale = (Locale)localeValue;
162                 }
163                 else
164                 {
165                     locale = LocaleUtils.toLocale(localeValue.toString());
166                 }
167                 if (null == locale)
168                 {
169                     locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
170                 }
171             }
172             converter.setLocale(locale);
173         }
174         if (null != _maxFractionDigits)
175         {
176             converter.setMaxFractionDigits(UIComponentELTagUtils.getIntegerValue(elContext, _maxFractionDigits));
177         }
178         if (null != _maxIntegerDigits)
179         {
180             converter.setMaxIntegerDigits(UIComponentELTagUtils.getIntegerValue(elContext, _maxIntegerDigits));
181         }
182         if (null != _minFractionDigits)
183         {
184             converter.setMinFractionDigits(UIComponentELTagUtils.getIntegerValue(elContext, _minFractionDigits));
185         }
186         if (null != _minIntegerDigits)
187         {
188             converter.setMinIntegerDigits(UIComponentELTagUtils.getIntegerValue(elContext, _minIntegerDigits));
189         }
190         if (null != _pattern)
191         {
192             converter.setPattern((String) UIComponentELTagUtils.evaluateValueExpression(elContext, _pattern));
193         }
194         if (null != _type)
195         {
196             converter.setType((String) UIComponentELTagUtils.evaluateValueExpression(elContext, _type));
197         } else
198         {
199             converter.setType("number");
200         }
201         return converter;
202     }
203 }