|
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 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public class TreePrinter |
|
52 |
| { |
|
53 |
| protected Writer writer; |
|
54 |
| protected IndentFilter indentFilter; |
|
55 |
| protected ExceptionHandler exceptionHandler; |
|
56 |
| |
|
57 |
0
| public static TreePrinter create( String file, ExceptionHandler exceptionHandler )
|
|
58 |
| { |
|
59 |
0
| return create( new File( file ), exceptionHandler );
|
|
60 |
| } |
|
61 |
| |
|
62 |
0
| public static
|
|
63 |
| TreePrinter create( String file, ExceptionHandler exceptionHandler, int indentStep ) |
|
64 |
| { |
|
65 |
0
| return create( new File( file ), exceptionHandler, indentStep );
|
|
66 |
| } |
|
67 |
| |
|
68 |
0
| public static TreePrinter create( File file, ExceptionHandler exceptionHandler )
|
|
69 |
| { |
|
70 |
0
| return create( file, exceptionHandler, IndentFilter.DEFAULT_INDENT_STEP );
|
|
71 |
| } |
|
72 |
| |
|
73 |
0
| public static TreePrinter create( File file, ExceptionHandler exceptionHandler, int indentStep )
|
|
74 |
| { |
|
75 |
0
| try
|
|
76 |
| { |
|
77 |
0
| return create( new FileWriter( file.getCanonicalPath() )
|
|
78 |
| , exceptionHandler |
|
79 |
| , indentStep |
|
80 |
| ); |
|
81 |
| } |
|
82 |
| catch( IOException e ) |
|
83 |
| { |
|
84 |
0
| exceptionHandler.process( e );
|
|
85 |
0
| return null;
|
|
86 |
| } |
|
87 |
| } |
|
88 |
| |
|
89 |
0
| public static TreePrinter create( Writer writer
|
|
90 |
| , ExceptionHandler exceptionHandler |
|
91 |
| ) |
|
92 |
| { |
|
93 |
0
| return create( writer, exceptionHandler, IndentFilter.DEFAULT_INDENT_STEP );
|
|
94 |
| } |
|
95 |
| |
|
96 |
0
| public static TreePrinter create( Writer writer
|
|
97 |
| , ExceptionHandler exceptionHandler |
|
98 |
| , int indentStep |
|
99 |
| ) |
|
100 |
| { |
|
101 |
0
| return new TreePrinter( writer, exceptionHandler, indentStep );
|
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
0
| public void close()
|
|
108 |
| { |
|
109 |
0
| try
|
|
110 |
| { |
|
111 |
0
| writer.close();
|
|
112 |
| } |
|
113 |
| catch( IOException e ) |
|
114 |
| { |
|
115 |
0
| exceptionHandler.process( e );
|
|
116 |
| } |
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
0
| public void dumpTree( Tree tree )
|
|
125 |
| { |
|
126 |
0
| txt( "tree " + tree.getFile() ); nl();
|
|
127 |
0
| block_nl();
|
|
128 |
0
| dumpTree( tree.getRootNode() );
|
|
129 |
0
| unblock_nl();
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
0
| public void dumpTree( Node node )
|
|
138 |
| { |
|
139 |
0
| dumpNode( node );
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| protected TreePrinter( Writer writer, ExceptionHandler exceptionHandler, int indentStep )
|
|
143 |
| { |
|
144 |
0
| this.writer = writer;
|
|
145 |
0
| WriterTextReceiver out = new WriterTextReceiver( writer, exceptionHandler );
|
|
146 |
0
| indentFilter = new IndentFilter( out, indentStep );
|
|
147 |
| } |
|
148 |
| |
|
149 |
0
| protected final void incIndent()
|
|
150 |
| { |
|
151 |
0
| indentFilter.pushIndent( indentFilter.getIndent()
|
|
152 |
| + indentFilter.getIndentStep() |
|
153 |
| ); |
|
154 |
| } |
|
155 |
| |
|
156 |
0
| protected final void popIndent()
|
|
157 |
| { |
|
158 |
0
| indentFilter.popIndent();
|
|
159 |
| } |
|
160 |
| |
|
161 |
0
| protected final void txt( String s )
|
|
162 |
| { |
|
163 |
0
| indentFilter.txt( s );
|
|
164 |
| } |
|
165 |
| |
|
166 |
0
| protected final void nl()
|
|
167 |
| { |
|
168 |
0
| indentFilter.nl();
|
|
169 |
| } |
|
170 |
| |
|
171 |
0
| protected final void block()
|
|
172 |
| { |
|
173 |
0
| txt( "{" );
|
|
174 |
0
| incIndent();
|
|
175 |
| } |
|
176 |
| |
|
177 |
0
| protected final void unblock()
|
|
178 |
| { |
|
179 |
0
| popIndent();
|
|
180 |
0
| txt( "}" );
|
|
181 |
| } |
|
182 |
| |
|
183 |
0
| protected final void block_nl()
|
|
184 |
| { |
|
185 |
0
| block();
|
|
186 |
0
| nl();
|
|
187 |
| } |
|
188 |
| |
|
189 |
0
| protected final void unblock_nl()
|
|
190 |
| { |
|
191 |
0
| unblock();
|
|
192 |
0
| nl();
|
|
193 |
| } |
|
194 |
| |
|
195 |
0
| protected void dumpNode( Node node )
|
|
196 |
| { |
|
197 |
0
| txt( node.toString() ); nl();
|
|
198 |
0
| block_nl();
|
|
199 |
| |
|
200 |
0
| for( Iterator attr_name_iterator = Accessor.getAttributeNames( node ).iterator();
|
|
201 |
0
| attr_name_iterator.hasNext();
|
|
202 |
| ) |
|
203 |
| { |
|
204 |
0
| String name = (String)attr_name_iterator.next();
|
|
205 |
0
| Object value = Accessor.getAttribute( node, name );
|
|
206 |
| |
|
207 |
0
| if( value instanceof List )
|
|
208 |
| { |
|
209 |
0
| txt( name + "[" + ((List)value).size() + "] =" ); nl();
|
|
210 |
0
| block_nl();
|
|
211 |
0
| for( int i = 0; i < ((List)value).size(); i++ )
|
|
212 |
| { |
|
213 |
0
| dumpAttribute( node
|
|
214 |
| , name + "[" + i + "]" |
|
215 |
| , ((List)value).get( i ) |
|
216 |
| ); |
|
217 |
| } |
|
218 |
0
| unblock_nl();
|
|
219 |
| } else { |
|
220 |
0
| dumpAttribute( node, name, value );
|
|
221 |
| } |
|
222 |
| } |
|
223 |
0
| unblock_nl();
|
|
224 |
| } |
|
225 |
| |
|
226 |
0
| protected void dumpAttribute( Node node, String name, Object value )
|
|
227 |
| { |
|
228 |
0
| txt( name + " = " );
|
|
229 |
| |
|
230 |
0
| if( value instanceof Node && ((Node)value).getParent() == node )
|
|
231 |
| { |
|
232 |
| |
|
233 |
0
| dumpNode( (Node)value );
|
|
234 |
| } else { |
|
235 |
0
| txt( "'" + value + "'" );
|
|
236 |
0
| nl();
|
|
237 |
| } |
|
238 |
| } |
|
239 |
| } |