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

treebuilder.TreeToXML Class Reference

Collaboration diagram for treebuilder.TreeToXML:

Collaboration graph
[legend]
List of all members.

Detailed Description

TreeToXML Sep 19, 2004.

Traverse the in-memory tree and build an XML representation for the tree. Leaving asside white space, this XML should be the same as the original XML that was read to build the tree. Or at least the same relative to the supported XML elements. For example, the TreeBuilder code does not support the "documentation" elements (DOCDECL).

Author:
Ian Kaplan, www.bearcave.com, iank@bearcave.com


Public Member Functions

 TreeToXML (TreeNode root)
String toString ()

Private Member Functions

void indent ()
void openTag ()
void closeTag (boolean first)
void serializeAttribute (Attribute attr)
void serializeTag (TreeNode node)
void serializeNode (TreeNode node)
void endTag (TreeNode root)
void leavesToString (TreeNode root)
void rootToString (TreeNode root)

Private Attributes

TreeNode mRoot
StringBuffer mBuf = null
final int INDENT = 4
int mIndentLevel


Constructor & Destructor Documentation

treebuilder.TreeToXML.TreeToXML TreeNode  root  ) 
 

00176 { 00177 mRoot = root; 00178 }


Member Function Documentation

void treebuilder.TreeToXML.closeTag boolean  first  )  [private]
 

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

void treebuilder.TreeToXML.endTag TreeNode  root  )  [private]
 

00125 { 00126 if (root != null && root.getType() == TreeNodeType.TAG) { 00127 String tagName = root.toString(); 00128 mBuf.append("</"); 00129 mBuf.append( tagName ); 00130 mBuf.append('>'); 00131 } 00132 } // endTag

void treebuilder.TreeToXML.indent  )  [private]
 

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

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

00137 { 00138 if (root != null) { 00139 for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { 00140 if (! n.isLeaf()) { 00141 rootToString( n ); 00142 } 00143 else { 00144 serializeNode( n ); 00145 } 00146 } 00147 } 00148 } // leavesToString

void treebuilder.TreeToXML.openTag  )  [private]
 

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

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

00153 { 00154 boolean first = true; 00155 00156 if (root != null) { 00157 openTag(); 00158 serializeNode( root ); 00159 if (! root.isLeaf()) { 00160 // foreach child of root ... 00161 for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { 00162 if (! n.isLeaf()) { 00163 first = false; 00164 } 00165 } // for 00166 leavesToString( root ); 00167 endTag( root ); 00168 } // if root is not a leaf 00169 closeTag( first ); 00170 } 00171 } // rootToString

void treebuilder.TreeToXML.serializeAttribute Attribute  attr  )  [private]
 

00070 { 00071 String attrName = attr.toString(); 00072 mBuf.append( attrName ); 00073 mBuf.append("=\""); 00074 String attrVal = attr.getValue(); 00075 mBuf.append( attrVal ); 00076 mBuf.append('"'); 00077 } // serializeAttribute

void treebuilder.TreeToXML.serializeNode TreeNode  node  )  [private]
 

00106 { 00107 if (node != null) { 00108 TreeNodeType ty = node.getType(); 00109 if (ty == TreeNodeType.TAG) { 00110 serializeTag( node ); 00111 } 00112 else { 00113 String nodeStr = node.toString(); 00114 if (ty == TreeNodeType.COMMENT) { 00115 nodeStr = "\n<--" + nodeStr + "-->"; 00116 } 00117 mBuf.append( nodeStr ); 00118 } 00119 } 00120 } // serializeNode

void treebuilder.TreeToXML.serializeTag TreeNode  node  )  [private]
 

00082 { 00083 String tagName = node.toString(); 00084 mBuf.append("<"); 00085 mBuf.append( tagName ); 00086 AttributeList attrList = ((TagNode)node).getAttrList(); 00087 if (attrList != null) { 00088 Iterator iter = attrList.getIterator(); 00089 while (iter.hasNext()) { 00090 Attribute attr = (Attribute)iter.next(); 00091 mBuf.append(' '); 00092 serializeAttribute( attr ); 00093 } // while 00094 } 00095 if (node.isLeaf()) { 00096 mBuf.append("/>"); 00097 } 00098 else { 00099 mBuf.append('>'); 00100 } 00101 } // serializeTag

String treebuilder.TreeToXML.toString  ) 
 

00182 { 00183 mBuf = new StringBuffer(); 00184 for (TreeNode t = mRoot; t != null; t = t.getSibling()) { 00185 mIndentLevel = 0; 00186 rootToString( t ); 00187 mBuf.append('\n'); 00188 } 00189 return mBuf.toString(); 00190 } // toString


Member Data Documentation

final int treebuilder.TreeToXML.INDENT = 4 [private]
 

StringBuffer treebuilder.TreeToXML.mBuf = null [private]
 

int treebuilder.TreeToXML.mIndentLevel [private]
 

TreeNode treebuilder.TreeToXML.mRoot [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