1 16 17 package com.sun.org.apache.xpath.internal; 18 19 import javax.xml.transform.TransformerException ; 20 21 import com.sun.org.apache.xalan.internal.res.XSLMessages; 22 import com.sun.org.apache.xpath.internal.objects.XObject; 23 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 24 25 33 public class VariableStack implements Cloneable 34 { 35 38 public static final int CLEARLIMITATION= 1024; 39 40 43 public VariableStack() 44 { 45 reset(); 46 } 47 48 55 public synchronized Object clone() throws CloneNotSupportedException 56 { 57 58 VariableStack vs = (VariableStack) super.clone(); 59 60 vs._stackFrames = (XObject[]) _stackFrames.clone(); 62 vs._links = (int[]) _links.clone(); 63 64 return vs; 65 } 66 67 71 XObject[] _stackFrames = new XObject[XPathContext.RECURSIONLIMIT * 2]; 72 73 77 int _frameTop; 78 79 83 private int _currentFrameBottom; 84 85 91 int[] _links = new int[XPathContext.RECURSIONLIMIT]; 92 93 96 int _linksTop; 97 98 105 public XObject elementAt(final int i) 106 { 107 return _stackFrames[i]; 108 } 109 110 115 public int size() 116 { 117 return _frameTop; 118 } 119 120 125 public void reset() 126 { 127 128 _frameTop = 0; 129 _linksTop = 0; 130 131 _links[_linksTop++] = 0; 135 _stackFrames = new XObject[_stackFrames.length]; 136 } 137 138 143 public void setStackFrame(int sf) 144 { 145 _currentFrameBottom = sf; 146 } 147 148 155 public int getStackFrame() 156 { 157 return _currentFrameBottom; 158 } 159 160 175 public int link(final int size) 176 { 177 178 _currentFrameBottom = _frameTop; 179 _frameTop += size; 180 181 if (_frameTop >= _stackFrames.length) 182 { 183 XObject newsf[] = new XObject[_stackFrames.length + XPathContext.RECURSIONLIMIT + size]; 184 185 System.arraycopy(_stackFrames, 0, newsf, 0, _stackFrames.length); 186 187 _stackFrames = newsf; 188 } 189 190 if (_linksTop + 1 >= _links.length) 191 { 192 int newlinks[] = new int[_links.length + (CLEARLIMITATION * 2)]; 193 194 System.arraycopy(_links, 0, newlinks, 0, _links.length); 195 196 _links = newlinks; 197 } 198 199 _links[_linksTop++] = _currentFrameBottom; 200 201 return _currentFrameBottom; 202 } 203 204 208 public void unlink() 209 { 210 _frameTop = _links[--_linksTop]; 211 _currentFrameBottom = _links[_linksTop - 1]; 212 } 213 214 220 public void unlink(int currentFrame) 221 { 222 _frameTop = _links[--_linksTop]; 223 _currentFrameBottom = currentFrame; 224 } 225 226 235 public void setLocalVariable(int index, XObject val) 236 { 237 _stackFrames[index + _currentFrameBottom] = val; 238 } 239 240 250 public void setLocalVariable(int index, XObject val, int stackFrame) 251 { 252 _stackFrames[index + stackFrame] = val; 253 } 254 255 269 public XObject getLocalVariable(XPathContext xctxt, int index) 270 throws TransformerException 271 { 272 273 index += _currentFrameBottom; 274 275 XObject val = _stackFrames[index]; 276 277 if(null == val) 278 throw new TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null), 279 xctxt.getSAXLocator()); 280 282 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 284 return (_stackFrames[index] = val.execute(xctxt)); 285 286 return val; 287 } 288 289 301 public XObject getLocalVariable(int index, int frame) 302 throws TransformerException 303 { 304 305 index += frame; 306 307 XObject val = _stackFrames[index]; 308 309 return val; 310 } 311 312 326 public XObject getLocalVariable(XPathContext xctxt, int index, boolean destructiveOK) 327 throws TransformerException 328 { 329 330 index += _currentFrameBottom; 331 332 XObject val = _stackFrames[index]; 333 334 if(null == val) 335 throw new TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null), 336 xctxt.getSAXLocator()); 337 339 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 341 return (_stackFrames[index] = val.execute(xctxt)); 342 343 return destructiveOK ? val : val.getFresh(); 344 } 345 346 356 public boolean isLocalSet(int index) throws TransformerException 357 { 358 return (_stackFrames[index + _currentFrameBottom] != null); 359 } 360 361 362 private static XObject[] m_nulls = new XObject[CLEARLIMITATION]; 363 364 373 public void clearLocalSlots(int start, int len) 374 { 375 376 start += _currentFrameBottom; 377 378 System.arraycopy(m_nulls, 0, _stackFrames, start, len); 379 } 380 381 390 public void setGlobalVariable(final int index, final XObject val) 391 { 392 _stackFrames[index] = val; 393 } 394 395 409 public XObject getGlobalVariable(XPathContext xctxt, final int index) 410 throws TransformerException 411 { 412 413 XObject val = _stackFrames[index]; 414 415 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 417 return (_stackFrames[index] = val.execute(xctxt)); 418 419 return val; 420 } 421 422 436 public XObject getGlobalVariable(XPathContext xctxt, final int index, boolean destructiveOK) 437 throws TransformerException 438 { 439 440 XObject val = _stackFrames[index]; 441 442 if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE) 444 return (_stackFrames[index] = val.execute(xctxt)); 445 446 return destructiveOK ? val : val.getFresh(); 447 } 448 449 462 public XObject getVariableOrParam( 463 XPathContext xctxt, com.sun.org.apache.xml.internal.utils.QName qname) 464 throws javax.xml.transform.TransformerException 465 { 466 467 474 throw new javax.xml.transform.TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object []{qname.toString()})); } 476 } 478 | Popular Tags |