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 java.lang.String g_false
           
static char g_FileMagic
           
static int g_FileVersion
           
static java.lang.String g_true
           
 
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, float data)
          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
 void clear()
          this deletes any children of this node
 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
 TreeNodeEnumerator enumerate()
          This method returns an object which can iterate over all the immediate child node of this node
 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
 float findChildFloatData(java.lang.String s)
          returns the data of the first child with the given label as a float
 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
 float floatData()
          Returns this node's data as an float
 void floatData(float i)
          sets this node's float data
 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
 void intData(boolean bool)
          sets this node's boolean data
 void intData(int i)
          sets this node's integer data
 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
 float nthChildFloatData(int index, float defaultData)
          this method returns the data of the nth child as a float
 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.
static TreeNode read(java.io.Reader in)
          This function attempts to read a TreeNode from the given reader.
 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, boolean include_tabs)
          This method writes the current node out to an output stream.
 void write(java.io.Writer out, boolean include_tabs)
          This method writes the current node out to a writer
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

g_FileMagic

public static final char g_FileMagic
See Also:
Constant Field Values

g_FileVersion

public static final int g_FileVersion
See Also:
Constant Field Values

g_true

public static final java.lang.String g_true
See Also:
Constant Field Values

g_false

public static final java.lang.String g_false
See Also:
Constant Field Values
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,
                float 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


floatData

public float floatData()
Returns this node's data as an float


data

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


intData

public void intData(int i)
sets this node's integer data


floatData

public void floatData(float i)
sets this node's float data


intData

public void intData(boolean bool)
sets this node's boolean 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


findChildIntData

public int findChildIntData(java.lang.String s)
returns the data of the first child with the given label as an integer


findChildFloatData

public float findChildFloatData(java.lang.String s)
returns the data of the first child with the given label as a float


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


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:
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

java.io.IOException

read

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

java.io.IOException

write

public void write(java.io.Writer out,
                  boolean include_tabs)
           throws java.io.IOException
This method writes the current node out to a writer

Parameters:
out - - the writer to write to
include_tabs - - include tabs in a non-compiled output to make appearance more attractive
java.io.IOException

write

public void write(java.io.OutputStream out,
                  boolean compiled,
                  boolean include_tabs)
           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
include_tabs - - include tabs in a non-compiled output to make appearance more attractive
java.io.IOException

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

nthChildFloatData

public float nthChildFloatData(int index,
                               float defaultData)
this method returns the data of the nth child as a float

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

clear

public void clear()
this deletes any children of this node


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

Overrides:
clone in class java.lang.Object

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


enumerate

public TreeNodeEnumerator enumerate()
This method returns an object which can iterate over all the immediate child node of this node