| 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 Innermost extends SystemFunction { |
| 32 |
|
| 33 |
boolean presorted = false; |
| 34 |
|
| 35 |
|
| 36 |
@Override |
| 37 |
public int getSpecialProperties(Expression[] arguments) { |
| 38 |
return StaticProperty.ORDERED_NODESET | StaticProperty.PEER_NODESET; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@Override |
| 53 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 54 |
if ((arguments[0].getSpecialProperties() & StaticProperty.PEER_NODESET) != 0) { |
| 55 |
return arguments[0]; |
| 56 |
} |
| 57 |
if ((arguments[0].getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0) { |
| 58 |
presorted = true; |
| 59 |
} |
| 60 |
return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public Sequence<? extends NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 73 |
return SequenceTool.toLazySequence(innermost(arguments[0].iterate())); |
| 74 |
} |
| 75 |
|
| 76 |
public SequenceIterator<? extends NodeInfo> innermost(SequenceIterator<? extends NodeInfo> in) throws XPathException { |
| 77 |
if (!presorted) { |
| 78 |
in = new DocumentOrderIterator(in, GlobalOrderComparer.getInstance()); |
| 79 |
} |
| 80 |
return new InnermostIterator(in); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public void exportAttributes(ExpressionPresenter out) { |
| 86 |
super.exportAttributes(out); |
| 87 |
if (presorted) { |
| 88 |
out.emitAttribute("flags", "p"); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void importAttributes(Properties attributes) throws XPathException { |
| 94 |
super.importAttributes(attributes); |
| 95 |
String flags = attributes.getProperty("flags"); |
| 96 |
if (flags != null && flags.contains("p")) { |
| 97 |
presorted = true; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private class InnermostIterator implements SequenceIterator<NodeInfo> { |
| 113 |
|
| 114 |
SequenceIterator<? extends NodeInfo> in; |
| 115 |
NodeInfo pending = null; |
| 116 |
int position = 0; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public InnermostIterator(SequenceIterator<? extends NodeInfo> in) throws XPathException { |
| 127 |
this.in = in; |
| 128 |
pending = in.next(); |
| 129 |
} |
| 130 |
|
| 131 |
public NodeInfo next() throws XPathException { |
| 132 |
if (pending == null) { |
| 133 |
|
| 134 |
position = -1; |
| 135 |
return null; |
| 136 |
} else { |
| 137 |
while (true) { |
| 138 |
NodeInfo next = in.next(); |
| 139 |
if (next == null) { |
| 140 |
NodeInfo current = pending; |
| 141 |
position++; |
| 142 |
pending = null; |
| 143 |
return current; |
| 144 |
} |
| 145 |
if (Navigator.isAncestorOrSelf(pending, next)) { |
| 146 |
|
| 147 |
pending = next; |
| 148 |
} else { |
| 149 |
|
| 150 |
position++; |
| 151 |
NodeInfo current = pending; |
| 152 |
pending = next; |
| 153 |
return current; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public void close() { |
| 160 |
in.close(); |
| 161 |
} |
| 162 |
|
| 163 |
public int getProperties() { |
| 164 |
return 0; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|