num = (NumericValue) val;
} else {
num = NumberFn.convert(val);
}
if (num.isNaN()) {
throw new XPathException("NaN"); // thrown to be caught
}
num = num.round();
if (num.compareTo(Int64Value.MAX_LONG) > 0) {
vec.add(((BigIntegerValue)num.convert(BuiltInAtomicType.INTEGER, true, context).asAtomic()).asBigInteger());
} else {
if (num.compareTo(Int64Value.ZERO) < 0) {
throw new XPathException("The numbers to be formatted must not be negative");
// thrown to be caught
}
long i = ((NumericValue)num.convert(BuiltInAtomicType.INTEGER, true, context).asAtomic()).longValue();
vec.add(new Long(i));
}
} catch (XPathException err) {
if (backwardsCompatible) {
vec.add("NaN");
} else {
vec.add(val.getStringValue());
XPathException e = new XPathException("Cannot convert supplied value to an integer. " + err.getMessage());
e.setErrorCode("XTDE0980");
e.setXPathContext(context);
throw e;
}
}
}
if (backwardsCompatible && vec.isEmpty()) {
vec.add("NaN");
}
} else {
NodeInfo source;
if (select != null) {
source = (NodeInfo) select.evaluateItem(context);
} else {
Item item = context.getContextItem();
if (!(item instanceof NodeInfo)) {
XPathException err = new XPathException("context item for xsl:number must be a node");
err.setErrorCode("XTTE0990");
err.setIsTypeError(true);
err.setXPathContext(context);
throw err;
}
source = (NodeInfo) item;
}
if (level == SIMPLE) {
value = Navigator.getNumberSimple(source, context);
} else if (level == SINGLE) {
value = Navigator.getNumberSingle(source, count, from, context);
if (value == 0) {
vec = Collections.EMPTY_LIST; // an empty list
}
} else if (level == ANY) {
value = Navigator.getNumberAny(this, source, count, from, context, hasVariablesInPatterns);
if (value == 0) {
vec = Collections.EMPTY_LIST; // an empty list
}
} else if (level == MULTI) {
vec = Navigator.getNumberMulti(source, count, from, context);
}
}
int gpsize = 0;
String gpseparator = "";
String letterVal;
String ordinalVal = null;
if (groupSize != null) {
String g = groupSize.evaluateAsString(context).toString();
try {
gpsize = Integer.parseInt(g);
} catch (NumberFormatException err) {
XPathException e = new XPathException("grouping-size must be numeric");
e.setXPathContext(context);
e.setErrorCode("XTDE0030");
throw e;
}
}
if (groupSeparator != null) {
gpseparator = groupSeparator.evaluateAsString(context).toString();
}
if (ordinal != null) {
ordinalVal = ordinal.evaluateAsString(context).toString();
}
// fast path for the simple case
if (vec == null && format == null && gpsize == 0 && lang == null) {
return new StringValue("" + value);
}
// Use the numberer decided at compile time if possible; otherwise try to get it from
// a table of numberers indexed by language; if not there, load the relevant class and
// add it to the table.
Numberer numb = numberer;
if (numb == null) {
String language = lang.evaluateAsString(context).toString();
if (nationalNumberers == null) {
nationalNumberers = new HashMap(4);
}
numb = (Numberer)nationalNumberers.get(language);
if (numb == null) {
numb = makeNumberer(language, null, context);
nationalNumberers.put(language, numb);
}
}
if (letterValue == null) {
letterVal = "";
} else {
letterVal = letterValue.evaluateAsString(context).toString();
if (!("alphabetic".equals(letterVal) || "traditional".equals(letterVal))) {
XPathException e = new XPathException("letter-value must be \"traditional\" or \"alphabetic\"");
e.setXPathContext(context);
e.setErrorCode("XTDE0030");
throw e;
}
}
if (vec == null) {