1 16 19 package org.apache.xalan.templates; 20 21 import javax.xml.transform.ErrorListener ; 22 import javax.xml.transform.TransformerException ; 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 39 public class FuncFormatNumb extends Function3Args 40 { 41 42 50 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 51 { 52 53 ElemTemplateElement templElem = 55 (ElemTemplateElement) xctxt.getNamespaceContext(); 56 StylesheetRoot ss = templElem.getStylesheetRoot(); 57 java.text.DecimalFormat formatter = null; 58 java.text.DecimalFormatSymbols dfs = null; 59 double num = getArg0().execute(xctxt).num(); 60 String patternStr = getArg1().execute(xctxt).str(); 61 62 if (patternStr.indexOf(0x00A4) > 0) 64 ss.error(XSLTErrorResources.ER_CURRENCY_SIGN_ILLEGAL); 66 try 69 { 70 Expression arg2Expr = getArg2(); 71 72 if (null != arg2Expr) 73 { 74 String 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 []{ dfName }); 84 } 86 else 87 { 88 89 formatter = new java.text.DecimalFormat (); 91 92 formatter.setDecimalFormatSymbols(dfs); 93 formatter.applyLocalizedPattern(patternStr); 94 } 95 } 96 97 if (null == formatter) 99 { 100 101 dfs = ss.getDecimalFormatComposed(new QName("")); 103 104 if (dfs != null) 105 { 106 formatter = new java.text.DecimalFormat (); 107 108 formatter.setDecimalFormatSymbols(dfs); 109 formatter.applyLocalizedPattern(patternStr); 110 } 111 else 112 { 113 dfs = new java.text.DecimalFormatSymbols (java.util.Locale.US); 114 115 dfs.setInfinity(Constants.ATTRVAL_INFINITY); 116 dfs.setNaN(Constants.ATTRVAL_NAN); 117 118 formatter = new java.text.DecimalFormat (); 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 iae) 130 { 131 templElem.error(XSLTErrorResources.ER_MALFORMED_FORMAT_STRING, 132 new Object []{ patternStr }); 133 134 return XString.EMPTYSTRING; 135 136 } 138 } 139 140 151 public void warn(XPathContext xctxt, String msg, Object args[]) 152 throws javax.xml.transform.TransformerException 153 { 154 155 String formattedMsg = XSLMessages.createWarning(msg, args); 156 ErrorListener errHandler = xctxt.getErrorListener(); 157 158 errHandler.warning(new TransformerException (formattedMsg, 159 (SAXSourceLocator)xctxt.getSAXLocator())); 160 } 161 162 170 public void checkNumberArgs(int argNum) throws WrongNumberArgsException 171 { 172 if ((argNum > 3) || (argNum < 2)) 173 reportWrongNumberArgs(); 174 } 175 176 182 protected void reportWrongNumberArgs() throws WrongNumberArgsException { 183 throw new WrongNumberArgsException(XSLMessages.createMessage(XSLTErrorResources.ER_TWO_OR_THREE, null)); } 185 } 186 | Popular Tags |