1 16 19 package org.apache.xpath; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.res.XSLMessages; 24 import org.apache.xpath.objects.XObject; 25 import org.apache.xpath.res.XPATHErrorResources; 26 27 35 public class VariableStack implements Cloneable 36 { 37 40 public static final int CLEARLIMITATION= 1024; 41 42 45 public VariableStack() 46 { 47 reset(); 48 } 49 50 57 public synchronized Object clone() throws CloneNotSupportedException 58 { 59 60 VariableStack vs = (VariableStack) super.clone(); 61 62 vs._stackFrames = (XObject[]) _stackFrames.clone(); 64 vs._links = (int[]) _links.clone(); 65 66 return vs; 67 } 68 69 73 XObject[] _stackFrames = new XObject[XPathContext.RECURSIONLIMIT * 2]; 74 75 79 int _frameTop; 80 81 85 private int _currentFrameBottom; 86 87 93 int[] _links = new int[XPathContext.RECURSIONLIMIT]; 94 95 98 int _linksTop; 99 100 107 public XObject elementAt(final int i) 108 { 109 return _stackFrames[i]; 110 } 111 112 117 public int size() 118 { 119 return _frameTop; 120 } 121 122 127 public void reset() 128 { 129 130 _frameTop = 0; 131 _linksTop = 0; 132 133 _links[_linksTop++] = 0; 137 _stackFrames = new XObject[_stackFrames.length]; 138 } 139 140 145 public void setStackFrame(int sf) 146 { 147 _currentFrameBottom = sf; 148 } 149 150 157 public int getStackFrame() 158 { 159 return _currentFrameBottom; 160 } 161 162 177 public int link(final int size) 178 { 179 180 _currentFrameBottom = _frameTop; 181 _frameTop += size; 182 183 if (_frameTop >= _stackFrames.length) 184 { 185 XObject newsf[] = new XObject[_stackFrames.length + XPathContext.RECURSIONLIMIT + size]; 186 187 System.arraycopy(_stackFrames, 0, newsf, 0, _stackFrames.length); 188 189 _stackFrames = newsf; 190 } 191 192 if (_linksTop + 1 >= _links.length) 193 { 194 int newlinks[] = new int[_links.length + (CLEARLIMITATION * 2)]; 195 196 System.arraycopy(_links, 0, newlinks, 0, _links.length); 197 198 _links = newlinks; 199 } 200 201 _links[_linksTop++] = _currentFrameBottom; 202 203 return _currentFrameBottom; 204 } 205 206 210 public void unlink() 211 { 212 _frameTop = _links[--_linksTop]; 213 _currentFrameBottom = _links[_linksTop - 1]; 214 } 215 216 222 public void unlink(int currentFrame) 223 { 224 _frameTop = _links[--_linksTop]; 225 _currentFrameBottom = currentFrame; 226 } 227 228 237 public void setLocalVariable(int index, XObject val) 238 { 239 _stackFrames[index + _currentFrameBottom] = val; 240 } 241 242 252 public void setLocalVariable(int index, XObject val, int stackFrame) 253 { 254 _stackFrames[index + stackFrame] = val; 255 } 256 257 271 public XObject getLocalVariable(XPathContext xctxt, int index) 272 throws TransformerException 273 { 274 275 index += _currentFrameBottom; 276 277 XObject val = _stackFrames[index]; 278 279 if(null == val) 280 throw new TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null), 281 xctxt.getSAXLocator()); 282 284 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 286 return (_stackFrames[index] = val.execute(xctxt)); 287 288 return val; 289 } 290 291 303 public XObject getLocalVariable(int index, int frame) 304 throws TransformerException 305 { 306 307 index += frame; 308 309 XObject val = _stackFrames[index]; 310 311 return val; 312 } 313 314 328 public XObject getLocalVariable(XPathContext xctxt, int index, boolean destructiveOK) 329 throws TransformerException 330 { 331 332 index += _currentFrameBottom; 333 334 XObject val = _stackFrames[index]; 335 336 if(null == val) 337 throw new TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null), 338 xctxt.getSAXLocator()); 339 341 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 343 return (_stackFrames[index] = val.execute(xctxt)); 344 345 return destructiveOK ? val : val.getFresh(); 346 } 347 348 358 public boolean isLocalSet(int index) throws TransformerException 359 { 360 return (_stackFrames[index + _currentFrameBottom] != null); 361 } 362 363 364 private static XObject[] m_nulls = new XObject[CLEARLIMITATION]; 365 366 375 public void clearLocalSlots(int start, int len) 376 { 377 378 start += _currentFrameBottom; 379 380 System.arraycopy(m_nulls, 0, _stackFrames, start, len); 381 } 382 383 392 public void setGlobalVariable(final int index, final XObject val) 393 { 394 _stackFrames[index] = val; 395 } 396 397 411 public XObject getGlobalVariable(XPathContext xctxt, final int index) 412 throws TransformerException 413 { 414 415 XObject val = _stackFrames[index]; 416 417 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 419 return (_stackFrames[index] = val.execute(xctxt)); 420 421 return val; 422 } 423 424 438 public XObject getGlobalVariable(XPathContext xctxt, final int index, boolean destructiveOK) 439 throws TransformerException 440 { 441 442 XObject val = _stackFrames[index]; 443 444 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 446 return (_stackFrames[index] = val.execute(xctxt)); 447 448 return destructiveOK ? val : val.getFresh(); 449 } 450 451 464 public XObject getVariableOrParam( 465 XPathContext xctxt, org.apache.xml.utils.QName qname) 466 throws javax.xml.transform.TransformerException 467 { 468 469 org.apache.xml.utils.PrefixResolver prefixResolver = 470 xctxt.getNamespaceContext(); 471 472 478 if (prefixResolver instanceof org.apache.xalan.templates.ElemTemplateElement) 479 { 480 481 org.apache.xalan.templates.ElemVariable vvar; 482 483 org.apache.xalan.templates.ElemTemplateElement prev = 484 (org.apache.xalan.templates.ElemTemplateElement) prefixResolver; 485 486 if (!(prev instanceof org.apache.xalan.templates.Stylesheet)) 487 { 488 while ( !(prev.getParentNode() instanceof org.apache.xalan.templates.Stylesheet) ) 489 { 490 org.apache.xalan.templates.ElemTemplateElement savedprev = prev; 491 492 while (null != (prev = prev.getPreviousSiblingElem())) 493 { 494 if (prev instanceof org.apache.xalan.templates.ElemVariable) 495 { 496 vvar = (org.apache.xalan.templates.ElemVariable) prev; 497 498 if (vvar.getName().equals(qname)) 499 return getLocalVariable(xctxt, vvar.getIndex()); 500 } 501 } 502 prev = savedprev.getParentElem(); 503 } 504 } 505 506 vvar = prev.getStylesheetRoot().getVariableOrParamComposed(qname); 507 if (null != vvar) 508 return getGlobalVariable(xctxt, vvar.getIndex()); 509 } 510 511 throw new javax.xml.transform.TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object []{qname.toString()})); } 513 } 515 | Popular Tags |