simkin
Class TreeNode

java.lang.Object
  |
  +--simkin.TreeNode

public final class TreeNode
extends java.lang.Object

This class encapsulates a single node in a tree

Each TreeNode consists of:

Each element is optional.

A label is a piece of text containing letters, numbers and underscores, but no whitespace.

The value can be any kind of text.

A treenode can be read and written to a text stream, and is represented as follows:

 Label [textual data] 
 {
   ChildLabel
 }

A TreeNode file contains an outermost set of braces:

 {
   Label [data]
 }
 


Field Summary
static char g_FileMagic
           
static int g_FileVersion
           
 
Constructor Summary
TreeNode()
          default constructor - blank label, data and an empty list of children
TreeNode(java.lang.String label)
          Constructor - passing a label
TreeNode(java.lang.String label, boolean bool)
          Constructor - passing a label and data, which is converted to String
TreeNode(java.lang.String label, int data)
          Constructor - passing a label and data, which is converted to String
TreeNode(java.lang.String label, java.lang.String data)
          Constructor - passing a label and data
 
Method Summary
 void addChild(TreeNode child)
          Adds a node to the list of this node's children
 boolean boolData()
          Returns this node's data as a boolean
 java.lang.Object clone()
          this method returns a cloned version of this node - it performs a deep copy
 void copyItems(TreeNode node)
          this function performs a deep copy from the children of another node into this one
 java.lang.String data()
          Returns this node's data
 void data(java.lang.String s)
          sets this node's data
 TreeNode findChild(java.lang.String s)
          finds the first child with the given label - returns null if none found
 TreeNode findChild(java.lang.String label, java.lang.String data)
          finds the first child with the given label and data - returns null if none found
 boolean findChildBoolData(java.lang.String s)
          returns the data of the first child with the given label as a boolean
 boolean findChildBoolData(java.lang.String label, boolean def)
          returns the data of the first child with the given label as a boolean
 java.lang.String findChildData(java.lang.String s)
          returns the data of the first child with the given label
 java.lang.String findChildData(java.lang.String label, java.lang.String def)
          returns the data of the first child with the given label
 int findChildIntData(java.lang.String s)
          returns the data of the first child with the given label as an integer
 int findChildIntData(java.lang.String s, int def)
          returns the data of the first child with the given label as a boolean
 java.util.Vector getItems()
          this function returns the list of children of this node
 TreeNodeIterator getIterator()
          This method returns an iterator that can be used to iterate over the children of this node
 int intData()
          Returns this node's data as an integer
 java.lang.String label()
          Returns this node's label
 void label(java.lang.String s)
          Changes this node's label
 void moveItemsFrom(TreeNode node)
          this function moves the children from the given node into this node
 TreeNode nthChild(int index)
          This method returns the nth child at this node
 java.lang.String nthChildData(int index)
          this method returns the data of the nth child
 int nthChildIntData(int index, int defaultData)
          this method returns the data of the nth child as an integer
 int numChildren()
          This method returns the number of children in the list at this node
static TreeNode read(java.io.InputStream in)
          This function attempts to read a TreeNode from the given stream.
 void removeChild(TreeNode node)
          This function removes the given node from the list of children
 void setChild(TreeNode node)
          replaces the first child with a matching label with the contents of the given node.
 void write(java.io.OutputStream out, boolean compiled)
          This method writes the current node out to an output stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

g_FileMagic

public static final char g_FileMagic

g_FileVersion

public static final int g_FileVersion
Constructor Detail

TreeNode

public TreeNode()
default constructor - blank label, data and an empty list of children

TreeNode

public TreeNode(java.lang.String label,
                java.lang.String data)
Constructor - passing a label and data

TreeNode

public TreeNode(java.lang.String label,
                int data)
Constructor - passing a label and data, which is converted to String

TreeNode

public TreeNode(java.lang.String label,
                boolean bool)
Constructor - passing a label and data, which is converted to String

TreeNode

public TreeNode(java.lang.String label)
Constructor - passing a label
Method Detail

addChild

public void addChild(TreeNode child)
Adds a node to the list of this node's children

label

public void label(java.lang.String s)
Changes this node's label

label

public java.lang.String label()
Returns this node's label

data

public java.lang.String data()
Returns this node's data

intData

public int intData()
Returns this node's data as an integer

data

public void data(java.lang.String s)
sets this node's data

findChild

public TreeNode findChild(java.lang.String s)
finds the first child with the given label - returns null if none found

findChild

public TreeNode findChild(java.lang.String label,
                          java.lang.String data)
finds the first child with the given label and data - returns null if none found

findChildData

public java.lang.String findChildData(java.lang.String label,
                                      java.lang.String def)
returns the data of the first child with the given label
Parameters:
label - - the label to search for
def - - a value to return if no child is found

findChildData

public java.lang.String findChildData(java.lang.String s)
returns the data of the first child with the given label
Parameters:
label - - the label to search for

findChildIntData

public int findChildIntData(java.lang.String s)
returns the data of the first child with the given label as an integer
Parameters:
label - - the label to search for

boolData

public boolean boolData()
Returns this node's data as a boolean

findChildBoolData

public boolean findChildBoolData(java.lang.String s)
returns the data of the first child with the given label as a boolean
Parameters:
label - - the label to search for

setChild

public void setChild(TreeNode node)
replaces the first child with a matching label with the contents of the given node. If no match is found, the node is added at the end of the list of children

findChildBoolData

public boolean findChildBoolData(java.lang.String label,
                                 boolean def)
returns the data of the first child with the given label as a boolean
Parameters:
label - - the label to search for
def - - the default value if the child is not found

findChildIntData

public int findChildIntData(java.lang.String s,
                            int def)
returns the data of the first child with the given label as a boolean
Parameters:
label - - the label to search for
def - - the default value if the child is not found

read

public static TreeNode read(java.io.InputStream in)
                     throws java.io.IOException
This function attempts to read a TreeNode from the given stream. If there is a parse error, an IOException is thrown

write

public void write(java.io.OutputStream out,
                  boolean compiled)
           throws java.io.IOException
This method writes the current node out to an output stream.
Parameters:
out - - the stream to write to
compiled - - if set to false, the output is textual, otherwise it is a compressed binary format

getItems

public java.util.Vector getItems()
this function returns the list of children of this node

numChildren

public int numChildren()
This method returns the number of children in the list at this node

nthChild

public TreeNode nthChild(int index)
This method returns the nth child at this node

nthChildIntData

public int nthChildIntData(int index,
                           int defaultData)
this method returns the data of the nth child as an integer
Parameters:
index - - the index of the child
defaultData - - the value to return if there are no children

nthChildData

public java.lang.String nthChildData(int index)
this method returns the data of the nth child
Parameters:
index - - the index of the child
defaultData - - the value to return if there are no children

removeChild

public void removeChild(TreeNode node)
This function removes the given node from the list of children

getIterator

public TreeNodeIterator getIterator()
This method returns an iterator that can be used to iterate over the children of this node

clone

public java.lang.Object clone()
this method returns a cloned version of this node - it performs a deep copy

moveItemsFrom

public void moveItemsFrom(TreeNode node)
this function moves the children from the given node into this node

copyItems

public void copyItems(TreeNode node)
this function performs a deep copy from the children of another node into this one