1 package net.sf.saxon.instruct; 2 3 import net.sf.saxon.expr.Atomizer; 4 import net.sf.saxon.expr.Expression; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.expr.StaticProperty; 7 import net.sf.saxon.om.Item; 8 import net.sf.saxon.om.SequenceIterator; 9 import net.sf.saxon.om.FastStringBuffer; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.type.Type; 12 import net.sf.saxon.value.AtomicValue; 13 import net.sf.saxon.value.StringValue; 14 15 22 23 public class QuerySimpleContentConstructor extends SimpleContentConstructor { 24 25 boolean noNodeIfEmpty; 26 27 public QuerySimpleContentConstructor(Expression select, Expression separator, boolean noNodeIfEmpty) { 28 super(select, separator); 29 this.noNodeIfEmpty = noNodeIfEmpty; 30 } 31 32 36 37 protected int computeCardinality() { 38 if (noNodeIfEmpty) { 39 return StaticProperty.ALLOWS_ZERO_OR_ONE; 40 } else { 41 return StaticProperty.EXACTLY_ONE; 42 } 43 } 44 45 50 51 public CharSequence expandChildren(XPathContext context) throws XPathException { 52 Item item = select.evaluateItem(context); 53 if (item==null) { 54 return (noNodeIfEmpty ? null : ""); 55 } else { 56 return item.getStringValueCS(); 57 } 58 } 59 60 75 76 public Item evaluateItem(XPathContext context) throws XPathException { 77 78 if (isSingleton && isAtomic) { 79 Item item = select.evaluateItem(context); 81 if (item == null) { 82 if (noNodeIfEmpty) { 83 return null; 84 } else { 85 return StringValue.EMPTY_STRING; 86 } 87 } 88 if (item instanceof StringValue) { 89 return item; 90 } else { 91 return ((AtomicValue)item).convert(Type.STRING, context); 92 } 93 } 94 SequenceIterator iter = select.iterate(context); 95 if (!isAtomic) { 96 iter = Atomizer.AtomizingFunction.getAtomizingIterator(iter); 97 } 98 FastStringBuffer sb = new FastStringBuffer(1024); 99 boolean first = true; 100 String sep = " "; 101 while (true) { 102 Item item = iter.next(); 103 if (item==null) { 104 break; 105 } 106 if (!first) { 107 sb.append(sep); 108 } 109 first = false; 110 sb.append(item.getStringValueCS()); 111 } 112 if (first && noNodeIfEmpty) { 113 return null; 114 } 115 return StringValue.makeStringValue(sb.condense()); 116 } 117 118 } 119 120 121 | Popular Tags |