Main Page | Packages | Class Hierarchy | Class List | File List | Class Members

treebuilder.TreeToString Class Reference

Collaboration diagram for treebuilder.TreeToString:

Collaboration graph
[legend]
List of all members.

Detailed Description

TreeToString.

A class to support representation of the TreeNode tree as a String. Note that there is a similar class, TreeToXML which will serialize the in-memory tree back to XML.

For those of you who speak the "gang of four" Design Patterns languauge, this class follows a "visitor" pattern.


Public Member Functions

 TreeToString (TreeNode root)
String toString ()

Private Member Functions

void indent ()
void openAngle ()
void closeAngle (boolean first)
void nodeToString (TreeNode node)
void leavesToString (TreeNode root)
void rootToString (TreeNode root)

Private Attributes

final char startChar = '<'
final char endChar = '>'
TreeNode mRoot
StringBuffer mBuf = null
final int INDENT = 2
int mIndentLevel


Constructor & Destructor Documentation

treebuilder.TreeToString.TreeToString TreeNode  root  ) 
 

00128 { 00129 mRoot = root; 00130 }


Member Function Documentation

void treebuilder.TreeToString.closeAngle boolean  first  )  [private]
 

00056 { 00057 assert mIndentLevel > 0; 00058 00059 mIndentLevel -= INDENT; 00060 if (! first ) { 00061 mBuf.append('\n'); 00062 indent(); 00063 } 00064 mBuf.append( endChar ); 00065 mBuf.append(' '); 00066 } // closeAngle

void treebuilder.TreeToString.indent  )  [private]
 

00038 { 00039 for (int i = 0; i < mIndentLevel; i++) { 00040 mBuf.append(' '); 00041 } 00042 } // indent

void treebuilder.TreeToString.leavesToString TreeNode  root  )  [private]
 

00091 { 00092 if (root != null) { 00093 for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { 00094 if (! n.isLeaf()) { 00095 rootToString( n ); 00096 } 00097 else { 00098 nodeToString( n ); 00099 } 00100 } 00101 } 00102 } // leavesToString

void treebuilder.TreeToString.nodeToString TreeNode  node  )  [private]
 

00070 { 00071 TreeNodeType ty = node.getType(); 00072 String nodeStr = node.toString(); 00073 if (ty == TreeNodeType.TEXT) { 00074 nodeStr = nodeStr.trim(); 00075 } 00076 else if (ty == TreeNodeType.COMMENT) { 00077 nodeStr = "\n<--" + nodeStr + "-->"; 00078 } 00079 if (nodeStr.length() > 0) { 00080 // String tyName = ty.toString(); 00081 // mBuf.append( tyName ); 00082 // mBuf.append(':'); 00083 mBuf.append( nodeStr ); 00084 mBuf.append(' '); 00085 } 00086 } // nodeToString

void treebuilder.TreeToString.openAngle  )  [private]
 

00045 { 00046 if (mIndentLevel > 0) { 00047 mBuf.append('\n'); 00048 } 00049 indent(); 00050 mBuf.append( startChar ); 00051 mIndentLevel += INDENT; 00052 } // openAngle

void treebuilder.TreeToString.rootToString TreeNode  root  )  [private]
 

00106 { 00107 boolean first = true; 00108 00109 if (root != null) { 00110 openAngle(); 00111 nodeToString(root ); 00112 if (! root.isLeaf()) { 00113 // foreach child of root ... 00114 for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { 00115 if (! n.isLeaf()) { 00116 first = false; 00117 } 00118 } // for 00119 leavesToString( root ); 00120 } // if root is not a leaf 00121 closeAngle( first ); 00122 } 00123 } // rootToString

String treebuilder.TreeToString.toString  ) 
 

00134 { 00135 mBuf = new StringBuffer(); 00136 for (TreeNode t = mRoot; t != null; t = t.getSibling()) { 00137 mIndentLevel = 0; 00138 rootToString( t ); 00139 mBuf.append('\n'); 00140 } 00141 return mBuf.toString(); 00142 } // toString


Member Data Documentation

final char treebuilder.TreeToString.endChar = '>' [private]
 

final int treebuilder.TreeToString.INDENT = 2 [private]
 

StringBuffer treebuilder.TreeToString.mBuf = null [private]
 

int treebuilder.TreeToString.mIndentLevel [private]
 

TreeNode treebuilder.TreeToString.mRoot [private]
 

final char treebuilder.TreeToString.startChar = '<' [private]
 


The documentation for this class was generated from the following file:
Generated on Tue Sep 21 22:08:44 2004 for Building an in-memory tree using the XmlPullParser by doxygen 1.3.8