1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package com.unitesk.atp.tree;
32
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.Writer;
37 import java.util.Iterator;
38 import java.util.List;
39
40 import com.unitesk.atp.dynattrs.Accessor;
41 import com.unitesk.atp.text.filters.ExceptionHandler;
42 import com.unitesk.atp.text.filters.IndentFilter;
43 import com.unitesk.atp.text.filters.WriterTextReceiver;
44
45 /***
46 * Dump tree structure for debugging
47 *
48 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
49 * @version $Id: TreePrinter.java,v 1.4 2005/11/30 10:06:18 all-x Exp $
50 */
51 public class TreePrinter
52 {
53 protected Writer writer;
54 protected IndentFilter indentFilter;
55 protected ExceptionHandler exceptionHandler;
56
57 public static TreePrinter create( String file, ExceptionHandler exceptionHandler )
58 {
59 return create( new File( file ), exceptionHandler );
60 }
61
62 public static
63 TreePrinter create( String file, ExceptionHandler exceptionHandler, int indentStep )
64 {
65 return create( new File( file ), exceptionHandler, indentStep );
66 }
67
68 public static TreePrinter create( File file, ExceptionHandler exceptionHandler )
69 {
70 return create( file, exceptionHandler, IndentFilter.DEFAULT_INDENT_STEP );
71 }
72
73 public static TreePrinter create( File file, ExceptionHandler exceptionHandler, int indentStep )
74 {
75 try
76 {
77 return create( new FileWriter( file.getCanonicalPath() )
78 , exceptionHandler
79 , indentStep
80 );
81 }
82 catch( IOException e )
83 {
84 exceptionHandler.process( e );
85 return null;
86 }
87 }
88
89 public static TreePrinter create( Writer writer
90 , ExceptionHandler exceptionHandler
91 )
92 {
93 return create( writer, exceptionHandler, IndentFilter.DEFAULT_INDENT_STEP );
94 }
95
96 public static TreePrinter create( Writer writer
97 , ExceptionHandler exceptionHandler
98 , int indentStep
99 )
100 {
101 return new TreePrinter( writer, exceptionHandler, indentStep );
102 }
103
104 /***
105 * Close output stream
106 */
107 public void close()
108 {
109 try
110 {
111 writer.close();
112 }
113 catch( IOException e )
114 {
115 exceptionHandler.process( e );
116 }
117 }
118
119 /***
120 * Dump tree
121 *
122 * @param tree Tree to be dumped
123 */
124 public void dumpTree( Tree tree )
125 {
126 txt( "tree " + tree.getFile() ); nl();
127 block_nl();
128 dumpTree( tree.getRootNode() );
129 unblock_nl();
130 }
131
132 /***
133 * Dump subtree.
134 *
135 * @param node Subtree root node.
136 */
137 public void dumpTree( Node node )
138 {
139 dumpNode( node );
140 }
141
142 protected TreePrinter( Writer writer, ExceptionHandler exceptionHandler, int indentStep )
143 {
144 this.writer = writer;
145 WriterTextReceiver out = new WriterTextReceiver( writer, exceptionHandler );
146 indentFilter = new IndentFilter( out, indentStep );
147 }
148
149 protected final void incIndent()
150 {
151 indentFilter.pushIndent( indentFilter.getIndent()
152 + indentFilter.getIndentStep()
153 );
154 }
155
156 protected final void popIndent()
157 {
158 indentFilter.popIndent();
159 }
160
161 protected final void txt( String s )
162 {
163 indentFilter.txt( s );
164 }
165
166 protected final void nl()
167 {
168 indentFilter.nl();
169 }
170
171 protected final void block()
172 {
173 txt( "{" );
174 incIndent();
175 }
176
177 protected final void unblock()
178 {
179 popIndent();
180 txt( "}" );
181 }
182
183 protected final void block_nl()
184 {
185 block();
186 nl();
187 }
188
189 protected final void unblock_nl()
190 {
191 unblock();
192 nl();
193 }
194
195 protected void dumpNode( Node node )
196 {
197 txt( node.toString() ); nl();
198 block_nl();
199
200 for( Iterator attr_name_iterator = Accessor.getAttributeNames( node ).iterator();
201 attr_name_iterator.hasNext();
202 )
203 {
204 String name = (String)attr_name_iterator.next();
205 Object value = Accessor.getAttribute( node, name );
206
207 if( value instanceof List )
208 {
209 txt( name + "[" + ((List)value).size() + "] =" ); nl();
210 block_nl();
211 for( int i = 0; i < ((List)value).size(); i++ )
212 {
213 dumpAttribute( node
214 , name + "[" + i + "]"
215 , ((List)value).get( i )
216 );
217 }
218 unblock_nl();
219 } else {
220 dumpAttribute( node, name, value );
221 }
222 }
223 unblock_nl();
224 }
225
226 protected void dumpAttribute( Node node, String name, Object value )
227 {
228 txt( name + " = " );
229
230 if( value instanceof Node && ((Node)value).getParent() == node )
231 {
232
233 dumpNode( (Node)value );
234 } else {
235 txt( "'" + value + "'" );
236 nl();
237 }
238 }
239 }