1 package org.apache.maven.shared.dependency.graph.traversal;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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.graph.DependencyNode;
27
28 /**
29 * A dependency node visitor that serializes visited nodes to a writer.
30 *
31 * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
32 */
33 public class SerializingDependencyNodeVisitor
34 implements DependencyNodeVisitor
35 {
36 // classes ----------------------------------------------------------------
37
38 /**
39 * Provides tokens to use when serializing the dependency graph.
40 */
41 public static class GraphTokens
42 {
43 private final String nodeIndent;
44
45 private final String lastNodeIndent;
46
47 private final String fillIndent;
48
49 private final String lastFillIndent;
50
51 public GraphTokens( String nodeIndent, String lastNodeIndent, String fillIndent, String lastFillIndent )
52 {
53 this.nodeIndent = nodeIndent;
54 this.lastNodeIndent = lastNodeIndent;
55 this.fillIndent = fillIndent;
56 this.lastFillIndent = lastFillIndent;
57 }
58
59 public String getNodeIndent( boolean last )
60 {
61 return last ? lastNodeIndent : nodeIndent;
62 }
63
64 public String getFillIndent( boolean last )
65 {
66 return last ? lastFillIndent : fillIndent;
67 }
68 }
69
70 // constants --------------------------------------------------------------
71
72 /**
73 * Whitespace tokens to use when outputing the dependency graph.
74 */
75 public static final GraphTokens WHITESPACE_TOKENS = new GraphTokens( " ", " ", " ", " " );
76
77 /**
78 * The standard ASCII tokens to use when outputing the dependency graph.
79 */
80 public static final GraphTokens STANDARD_TOKENS = new GraphTokens( "+- ", "\\- ", "| ", " " );
81
82 /**
83 * The extended ASCII tokens to use when outputing the dependency graph.
84 */
85 public static final GraphTokens EXTENDED_TOKENS = new GraphTokens( "\u00c3\u00c4 ", "\u00c0\u00c4 ", "\u00b3 ",
86 " " );
87
88 // fields -----------------------------------------------------------------
89
90 /**
91 * The writer to serialize to.
92 */
93 private final PrintWriter writer;
94
95 /**
96 * The tokens to use when serializing the dependency graph.
97 */
98 private final GraphTokens tokens;
99
100 /**
101 * The depth of the currently visited dependency node.
102 */
103 private int depth;
104
105 // constructors -----------------------------------------------------------
106
107 /**
108 * Creates a dependency node visitor that serializes visited nodes to the specified writer using whitespace tokens.
109 *
110 * @param writer the writer to serialize to
111 */
112 public SerializingDependencyNodeVisitor( Writer writer )
113 {
114 this( writer, WHITESPACE_TOKENS );
115 }
116
117 /**
118 * Creates a dependency node visitor that serializes visited nodes to the specified writer using the specified
119 * tokens.
120 *
121 * @param writer the writer to serialize to
122 * @param tokens the tokens to use when serializing the dependency graph
123 */
124 public SerializingDependencyNodeVisitor( Writer writer, GraphTokens tokens )
125 {
126 if ( writer instanceof PrintWriter )
127 {
128 this.writer = (PrintWriter) writer;
129 }
130 else
131 {
132 this.writer = new PrintWriter( writer, true );
133 }
134
135 this.tokens = tokens;
136
137 depth = 0;
138 }
139
140 // DependencyNodeVisitor methods ------------------------------------------
141
142 /**
143 * {@inheritDoc}
144 */
145 public boolean visit( DependencyNode node )
146 {
147 indent( node );
148
149 writer.println( node.toNodeString() );
150
151 depth++;
152
153 return true;
154 }
155
156 /**
157 * {@inheritDoc}
158 */
159 public boolean endVisit( DependencyNode node )
160 {
161 depth--;
162
163 return true;
164 }
165
166 // private methods --------------------------------------------------------
167
168 /**
169 * Writes the necessary tokens to indent the specified dependency node to this visitor's writer.
170 *
171 * @param node the dependency node to indent
172 */
173 private void indent( DependencyNode node )
174 {
175 for ( int i = 1; i < depth; i++ )
176 {
177 writer.write( tokens.getFillIndent( isLast( node, i ) ) );
178 }
179
180 if ( depth > 0 )
181 {
182 writer.write( tokens.getNodeIndent( isLast( node ) ) );
183 }
184 }
185
186 /**
187 * Gets whether the specified dependency node is the last of its siblings.
188 *
189 * @param node the dependency node to check
190 * @return <code>true</code> if the specified dependency node is the last of its last siblings
191 */
192 private boolean isLast( DependencyNode node )
193 {
194 // TODO: remove node argument and calculate from visitor calls only
195
196 DependencyNode parent = node.getParent();
197
198 boolean last;
199
200 if ( parent == null )
201 {
202 last = true;
203 }
204 else
205 {
206 List<DependencyNode> siblings = parent.getChildren();
207
208 last = ( siblings.indexOf( node ) == siblings.size() - 1 );
209 }
210
211 return last;
212 }
213
214 /**
215 * Gets whether the specified dependency node ancestor is the last of its siblings.
216 *
217 * @param node the dependency node whose ancestor to check
218 * @param ancestorDepth the depth of the ancestor of the specified dependency node to check
219 * @return <code>true</code> if the specified dependency node ancestor is the last of its siblings
220 */
221 private boolean isLast( DependencyNode node, int ancestorDepth )
222 {
223 // TODO: remove node argument and calculate from visitor calls only
224
225 int distance = depth - ancestorDepth;
226
227 while ( distance-- > 0 )
228 {
229 node = node.getParent();
230 }
231
232 return isLast( node );
233 }
234 }