| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.event.PipelineConfiguration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 16 |
import net.sf.saxon.lib.TraceListener; |
| 17 |
import net.sf.saxon.om.FocusIterator; |
| 18 |
import net.sf.saxon.om.Item; |
| 19 |
import net.sf.saxon.om.StandardNames; |
| 20 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.type.*; |
| 23 |
import net.sf.saxon.value.SequenceType; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class IterateInstr extends Instruction implements ContextSwitchingExpression { |
| 30 |
|
| 31 |
private Operand selectOp; |
| 32 |
private Operand actionOp; |
| 33 |
private Operand initiallyOp; |
| 34 |
private Operand onCompletionOp; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public IterateInstr(Expression select, LocalParamBlock initiallyExp, Expression action, Expression onCompletion) { |
| 46 |
if (onCompletion == null) { |
| 47 |
onCompletion = Literal.makeEmptySequence(); |
| 48 |
} |
| 49 |
selectOp = new Operand(this, select, OperandRole.FOCUS_CONTROLLING_SELECT); |
| 50 |
actionOp = new Operand(this, action, OperandRole.FOCUS_CONTROLLED_ACTION); |
| 51 |
initiallyOp = new Operand(this, initiallyExp, |
| 52 |
new OperandRole(OperandRole.CONSTRAINED_CLASS, OperandUsage.NAVIGATION, SequenceType.ANY_SEQUENCE)); |
| 53 |
onCompletionOp = new Operand(this, onCompletion, |
| 54 |
new OperandRole(OperandRole.USES_NEW_FOCUS, OperandUsage.TRANSMISSION)); |
| 55 |
} |
| 56 |
|
| 57 |
public void setSelect(Expression select) { |
| 58 |
selectOp.setChildExpression(select); |
| 59 |
} |
| 60 |
|
| 61 |
public LocalParamBlock getInitiallyExp() { |
| 62 |
return (LocalParamBlock)initiallyOp.getChildExpression(); |
| 63 |
} |
| 64 |
|
| 65 |
public void setInitiallyExp(LocalParamBlock initiallyExp) { |
| 66 |
initiallyOp.setChildExpression(initiallyExp); |
| 67 |
} |
| 68 |
|
| 69 |
public void setAction(Expression action) { |
| 70 |
actionOp.setChildExpression(action); |
| 71 |
} |
| 72 |
|
| 73 |
public Expression getOnCompletion() { |
| 74 |
return onCompletionOp.getChildExpression(); |
| 75 |
} |
| 76 |
|
| 77 |
public void setOnCompletion(Expression onCompletion) { |
| 78 |
onCompletionOp.setChildExpression(onCompletion); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Iterable<Operand> operands() { |
| 83 |
return operandList(selectOp, actionOp, initiallyOp, onCompletionOp); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@Override |
| 96 |
public int getInstructionNameCode() { |
| 97 |
return StandardNames.XSL_ITERATE; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public Expression getSelectExpression() { |
| 107 |
return selectOp.getChildExpression(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public Expression getActionExpression() { |
| 118 |
return actionOp.getChildExpression(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 127 |
|
| 128 |
selectOp.typeCheck(visitor, contextInfo); |
| 129 |
initiallyOp.typeCheck(visitor, contextInfo); |
| 130 |
|
| 131 |
ItemType selectType = getSelectExpression().getItemType(); |
| 132 |
if (selectType == ErrorType.getInstance()) { |
| 133 |
return Literal.makeEmptySequence(); |
| 134 |
} |
| 135 |
|
| 136 |
ContextItemStaticInfo cit = visitor.getConfiguration().makeContextItemStaticInfo(getSelectExpression().getItemType(), false); |
| 137 |
cit.setContextSettingExpression(getSelectExpression()); |
| 138 |
actionOp.typeCheck(visitor, cit); |
| 139 |
|
| 140 |
onCompletionOp.typeCheck(visitor, ContextItemStaticInfo.ABSENT); |
| 141 |
|
| 142 |
if (Literal.isEmptySequence(getSelectExpression())) { |
| 143 |
return getSelectExpression(); |
| 144 |
} |
| 145 |
if (Literal.isEmptySequence(getActionExpression())) { |
| 146 |
return getActionExpression(); |
| 147 |
} |
| 148 |
return this; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 153 |
|
| 154 |
selectOp.optimize(visitor, contextInfo); |
| 155 |
initiallyOp.optimize(visitor, contextInfo); |
| 156 |
|
| 157 |
ContextItemStaticInfo cit2 = visitor.getConfiguration().makeContextItemStaticInfo(getSelectExpression().getItemType(), false); |
| 158 |
cit2.setContextSettingExpression(getSelectExpression()); |
| 159 |
actionOp.optimize(visitor, cit2); |
| 160 |
|
| 161 |
onCompletionOp.optimize(visitor, ContextItemStaticInfo.ABSENT); |
| 162 |
|
| 163 |
if (Literal.isEmptySequence(getSelectExpression())) { |
| 164 |
return getSelectExpression(); |
| 165 |
} |
| 166 |
if (Literal.isEmptySequence(getActionExpression())) { |
| 167 |
return getActionExpression(); |
| 168 |
} |
| 169 |
|
| 170 |
return this; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public boolean isCompilable() { |
| 183 |
return !containsBreakOrNextIterationWithinTryCatch(this, false); |
| 184 |
} |
| 185 |
|
| 186 |
private static boolean containsBreakOrNextIterationWithinTryCatch(Expression exp, boolean withinTryCatch) { |
| 187 |
if (exp instanceof BreakInstr || exp instanceof NextIteration) { |
| 188 |
return withinTryCatch; |
| 189 |
} else { |
| 190 |
boolean found = false; |
| 191 |
boolean inTryCatch = withinTryCatch || exp instanceof TryCatch; |
| 192 |
for (Operand o : exp.operands()) { |
| 193 |
if (containsBreakOrNextIterationWithinTryCatch(o.getChildExpression(), inTryCatch)) { |
| 194 |
found = true; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
return found; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public final ItemType getItemType() { |
| 210 |
if (Literal.isEmptySequence(getOnCompletion())) { |
| 211 |
return getActionExpression().getItemType(); |
| 212 |
} else { |
| 213 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 214 |
return Type.getCommonSuperType(getActionExpression().getItemType(), getOnCompletion().getItemType(), th); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public final boolean mayCreateNewNodes() { |
| 225 |
return (getActionExpression().getSpecialProperties() & |
| 226 |
getOnCompletion().getSpecialProperties() & |
| 227 |
StaticProperty.NO_NODES_NEWLY_CREATED) == 0; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
@Override |
| 239 |
public boolean hasVariableBinding(Binding binding) { |
| 240 |
LocalParamBlock paramBlock = getInitiallyExp(); |
| 241 |
for (Operand o : paramBlock.operands()) { |
| 242 |
LocalParam setter = (LocalParam)o.getChildExpression(); |
| 243 |
if (setter == binding) { |
| 244 |
return true; |
| 245 |
} |
| 246 |
} |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
@Override |
| 257 |
public String getStreamerName() { |
| 258 |
return "Iterate"; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public int getImplementationMethod() { |
| 268 |
return PROCESS_METHOD; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException { |
| 280 |
getActionExpression().checkPermittedContents(parentType, false); |
| 281 |
getOnCompletion().checkPermittedContents(parentType, false); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public Expression copy(RebindingMap rebindings) { |
| 295 |
IterateInstr exp = new IterateInstr( |
| 296 |
getSelectExpression().copy(rebindings), |
| 297 |
(LocalParamBlock) getInitiallyExp().copy(rebindings), |
| 298 |
getActionExpression().copy(rebindings), |
| 299 |
getOnCompletion().copy(rebindings)); |
| 300 |
ExpressionTool.copyLocationInfo(this, exp); |
| 301 |
return exp; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 306 |
XPathContextMajor c2 = context.newContext(); |
| 307 |
c2.setOrigin(this); |
| 308 |
FocusIterator iter = c2.trackFocus(getSelectExpression().iterate(context)); |
| 309 |
c2.setCurrentTemplateRule(null); |
| 310 |
PipelineConfiguration pipe = c2.getReceiver().getPipelineConfiguration(); |
| 311 |
pipe.setXPathContext(c2); |
| 312 |
|
| 313 |
boolean tracing = context.getController().isTracing(); |
| 314 |
TraceListener listener = tracing ? context.getController().getTraceListener() : null; |
| 315 |
|
| 316 |
getInitiallyExp().process(context); |
| 317 |
|
| 318 |
while (true) { |
| 319 |
Item item = iter.next(); |
| 320 |
if (item != null) { |
| 321 |
if (tracing) { |
| 322 |
listener.startCurrentItem(item); |
| 323 |
} |
| 324 |
getActionExpression().process(c2); |
| 325 |
if (tracing) { |
| 326 |
listener.endCurrentItem(item); |
| 327 |
} |
| 328 |
TailCallLoop.TailCallInfo comp = c2.getTailCallInfo(); |
| 329 |
if (comp == null) { |
| 330 |
|
| 331 |
} else if (comp instanceof BreakInstr) { |
| 332 |
|
| 333 |
|
| 334 |
iter.close(); |
| 335 |
return null; |
| 336 |
} else { |
| 337 |
|
| 338 |
|
| 339 |
} |
| 340 |
} else { |
| 341 |
|
| 342 |
XPathContextMinor c3 = context.newMinorContext(); |
| 343 |
c3.setCurrentIterator(null); |
| 344 |
getOnCompletion().process(c3); |
| 345 |
break; |
| 346 |
} |
| 347 |
} |
| 348 |
pipe.setXPathContext(context); |
| 349 |
return null; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public void export(ExpressionPresenter out) throws XPathException { |
| 359 |
out.startElement("iterate", this); |
| 360 |
out.setChildRole("select"); |
| 361 |
getSelectExpression().export(out); |
| 362 |
out.setChildRole("params"); |
| 363 |
getInitiallyExp().export(out); |
| 364 |
if (!Literal.isEmptySequence(getOnCompletion())) { |
| 365 |
out.setChildRole("on-completion"); |
| 366 |
getOnCompletion().export(out); |
| 367 |
} |
| 368 |
out.setChildRole("action"); |
| 369 |
getActionExpression().export(out); |
| 370 |
out.endElement(); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|