1 package com.icl.saxon.functions; 2 import com.icl.saxon.*; 3 import com.icl.saxon.expr.*; 4 import com.icl.saxon.om.NodeInfo; 5 6 import java.util.*; 7 import java.text.*; 8 9 10 11 public class NumberFn extends Function { 12 13 16 17 public String getName() { 18 return "number"; 19 }; 20 21 25 26 public int getDataType() { 27 return Value.NUMBER; 28 } 29 30 34 35 public Expression simplify() throws XPathException { 36 int numArgs = checkArgumentCount(0, 1); 37 if (numArgs==1) { 38 argument[0] = argument[0].simplify(); 39 if (argument[0].getDataType()==Value.NUMBER) { 40 return argument[0]; 41 } 42 if (argument[0] instanceof Value) { 43 return new NumericValue(((Value)argument[0]).asNumber()); 44 } 45 } 46 return this; 47 } 48 49 52 53 public double evaluateAsNumber(Context c) throws XPathException { 54 if (getNumberOfArguments()==1) { 55 return argument[0].evaluateAsNumber(c); 56 } else { 57 return Value.stringToNumber( 58 (c.getContextNodeInfo()).getStringValue()); 59 } 60 } 61 62 65 66 public Value evaluate(Context c) throws XPathException { 67 return new NumericValue(evaluateAsNumber(c)); 68 } 69 70 73 74 public int getDependencies() { 75 if (getNumberOfArguments()==1) { 76 return argument[0].getDependencies(); 77 } else { 78 return Context.CONTEXT_NODE; 79 } 80 } 81 82 85 86 public Expression reduce(int dep, Context c) throws XPathException { 87 if (getNumberOfArguments()==1) { 88 NumberFn f = new NumberFn(); 89 f.addArgument(argument[0].reduce(dep, c)); 90 f.setStaticContext(getStaticContext()); 91 return f.simplify(); 92 } else { 93 if ((dep & Context.CONTEXT_NODE) != 0) { 94 return evaluate(c); 95 } else { 96 return this; 97 } 98 } 99 } 100 101 } 102 103 | Popular Tags |