1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.trans.XPathException; 5 import net.sf.saxon.value.AtomicValue; 6 import net.sf.saxon.value.IntegerValue; 7 import net.sf.saxon.value.StringValue; 8 9 12 13 public class StringLength extends SystemFunction { 14 15 private boolean shortcut = false; 16 22 23 public Expression simplify(StaticContext env) throws XPathException { 24 return simplifyArguments(env); 26 } 27 28 37 38 public int getIntrinsicDependencies() { 39 int d = super.getIntrinsicDependencies(); 40 if (argument.length == 0) { 41 d |= StaticProperty.DEPENDS_ON_CONTEXT_ITEM; 42 } 43 return d; 44 } 45 46 50 51 public Expression preEvaluate(StaticContext env) throws XPathException { 52 if (argument.length == 0) { 53 return this; 54 } else { 55 return ExpressionTool.eagerEvaluate(this, null); 56 } 57 } 58 59 62 63 public void setShortcut() { 64 shortcut = true; 65 } 66 67 70 71 public Item evaluateItem(XPathContext c) throws XPathException { 72 AtomicValue sv; 73 if (argument.length == 0) { 74 sv = StringValue.makeStringValue(c.getContextItem().getStringValueCS()); 75 } else { 76 sv = (AtomicValue)argument[0].evaluateItem(c); 77 } 78 if (sv==null) { 79 return IntegerValue.ZERO; 80 } 81 CharSequence s = sv.getStringValueCS(); 82 83 if (shortcut) { 84 return new IntegerValue((s.length()>0 ? 1 : 0)); 85 } else { 86 return new IntegerValue(StringValue.getStringLength(s)); 87 } 88 } 89 90 } 91 92 93 94 | Popular Tags |