1 package org.apache.maven.resolver.examples.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.PrintStream;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.eclipse.aether.artifact.Artifact;
28 import org.eclipse.aether.graph.Dependency;
29 import org.eclipse.aether.graph.DependencyNode;
30 import org.eclipse.aether.graph.DependencyVisitor;
31 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
32 import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
33 import org.eclipse.aether.util.graph.transformer.ConflictResolver;
34
35
36
37
38 public class ConsoleDependencyGraphDumper
39 implements DependencyVisitor
40 {
41
42 private PrintStream out;
43
44 private List<ChildInfo> childInfos = new ArrayList<ChildInfo>();
45
46 public ConsoleDependencyGraphDumper()
47 {
48 this( null );
49 }
50
51 public ConsoleDependencyGraphDumper( PrintStream out )
52 {
53 this.out = ( out != null ) ? out : System.out;
54 }
55
56 public boolean visitEnter( DependencyNode node )
57 {
58 out.println( formatIndentation() + formatNode( node ) );
59 childInfos.add( new ChildInfo( node.getChildren().size() ) );
60 return true;
61 }
62
63 private String formatIndentation()
64 {
65 StringBuilder buffer = new StringBuilder( 128 );
66 for ( Iterator<ChildInfo> it = childInfos.iterator(); it.hasNext(); )
67 {
68 buffer.append( it.next().formatIndentation( !it.hasNext() ) );
69 }
70 return buffer.toString();
71 }
72
73 private String formatNode( DependencyNode node )
74 {
75 StringBuilder buffer = new StringBuilder( 128 );
76 Artifact a = node.getArtifact();
77 Dependency d = node.getDependency();
78 buffer.append( a );
79 if ( d != null && d.getScope().length() > 0 )
80 {
81 buffer.append( " [" ).append( d.getScope() );
82 if ( d.isOptional() )
83 {
84 buffer.append( ", optional" );
85 }
86 buffer.append( "]" );
87 }
88 {
89 String premanaged = DependencyManagerUtils.getPremanagedVersion( node );
90 if ( premanaged != null && !premanaged.equals( a.getBaseVersion() ) )
91 {
92 buffer.append( " (version managed from " ).append( premanaged ).append( ")" );
93 }
94 }
95 {
96 String premanaged = DependencyManagerUtils.getPremanagedScope( node );
97 if ( premanaged != null && !premanaged.equals( d.getScope() ) )
98 {
99 buffer.append( " (scope managed from " ).append( premanaged ).append( ")" );
100 }
101 }
102 DependencyNode winner = (DependencyNode) node.getData().get( ConflictResolver.NODE_DATA_WINNER );
103 if ( winner != null && !ArtifactIdUtils.equalsId( a, winner.getArtifact() ) )
104 {
105 Artifact w = winner.getArtifact();
106 buffer.append( " (conflicts with " );
107 if ( ArtifactIdUtils.toVersionlessId( a ).equals( ArtifactIdUtils.toVersionlessId( w ) ) )
108 {
109 buffer.append( w.getVersion() );
110 }
111 else
112 {
113 buffer.append( w );
114 }
115 buffer.append( ")" );
116 }
117 return buffer.toString();
118 }
119
120 public boolean visitLeave( DependencyNode node )
121 {
122 if ( !childInfos.isEmpty() )
123 {
124 childInfos.remove( childInfos.size() - 1 );
125 }
126 if ( !childInfos.isEmpty() )
127 {
128 childInfos.get( childInfos.size() - 1 ).index++;
129 }
130 return true;
131 }
132
133 private static class ChildInfo
134 {
135
136 final int count;
137
138 int index;
139
140 public ChildInfo( int count )
141 {
142 this.count = count;
143 }
144
145 public String formatIndentation( boolean end )
146 {
147 boolean last = index + 1 >= count;
148 if ( end )
149 {
150 return last ? "\\- " : "+- ";
151 }
152 return last ? " " : "| ";
153 }
154
155 }
156
157 }