Search in sources :

Example 21 with XString

use of org.apache.xpath.objects.XString in project j2objc by google.

the class FuncFormatNumb method execute.

/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    // A bit of an ugly hack to get our context.
    ElemTemplateElement templElem = (ElemTemplateElement) xctxt.getNamespaceContext();
    StylesheetRoot ss = templElem.getStylesheetRoot();
    java.text.DecimalFormat formatter = null;
    java.text.DecimalFormatSymbols dfs = null;
    double num = getArg0().execute(xctxt).num();
    String patternStr = getArg1().execute(xctxt).str();
    // TODO: what should be the behavior here??
    if (patternStr.indexOf(0x00A4) > 0)
        // currency sign not allowed
        ss.error(XSLTErrorResources.ER_CURRENCY_SIGN_ILLEGAL);
    // decimal-format declared in the stylesheet!(xsl:decimal-format
    try {
        Expression arg2Expr = getArg2();
        if (null != arg2Expr) {
            String dfName = arg2Expr.execute(xctxt).str();
            QName qname = new QName(dfName, xctxt.getNamespaceContext());
            dfs = ss.getDecimalFormatComposed(qname);
            if (null == dfs) {
                warn(xctxt, XSLTErrorResources.WG_NO_DECIMALFORMAT_DECLARATION, // "not found!!!
                new Object[] { dfName });
            // formatter = new java.text.DecimalFormat(patternStr);
            } else {
                // formatter = new java.text.DecimalFormat(patternStr, dfs);
                formatter = new java.text.DecimalFormat();
                formatter.setDecimalFormatSymbols(dfs);
                formatter.applyLocalizedPattern(patternStr);
            }
        }
        // else
        if (null == formatter) {
            // look for a possible default decimal-format
            dfs = ss.getDecimalFormatComposed(new QName(""));
            if (dfs != null) {
                formatter = new java.text.DecimalFormat();
                formatter.setDecimalFormatSymbols(dfs);
                formatter.applyLocalizedPattern(patternStr);
            } else {
                dfs = new java.text.DecimalFormatSymbols(java.util.Locale.US);
                dfs.setInfinity(Constants.ATTRVAL_INFINITY);
                dfs.setNaN(Constants.ATTRVAL_NAN);
                formatter = new java.text.DecimalFormat();
                formatter.setDecimalFormatSymbols(dfs);
                if (null != patternStr)
                    formatter.applyLocalizedPattern(patternStr);
            }
        }
        return new XString(formatter.format(num));
    } catch (Exception iae) {
        templElem.error(XSLTErrorResources.ER_MALFORMED_FORMAT_STRING, new Object[] { patternStr });
        return XString.EMPTYSTRING;
    // throw new XSLProcessorException(iae);
    }
}
Also used : Expression(org.apache.xpath.Expression) QName(org.apache.xml.utils.QName) XString(org.apache.xpath.objects.XString) XObject(org.apache.xpath.objects.XObject) XPathContext(org.apache.xpath.XPathContext) XString(org.apache.xpath.objects.XString) TransformerException(javax.xml.transform.TransformerException) WrongNumberArgsException(org.apache.xpath.functions.WrongNumberArgsException)

Aggregations

XString (org.apache.xpath.objects.XString)21 DTM (org.apache.xml.dtm.DTM)10 XObject (org.apache.xpath.objects.XObject)7 TransformerException (javax.xml.transform.TransformerException)3 Properties (java.util.Properties)2 QName (org.apache.xml.utils.QName)2 XMLString (org.apache.xml.utils.XMLString)2 Expression (org.apache.xpath.Expression)2 XPathContext (org.apache.xpath.XPathContext)2 WrongNumberArgsException (org.apache.xpath.functions.WrongNumberArgsException)2 SimpleNumber (freemarker.template.SimpleNumber)1 SimpleScalar (freemarker.template.SimpleScalar)1 TemplateModelException (freemarker.template.TemplateModelException)1 List (java.util.List)1 XPath (org.apache.xpath.XPath)1 XBoolean (org.apache.xpath.objects.XBoolean)1 XNodeSet (org.apache.xpath.objects.XNodeSet)1 XNull (org.apache.xpath.objects.XNull)1 XNumber (org.apache.xpath.objects.XNumber)1 Node (org.w3c.dom.Node)1