1 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 ; 38 39 import java.text.DecimalFormat ; 40 import java.text.DecimalFormatSymbols ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 44 47 public class FormatNumberFun extends XPathFun { 48 private HashMap locales; 49 50 public FormatNumberFun() 51 { 52 locales = new HashMap (); 53 } 54 55 58 public void addLocale(String name, DecimalFormatSymbols symbols) 59 { 60 locales.put(name, symbols); 61 } 62 63 public HashMap getLocales() 64 { 65 return locales; 66 } 67 73 public Object eval(Node node, ExprEnvironment env, 74 AbstractPattern pattern, ArrayList args) 75 throws XPathException 76 { 77 if (args.size() < 2) 78 return null; 79 80 double number = Expr.toDouble(args.get(0)); 81 String format = Expr.toString(args.get(1)); 82 String 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 symbols; 92 symbols = (DecimalFormatSymbols ) locales.get(localeName); 93 94 try { 95 DecimalFormat form; 96 97 if (symbols == null) 98 form = new DecimalFormat (format); 99 else { 100 form = new DecimalFormat (format, symbols); 101 } 102 109 110 return form.format(number); 111 } catch (Exception e) { 112 throw new XPathException(e); 113 } 114 } 115 } 116 | Popular Tags |