net.sf.saxon.tinytree
Class TinyTree

java.lang.Object
  extended by net.sf.saxon.tinytree.TinyTree

public final class TinyTree
extends java.lang.Object

A data structure to hold the contents of a tree. As the name implies, this implementation of the data model is optimized for size, and for speed of creation: it minimizes the number of Java objects used.

It can be used to represent a tree that is rooted at a document node, or one that is rooted at an element node.


Field Summary
protected  int[] alpha
           
protected  int[] attCode
           
protected  int[] attParent
           
protected  int[] attTypeCode
           
protected  Value[] attTypedValue
           
protected  java.lang.CharSequence[] attValue
           
protected  int[] beta
           
protected  LargeStringBuffer charBuffer
           
protected  FastStringBuffer commentBuffer
           
protected  short[] depth
           
protected  long documentNumber
           
protected  int[] nameCode
           
protected  int[] namespaceCode
           
protected  int[] namespaceParent
           
protected  int[] next
           
 byte[] nodeKind
           
protected  int numberOfAttributes
           
protected  int numberOfNamespaces
           
protected  int numberOfNodes
           
protected  int[] prior
           
protected  int rootIndexUsed
           
protected  int[] typeCodeArray
           
protected  Value[] typedValueArray
           
protected  boolean usesNamespaces
           
 
Constructor Summary
TinyTree()
          Create a TinyTree.
TinyTree(int nodes, int attributes, int namespaces, int characters)
          Create a tree with a specified initial size
 
Method Summary
(package private)  void addAttribute(NodeInfo root, int parent, int nameCode, int typeCode, java.lang.CharSequence attValue, int properties)
          Add an attribute node to the tree
(package private)  int addDocumentNode(TinyDocumentImpl doc)
          Add a document node to the tree.
(package private)  void addNamespace(int parent, int nscode)
          Add a namespace node to the current element
(package private)  int addNode(short kind, int depth, int alpha, int beta, int nameCode)
          Add a node to the tree
 int addTextNodeCopy(int depth, int existingNodeNr)
          Create a new text node that is a copy of an existing text node
(package private)  void appendChars(java.lang.CharSequence chars)
          Append character data to the current text node
(package private)  void condense()
          Condense the tree: release unused memory.
 void diagnosticDump()
          Produce diagnostic print of main tree arrays
static void diagnosticDump(NodeInfo node)
          Create diagnostic dump of the tree containing a particular node.
(package private)  void ensurePriorIndex()
          On demand, make an index for quick access to preceding-sibling nodes
 int[] getAlphaArray()
          Get the array holding alpha information
(package private)  AtomicValue getAtomizedValueOfUntypedNode(int nodeNr)
          Get the typed value of a node whose type is known to be untypedAtomic.
(package private)  int getAttributeAnnotation(int nr)
          Get the type annotation of an attribute node.
 int[] getAttributeNameCodeArray()
          Get the array used to hold the name codes of all attributes
(package private)  TinyAttributeImpl getAttributeNode(int nr)
          Make a (transient) attribute node from the array of attributes
 int[] getAttributeParentArray()
          Get the array used to hold the parent pointers of all attributes
 int[] getAttributeTypeCodeArray()
          Get the array used to hold the type codes of all attributes
 java.lang.CharSequence[] getAttributeValueArray()
          Get the array used to hold the name codes of all attributes
 int[] getBetaArray()
          Get the array holding beta information
 java.lang.CharSequence getCharacterBuffer()
          Get the character buffer used to hold all the text data of the document
(package private)  int getColumnNumber(int sequence)
          Get the column number for a node.
 java.lang.CharSequence getCommentBuffer()
          Get the character buffer used to hold all the comment data of the document
 Configuration getConfiguration()
          Get the configuration previously set using setConfiguration
 long getDocumentNumber()
          Get the document number (actually, the tree number)
 int getLineNumber(int sequence)
          Get the line number for a node.
 int getNameCode(int nodeNr)
          Get the nameCode for a given node, which must be a document, element, text, comment, or processing instruction node
 int[] getNameCodeArray()
          Get the array holding node name information
 NamePool getNamePool()
          Get the name pool used for the names in this document
 int[] getNamespaceCodeArray()
          Get the array used to hold the namespace codes of namespace declarations
 int[] getNamespaceParentArray()
          Get the array used to hold the parent pointers of all namespace declarations
 int[] getNextPointerArray()
          Get the array holding next-sibling pointers
 TinyNodeImpl getNode(int nr)
          Get the node at a given position in the tree
 short[] getNodeDepthArray()
          Get the array holding node depth information
 int getNodeKind(int nodeNr)
          Get the node kind of a given node, which must be a document, element, text, comment, or processing instruction node
 byte[] getNodeKindArray()
          Get the array holding node kind information
 int getNumberOfAttributes()
          Get the number of attributes in the tree
 int getNumberOfNamespaces()
          Get the number of namespace declarations in the tree
 int getNumberOfNodes()
          Get the number of nodes in the tree, excluding attributes and namespace nodes
(package private)  int getRootNode(int nodeNr)
          Get the root node for a given node
 java.lang.String getSystemId(int seq)
          Get the system id of an element in the document
 int getTypeAnnotation(int nodeNr)
          Get the type annotation of a node.
 int[] getTypeCodeArray()
          Get the array holding node type information
 Value getTypedValueOfAttribute(int nodeNr)
          Get the type value of an attribute node, given only the node number.
 Value getTypedValueOfAttribute(TinyAttributeImpl att)
          Get the type value of an attribute node.
 Value getTypedValueOfElement(int nodeNr)
          Get the type value of an element node, given only the node number
 Value getTypedValueOfElement(TinyElementImpl element)
          Get the type value of an element node.
 void indexIDElement(NodeInfo root, int nodeNr, NameChecker checker)
          Index an element of type xs:ID
 boolean isIdAttribute(int nr)
          Determine whether an attribute is an IDREF/IDREFS attribute.
 boolean isIdElement(int nr)
          Ask whether an element is an ID element.
 boolean isIdrefAttribute(int nr)
          Determine whether an attribute is an IDREF/IDREFS attribute.
 boolean isIdrefElement(int nr)
          Ask whether an element is an IDREF/IDREFS element.
 boolean isNilled(int nodeNr)
          Ask whether a given node is nilled
 void setConfiguration(Configuration config)
          Set the Configuration that contains this document
(package private)  void setElementAnnotation(int nodeNr, int typeCode)
          Set the type annotation of an element node
(package private)  void setLineNumber(int sequence, int line, int column)
          Set the line number for a node.
 void setLineNumbering()
          Set line numbering on
(package private)  void setSystemId(int seq, java.lang.String uri)
          Set the system id of an element in the document.
 void showSize()
          Output a statistical summary to System.err
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

documentNumber

protected long documentNumber

charBuffer

protected LargeStringBuffer charBuffer

commentBuffer

protected FastStringBuffer commentBuffer

numberOfNodes

protected int numberOfNodes

nodeKind

public byte[] nodeKind

depth

protected short[] depth

next

protected int[] next

alpha

protected int[] alpha

beta

protected int[] beta

nameCode

protected int[] nameCode

prior

protected int[] prior

typeCodeArray

protected int[] typeCodeArray

typedValueArray

protected Value[] typedValueArray

numberOfAttributes

protected int numberOfAttributes

attParent

protected int[] attParent

attCode

protected int[] attCode

attValue

protected java.lang.CharSequence[] attValue

attTypedValue

protected Value[] attTypedValue

attTypeCode

protected int[] attTypeCode

numberOfNamespaces

protected int numberOfNamespaces

namespaceParent

protected int[] namespaceParent

namespaceCode

protected int[] namespaceCode

rootIndexUsed

protected int rootIndexUsed

usesNamespaces

protected boolean usesNamespaces
Constructor Detail

TinyTree

public TinyTree()
Create a TinyTree. The initial size is based on the average size of trees previously built in this session


TinyTree

public TinyTree(int nodes,
                int attributes,
                int namespaces,
                int characters)
Create a tree with a specified initial size

Parameters:
nodes - the expected number of (non attribute or namespace) nodes
attributes - the expected number of attributes
namespaces - the expected number of namespace declarations
characters - the expected number of characters in the document (in text nodes)
Method Detail

setConfiguration

public void setConfiguration(Configuration config)
Set the Configuration that contains this document

Parameters:
config - the Saxon configuration

getConfiguration

public Configuration getConfiguration()
Get the configuration previously set using setConfiguration

Returns:
the Saxon configuration

getNamePool

public NamePool getNamePool()
Get the name pool used for the names in this document

Returns:
the name pool

addDocumentNode

int addDocumentNode(TinyDocumentImpl doc)
Add a document node to the tree. The data structure can contain any number of document (or element) nodes as top-level nodes. The document node is retained in the documentList list, and its offset in that list is held in the alpha array for the relevant node number.

Parameters:
doc - the document node to be added
Returns:
the number of the node that was added

addNode

int addNode(short kind,
            int depth,
            int alpha,
            int beta,
            int nameCode)
Add a node to the tree

Parameters:
kind - The kind of the node. This must be a document, element, text, comment, or processing-instruction node (not an attribute or namespace)
depth - The depth in the tree
alpha - Pointer to attributes or text
beta - Pointer to namespaces or text
nameCode - The name of the node
Returns:
the node number of the node that was added

appendChars

void appendChars(java.lang.CharSequence chars)
Append character data to the current text node

Parameters:
chars - the character data to be appended

addTextNodeCopy

public int addTextNodeCopy(int depth,
                           int existingNodeNr)
Create a new text node that is a copy of an existing text node

Parameters:
depth - the depth of the new node
existingNodeNr - the node to be copied
Returns:
the node number of the new node

condense

void condense()
Condense the tree: release unused memory. This is done after the full tree has been built. The method makes a pragmatic judgement as to whether it is worth reclaiming space; this is only done when the constructed tree is very small compared with the space allocated.


setElementAnnotation

void setElementAnnotation(int nodeNr,
                          int typeCode)
Set the type annotation of an element node

Parameters:
nodeNr - the node whose type annotation is to be set
typeCode - the type annotation

getTypeAnnotation

public int getTypeAnnotation(int nodeNr)
Get the type annotation of a node. Applies only to document, element, text, processing instruction, and comment nodes.

Parameters:
nodeNr - the node whose type annotation is required
Returns:
the fingerprint of the type annotation for elements and attributes, otherwise undefined.

getTypedValueOfElement

public Value getTypedValueOfElement(TinyElementImpl element)
                             throws XPathException
Get the type value of an element node.

Parameters:
element - the element node
Returns:
the typed value of the node
Throws:
XPathException

getTypedValueOfElement

public Value getTypedValueOfElement(int nodeNr)
                             throws XPathException
Get the type value of an element node, given only the node number

Parameters:
nodeNr - the node number of the element node
Returns:
the typed value of the node
Throws:
XPathException

getTypedValueOfAttribute

public Value getTypedValueOfAttribute(TinyAttributeImpl att)
                               throws XPathException
Get the type value of an attribute node.

Parameters:
att - the attribute node
Returns:
the typed value of the node
Throws:
XPathException

getTypedValueOfAttribute

public Value getTypedValueOfAttribute(int nodeNr)
                               throws XPathException
Get the type value of an attribute node, given only the node number. This method avoids materializing the attribute node if possible

Parameters:
nodeNr - the node number of the attribute node
Returns:
the typed value of the node
Throws:
XPathException

getNodeKind

public int getNodeKind(int nodeNr)
Get the node kind of a given node, which must be a document, element, text, comment, or processing instruction node

Parameters:
nodeNr - the node number
Returns:
the node kind

getNameCode

public int getNameCode(int nodeNr)
Get the nameCode for a given node, which must be a document, element, text, comment, or processing instruction node

Parameters:
nodeNr - the node number
Returns:
the name code

ensurePriorIndex

void ensurePriorIndex()
On demand, make an index for quick access to preceding-sibling nodes


addAttribute

void addAttribute(NodeInfo root,
                  int parent,
                  int nameCode,
                  int typeCode,
                  java.lang.CharSequence attValue,
                  int properties)
Add an attribute node to the tree

Parameters:
root - the root of the tree to contain the attribute
parent - the parent element of the new attribute
nameCode - the name code of the attribute
typeCode - the type annotation of the attribute
attValue - the string value of the attribute
properties - any special properties of the attribute (bit-significant)

indexIDElement

public void indexIDElement(NodeInfo root,
                           int nodeNr,
                           NameChecker checker)
Index an element of type xs:ID

Parameters:
root - the root node of the document
nodeNr - the element of type xs:ID
checker - checks names against XML 1.0 or XML 1.1 rules

addNamespace

void addNamespace(int parent,
                  int nscode)
Add a namespace node to the current element

Parameters:
parent - the node number of the element
nscode - namespace code identifying the prefix and uri

getNode

public final TinyNodeImpl getNode(int nr)
Get the node at a given position in the tree

Parameters:
nr - the node number
Returns:
the node at the given position

getAtomizedValueOfUntypedNode

AtomicValue getAtomizedValueOfUntypedNode(int nodeNr)
Get the typed value of a node whose type is known to be untypedAtomic. The node must be a document, element, text, comment, or processing-instruction node, and it must have no type annotation. This method gets the typed value of a numbered node without actually instantiating the NodeInfo object, as a performance optimization.

Parameters:
nodeNr - the node whose typed value is required
Returns:
the atomic value of the node

getAttributeNode

TinyAttributeImpl getAttributeNode(int nr)
Make a (transient) attribute node from the array of attributes

Parameters:
nr - the node number of the attribute
Returns:
an attribute node

getAttributeAnnotation

int getAttributeAnnotation(int nr)
Get the type annotation of an attribute node. The bit NodeInfo.IS_DTD_TYPE (1<<30) will be set in the case of an attribute node if the type annotation is one of ID, IDREF, or IDREFS and this is derived from DTD rather than schema validation.

Parameters:
nr - the node number of the attribute
Returns:
Type.UNTYPED_ATOMIC if there is no annotation

isIdAttribute

public boolean isIdAttribute(int nr)
Determine whether an attribute is an IDREF/IDREFS attribute. (The represents the is-idref property in the data model)

Parameters:
nr - the node number of the attribute
Returns:
true if this is an IDREF/IDREFS attribute

isIdrefAttribute

public boolean isIdrefAttribute(int nr)
Determine whether an attribute is an IDREF/IDREFS attribute. (The represents the is-idref property in the data model)

Parameters:
nr - the node number of the attribute
Returns:
true if this is an IDREF/IDREFS attribute

isIdElement

public boolean isIdElement(int nr)
Ask whether an element is an ID element. (The represents the is-id property in the data model)

Parameters:
nr - the element node whose is-idref property is required
Returns:
true if the node has the is-idref property

isIdrefElement

public boolean isIdrefElement(int nr)
Ask whether an element is an IDREF/IDREFS element. (The represents the is-idref property in the data model)

Parameters:
nr - the element node whose is-idref property is required
Returns:
true if the node has the is-idref property

setSystemId

void setSystemId(int seq,
                 java.lang.String uri)
Set the system id of an element in the document. This identifies the external entity containing the node - this is not necessarily the same as the base URI.

Parameters:
seq - the node number
uri - the system ID

getSystemId

public java.lang.String getSystemId(int seq)
Get the system id of an element in the document

Parameters:
seq - the node number of the element node
Returns:
the system id (base URI) of the element

getRootNode

int getRootNode(int nodeNr)
Get the root node for a given node

Parameters:
nodeNr - the node number of the given node
Returns:
the node number of the root of the tree containing the given node

setLineNumbering

public void setLineNumbering()
Set line numbering on


setLineNumber

void setLineNumber(int sequence,
                   int line,
                   int column)
Set the line number for a node. Ignored if line numbering is off.

Parameters:
sequence - the node number
line - the line number to be set for the node
column - the column number for the node

getLineNumber

public int getLineNumber(int sequence)
Get the line number for a node.

Parameters:
sequence - the node number
Returns:
the line number of the node. Return -1 if line numbering is off.

getColumnNumber

int getColumnNumber(int sequence)
Get the column number for a node.

Parameters:
sequence - the node number
Returns:
the line number of the node. Return -1 if line numbering is off.

getDocumentNumber

public long getDocumentNumber()
Get the document number (actually, the tree number)

Returns:
the unique number of this TinyTree structure

isNilled

public boolean isNilled(int nodeNr)
Ask whether a given node is nilled

Parameters:
nodeNr - the node in question (which must be an element node)
Returns:
true if the node has the nilled property

diagnosticDump

public void diagnosticDump()
Produce diagnostic print of main tree arrays


diagnosticDump

public static void diagnosticDump(NodeInfo node)
Create diagnostic dump of the tree containing a particular node. Designed to be called as an extension function for diagnostics.

Parameters:
node - the node in question

showSize

public void showSize()
Output a statistical summary to System.err


getNumberOfNodes

public int getNumberOfNodes()
Get the number of nodes in the tree, excluding attributes and namespace nodes

Returns:
the number of nodes.

getNumberOfAttributes

public int getNumberOfAttributes()
Get the number of attributes in the tree

Returns:
the number of attributes

getNumberOfNamespaces

public int getNumberOfNamespaces()
Get the number of namespace declarations in the tree

Returns:
the number of namespace declarations

getNodeKindArray

public byte[] getNodeKindArray()
Get the array holding node kind information

Returns:
an array of bytes, byte N is the node kind of node number N

getNodeDepthArray

public short[] getNodeDepthArray()
Get the array holding node depth information

Returns:
an array of shorts, byte N is the node depth of node number N

getNameCodeArray

public int[] getNameCodeArray()
Get the array holding node name information

Returns:
an array of integers, integer N is the name code of node number N

getTypeCodeArray

public int[] getTypeCodeArray()
Get the array holding node type information

Returns:
an array of integers, integer N is the type code of node number N

getNextPointerArray

public int[] getNextPointerArray()
Get the array holding next-sibling pointers

Returns:
an array of integers, integer N is the next-sibling pointer for node number N

getAlphaArray

public int[] getAlphaArray()
Get the array holding alpha information

Returns:
an array of integers, whose meaning depends on the node kind. For elements it is a pointer to the first attribute, for text, comment, and processing instruction nodes it is a pointer to the content

getBetaArray

public int[] getBetaArray()
Get the array holding beta information

Returns:
an array of integers, whose meaning depends on the node kind. For elements it is a pointer to the first namespace declaration

getCharacterBuffer

public java.lang.CharSequence getCharacterBuffer()
Get the character buffer used to hold all the text data of the document

Returns:
the character buffer

getCommentBuffer

public java.lang.CharSequence getCommentBuffer()
Get the character buffer used to hold all the comment data of the document

Returns:
the character buffer used for comments

getAttributeNameCodeArray

public int[] getAttributeNameCodeArray()
Get the array used to hold the name codes of all attributes

Returns:
an integer array; the Nth integer holds the attribute name code of attribute N

getAttributeTypeCodeArray

public int[] getAttributeTypeCodeArray()
Get the array used to hold the type codes of all attributes

Returns:
an integer array; the Nth integer holds the attribute type code of attribute N

getAttributeParentArray

public int[] getAttributeParentArray()
Get the array used to hold the parent pointers of all attributes

Returns:
an integer array; the Nth integer holds the pointer to the parent element of attribute N

getAttributeValueArray

public java.lang.CharSequence[] getAttributeValueArray()
Get the array used to hold the name codes of all attributes

Returns:
an array of strings; the Nth string holds the string value of attribute N

getNamespaceCodeArray

public int[] getNamespaceCodeArray()
Get the array used to hold the namespace codes of namespace declarations

Returns:
an array of integer namespace codes

getNamespaceParentArray

public int[] getNamespaceParentArray()
Get the array used to hold the parent pointers of all namespace declarations

Returns:
an integer array; the Nth integer holds the pointer to the parent element of namespace N