| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.Operand; |
| 12 |
import net.sf.saxon.expr.OperandRole; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.om.Sequence; |
| 16 |
import net.sf.saxon.om.StandardNames; |
| 17 |
import net.sf.saxon.om.StructuredQName; |
| 18 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.value.SequenceType; |
| 21 |
|
| 22 |
import java.util.List; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class WithParam { |
| 29 |
|
| 30 |
public static WithParam[] EMPTY_ARRAY = new WithParam[0]; |
| 31 |
|
| 32 |
private Operand selectOp; |
| 33 |
|
| 34 |
private boolean typeChecked = false; |
| 35 |
private int slotNumber = -1; |
| 36 |
private SequenceType requiredType; |
| 37 |
private StructuredQName variableQName; |
| 38 |
private Evaluator evaluator = null; |
| 39 |
|
| 40 |
public WithParam() { |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public void setSelectExpression(Expression parent, Expression select) { |
| 50 |
selectOp = new Operand(parent, select, OperandRole.NAVIGATE); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public Expression getSelectExpression() { |
| 60 |
return selectOp.getChildExpression(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void setRequiredType(SequenceType required) { |
| 70 |
requiredType = required; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public SequenceType getRequiredType() { |
| 80 |
return requiredType; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public int getSlotNumber() { |
| 91 |
return slotNumber; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void setSlotNumber(int s) { |
| 101 |
slotNumber = s; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void setVariableQName(StructuredQName s) { |
| 111 |
variableQName = s; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public StructuredQName getVariableQName() { |
| 121 |
return variableQName; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void setTypeChecked(boolean checked) { |
| 132 |
typeChecked = checked; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
public int getInstructionNameCode() { |
| 137 |
return StandardNames.XSL_WITH_PARAM; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public static void simplify(WithParam[] params) throws XPathException { |
| 148 |
if (params != null) { |
| 149 |
for (WithParam param : params) { |
| 150 |
param.selectOp.setChildExpression(param.selectOp.getChildExpression().simplify()); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static void typeCheck(WithParam[] params, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 166 |
if (params != null) { |
| 167 |
for (WithParam param : params) { |
| 168 |
param.selectOp.typeCheck(visitor, contextItemType); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public static void optimize(ExpressionVisitor visitor, WithParam[] params, ContextItemStaticInfo contextItemType) throws XPathException { |
| 183 |
if (params != null) { |
| 184 |
for (WithParam param : params) { |
| 185 |
param.selectOp.optimize(visitor, contextItemType); |
| 186 |
param.computeEvaluator(); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public int getEvaluationMode() { |
| 198 |
if (evaluator == null) { |
| 199 |
computeEvaluator(); |
| 200 |
} |
| 201 |
return evaluator.getCode(); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private void computeEvaluator() { |
| 207 |
evaluator = ExpressionTool.lazyEvaluator(selectOp.getChildExpression(), true); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public static WithParam[] copy(Expression parent, WithParam[] params, RebindingMap rebindings) { |
| 219 |
if (params == null) { |
| 220 |
return null; |
| 221 |
} |
| 222 |
WithParam[] result = new WithParam[params.length]; |
| 223 |
for (int i = 0; i < params.length; i++) { |
| 224 |
result[i] = new WithParam(); |
| 225 |
|
| 226 |
result[i].slotNumber = params[i].slotNumber; |
| 227 |
result[i].typeChecked = params[i].typeChecked; |
| 228 |
result[i].selectOp = new Operand(parent, params[i].selectOp.getChildExpression().copy(rebindings), OperandRole.NAVIGATE); |
| 229 |
result[i].requiredType = params[i].requiredType; |
| 230 |
result[i].variableQName = params[i].variableQName; |
| 231 |
} |
| 232 |
return result; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public static void gatherOperands(Expression parent, WithParam[] params, List<Operand> list) { |
| 244 |
if (params != null) { |
| 245 |
for (WithParam param : params) { |
| 246 |
list.add(param.selectOp); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public static void exportParameters(WithParam[] params, ExpressionPresenter out, boolean tunnel) throws XPathException{ |
| 261 |
if (params != null) { |
| 262 |
for (WithParam param : params) { |
| 263 |
out.startElement("withParam"); |
| 264 |
out.emitAttribute("name", param.variableQName); |
| 265 |
String flags = ""; |
| 266 |
if (tunnel) { |
| 267 |
flags += "t"; |
| 268 |
} |
| 269 |
if (param.isTypeChecked()) { |
| 270 |
flags += "c"; |
| 271 |
} |
| 272 |
if (!flags.isEmpty()) { |
| 273 |
out.emitAttribute("flags", flags); |
| 274 |
} |
| 275 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions(); |
| 276 |
if (param.getRequiredType() != SequenceType.ANY_SEQUENCE) { |
| 277 |
out.emitAttribute("as", param.getRequiredType().toExportString()); |
| 278 |
if (options.target.equals("JS") && options.targetVersion == 2) { |
| 279 |
out.emitAttribute("asJ", param.getRequiredType().toExportString2()); |
| 280 |
} |
| 281 |
} |
| 282 |
if (param.getSlotNumber() != -1) { |
| 283 |
out.emitAttribute("slot", param.getSlotNumber() + ""); |
| 284 |
} |
| 285 |
param.selectOp.getChildExpression().export(out); |
| 286 |
out.endElement(); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public Sequence<?> getSelectValue(XPathContext context) throws XPathException { |
| 304 |
|
| 305 |
|
| 306 |
if (evaluator == null) { |
| 307 |
computeEvaluator(); |
| 308 |
} |
| 309 |
int savedOutputState = context.getTemporaryOutputState(); |
| 310 |
context.setTemporaryOutputState(StandardNames.XSL_WITH_PARAM); |
| 311 |
Sequence<?> result = evaluator.evaluate(selectOp.getChildExpression(), context); |
| 312 |
context.setTemporaryOutputState(savedOutputState); |
| 313 |
return result; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public boolean isTypeChecked() { |
| 324 |
return typeChecked; |
| 325 |
} |
| 326 |
} |
| 327 |
|