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.SequenceIterator; 5 import net.sf.saxon.om.FastStringBuffer; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.ItemType; 8 import net.sf.saxon.value.Cardinality; 9 import net.sf.saxon.value.StringValue; 10 11 14 15 public class StringJoin extends SystemFunction { 16 17 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 18 Expression exp = super.optimize(opt, env, contextItemType); 19 if (exp instanceof StringJoin) { 20 return ((StringJoin)exp).simplifySingleton(env); 21 } else { 22 return exp; 23 } 24 } 25 26 private Expression simplifySingleton(StaticContext env) { 27 int card = argument[0].getCardinality(); 28 if (!Cardinality.allowsMany(card)) { 29 if (Cardinality.allowsZero(card)) { 30 FunctionCall f = SystemFunction.makeSystemFunction("string", 1, env.getNamePool()); 31 Expression[] args = {argument[0]}; 32 f.setArguments(args); 33 return f; 34 } else { 35 return argument[0]; 36 } 37 } 38 return this; 39 } 40 41 public Item evaluateItem(XPathContext c) throws XPathException { 42 43 46 SequenceIterator iter = argument[0].iterate(c); 47 Item it = iter.next(); 48 if (it==null) { 49 return StringValue.EMPTY_STRING; 50 } 51 52 String first = it.getStringValue(); 53 54 it = iter.next(); 55 if (it==null) { 56 return StringValue.makeStringValue(first); 57 } 58 59 FastStringBuffer sb = new FastStringBuffer(1024); 60 sb.append(first); 61 62 CharSequence sep = argument[1].evaluateItem(c).getStringValueCS(); 64 sb.append(sep); 65 sb.append(it.getStringValueCS()); 66 67 while (true) { 68 it = iter.next(); 69 if (it == null) { 70 return StringValue.makeStringValue(sb.condense()); 71 } 72 sb.append(sep); 73 sb.append(it.getStringValueCS()); 74 } 75 } 76 77 } 78 79 80 81 | Popular Tags |