1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.MappingFunction; 4 import net.sf.saxon.expr.StaticContext; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.om.Item; 7 import net.sf.saxon.trans.XPathException; 8 import net.sf.saxon.type.Type; 9 import net.sf.saxon.value.*; 10 11 15 16 public class NumberFn extends SystemFunction implements MappingFunction { 17 18 22 23 public Expression simplify(StaticContext env) throws XPathException { 24 useContextItemAsDefault(); 25 return simplifyArguments(env); 26 } 27 28 31 32 public Item evaluateItem(XPathContext context) throws XPathException { 33 Item arg0 = argument[0].evaluateItem(context); 34 if (arg0==null) { 35 return DoubleValue.NaN; 36 } 37 if (arg0 instanceof BooleanValue || arg0 instanceof NumericValue) { 38 return ((AtomicValue)arg0).convert(Type.DOUBLE, context); 39 } 40 CharSequence s = arg0.getStringValueCS(); 41 try { 42 return new DoubleValue(Value.stringToNumber(s)); 43 } catch (NumberFormatException e) { 44 return DoubleValue.NaN; 45 } 46 } 47 48 52 53 public static DoubleValue convert(AtomicValue value) { 54 try { 55 if (value==null) { 56 return DoubleValue.NaN; 57 } 58 if (value instanceof BooleanValue || value instanceof NumericValue) { 59 return (DoubleValue)value.convert(Type.DOUBLE, null); 60 } 61 CharSequence s = value.getStringValueCS(); 62 return new DoubleValue(Value.stringToNumber(s)); 63 } catch (NumberFormatException e) { 64 return DoubleValue.NaN; 65 } catch (XPathException e) { 66 return DoubleValue.NaN; 67 } 68 } 69 70 74 75 public Object map(Item item, XPathContext context) throws XPathException { 76 return convert((AtomicValue)item); 77 } 78 } 79 80 | Popular Tags |