1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.templates;
  58. import java.util.Vector;
  59. import org.apache.xml.utils.QName;
  60. import org.apache.xpath.functions.Function;
  61. import org.apache.xpath.functions.Function3Args;
  62. import org.apache.xpath.XPathContext;
  63. import org.apache.xpath.objects.XObject;
  64. import org.apache.xpath.objects.XString;
  65. import org.apache.xpath.XPath;
  66. import org.apache.xpath.Expression;
  67. import org.apache.xpath.functions.WrongNumberArgsException;
  68. import org.apache.xalan.res.XSLMessages;
  69. import org.apache.xalan.res.XSLTErrorResources;
  70. import org.w3c.dom.Node;
  71. import javax.xml.transform.TransformerException;
  72. import javax.xml.transform.ErrorListener;
  73. import org.apache.xml.utils.SAXSourceLocator;
  74. /**
  75. * <meta name="usage" content="advanced"/>
  76. * Execute the FormatNumber() function.
  77. */
  78. public class FuncFormatNumb extends Function3Args
  79. {
  80. /**
  81. * Execute the function. The function must return
  82. * a valid object.
  83. * @param xctxt The current execution context.
  84. * @return A valid XObject.
  85. *
  86. * @throws javax.xml.transform.TransformerException
  87. */
  88. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  89. {
  90. // A bit of an ugly hack to get our context.
  91. ElemTemplateElement templElem =
  92. (ElemTemplateElement) xctxt.getNamespaceContext();
  93. StylesheetRoot ss = templElem.getStylesheetRoot();
  94. java.text.DecimalFormat formatter = null;
  95. java.text.DecimalFormatSymbols dfs = null;
  96. double num = getArg0().execute(xctxt).num();
  97. String patternStr = getArg1().execute(xctxt).str();
  98. // TODO: what should be the behavior here??
  99. if (patternStr.indexOf(0x00A4) > 0)
  100. ss.error(XSLTErrorResources.ER_CURRENCY_SIGN_ILLEGAL); // currency sign not allowed
  101. // this third argument is not a locale name. It is the name of a
  102. // decimal-format declared in the stylesheet!(xsl:decimal-format
  103. try
  104. {
  105. Expression arg2Expr = getArg2();
  106. if (null != arg2Expr)
  107. {
  108. String dfName = arg2Expr.execute(xctxt).str();
  109. QName qname = new QName(dfName, xctxt.getNamespaceContext());
  110. dfs = ss.getDecimalFormatComposed(qname);
  111. if (null == dfs)
  112. {
  113. warn(xctxt, XSLTErrorResources.WG_NO_DECIMALFORMAT_DECLARATION,
  114. new Object[]{ dfName }); //"not found!!!
  115. //formatter = new java.text.DecimalFormat(patternStr);
  116. }
  117. else
  118. {
  119. //formatter = new java.text.DecimalFormat(patternStr, dfs);
  120. formatter = new java.text.DecimalFormat();
  121. formatter.setDecimalFormatSymbols(dfs);
  122. formatter.applyLocalizedPattern(patternStr);
  123. }
  124. }
  125. //else
  126. if (null == formatter)
  127. {
  128. // look for a possible default decimal-format
  129. if (ss.getDecimalFormatCount() > 0)
  130. dfs = ss.getDecimalFormatComposed(new QName(""));
  131. if (dfs != null)
  132. {
  133. formatter = new java.text.DecimalFormat();
  134. formatter.setDecimalFormatSymbols(dfs);
  135. formatter.applyLocalizedPattern(patternStr);
  136. }
  137. else
  138. {
  139. dfs = new java.text.DecimalFormatSymbols(java.util.Locale.US);
  140. dfs.setInfinity(Constants.ATTRVAL_INFINITY);
  141. dfs.setNaN(Constants.ATTRVAL_NAN);
  142. formatter = new java.text.DecimalFormat();
  143. formatter.setDecimalFormatSymbols(dfs);
  144. if (null != patternStr)
  145. formatter.applyLocalizedPattern(patternStr);
  146. }
  147. }
  148. return new XString(formatter.format(num));
  149. }
  150. catch (Exception iae)
  151. {
  152. templElem.error(XSLTErrorResources.ER_MALFORMED_FORMAT_STRING,
  153. new Object[]{ patternStr });
  154. return XString.EMPTYSTRING;
  155. //throw new XSLProcessorException(iae);
  156. }
  157. }
  158. /**
  159. * Warn the user of a problem.
  160. *
  161. * @param xctxt The XPath runtime state.
  162. * @param msg Warning message key
  163. * @param args Arguments to be used in warning message
  164. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  165. * the error condition is severe enough to halt processing.
  166. *
  167. * @throws javax.xml.transform.TransformerException
  168. */
  169. public void warn(XPathContext xctxt, String msg, Object args[])
  170. throws javax.xml.transform.TransformerException
  171. {
  172. String formattedMsg = XSLMessages.createWarning(msg, args);
  173. ErrorListener errHandler = xctxt.getErrorListener();
  174. errHandler.warning(new TransformerException(formattedMsg,
  175. (SAXSourceLocator)xctxt.getSAXLocator()));
  176. }
  177. /**
  178. * Overide the superclass method to allow one or two arguments.
  179. *
  180. *
  181. * @param argNum Number of arguments passed in
  182. *
  183. * @throws WrongNumberArgsException
  184. */
  185. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  186. {
  187. if ((argNum > 3) || (argNum < 2))
  188. reportWrongNumberArgs();
  189. }
  190. /**
  191. * Constructs and throws a WrongNumberArgException with the appropriate
  192. * message for this function object.
  193. *
  194. * @throws WrongNumberArgsException
  195. */
  196. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  197. throw new WrongNumberArgsException(XSLMessages.createMessage(XSLTErrorResources.ER_TWO_OR_THREE, null)); //"2 or 3");
  198. }
  199. }