| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.expr.Component; |
| 12 |
import net.sf.saxon.expr.Expression; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.expr.XPathContextMajor; |
| 15 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 16 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 17 |
import net.sf.saxon.om.Item; |
| 18 |
import net.sf.saxon.om.StandardNames; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.Mode; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.trans.rules.Rule; |
| 23 |
|
| 24 |
import java.util.Arrays; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class NextMatch extends ApplyNextMatchingTemplate { |
| 32 |
|
| 33 |
boolean useTailRecursion; |
| 34 |
|
| 35 |
public NextMatch(boolean useTailRecursion) { |
| 36 |
super(); |
| 37 |
this.useTailRecursion = useTailRecursion; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public int getInstructionNameCode() { |
| 45 |
return StandardNames.XSL_NEXT_MATCH; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public Expression copy(RebindingMap rebindings) { |
| 57 |
NextMatch nm2 = new NextMatch(useTailRecursion); |
| 58 |
nm2.setActualParams(WithParam.copy(nm2, getActualParams(), rebindings)); |
| 59 |
nm2.setTunnelParams(WithParam.copy(nm2, getTunnelParams(), rebindings)); |
| 60 |
ExpressionTool.copyLocationInfo(this, nm2); |
| 61 |
return nm2; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 67 |
|
| 68 |
Controller controller = context.getController(); |
| 69 |
assert controller != null; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ParameterSet params = assembleParams(context, getActualParams()); |
| 74 |
ParameterSet tunnels = assembleTunnelParams(context, getTunnelParams()); |
| 75 |
|
| 76 |
Rule currentRule = context.getCurrentTemplateRule(); |
| 77 |
if (currentRule == null) { |
| 78 |
XPathException e = new XPathException("There is no current template rule", "XTDE0560"); |
| 79 |
e.setXPathContext(context); |
| 80 |
e.setLocation(getLocation()); |
| 81 |
throw e; |
| 82 |
} |
| 83 |
Component.M modeComponent = context.getCurrentMode(); |
| 84 |
if (modeComponent == null) { |
| 85 |
throw new AssertionError("Current mode is null"); |
| 86 |
} |
| 87 |
Mode mode = modeComponent.getActor(); |
| 88 |
|
| 89 |
Item currentItem = context.getCurrentIterator().current(); |
| 90 |
|
| 91 |
Rule rule = mode.getNextMatchRule(currentItem, currentRule, context); |
| 92 |
|
| 93 |
|
| 94 |
if (rule == null) { |
| 95 |
mode.getBuiltInRuleSet().process(currentItem, params, tunnels, context, getLocation()); |
| 96 |
} else if (useTailRecursion) { |
| 97 |
|
| 98 |
Arrays.fill(context.getStackFrame().getStackFrameValues(), null); |
| 99 |
((XPathContextMajor) context).setCurrentComponent(modeComponent); |
| 100 |
return new NextMatchPackage(rule, params, tunnels, context); |
| 101 |
} else { |
| 102 |
TemplateRule nh = (TemplateRule) rule.getAction(); |
| 103 |
nh.initialize(); |
| 104 |
XPathContextMajor c2 = context.newContext(); |
| 105 |
c2.setOrigin(this); |
| 106 |
|
| 107 |
c2.openStackFrame(nh.getStackFrameMap()); |
| 108 |
c2.setLocalParameters(params); |
| 109 |
c2.setTunnelParameters(tunnels); |
| 110 |
c2.setCurrentTemplateRule(rule); |
| 111 |
c2.setCurrentComponent(modeComponent); |
| 112 |
nh.apply(c2); |
| 113 |
} |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public void export(ExpressionPresenter out) throws XPathException { |
| 123 |
out.startElement("nextMatch", this); |
| 124 |
String flags = "i"; |
| 125 |
if (useTailRecursion) { |
| 126 |
flags = "t"; |
| 127 |
} |
| 128 |
out.emitAttribute("flags", flags); |
| 129 |
if (getActualParams().length != 0) { |
| 130 |
WithParam.exportParameters(getActualParams(), out, false); |
| 131 |
} |
| 132 |
if (getTunnelParams().length != 0) { |
| 133 |
WithParam.exportParameters(getTunnelParams(), out, true); |
| 134 |
} |
| 135 |
out.endElement(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private class NextMatchPackage implements TailCall { |
| 146 |
|
| 147 |
private Rule rule; |
| 148 |
private ParameterSet params; |
| 149 |
private ParameterSet tunnelParams; |
| 150 |
private XPathContext evaluationContext; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public NextMatchPackage(Rule rule, |
| 164 |
ParameterSet params, |
| 165 |
ParameterSet tunnelParams, |
| 166 |
XPathContext evaluationContext) { |
| 167 |
this.rule = rule; |
| 168 |
this.params = params; |
| 169 |
this.tunnelParams = tunnelParams; |
| 170 |
this.evaluationContext = evaluationContext; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public TailCall processLeavingTail() throws XPathException { |
| 184 |
TemplateRule nh = (TemplateRule) rule.getAction(); |
| 185 |
nh.initialize(); |
| 186 |
XPathContextMajor c2 = evaluationContext.newContext(); |
| 187 |
c2.setOrigin(NextMatch.this); |
| 188 |
|
| 189 |
c2.setLocalParameters(params); |
| 190 |
c2.setTunnelParameters(tunnelParams); |
| 191 |
c2.openStackFrame(nh.getStackFrameMap()); |
| 192 |
c2.setCurrentTemplateRule(rule); |
| 193 |
c2.setCurrentComponent(evaluationContext.getCurrentComponent()); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
return nh.applyLeavingTail(c2); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@Override |
| 208 |
public String getStreamerName() { |
| 209 |
return "NextMatch"; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
} |
| 214 |
|