1 package org.apache.maven.shared.dependency.tree.traversal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.PrintWriter;
23 import java.io.Writer;
24 import java.util.List;
25
26 import org.apache.maven.shared.dependency.tree.DependencyNode;
27
28
29
30
31
32
33
34
35 public class SerializingDependencyNodeVisitor
36 implements DependencyNodeVisitor
37 {
38
39
40
41
42
43 public static class TreeTokens
44 {
45 private final String nodeIndent;
46
47 private final String lastNodeIndent;
48
49 private final String fillIndent;
50
51 private final String lastFillIndent;
52
53 public TreeTokens( String nodeIndent, String lastNodeIndent, String fillIndent, String lastFillIndent )
54 {
55 this.nodeIndent = nodeIndent;
56 this.lastNodeIndent = lastNodeIndent;
57 this.fillIndent = fillIndent;
58 this.lastFillIndent = lastFillIndent;
59 }
60
61 public String getNodeIndent( boolean last )
62 {
63 return last ? lastNodeIndent : nodeIndent;
64 }
65
66 public String getFillIndent( boolean last )
67 {
68 return last ? lastFillIndent : fillIndent;
69 }
70 }
71
72
73
74
75
76
77 public static final TreeTokens WHITESPACE_TOKENS = new TreeTokens( " ", " ", " ", " " );
78
79
80
81
82 public static final TreeTokens STANDARD_TOKENS = new TreeTokens( "+- ", "\\- ", "| ", " " );
83
84
85
86
87 public static final TreeTokens EXTENDED_TOKENS =
88 new TreeTokens( "\u00c3\u00c4 ", "\u00c0\u00c4 ", "\u00b3 ", " " );
89
90
91
92
93
94
95 private final PrintWriter writer;
96
97
98
99
100 private final TreeTokens tokens;
101
102
103
104
105 private int depth;
106
107
108
109
110
111
112
113
114
115 public SerializingDependencyNodeVisitor( Writer writer )
116 {
117 this( writer, WHITESPACE_TOKENS );
118 }
119
120
121
122
123
124
125
126
127
128
129 public SerializingDependencyNodeVisitor( Writer writer, TreeTokens tokens )
130 {
131 if ( writer instanceof PrintWriter )
132 {
133 this.writer = (PrintWriter) writer;
134 }
135 else
136 {
137 this.writer = new PrintWriter( writer, true );
138 }
139
140 this.tokens = tokens;
141
142 depth = 0;
143 }
144
145
146
147
148
149
150 public boolean visit( DependencyNode node )
151 {
152 indent( node );
153
154 writer.println( node.toNodeString() );
155
156 depth++;
157
158 return true;
159 }
160
161
162
163
164 public boolean endVisit( DependencyNode node )
165 {
166 depth--;
167
168 return true;
169 }
170
171
172
173
174
175
176
177
178
179 private void indent( DependencyNode node )
180 {
181 for ( int i = 1; i < depth; i++ )
182 {
183 writer.write( tokens.getFillIndent( isLast( node, i ) ) );
184 }
185
186 if ( depth > 0 )
187 {
188 writer.write( tokens.getNodeIndent( isLast( node ) ) );
189 }
190 }
191
192
193
194
195
196
197
198
199 private boolean isLast( DependencyNode node )
200 {
201
202
203 DependencyNode parent = node.getParent();
204
205 boolean last;
206
207 if ( parent == null )
208 {
209 last = true;
210 }
211 else
212 {
213 List<DependencyNode> siblings = parent.getChildren();
214
215 last = ( siblings.indexOf( node ) == siblings.size() - 1 );
216 }
217
218 return last;
219 }
220
221
222
223
224
225
226
227
228
229
230 private boolean isLast( DependencyNode node, int ancestorDepth )
231 {
232
233
234 int distance = depth - ancestorDepth;
235
236 while ( distance-- > 0 )
237 {
238 node = node.getParent();
239 }
240
241 return isLast( node );
242 }
243 }