1 package net.sf.saxon.sort; 2 3 import net.sf.saxon.expr.*; 4 import net.sf.saxon.om.Item; 5 import net.sf.saxon.om.NamePool; 6 import net.sf.saxon.om.SequenceIterator; 7 import net.sf.saxon.trans.XPathException; 8 import net.sf.saxon.type.AnyItemType; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.type.Type; 11 import net.sf.saxon.value.EmptySequence; 12 import net.sf.saxon.value.ObjectValue; 13 import net.sf.saxon.value.Value; 14 15 import java.io.PrintStream ; 16 import java.util.Iterator ; 17 18 22 public class TupleSorter extends ComputedExpression implements MappingFunction { 23 24 private Expression base; 25 private FixedSortKeyDefinition[] sortKeys; 26 27 32 public TupleSorter(Expression base, FixedSortKeyDefinition[] keys) { 33 this.base = base; 34 this.sortKeys = keys; 35 adoptChildExpression(base); 36 } 37 38 public Expression simplify(StaticContext env) throws XPathException { 39 base = base.simplify(env); 40 return this; 41 } 42 43 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 44 base = base.typeCheck(env, contextItemType); 45 return this; 46 } 47 48 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 49 base = base.optimize(opt, env, contextItemType); 50 if (base instanceof EmptySequence) { 51 return base; 52 } 53 return this; 54 } 55 56 public ItemType getItemType() { 57 return AnyItemType.getInstance(); 58 } 60 61 public int computeCardinality() { 62 return StaticProperty.ALLOWS_ZERO_OR_MORE; 63 } 64 65 public Iterator iterateSubExpressions() { 66 return new MonoIterator(base); 67 } 68 69 72 73 public Expression promote(PromotionOffer offer) throws XPathException { 74 Expression exp = offer.accept(this); 75 if (exp != null) { 76 return exp; 77 } else { 78 base = base.promote(offer); 79 return this; 80 } 81 } 82 83 public SequenceIterator iterate(XPathContext context) throws XPathException { 84 SequenceIterator iter = new SortedTupleIterator(context, base.iterate(context), sortKeys); 85 MappingIterator mapper = new MappingIterator(iter, this, context); 86 return mapper; 87 } 88 89 public boolean effectiveBooleanValue(XPathContext context) throws XPathException { 90 ItemType type = base.getItemType(); 94 if (type == Type.ITEM_TYPE) { 95 return super.effectiveBooleanValue(context); 96 } else { 97 return base.effectiveBooleanValue(context); 98 } 99 } 100 101 public void display(int level, NamePool pool, PrintStream out) { 102 out.println(ExpressionTool.indent(level) + "TupleSorter"); 103 base.display(level+1, pool, out); 104 } 105 106 111 112 public Object map(Item item, XPathContext context) throws XPathException { 113 ObjectValue tuple = (ObjectValue)item; 114 Object o = tuple.getObject(); 115 if (o == null) { 116 return o; 117 } 118 if (o instanceof Item) { 119 return o; 120 } 121 Value value = (Value)o; 122 return value.iterate(context); 123 } 124 125 } 126 127 128 | Popular Tags |