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 import java.util.*; 6 7 public class StringLength extends Function { 8 9 12 13 public String getName() { 14 return "string-length"; 15 }; 16 17 21 22 public int getDataType() { 23 return Value.NUMBER; 24 } 25 26 30 31 public Expression simplify() throws XPathException { 32 int numArgs = checkArgumentCount(0, 1); 33 if (numArgs==1) { 34 argument[0] = argument[0].simplify(); 35 if (argument[0] instanceof Value) { 36 return evaluate(null); 37 } 38 } 39 return this; 40 } 41 42 45 46 public double evaluateAsNumber(Context c) throws XPathException { 47 if (getNumberOfArguments()==1) { 48 return StringValue.getLength( 49 argument[0].evaluateAsString(c)); 50 } else { 51 return StringValue.getLength( 52 (c.getContextNodeInfo()).getStringValue()); 53 } 54 } 55 56 59 60 public Value evaluate(Context c) throws XPathException { 61 return new NumericValue(evaluateAsNumber(c)); 62 } 63 64 67 68 public int getDependencies() { 69 if (getNumberOfArguments()==1) { 70 return argument[0].getDependencies(); 71 } else { 72 return Context.CONTEXT_NODE; 73 } 74 } 75 76 79 80 public Expression reduce(int dep, Context c) throws XPathException { 81 if (getNumberOfArguments()==1) { 82 StringLength f = new StringLength(); 83 f.addArgument(argument[0].reduce(dep, c)); 84 f.setStaticContext(getStaticContext()); 85 return f.simplify(); 86 } else { 87 if ((dep & Context.CONTEXT_NODE) != 0) { 88 return evaluate(c); 89 } else { 90 return this; 91 } 92 } 93 } 94 95 } 96 97 98 99 | Popular Tags |