| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.ItemType; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class ConditionalSorter extends Expression { |
| 26 |
|
| 27 |
private Operand conditionOp; |
| 28 |
private Operand sorterOp; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
@FunctionalInterface |
| 36 |
private interface RewriteAction { |
| 37 |
|
| 38 |
Expression rewrite(Expression e) throws XPathException; |
| 39 |
} |
| 40 |
|
| 41 |
private final static OperandRole DOC_SORTER_ROLE = |
| 42 |
new OperandRole(OperandRole.CONSTRAINED_CLASS, OperandUsage.TRANSMISSION, SequenceType.ANY_SEQUENCE); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public ConditionalSorter(Expression condition, DocumentSorter sorter) { |
| 52 |
conditionOp = new Operand(this, condition, OperandRole.SINGLE_ATOMIC); |
| 53 |
sorterOp = new Operand(this, sorter, DOC_SORTER_ROLE); |
| 54 |
adoptChildExpression(condition); |
| 55 |
adoptChildExpression(sorter); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public Iterable<Operand> operands() { |
| 60 |
return operandList(conditionOp, sorterOp); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
public void setCondition(Expression condition) { |
| 65 |
conditionOp.setChildExpression(condition); |
| 66 |
} |
| 67 |
|
| 68 |
public void setDocumentSorter(DocumentSorter documentSorter) { |
| 69 |
sorterOp.setChildExpression(documentSorter); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public Expression getCondition() { |
| 80 |
return conditionOp.getChildExpression(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public DocumentSorter getDocumentSorter() { |
| 91 |
return (DocumentSorter)sorterOp.getChildExpression(); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public Expression simplify() throws XPathException { |
| 96 |
return rewrite(Expression::simplify); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 106 |
return rewrite(exp -> exp.typeCheck(visitor, contextInfo)); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public int getCardinality() { |
| 122 |
return getDocumentSorter().getCardinality(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
protected int computeSpecialProperties() { |
| 136 |
return getCondition().getSpecialProperties() |
| 137 |
| StaticProperty.ORDERED_NODESET |
| 138 |
& ~StaticProperty.REVERSE_DOCUMENT_ORDER; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public int getImplementationMethod() { |
| 151 |
return ITERATE_METHOD; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
|
| 156 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 157 |
return rewrite(exp -> exp.optimize(visitor, contextInfo)); |
| 158 |
} |
| 159 |
|
| 160 |
private Expression rewrite(RewriteAction rewriter) throws XPathException { |
| 161 |
Expression base = rewriter.rewrite(getDocumentSorter()); |
| 162 |
if (base instanceof DocumentSorter) { |
| 163 |
sorterOp.setChildExpression(base); |
| 164 |
} else { |
| 165 |
return base; |
| 166 |
} |
| 167 |
Expression cond = rewriter.rewrite(getCondition()); |
| 168 |
if (cond instanceof Literal) { |
| 169 |
boolean b = ((Literal) cond).getValue().effectiveBooleanValue(); |
| 170 |
if (b) { |
| 171 |
return base; |
| 172 |
} else { |
| 173 |
return ((DocumentSorter) base).getBaseExpression(); |
| 174 |
} |
| 175 |
} else { |
| 176 |
conditionOp.setChildExpression(cond); |
| 177 |
return this; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException { |
| 183 |
Expression base = getDocumentSorter().unordered(retainAllNodes, forStreaming); |
| 184 |
if (base instanceof DocumentSorter) { |
| 185 |
return this; |
| 186 |
} else { |
| 187 |
return base; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
protected int computeCardinality() { |
| 200 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public Expression copy(RebindingMap rebindings) { |
| 212 |
ConditionalSorter cs = new ConditionalSorter(getCondition().copy(rebindings), (DocumentSorter) getDocumentSorter().copy(rebindings)); |
| 213 |
ExpressionTool.copyLocationInfo(this, cs); |
| 214 |
return cs; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public void export(ExpressionPresenter out) throws XPathException { |
| 225 |
out.startElement("conditionalSort", this); |
| 226 |
getCondition().export(out); |
| 227 |
getDocumentSorter().export(out); |
| 228 |
out.endElement(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public ItemType getItemType() { |
| 245 |
return getDocumentSorter().getItemType(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 266 |
boolean b = getCondition().effectiveBooleanValue(context); |
| 267 |
if (b) { |
| 268 |
return getDocumentSorter().iterate(context); |
| 269 |
} else { |
| 270 |
return getDocumentSorter().getBaseExpression().iterate(context); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
@Override |
| 282 |
public String getExpressionName() { |
| 283 |
return "conditionalSort"; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
} |
| 289 |
|