001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl.fun;
030:
031: import com.caucho.xpath.Expr;
032: import com.caucho.xpath.ExprEnvironment;
033: import com.caucho.xpath.XPathException;
034: import com.caucho.xpath.XPathFun;
035: import com.caucho.xpath.pattern.AbstractPattern;
036:
037: import org.w3c.dom.Node;
038:
039: import java.text.DecimalFormat;
040: import java.text.DecimalFormatSymbols;
041: import java.util.ArrayList;
042: import java.util.HashMap;
043:
044: /**
045: * The format-number(...) function.
046: */
047: public class FormatNumberFun extends XPathFun {
048: private HashMap locales;
049:
050: public FormatNumberFun() {
051: locales = new HashMap();
052: }
053:
054: /**
055: * Add a new xsl:locale
056: */
057: public void addLocale(String name, DecimalFormatSymbols symbols) {
058: locales.put(name, symbols);
059: }
060:
061: public HashMap getLocales() {
062: return locales;
063: }
064:
065: /**
066: * Evaluate the function.
067: *
068: * @param pattern The context pattern.
069: * @param args The evaluated arguments
070: */
071: public Object eval(Node node, ExprEnvironment env,
072: AbstractPattern pattern, ArrayList args)
073: throws XPathException {
074: if (args.size() < 2)
075: return null;
076:
077: double number = Expr.toDouble(args.get(0));
078: String format = Expr.toString(args.get(1));
079: String localeName = null;
080: if (args.size() > 2)
081: localeName = Expr.toString(args.get(2));
082: else
083: localeName = "*";
084:
085: if (format == null)
086: return null;
087:
088: DecimalFormatSymbols symbols;
089: symbols = (DecimalFormatSymbols) locales.get(localeName);
090:
091: try {
092: DecimalFormat form;
093:
094: if (symbols == null)
095: form = new DecimalFormat(format);
096: else {
097: form = new DecimalFormat(format, symbols);
098: }
099: /*
100: formatter = new java.text.DecimalFormat();
101:
102: formatter.setDecimalFormatSymbols(dfs);
103:
104: form.applyLocalizedPattern(format);
105: */
106:
107: return form.format(number);
108: } catch (Exception e) {
109: throw new XPathException(e);
110: }
111: }
112: }
|