KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > FormatNumberFunction


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.apache.taglibs.standard.lang.jpath.expression;
18
19 import java.text.DecimalFormat JavaDoc;
20 import java.text.NumberFormat JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.servlet.jsp.PageContext JavaDoc;
25
26 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
27 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
28 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
29
30 /**
31  * The FormatNumberFunction class
32  *
33  *
34  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
35  * @version
36  */

37 public class FormatNumberFunction extends SimpleNode {
38
39     /**
40      * Used to create an instance of the FormatNumberFunction class
41      *
42      *
43      * @param id
44      *
45      */

46     public FormatNumberFunction(int id) {
47         super(id);
48     }
49
50     /**
51      * Used to create an instance of the FormatNumberFunction class
52      *
53      *
54      * @param p
55      * @param id
56      *
57      */

58     public FormatNumberFunction(Parser p, int id) {
59         super(p, id);
60     }
61
62     /**
63      * Provides a method to print a normalized version of the original
64      * expression. The normalized version has standardized spacing and
65      * parenthesis, and can be used to compare expressions formatted
66      * in different ways to see if they are actually the same expression.
67      *
68      *
69      * @return The normalized version of the original expression
70      *
71      */

72     public String JavaDoc toNormalizedString() {
73
74         String JavaDoc normalized = "";
75
76         normalized = "format-number(" + jjtGetChild(0).toNormalizedString()
77                 + jjtGetChild(1).toNormalizedString() + "," + ")";
78
79         return normalized;
80     }
81
82     /**
83      * This method evaluates this node of the expression and all child nodes.
84      * It returns the result of the
85      * evaluation as an <tt>Object</tt>. If any problems are encountered
86      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
87      *
88      *
89      * @param pageContext the current JSP PageContext
90      *
91      * @param icontext the Iteration Context of the expression. If there is
92      * no interation context, this should be null.
93      *
94      * @return the result of the expression evaluation as an object
95      *
96      * @throws EvaluationException if a problem is encountered during the
97      * evaluation
98      */

99     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
100             throws EvaluationException {
101
102         String JavaDoc formatted;
103
104         try {
105             Double JavaDoc arg = Convert.toDouble(jjtGetChild(0).evaluate(pageContext,
106                              icontext));
107             String JavaDoc pattern =
108                 Convert.toString(jjtGetChild(1).evaluate(pageContext,
109                     icontext));
110             NumberFormat JavaDoc form;
111
112             if (jjtGetNumChildren() > 2) {
113                 String JavaDoc arg3 =
114                     Convert.toString(jjtGetChild(2).evaluate(pageContext,
115                         icontext));
116                 Locale JavaDoc locale = getLocale(arg3);
117
118                 form = NumberFormat.getInstance(locale);
119             } else {
120                 form = NumberFormat.getInstance();
121             }
122
123             try {
124                 ((DecimalFormat JavaDoc) form).applyPattern(pattern);
125
126                 formatted = form.format(arg);
127             } catch (IllegalArgumentException JavaDoc iae) {
128                 formatted = new String JavaDoc("NaN");
129             }
130         } catch (ConversionException ce) {
131             throw new EvaluationException(this, ce.getMessage());
132         }
133
134         return formatted;
135     }
136
137     /**
138      * The getLocale method
139      *
140      *
141      * @param arg
142      *
143      * @return
144      *
145      */

146     private Locale JavaDoc getLocale(String JavaDoc arg) {
147
148         Locale JavaDoc result;
149         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(arg, "_");
150         String JavaDoc language = null;
151         String JavaDoc country = null;
152         String JavaDoc variant = null;
153
154         if (st.hasMoreTokens()) {
155             language = st.nextToken();
156
157             if (st.hasMoreTokens()) {
158                 country = st.nextToken();
159
160                 if (st.hasMoreTokens()) {
161                     variant = st.nextToken();
162                     result = new Locale JavaDoc(language, country, variant);
163                 } else {
164                     result = new Locale JavaDoc(language, country);
165                 }
166             } else {
167                 result = new Locale JavaDoc(language, "");
168             }
169         } else {
170             result = Locale.getDefault();
171         }
172
173         return result;
174     }
175 }
176
Popular Tags