FileDocCategorySizeDatePackage
VariableStack.javaAPI DocJava SE 5 API13186Fri Aug 26 14:56:06 BST 2005com.sun.org.apache.xpath.internal

VariableStack

public class VariableStack extends Object implements Cloneable
Defines a class to keep track of a stack for template arguments and variables.

This has been changed from the previous incarnations of this class to be fairly low level.

xsl.usage
internal

Fields Summary
public static final int
CLEARLIMITATION
limitation for 1K
XObject[]
_stackFrames
The stack frame where all variables and params will be kept.
int
_frameTop
The top of the stack frame (_stackFrames).
private int
_currentFrameBottom
The bottom index of the current frame (relative to _stackFrames).
int[]
_links
The stack of frame positions. I call 'em links because of distant Motorola 68000 assembler memories. :-)
int
_linksTop
The top of the links stack.
private static XObject[]
m_nulls
NEEDSDOC Field m_nulls
Constructors Summary
public VariableStack()
Constructor for a variable stack.


          
   
  
    reset();
  
Methods Summary
public voidclearLocalSlots(int start, int len)
Use this to clear the variables in a section of the stack. This is used to clear the parameter section of the stack, so that default param values can tell if they've already been set. It is important to note that this function has a 1K limitation.

param
start The start position, relative to the current local stack frame.
param
len The number of slots to be cleared.


                                                                           
       
  

    start += _currentFrameBottom;

    System.arraycopy(m_nulls, 0, _stackFrames, start, len);
  
public synchronized java.lang.Objectclone()
Returns a clone of this variable stack.

return
a clone of this variable stack.
throws
CloneNotSupportedException


    VariableStack vs = (VariableStack) super.clone();

    // I *think* I can get away with a shallow clone here?
    vs._stackFrames = (XObject[]) _stackFrames.clone();
    vs._links = (int[]) _links.clone();

    return vs;
  
public com.sun.org.apache.xpath.internal.objects.XObjectelementAt(int i)
Get the element at the given index, regardless of stackframe.

param
i index from zero.
return
The item at the given index.


                           
      
  
    return _stackFrames[i];
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetGlobalVariable(com.sun.org.apache.xpath.internal.XPathContext xctxt, int index)
Get a global variable or parameter from the global stack frame.

param
xctxt The XPath context, which must be passed in order to lazy evaluate variables.
param
index Global variable index relative to the global stack frame bottom.
return
The value of the variable.
throws
TransformerException


    XObject val = _stackFrames[index];

    // Lazy execution of variables.
    if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
      return (_stackFrames[index] = val.execute(xctxt));

    return val;
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetGlobalVariable(com.sun.org.apache.xpath.internal.XPathContext xctxt, int index, boolean destructiveOK)
Get a global variable or parameter from the global stack frame.

param
xctxt The XPath context, which must be passed in order to lazy evaluate variables.
param
index Global variable index relative to the global stack frame bottom.
return
The value of the variable.
throws
TransformerException


    XObject val = _stackFrames[index];

    // Lazy execution of variables.
    if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
      return (_stackFrames[index] = val.execute(xctxt));

    return destructiveOK ? val : val.getFresh();
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetLocalVariable(com.sun.org.apache.xpath.internal.XPathContext xctxt, int index)
Get a local variable or parameter in the current stack frame.

param
xctxt The XPath context, which must be passed in order to lazy evaluate variables.
param
index Local variable index relative to the current stack frame bottom.
return
The value of the variable.
throws
TransformerException


    index += _currentFrameBottom;

    XObject val = _stackFrames[index];
    
    if(null == val)
      throw new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null),
                     xctxt.getSAXLocator());
      // "Variable accessed before it is bound!", xctxt.getSAXLocator());

    // Lazy execution of variables.
    if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
      return (_stackFrames[index] = val.execute(xctxt));

    return val;
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetLocalVariable(int index, int frame)
Get a local variable or parameter in the current stack frame.

param
index Local variable index relative to the given frame bottom. NEEDSDOC @param frame
return
The value of the variable.
throws
TransformerException


    index += frame;

    XObject val = _stackFrames[index];

    return val;
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetLocalVariable(com.sun.org.apache.xpath.internal.XPathContext xctxt, int index, boolean destructiveOK)
Get a local variable or parameter in the current stack frame.

param
xctxt The XPath context, which must be passed in order to lazy evaluate variables.
param
index Local variable index relative to the current stack frame bottom.
return
The value of the variable.
throws
TransformerException


    index += _currentFrameBottom;

    XObject val = _stackFrames[index];
    
    if(null == val)
      throw new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null),
                     xctxt.getSAXLocator());
      // "Variable accessed before it is bound!", xctxt.getSAXLocator());

    // Lazy execution of variables.
    if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
      return (_stackFrames[index] = val.execute(xctxt));

    return destructiveOK ? val : val.getFresh();
  
public intgetStackFrame()
Get the position from where the search should start, which is either the searchStart property, or the top of the stack if that value is -1.

return
The current stack frame position.

    return _currentFrameBottom;
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetVariableOrParam(com.sun.org.apache.xpath.internal.XPathContext xctxt, com.sun.org.apache.xml.internal.utils.QName qname)
Get a variable based on it's qualified name. This is for external use only.

param
xctxt The XPath context, which must be passed in order to lazy evaluate variables.
param
qname The qualified name of the variable.
return
The evaluated value of the variable.
throws
javax.xml.transform.TransformerException


    // <<<<<<<   TIGER SPECIFIC CHANGE >>>>>>>>>
    // As we are not supporting Xalan interpretive we are taking away the functionality
    // dependent on XSLT interpretive Transformer. Only way supported is to use XSLTC 
    // and the execution path needed for supporting standard XPath API defined by 
    // JAXP 1.3 . This method is overridden in XPath implementation to support 
    // standard XPath functionality with xpath package of Xalan   

    throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object[]{qname.toString()})); //"Variable not resolvable: " + qname);
  
public booleanisLocalSet(int index)
Tell if a local variable has been set or not.

param
index Local variable index relative to the current stack frame bottom.
return
true if the value at the index is not null.
throws
TransformerException

    return (_stackFrames[index + _currentFrameBottom] != null);
  
public intlink(int size)
Allocates memory (called a stackframe) on the stack; used to store local variables and parameter arguments.

I use the link/unlink concept because of distant Motorola 68000 assembler memories.

param
size The size of the stack frame allocation. This ammount should normally be the maximum number of variables that you can have allocated at one time in the new stack frame.
return
The bottom of the stack frame, from where local variable addressing should start from.


    _currentFrameBottom = _frameTop;
    _frameTop += size;

    if (_frameTop >= _stackFrames.length)
    {
      XObject newsf[] = new XObject[_stackFrames.length + XPathContext.RECURSIONLIMIT + size];

      System.arraycopy(_stackFrames, 0, newsf, 0, _stackFrames.length);

      _stackFrames = newsf;
    }

    if (_linksTop + 1 >= _links.length)
    {
      int newlinks[] = new int[_links.length + (CLEARLIMITATION * 2)];

      System.arraycopy(_links, 0, newlinks, 0, _links.length);

      _links = newlinks;
    }

    _links[_linksTop++] = _currentFrameBottom;

    return _currentFrameBottom;
  
public voidreset()
Reset the stack to a start position.

return
the total size of the execution stack.


    _frameTop = 0;
    _linksTop = 0;

    // Adding one here to the stack of frame positions will allow us always 
    // to look one under without having to check if we're at zero.
    // (As long as the caller doesn't screw up link/unlink.)
    _links[_linksTop++] = 0;
    _stackFrames = new XObject[_stackFrames.length]; 
  
public voidsetGlobalVariable(int index, com.sun.org.apache.xpath.internal.objects.XObject val)
Set a global variable or parameter in the global stack frame.

param
index Local variable index relative to the global stack frame bottom.
param
val The value of the variable that is being set.

    _stackFrames[index] = val;
  
public voidsetLocalVariable(int index, com.sun.org.apache.xpath.internal.objects.XObject val)
Set a local variable or parameter in the current stack frame.

param
index Local variable index relative to the current stack frame bottom.
param
val The value of the variable that is being set.

    _stackFrames[index + _currentFrameBottom] = val;
  
public voidsetLocalVariable(int index, com.sun.org.apache.xpath.internal.objects.XObject val, int stackFrame)
Set a local variable or parameter in the specified stack frame.

param
index Local variable index relative to the current stack frame bottom. NEEDSDOC @param stackFrame
param
val The value of the variable that is being set.

    _stackFrames[index + stackFrame] = val;
  
public voidsetStackFrame(int sf)
Set the current stack frame.

param
sf The new stack frame position.

    _currentFrameBottom = sf;
  
public intsize()
Get size of the stack.

return
the total size of the execution stack.

    return _frameTop;
  
public voidunlink(int currentFrame)
Free up the stack frame that was last allocated with {@link link(int size)}.

param
currentFrame The current frame to set to after the unlink.

    _frameTop = _links[--_linksTop];
    _currentFrameBottom = currentFrame; 
  
public voidunlink()
Free up the stack frame that was last allocated with {@link link(int size)}.

    _frameTop = _links[--_linksTop];
    _currentFrameBottom = _links[_linksTop - 1];