KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > fun > FormatNumberFun


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl.fun;
30
31 import com.caucho.xpath.Expr;
32 import com.caucho.xpath.ExprEnvironment;
33 import com.caucho.xpath.XPathException;
34 import com.caucho.xpath.XPathFun;
35 import com.caucho.xpath.pattern.AbstractPattern;
36
37 import org.w3c.dom.Node JavaDoc;
38
39 import java.text.DecimalFormat JavaDoc;
40 import java.text.DecimalFormatSymbols JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * The format-number(...) function.
46  */

47 public class FormatNumberFun extends XPathFun {
48   private HashMap JavaDoc locales;
49
50   public FormatNumberFun()
51   {
52     locales = new HashMap JavaDoc();
53   }
54
55   /**
56    * Add a new xsl:locale
57    */

58   public void addLocale(String JavaDoc name, DecimalFormatSymbols JavaDoc symbols)
59   {
60     locales.put(name, symbols);
61   }
62
63   public HashMap JavaDoc getLocales()
64   {
65     return locales;
66   }
67   /**
68    * Evaluate the function.
69    *
70    * @param pattern The context pattern.
71    * @param args The evaluated arguments
72    */

73   public Object JavaDoc eval(Node JavaDoc node, ExprEnvironment env,
74              AbstractPattern pattern, ArrayList JavaDoc args)
75     throws XPathException
76   {
77     if (args.size() < 2)
78       return null;
79
80     double number = Expr.toDouble(args.get(0));
81     String JavaDoc format = Expr.toString(args.get(1));
82     String JavaDoc localeName = null;
83     if (args.size() > 2)
84       localeName = Expr.toString(args.get(2));
85     else
86       localeName = "*";
87
88     if (format == null)
89       return null;
90
91     DecimalFormatSymbols JavaDoc symbols;
92     symbols = (DecimalFormatSymbols JavaDoc) locales.get(localeName);
93
94     try {
95       DecimalFormat JavaDoc form;
96
97       if (symbols == null)
98     form = new DecimalFormat JavaDoc(format);
99       else {
100         form = new DecimalFormat JavaDoc(format, symbols);
101       }
102       /*
103       formatter = new java.text.DecimalFormat();
104
105           formatter.setDecimalFormatSymbols(dfs);
106       
107       form.applyLocalizedPattern(format);
108       */

109     
110       return form.format(number);
111     } catch (Exception JavaDoc e) {
112       throw new XPathException(e);
113     }
114   }
115 }
116
Popular Tags