| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.StaticProperty; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.sort.DocumentOrderIterator; |
| 16 |
import net.sf.saxon.expr.sort.GlobalOrderComparer; |
| 17 |
import net.sf.saxon.om.NodeInfo; |
| 18 |
import net.sf.saxon.om.Sequence; |
| 19 |
import net.sf.saxon.om.SequenceIterator; |
| 20 |
import net.sf.saxon.om.SequenceTool; |
| 21 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.tree.util.Navigator; |
| 24 |
|
| 25 |
import java.util.Properties; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class Outermost extends SystemFunction { |
| 32 |
|
| 33 |
boolean presorted = false; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@Override |
| 46 |
public Expression makeOptimizedFunctionCall( |
| 47 |
ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 48 |
if ((arguments[0].getSpecialProperties() & StaticProperty.PEER_NODESET) != 0) { |
| 49 |
return arguments[0]; |
| 50 |
} |
| 51 |
presorted = (arguments[0].getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0; |
| 52 |
return null; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public int getSpecialProperties(Expression[] arguments) { |
| 57 |
return StaticProperty.ORDERED_NODESET | StaticProperty.PEER_NODESET; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 70 |
SequenceIterator<?> in = arguments[0].iterate(); |
| 71 |
if (!presorted) { |
| 72 |
in = new DocumentOrderIterator(in, GlobalOrderComparer.getInstance()); |
| 73 |
} |
| 74 |
SequenceIterator<?> out = new OutermostIterator(in); |
| 75 |
return SequenceTool.toLazySequence(out); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
@Override |
| 80 |
public void exportAttributes(ExpressionPresenter out) { |
| 81 |
super.exportAttributes(out); |
| 82 |
if (presorted) { |
| 83 |
out.emitAttribute("flags", "p"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void importAttributes(Properties attributes) throws XPathException { |
| 89 |
super.importAttributes(attributes); |
| 90 |
String flags = attributes.getProperty("flags"); |
| 91 |
if (flags != null && flags.contains("p")) { |
| 92 |
presorted = true; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private class OutermostIterator implements SequenceIterator { |
| 106 |
|
| 107 |
SequenceIterator in; |
| 108 |
NodeInfo current = null; |
| 109 |
int position = 0; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public OutermostIterator(SequenceIterator in) { |
| 119 |
this.in = in; |
| 120 |
} |
| 121 |
|
| 122 |
public NodeInfo next() throws XPathException { |
| 123 |
while (true) { |
| 124 |
NodeInfo next = (NodeInfo)in.next(); |
| 125 |
if (next == null) { |
| 126 |
current = null; |
| 127 |
position = -1; |
| 128 |
return null; |
| 129 |
} |
| 130 |
if (current == null || !Navigator.isAncestorOrSelf(current, next)) { |
| 131 |
current = next; |
| 132 |
position++; |
| 133 |
return current; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void close() { |
| 139 |
in.close(); |
| 140 |
} |
| 141 |
|
| 142 |
public int getProperties() { |
| 143 |
return 0; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public String getStreamerName() { |
| 148 |
return "Outermost"; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|