1 package net.sf.saxon.expr; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.event.*; 5 import net.sf.saxon.instruct.LocalParam; 6 import net.sf.saxon.instruct.ParameterSet; 7 import net.sf.saxon.instruct.RegexIterator; 8 import net.sf.saxon.instruct.Template; 9 import net.sf.saxon.om.*; 10 import net.sf.saxon.sort.CodepointCollator; 11 import net.sf.saxon.sort.GroupIterator; 12 import net.sf.saxon.trace.InstructionInfoProvider; 13 import net.sf.saxon.trans.DynamicError; 14 import net.sf.saxon.trans.Mode; 15 import net.sf.saxon.trans.XPathException; 16 import net.sf.saxon.type.SchemaType; 17 18 import javax.xml.transform.Result ; 19 import java.util.Comparator ; 20 import java.util.Properties ; 21 22 27 28 public class XPathContextMinor implements XPathContext { 29 30 Controller controller; 31 SequenceIterator currentIterator; 32 int last = -1; 33 SequenceReceiver currentReceiver; 34 boolean isTemporaryDestination = false; 35 XPathContext caller = null; 36 Object origin = null; 37 38 41 42 protected XPathContextMinor() { 43 } 44 45 49 50 public XPathContextMajor newContext() { 51 return XPathContextMajor.newContext(this); 52 } 53 54 public XPathContextMinor newMinorContext() { 55 XPathContextMinor c = new XPathContextMinor(); 56 c.controller = controller; 57 c.caller = this; 58 c.currentIterator = currentIterator; 59 c.currentReceiver = currentReceiver; 60 c.last = last; 61 c.isTemporaryDestination = isTemporaryDestination; 62 return c; 63 } 64 65 68 69 public void setCaller(XPathContext caller) { 70 this.caller = caller; 71 } 72 73 76 77 public XPathContextMajor newCleanContext() { 78 XPathContextMajor c = new XPathContextMajor(this.getController()); 79 c.setCaller(this); 80 return c; 81 } 82 83 86 87 public XPathContextMajor.XSLTContext getXSLTContext() { 88 return getCaller().getXSLTContext(); 89 } 90 91 95 96 public ParameterSet getLocalParameters() { 97 return getCaller().getLocalParameters(); 98 } 99 100 104 105 public ParameterSet getTunnelParameters() { 106 return getCaller().getTunnelParameters(); 107 } 108 109 115 116 public void setOrigin(InstructionInfoProvider expr) { 117 origin = expr; 118 } 119 120 126 127 public void setOriginatingConstructType(int loc) { 128 origin = new Integer (loc); 129 } 130 131 134 135 public int getOriginatingConstructType() { 136 if (origin instanceof InstructionInfoProvider) { 137 return ((InstructionInfoProvider)origin).getInstructionInfo().getConstructType(); 138 } else { 139 return ((Integer )origin).intValue(); 140 } 141 } 142 143 146 147 public InstructionInfoProvider getOrigin() { 148 if (origin instanceof InstructionInfoProvider) { 149 return (InstructionInfoProvider)origin; 150 } else { 151 return null; 152 } 153 } 154 155 158 159 public Controller getController() { 160 return controller; 161 } 162 163 167 168 public XPathContext getCaller() { 169 return caller; 170 } 171 172 175 176 public void setCurrentIterator(SequenceIterator iter) { 177 currentIterator = iter; 178 last = 0; 179 } 180 181 187 188 public SequenceIterator getCurrentIterator() { 189 return currentIterator; 190 } 191 192 197 198 public int getContextPosition() throws DynamicError { 199 if (currentIterator==null) { 200 DynamicError e = new DynamicError("The context position is currently undefined"); 201 e.setXPathContext(this); 202 e.setErrorCode("XPDY0002"); 203 throw e; 204 } 205 return currentIterator.position(); 206 } 207 208 212 213 public Item getContextItem() { 214 if (currentIterator==null) { 215 return null; 216 } 217 return currentIterator.current(); 218 } 219 220 225 226 public int getLast() throws XPathException { 227 if (last>0) return last; 228 if (currentIterator==null) { 229 DynamicError e = new DynamicError("The context size is currently undefined"); 230 e.setXPathContext(this); 231 e.setErrorCode("XPDY0002"); 232 throw e; 233 } 234 if ((currentIterator.getProperties() & SequenceIterator.LAST_POSITION_FINDER) != 0) { 235 last = ((LastPositionFinder)currentIterator).getLastPosition(); 236 return last; 237 } else { 238 SequenceIterator another = currentIterator.getAnother(); 239 last = 0; 240 while (another.next() != null) { 241 last++; 242 } 243 return last; 244 } 245 } 246 247 251 252 public boolean isAtLast() throws XPathException { 253 if ((currentIterator.getProperties() & SequenceIterator.LOOKAHEAD) != 0) { 254 return !((LookaheadIterator)currentIterator).hasNext(); 255 } 257 return getContextPosition() == getLast(); 258 } 259 260 263 264 public Comparator getCollation(String name) throws XPathException { 265 if (name.equals(NamespaceConstant.CODEPOINT_COLLATION_URI)) { 266 return CodepointCollator.getInstance(); 267 } 268 Comparator collation = null; 269 if (controller != null) { 270 collation = controller.getExecutable().getNamedCollation(name); 271 272 if (collation == null) { 273 Configuration config = controller.getConfiguration(); 274 collation = config.getCollationURIResolver().resolve(name, null, config); 275 } 277 } 278 if (collation==null) { 279 DynamicError e = new DynamicError("Unknown collation " + name); 280 e.setXPathContext(this); 281 throw e; 282 } 283 return collation; 284 } 285 286 289 290 public Comparator getDefaultCollation() { 291 if (controller != null) { 292 return controller.getExecutable().getDefaultCollation(); 293 } else { 294 return CodepointCollator.getInstance(); 295 } 296 } 297 298 304 305 public StackFrame getStackFrame() { 306 return getCaller().getStackFrame(); 307 } 308 309 310 313 314 public ValueRepresentation evaluateLocalVariable(int slotnumber) { 315 return getCaller().evaluateLocalVariable(slotnumber); 316 } 317 318 321 322 public void setLocalVariable(int slotnumber, ValueRepresentation value) { 323 getCaller().setLocalVariable(slotnumber, value); 324 } 325 326 340 341 public void changeOutputDestination(Properties props, 342 Result result, 343 boolean isFinal, 344 int validation, 345 SchemaType schemaType) 346 throws XPathException { 347 if (isFinal && isTemporaryDestination) { 348 DynamicError err = new DynamicError("Cannot switch to a final result destination while writing a temporary tree"); 349 err.setErrorCode("XTDE1480"); 350 throw err; 351 } 352 if (isFinal) { 353 validation |= Validation.VALIDATE_OUTPUT; 354 } else { 355 isTemporaryDestination = true; 356 } 357 PipelineConfiguration pipe = controller.makePipelineConfiguration(); 358 pipe.setSerializing(isFinal); 359 ComplexContentOutputter out = new ComplexContentOutputter(); 360 out.setPipelineConfiguration(pipe); 361 362 if (props == null) { 363 props = new Properties (); 364 } 365 366 367 Receiver receiver = ResultWrapper.getReceiver( 368 result, 369 pipe, 370 props); 371 372 374 receiver = controller.getConfiguration().getDocumentValidator( 375 receiver, receiver.getSystemId(), controller.getNamePool(), validation, 376 schemaType); 377 378 380 NamespaceReducer ne = new NamespaceReducer(); 381 ne.setUnderlyingReceiver(receiver); 382 ne.setPipelineConfiguration(pipe); 383 out.setReceiver(ne); 384 385 out.open(); 386 currentReceiver = out; 387 } 388 389 395 396 public void setTemporaryReceiver(SequenceReceiver out) { 397 isTemporaryDestination = true; 398 currentReceiver = out; 399 } 400 401 404 405 public void setReceiver(SequenceReceiver receiver) { 406 currentReceiver = receiver; 407 } 408 409 414 public SequenceReceiver getReceiver() { 415 return currentReceiver; 416 } 417 418 427 428 public boolean useLocalParameter(int fingerprint, 429 LocalParam binding, 430 boolean isTunnel) throws XPathException { 431 return getCaller().useLocalParameter(fingerprint, binding, isTunnel); 432 } 433 434 438 439 public Mode getCurrentMode() { 440 return getCaller().getCurrentMode(); 441 } 442 443 448 449 public Template getCurrentTemplate() { 450 return getCaller().getCurrentTemplate(); 451 } 452 453 458 459 public GroupIterator getCurrentGroupIterator() { 460 return getCaller().getCurrentGroupIterator(); 461 } 462 463 468 469 public RegexIterator getCurrentRegexIterator() { 470 return getCaller().getCurrentRegexIterator(); 471 } 472 473 477 478 public int getImplicitTimezone() { 479 return getController().getConfiguration().getImplicitTimezone(); 480 } 481 } 482 483 | Popular Tags |