1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.FastStringBuffer; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.value.AtomicValue; 7 import net.sf.saxon.value.StringValue; 8 9 import java.util.StringTokenizer ; 10 11 14 15 public class NormalizeSpace extends SystemFunction { 16 17 20 21 public Expression simplify(StaticContext env) throws XPathException { 22 return simplifyArguments(env); 23 } 24 33 34 public int getIntrinsicDependencies() { 35 int d = super.getIntrinsicDependencies(); 36 if (argument.length == 0) { 37 d |= StaticProperty.DEPENDS_ON_CONTEXT_ITEM; 38 } 39 return d; 40 } 41 42 46 47 public Expression preEvaluate(StaticContext env) throws XPathException { 48 if (argument.length == 0) { 49 return this; 50 } else { 51 return ExpressionTool.eagerEvaluate(this, null); 52 } 53 } 54 55 58 59 public Item evaluateItem(XPathContext c) throws XPathException { 60 if (argument.length == 0) { 61 return StringValue.makeStringValue(normalize(c.getContextItem().getStringValueCS())); 62 } else { 63 AtomicValue sv = (AtomicValue)argument[0].evaluateItem(c); 64 if (sv==null) return StringValue.EMPTY_STRING; 65 return StringValue.makeStringValue(normalize(sv.getStringValueCS())); 66 } 67 } 68 69 72 73 public static CharSequence normalize(CharSequence s) { 74 FastStringBuffer sb = new FastStringBuffer(s.length()); 75 StringTokenizer st = new StringTokenizer (s.toString()); 76 while (st.hasMoreTokens()) { 77 sb.append(st.nextToken()); 78 if (st.hasMoreTokens()) { 79 sb.append(' '); 80 } 81 } 82 return sb.condense(); 83 } 84 85 } 86 87 88 89 90 | Popular Tags |