1 package com.icl.saxon; 2 import com.icl.saxon.om.*; 3 import com.icl.saxon.expr.*; 4 import com.icl.saxon.output.Outputter; 5 import com.icl.saxon.style.XSLTemplate; 6 import com.icl.saxon.functions.SystemProperty; 7 import javax.xml.transform.TransformerException ; 8 9 import java.util.Stack ; 10 import java.net.URL ; 11 import java.io.Writer ; 12 import org.w3c.dom.Node ; 13 import org.w3c.dom.Document ; 14 15 import org.w3c.xsl.XSLTContext; 16 17 18 23 24 public final class Context implements XSLTContext, LastPositionFinder { 25 26 28 public static final int VARIABLES = 1; public static final int CURRENT_NODE = 4; public static final int CONTEXT_NODE = 8; public static final int POSITION = 16; public static final int LAST = 32; public static final int CONTROLLER = 64; public static final int CONTEXT_DOCUMENT = 128; public static final int NO_DEPENDENCIES = 0; 37 public static final int ALL_DEPENDENCIES = 255; 38 public static final int XSLT_CONTEXT = CONTROLLER | VARIABLES | CURRENT_NODE; 39 40 private NodeInfo contextNode; 41 private NodeInfo currentNode; 42 private int position = -1; 43 private int last = -1; 44 private LastPositionFinder lastPositionFinder; 45 private Controller controller; 46 private Mode currentMode; 48 private XSLTemplate currentTemplate; 49 private Stack groupActivationStack; private StaticContext staticContext; 51 private ParameterSet tailRecursion; private NodeInfo lastRememberedNode = null; 53 private int lastRememberedNumber = -1; 54 private Value returnValue = null; 55 private XPathException exception = null; 56 57 private static Controller defaultController = null; 58 59 67 68 public Context() { 69 if (defaultController==null) { 70 defaultController = new Controller(); 71 } 72 controller = defaultController; 73 lastPositionFinder = this; 74 } 75 76 77 80 81 public Context(Controller c) { 82 controller = c; 83 lastPositionFinder = this; 84 } 85 86 89 90 public Context newContext() { 91 Context c = new Context(controller); 92 c.staticContext = staticContext; 93 c.currentNode = currentNode; 94 c.contextNode = contextNode; 95 c.position = position; 96 c.last = last; 97 c.lastPositionFinder = lastPositionFinder; 98 c.currentMode = currentMode; 99 c.currentTemplate = currentTemplate; 100 c.groupActivationStack = groupActivationStack; 102 c.lastRememberedNode = lastRememberedNode; 103 c.lastRememberedNumber = lastRememberedNumber; 104 c.returnValue = null; 105 return c; 106 } 107 108 111 112 public void setController(Controller c) { 113 controller = c; 114 } 115 116 119 120 public Controller getController() { 121 return controller; 122 } 123 124 127 128 public Bindery getBindery() { 129 return controller.getBindery(); 130 } 131 132 137 138 public Outputter getOutputter() { 139 return controller.getOutputter(); 140 } 141 142 145 146 public void setMode(Mode mode) { 147 currentMode = mode; 148 } 149 150 153 154 public Mode getMode() { 155 return currentMode; 156 } 157 158 163 164 public void setContextNode(NodeInfo node) { 165 this.contextNode = node; 166 } 167 168 172 173 public NodeInfo getContextNodeInfo() { 174 return contextNode; 175 } 176 177 181 182 public Node getContextNode() { 183 if (contextNode instanceof Node ) { 184 return (Node )contextNode; 185 } else { 186 return null; 187 } 188 } 189 190 193 194 public void setPosition(int pos) { 195 position = pos; 196 } 197 198 202 203 public int getContextPosition() { 204 return position; 205 } 206 207 211 212 public void setLast(int last) { 213 this.last = last; 214 lastPositionFinder = this; 215 } 216 217 221 222 public void setLastPositionFinder(LastPositionFinder finder) { 223 lastPositionFinder = finder; 224 } 225 226 230 231 public int getLast() throws XPathException { 232 if (lastPositionFinder==null) return 1; return lastPositionFinder.getLastPosition(); 234 } 235 236 240 241 public boolean isAtLast() throws XPathException { 242 if (lastPositionFinder!=null && lastPositionFinder instanceof NodeEnumeration) { 245 return !((NodeEnumeration)lastPositionFinder).hasMoreElements(); 246 } else { 247 return getContextPosition() == getLast(); 248 } 249 } 250 251 259 260 public int getContextSize() { 261 try { 262 return getLast(); 263 } catch (XPathException err) { 264 setException(err); 267 return getContextPosition(); } 269 } 270 271 275 276 public int getLastPosition() { 277 return last; 278 } 279 280 284 285 public void setCurrentNode(NodeInfo node) { 286 currentNode = node; 287 } 288 289 294 295 public NodeInfo getCurrentNodeInfo() { 296 return currentNode; 297 } 298 299 306 307 public Node getCurrentNode() { 308 if (currentNode instanceof Node ) { 309 return (Node )currentNode; 310 } else { 311 return null; 312 } 313 } 314 315 318 319 public void setCurrentTemplate(XSLTemplate template) { 320 currentTemplate = template; 321 } 322 323 326 327 public XSLTemplate getCurrentTemplate() { 328 return currentTemplate; 329 } 330 331 334 335 public Document getOwnerDocument() { 336 return (Document )(Node )contextNode.getDocumentRoot(); 337 } 338 339 342 343 public Object systemProperty(String namespaceURI, String localName) { 344 try { 345 Value prop = SystemProperty.getProperty(namespaceURI, localName); 346 if (prop==null) { 347 return null; 348 } else if (prop instanceof StringValue) { 349 return prop.asString(); 350 } else if (prop instanceof NumericValue) { 351 return new Double (prop.asNumber()); 352 } else if (prop instanceof BooleanValue) { 353 return new Boolean (prop.asBoolean()); 354 } else { 355 return prop; 356 } 357 } catch (Exception err) { 358 return null; 359 } 360 } 361 362 366 367 public String stringValue(Node n) { 368 if (n instanceof NodeInfo) { 369 return ((NodeInfo)n).getStringValue(); 370 } else { 371 throw new IllegalArgumentException ("Node is not a Saxon node"); 372 } 373 } 374 375 378 379 public void setStaticContext(StaticContext sc) { 380 staticContext = sc; 381 } 382 383 387 388 public StaticContext getStaticContext() { 389 return staticContext; 390 } 391 392 397 398 public void setException(XPathException err) { 399 exception = err; 400 } 401 402 405 406 public XPathException getException() { 407 return exception; 408 } 409 410 413 414 public Stack getGroupActivationStack() { 415 if (groupActivationStack==null) { 416 groupActivationStack = new Stack (); 417 } 418 return groupActivationStack; 419 } 420 421 424 425 public void setRememberedNumber(NodeInfo node, int number) { 426 lastRememberedNode = node; 427 lastRememberedNumber = number; 428 } 429 430 434 435 public int getRememberedNumber(NodeInfo node) { 436 if (lastRememberedNode == node) return lastRememberedNumber; 437 return -1; 438 } 439 440 443 444 public void setTailRecursion(ParameterSet p) { 445 tailRecursion = p; 446 } 447 448 451 452 public ParameterSet getTailRecursion() { 453 return tailRecursion; 454 } 455 456 459 460 public void setReturnValue(Value value) throws TransformerException { 461 if (value != null && returnValue != null) { 462 throw new TransformerException ("A function can only return one result"); 463 } 464 returnValue = value; 465 } 466 467 470 471 public Value getReturnValue() { 472 return returnValue; 473 } 474 } 475 476 | Popular Tags |