KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > FuncFormatNumb


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  * $Id: FuncFormatNumb.java,v 1.23 2004/02/16 20:32:33 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.ErrorListener JavaDoc;
22 import javax.xml.transform.TransformerException JavaDoc;
23
24 import org.apache.xalan.res.XSLMessages;
25 import org.apache.xalan.res.XSLTErrorResources;
26 import org.apache.xml.utils.QName;
27 import org.apache.xml.utils.SAXSourceLocator;
28 import org.apache.xpath.Expression;
29 import org.apache.xpath.XPathContext;
30 import org.apache.xpath.functions.Function3Args;
31 import org.apache.xpath.functions.WrongNumberArgsException;
32 import org.apache.xpath.objects.XObject;
33 import org.apache.xpath.objects.XString;
34
35 /**
36  * Execute the FormatNumber() function.
37  * @xsl.usage advanced
38  */

39 public class FuncFormatNumb extends Function3Args
40 {
41
42   /**
43    * Execute the function. The function must return
44    * a valid object.
45    * @param xctxt The current execution context.
46    * @return A valid XObject.
47    *
48    * @throws javax.xml.transform.TransformerException
49    */

50   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
51   {
52
53     // A bit of an ugly hack to get our context.
54
ElemTemplateElement templElem =
55       (ElemTemplateElement) xctxt.getNamespaceContext();
56     StylesheetRoot ss = templElem.getStylesheetRoot();
57     java.text.DecimalFormat JavaDoc formatter = null;
58     java.text.DecimalFormatSymbols JavaDoc dfs = null;
59     double num = getArg0().execute(xctxt).num();
60     String JavaDoc patternStr = getArg1().execute(xctxt).str();
61
62     // TODO: what should be the behavior here??
63
if (patternStr.indexOf(0x00A4) > 0)
64       ss.error(XSLTErrorResources.ER_CURRENCY_SIGN_ILLEGAL); // currency sign not allowed
65

66     // this third argument is not a locale name. It is the name of a
67
// decimal-format declared in the stylesheet!(xsl:decimal-format
68
try
69     {
70       Expression arg2Expr = getArg2();
71
72       if (null != arg2Expr)
73       {
74         String JavaDoc dfName = arg2Expr.execute(xctxt).str();
75         QName qname = new QName(dfName, xctxt.getNamespaceContext());
76
77         dfs = ss.getDecimalFormatComposed(qname);
78
79         if (null == dfs)
80         {
81           warn(xctxt, XSLTErrorResources.WG_NO_DECIMALFORMAT_DECLARATION,
82                new Object JavaDoc[]{ dfName }); //"not found!!!
83

84           //formatter = new java.text.DecimalFormat(patternStr);
85
}
86         else
87         {
88
89           //formatter = new java.text.DecimalFormat(patternStr, dfs);
90
formatter = new java.text.DecimalFormat JavaDoc();
91
92           formatter.setDecimalFormatSymbols(dfs);
93           formatter.applyLocalizedPattern(patternStr);
94         }
95       }
96
97       //else
98
if (null == formatter)
99       {
100
101         // look for a possible default decimal-format
102
dfs = ss.getDecimalFormatComposed(new QName(""));
103
104         if (dfs != null)
105         {
106           formatter = new java.text.DecimalFormat JavaDoc();
107
108           formatter.setDecimalFormatSymbols(dfs);
109           formatter.applyLocalizedPattern(patternStr);
110         }
111         else
112         {
113           dfs = new java.text.DecimalFormatSymbols JavaDoc(java.util.Locale.US);
114
115           dfs.setInfinity(Constants.ATTRVAL_INFINITY);
116           dfs.setNaN(Constants.ATTRVAL_NAN);
117
118           formatter = new java.text.DecimalFormat JavaDoc();
119
120           formatter.setDecimalFormatSymbols(dfs);
121
122           if (null != patternStr)
123             formatter.applyLocalizedPattern(patternStr);
124         }
125       }
126
127       return new XString(formatter.format(num));
128     }
129     catch (Exception JavaDoc iae)
130     {
131       templElem.error(XSLTErrorResources.ER_MALFORMED_FORMAT_STRING,
132                       new Object JavaDoc[]{ patternStr });
133
134       return XString.EMPTYSTRING;
135
136       //throw new XSLProcessorException(iae);
137
}
138   }
139
140   /**
141    * Warn the user of a problem.
142    *
143    * @param xctxt The XPath runtime state.
144    * @param msg Warning message key
145    * @param args Arguments to be used in warning message
146    * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
147    * the error condition is severe enough to halt processing.
148    *
149    * @throws javax.xml.transform.TransformerException
150    */

151   public void warn(XPathContext xctxt, String JavaDoc msg, Object JavaDoc args[])
152           throws javax.xml.transform.TransformerException JavaDoc
153   {
154
155     String JavaDoc formattedMsg = XSLMessages.createWarning(msg, args);
156     ErrorListener JavaDoc errHandler = xctxt.getErrorListener();
157
158     errHandler.warning(new TransformerException JavaDoc(formattedMsg,
159                                              (SAXSourceLocator)xctxt.getSAXLocator()));
160   }
161
162   /**
163    * Overide the superclass method to allow one or two arguments.
164    *
165    *
166    * @param argNum Number of arguments passed in
167    *
168    * @throws WrongNumberArgsException
169    */

170   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
171   {
172     if ((argNum > 3) || (argNum < 2))
173       reportWrongNumberArgs();
174   }
175
176   /**
177    * Constructs and throws a WrongNumberArgException with the appropriate
178    * message for this function object.
179    *
180    * @throws WrongNumberArgsException
181    */

182   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
183       throw new WrongNumberArgsException(XSLMessages.createMessage(XSLTErrorResources.ER_TWO_OR_THREE, null)); //"2 or 3");
184
}
185 }
186
Popular Tags