View Javadoc
1   package org.apache.maven.resolver.examples.util;
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.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   * A dependency visitor that dumps the graph to the console.
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 }