1 package net.sf.saxon.instruct; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.value.Value; 5 import net.sf.saxon.event.SequenceOutputter; 6 import net.sf.saxon.event.SequenceReceiver; 7 import net.sf.saxon.expr.*; 8 import net.sf.saxon.om.*; 9 import net.sf.saxon.trace.InstructionInfo; 10 import net.sf.saxon.trans.DynamicError; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.type.ItemType; 13 import net.sf.saxon.type.Type; 14 15 import javax.xml.transform.SourceLocator ; 16 17 24 25 public abstract class Instruction extends ComputedExpression implements SourceLocator , TailCallReturner { 26 27 30 31 public Instruction() {} 32 33 37 38 public int getImplementationMethod() { 39 return Expression.PROCESS_METHOD; 40 } 41 42 45 46 public int getInstructionNameCode() { 47 return -1; 48 }; 49 50 54 55 public ItemType getItemType() { 56 return Type.ITEM_TYPE; 57 } 58 59 63 64 public int computeCardinality() { 65 return StaticProperty.ALLOWS_ZERO_OR_MORE; 66 } 67 68 78 79 public abstract TailCall processLeavingTail(XPathContext context) throws XPathException; 80 81 86 87 public void process(XPathContext context) throws XPathException { 88 TailCall tc = processLeavingTail(context); 89 while (tc != null) { 90 tc = tc.processLeavingTail(context); 91 } 92 } 93 94 97 98 public SourceLocator getSourceLocator() { 99 return this; 100 } 101 102 110 111 protected static XPathException dynamicError(SourceLocator loc, XPathException error, XPathContext context) { 112 if (error instanceof TerminationException) return error; 113 if (error.getLocator()==null || 114 (error.getLocator() instanceof ExpressionLocation && 115 context.getController().getExecutable().getHostLanguage() != Configuration.XQUERY) || 116 error.getLocator().getLineNumber()==-1) { 117 try { 120 DynamicError de = new DynamicError(error.getMessage(), 121 loc, 122 error.getException()); 123 if (error instanceof DynamicError) { 124 de.setErrorCode(error.getErrorCodeLocalPart()); 125 if (((DynamicError)error).getXPathContext()==null) { 126 de.setXPathContext(context); 127 } else { 128 de.setXPathContext(((DynamicError)error).getXPathContext()); 129 } 130 } 131 return de; 132 } catch (Exception secondaryError) { 133 return error; 135 } 136 } 137 return error; 138 } 139 140 146 147 protected XPathException dynamicError(String message, Controller controller) { 148 return new DynamicError(message, getSourceLocator()); 149 } 150 151 155 156 protected static ParameterSet assembleParams(XPathContext context, 157 WithParam[] actualParams) 158 throws XPathException { 159 if (actualParams == null || actualParams.length == 0) { 160 return null; 161 } 162 ParameterSet params = new ParameterSet(actualParams.length); 163 for (int i=0; i<actualParams.length; i++) { 164 params.put(actualParams[i].getVariableFingerprint(), 165 actualParams[i].getSelectValue(context)); 166 } 167 return params; 168 } 169 170 174 175 protected static ParameterSet assembleTunnelParams(XPathContext context, 176 WithParam[] actualParams) 177 throws XPathException { 178 ParameterSet existingParams = context.getTunnelParameters(); 179 if (existingParams == null) { 180 return assembleParams(context, actualParams); 181 } 182 ParameterSet newParams = new ParameterSet(existingParams, (actualParams==null ? 0 : actualParams.length)); 183 if (actualParams == null || actualParams.length == 0) { 184 return newParams; 185 } 186 for (int i=0; i<actualParams.length; i++) { 187 newParams.put(actualParams[i].getVariableFingerprint(), 188 actualParams[i].getSelectValue(context)); 189 } 190 return newParams; 191 } 192 193 201 202 public abstract Expression simplify(StaticContext env) throws XPathException; 203 204 211 212 public int computeSpecialProperties() { 213 int p = super.computeSpecialProperties(); 214 if (createsNewNodes()) { 215 return p; 216 } else { 217 return p | StaticProperty.NON_CREATIVE; 218 } 219 } 220 221 225 226 public boolean createsNewNodes() { 227 return false; 228 } 229 230 231 236 237 protected void promoteInst(PromotionOffer offer) throws XPathException { 238 } 239 240 253 254 public Expression promote(PromotionOffer offer) throws XPathException { 255 Expression exp = offer.accept(this); 256 if (exp!=null) { 257 return exp; 258 } else { 259 promoteInst(offer); 260 return this; 261 } 262 } 263 264 278 279 public Item evaluateItem(XPathContext context) throws XPathException { 280 int m = getImplementationMethod(); 281 if ((m & EVALUATE_METHOD) != 0) { 282 dynamicError("evaluateItem() is not implemented in the subclass " + this.getClass(), context.getController()); 283 } else if ((m & ITERATE_METHOD) != 0) { 284 return iterate(context).next(); 285 } else { 286 Controller controller = context.getController(); 287 XPathContext c2 = context.newMinorContext(); 288 c2.setOrigin(this); 289 SequenceOutputter seq = new SequenceOutputter(1); 290 seq.setPipelineConfiguration(controller.makePipelineConfiguration()); 291 c2.setTemporaryReceiver(seq); 292 process(c2); 293 seq.close(); 294 return seq.getFirstItem(); 295 } 296 return null; 297 } 298 299 312 313 public SequenceIterator iterate(XPathContext context) throws XPathException { 314 int m = getImplementationMethod(); 315 if ((m & EVALUATE_METHOD) != 0) { 316 Item item = evaluateItem(context); 317 if (item==null) { 318 return EmptyIterator.getInstance(); 319 } else { 320 return SingletonIterator.makeIterator(item); 321 } 322 } else if ((m & ITERATE_METHOD) != 0) { 323 dynamicError("iterate() is not implemented in the subclass " + this.getClass(), context.getController()); 324 } else { 325 Controller controller = context.getController(); 326 XPathContext c2 = context.newMinorContext(); 327 c2.setOrigin(this); 328 SequenceOutputter seq = new SequenceOutputter(); 329 seq.setPipelineConfiguration(controller.makePipelineConfiguration()); 330 c2.setTemporaryReceiver(seq); 331 process(c2); 332 seq.close(); 333 return seq.iterate(); 334 } 335 return null; 336 } 337 338 354 355 public final String evaluateAsString(XPathContext context) throws XPathException { 356 Item item = evaluateItem(context); 357 if (item==null) { 358 return ""; 359 } else { 360 return item.getStringValue(); 361 } 362 } 363 364 374 375 376 public final boolean effectiveBooleanValue(XPathContext context) throws XPathException { 377 return ExpressionTool.effectiveBooleanValue(iterate(context)); 378 } 379 380 public InstructionInfo getInstructionInfo() { 381 InstructionDetails details = new InstructionDetails(); 382 details.setSystemId(getSystemId()); 383 details.setLineNumber(getLineNumber()); 384 details.setConstructType(getInstructionNameCode()); 385 return details; 386 } 387 388 397 398 public static void appendItem(Item it, SequenceReceiver out, int locationId) throws XPathException { 399 while (true) { 404 if (it == null) { 405 return; 406 } else if (it instanceof UserFunctionCall.FunctionCallPackage) { 407 UserFunctionCall.FunctionCallPackage fcp = (UserFunctionCall.FunctionCallPackage)it; 411 ValueRepresentation v = fcp.call(); 412 SequenceIterator fv = Value.getIterator(v); 413 while (true) { 414 Item fvit = fv.next(); 415 if (fvit == null) { 416 it = null; 417 break; 418 } else if (fvit instanceof UserFunctionCall.FunctionCallPackage) { 419 it = fvit; 420 break; 421 } else { 422 out.append(fvit, locationId, NodeInfo.ALL_NAMESPACES); 423 } 424 } 425 } else { 427 out.append(it, locationId, NodeInfo.ALL_NAMESPACES); 428 return; 429 } 430 } 431 } 432 433 439 public boolean isXSLT(XPathContext context) { 440 return context.getController().getExecutable().getHostLanguage() == Configuration.XSLT; 441 } 442 } 443 444 445 | Popular Tags |