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 NormalizeSpace extends Function { 12 13 16 17 public String getName() { 18 return "normalize-space"; 19 }; 20 21 25 26 public int getDataType() { 27 return Value.STRING; 28 } 29 30 33 34 public Expression simplify() throws XPathException { 35 int numArgs = checkArgumentCount(0, 1); 36 if (numArgs==1) { 37 argument[0] = argument[0].simplify(); 38 if (argument[0] instanceof Value) { 39 return evaluate(null); 40 } 41 } 42 return this; 43 } 44 45 48 49 public String evaluateAsString(Context c) throws XPathException { 50 if (getNumberOfArguments()==1) { 51 return normalize(argument[0].evaluateAsString(c)); 52 } else { 53 return normalize((c.getContextNodeInfo()).getStringValue()); 54 } 55 } 56 57 60 61 public Value evaluate(Context c) throws XPathException { 62 return new StringValue(evaluateAsString(c)); 63 } 64 65 68 69 public int getDependencies() { 70 if (getNumberOfArguments()==1) { 71 return argument[0].getDependencies(); 72 } else { 73 return Context.CONTEXT_NODE; 74 } 75 } 76 77 80 81 public Expression reduce(int dep, Context c) throws XPathException { 82 if (getNumberOfArguments()==1) { 83 NormalizeSpace f = new NormalizeSpace(); 84 f.addArgument(argument[0].reduce(dep, c)); 85 f.setStaticContext(getStaticContext()); 86 return f.simplify(); 87 } else { 88 if ((dep & Context.CONTEXT_NODE)!=0) { 89 return evaluate(c); 90 } else { 91 return this; 92 } 93 } 94 } 95 96 99 100 private static String normalize(String s) { 101 StringBuffer sb = new StringBuffer (); 102 StringTokenizer st = new StringTokenizer(s); 103 while (st.hasMoreTokens()) { 104 sb.append(st.nextToken()); 105 if (st.hasMoreTokens()) sb.append(" "); 106 } 107 return sb.toString(); 108 } 109 110 } 111 112 113 114 115 | Popular Tags |