| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.*; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.SequenceIterator; |
| 15 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 16 |
import net.sf.saxon.pattern.Pattern; |
| 17 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.TypeHierarchy; |
| 20 |
import net.sf.saxon.value.Cardinality; |
| 21 |
import net.sf.saxon.value.SequenceType; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class DocumentSorter extends UnaryExpression { |
| 29 |
|
| 30 |
private ItemOrderComparer comparer; |
| 31 |
|
| 32 |
public DocumentSorter(Expression base) { |
| 33 |
super(base); |
| 34 |
int props = base.getSpecialProperties(); |
| 35 |
if (((props & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0) || |
| 36 |
(props & StaticProperty.SINGLE_DOCUMENT_NODESET) != 0) { |
| 37 |
comparer = LocalOrderComparer.getInstance(); |
| 38 |
} else { |
| 39 |
comparer = GlobalOrderComparer.getInstance(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public DocumentSorter(Expression base, boolean intraDocument) { |
| 44 |
super(base); |
| 45 |
if (intraDocument) { |
| 46 |
comparer = LocalOrderComparer.getInstance(); |
| 47 |
} else { |
| 48 |
comparer = GlobalOrderComparer.getInstance(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
protected OperandRole getOperandRole() { |
| 53 |
return OperandRole.SAME_FOCUS_ACTION; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public String getExpressionName() { |
| 65 |
return "docOrder"; |
| 66 |
} |
| 67 |
|
| 68 |
public ItemOrderComparer getComparer() { |
| 69 |
return comparer; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public Expression simplify() throws XPathException { |
| 74 |
Expression operand = getBaseExpression().simplify(); |
| 75 |
if (operand.hasSpecialProperty(StaticProperty.ORDERED_NODESET)) { |
| 76 |
|
| 77 |
return operand; |
| 78 |
} |
| 79 |
return this; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 84 |
Expression e2 = super.typeCheck(visitor, contextInfo); |
| 85 |
if (e2 != this) { |
| 86 |
return e2; |
| 87 |
} |
| 88 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 89 |
if (th.relationship(getBaseExpression().getItemType(), AnyNodeTest.getInstance()) == TypeHierarchy.DISJOINT) { |
| 90 |
return getBaseExpression(); |
| 91 |
} |
| 92 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.MISC, "document-order sorter", 0); |
| 93 |
Expression operand = visitor.getConfiguration().getTypeChecker(false).staticTypeCheck( |
| 94 |
getBaseExpression(), SequenceType.NODE_SEQUENCE, role, visitor); |
| 95 |
setBaseExpression(operand); |
| 96 |
return this; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 101 |
getOperand().optimize(visitor, contextInfo); |
| 102 |
Expression sortable = getBaseExpression(); |
| 103 |
boolean tryHarder = sortable.isStaticPropertiesKnown(); |
| 104 |
while (true) { |
| 105 |
if (sortable.hasSpecialProperty(StaticProperty.ORDERED_NODESET)) { |
| 106 |
|
| 107 |
return sortable; |
| 108 |
} |
| 109 |
if (!Cardinality.allowsMany(sortable.getCardinality())) { |
| 110 |
return sortable; |
| 111 |
} |
| 112 |
|
| 113 |
if (sortable instanceof SlashExpression) { |
| 114 |
SlashExpression slash = (SlashExpression)sortable; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
Expression lhs = slash.getLhsExpression(); |
| 119 |
Expression rhs = slash.getRhsExpression(); |
| 120 |
if (lhs instanceof ConditionalSorter && |
| 121 |
slash.getRhsExpression().hasSpecialProperty(StaticProperty.PEER_NODESET)) { |
| 122 |
ConditionalSorter c = (ConditionalSorter) lhs; |
| 123 |
DocumentSorter d = c.getDocumentSorter(); |
| 124 |
Expression condition = c.getCondition(); |
| 125 |
Expression s = new SlashExpression(d.getBaseExpression(), rhs); |
| 126 |
s = s.optimize(visitor, contextInfo); |
| 127 |
return new ConditionalSorter(condition, new DocumentSorter(s)); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
if (lhs instanceof DocumentSorter && rhs instanceof AxisExpression && ((AxisExpression)rhs).getAxis() == AxisInfo.CHILD) { |
| 133 |
SlashExpression s1 = new SlashExpression(((DocumentSorter)lhs).getBaseExpression(), rhs); |
| 134 |
ExpressionTool.copyLocationInfo(this, s1); |
| 135 |
return new DocumentSorter(s1).optimize(visitor, contextInfo); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if (!ExpressionTool.dependsOnFocus(rhs) && |
| 140 |
!rhs.hasSpecialProperty(StaticProperty.HAS_SIDE_EFFECTS) && |
| 141 |
rhs.hasSpecialProperty(StaticProperty.NO_NODES_NEWLY_CREATED)) { |
| 142 |
setBaseExpression(slash.getRhsExpression()); |
| 143 |
return optimize(visitor, contextInfo); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (tryHarder) { |
| 148 |
sortable.resetLocalStaticProperties(); |
| 149 |
tryHarder = false; |
| 150 |
} else { |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
if (sortable instanceof SlashExpression && !visitor.isOptimizeForStreaming() && !(getParentExpression() instanceof ConditionalSorter)) { |
| 155 |
return visitor.obtainOptimizer().makeConditionalDocumentSorter( |
| 156 |
this, (SlashExpression) sortable); |
| 157 |
} |
| 158 |
return this; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@Override |
| 170 |
public int getNetCost() { |
| 171 |
return 30; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
@Override |
| 183 |
public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException { |
| 184 |
Expression operand = getBaseExpression().unordered(retainAllNodes, forStreaming); |
| 185 |
if (operand.hasSpecialProperty(StaticProperty.ORDERED_NODESET)) { |
| 186 |
return operand; |
| 187 |
} |
| 188 |
if (!retainAllNodes) { |
| 189 |
return operand; |
| 190 |
} else if (operand instanceof SlashExpression) { |
| 191 |
|
| 192 |
|
| 193 |
SlashExpression exp = (SlashExpression)operand; |
| 194 |
Expression a = exp.getSelectExpression(); |
| 195 |
Expression b = exp.getActionExpression(); |
| 196 |
a = ExpressionTool.unfilteredExpression(a, false); |
| 197 |
b = ExpressionTool.unfilteredExpression(b, false); |
| 198 |
if (a instanceof AxisExpression && |
| 199 |
(((AxisExpression)a).getAxis() == AxisInfo.DESCENDANT || ((AxisExpression)a).getAxis() == AxisInfo.DESCENDANT_OR_SELF) && |
| 200 |
b instanceof AxisExpression && |
| 201 |
((AxisExpression)b).getAxis() == AxisInfo.CHILD) { |
| 202 |
return operand.unordered(retainAllNodes, false); |
| 203 |
} |
| 204 |
} |
| 205 |
setBaseExpression(operand); |
| 206 |
return this; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
public int computeSpecialProperties() { |
| 211 |
return getBaseExpression().getSpecialProperties() | StaticProperty.ORDERED_NODESET; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public Expression copy(RebindingMap rebindings) { |
| 223 |
DocumentSorter ds = new DocumentSorter(getBaseExpression().copy(rebindings)); |
| 224 |
ExpressionTool.copyLocationInfo(this, ds); |
| 225 |
return ds; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
@Override |
| 237 |
public Pattern toPattern(Configuration config) throws XPathException { |
| 238 |
return getBaseExpression().toPattern(config); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
@Override |
| 250 |
public int getImplementationMethod() { |
| 251 |
return ITERATE_METHOD; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 256 |
return new DocumentOrderIterator(getBaseExpression().iterate(context), comparer); |
| 257 |
} |
| 258 |
|
| 259 |
public boolean effectiveBooleanValue(XPathContext context) throws XPathException { |
| 260 |
return getBaseExpression().effectiveBooleanValue(context); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public void export(ExpressionPresenter out) throws XPathException { |
| 269 |
out.startElement("docOrder", this); |
| 270 |
out.emitAttribute("intra", comparer instanceof LocalOrderComparer ? "1" : "0"); |
| 271 |
getBaseExpression().export(out); |
| 272 |
out.endElement(); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
@Override |
| 283 |
public String getStreamerName() { |
| 284 |
return "DocumentSorterAdjunct"; |
| 285 |
} |
| 286 |
} |
| 287 |
|