View Javadoc
1   package com.simpligility.maven.plugins.androidndk.common;
2   
3   import org.apache.maven.artifact.Artifact;
4   import org.apache.maven.shared.dependency.graph.DependencyNode;
5   import org.codehaus.plexus.logging.Logger;
6   
7   import java.util.Collections;
8   import java.util.HashSet;
9   import java.util.Set;
10  
11  /**
12   * Filters a graph of Artifacts and transforms it into a Set.
13   */
14  final class DependencyCollector
15  {
16      private final Logger log;
17      private final Set<Artifact> dependencies = new HashSet<Artifact>();
18      private final Artifact target;
19  
20      /**
21       * @param logger    Logger on which to output and messages.
22       * @param target    Artifact from which we will start collecting.
23       */
24      DependencyCollector( Logger logger, Artifact target )
25      {
26          this.log = logger;
27          this.target = target;
28      }
29  
30      /**
31       * Visits all nodes from the given node and collects dependencies.
32       *
33       * @param node          DependencyNode from which to search.
34       * @param collecting    Whether we are currently collecting artifacts.
35       */
36      public void visit( DependencyNode node, boolean collecting )
37      {
38          if ( collecting )
39          {
40              dependencies.add( node.getArtifact() );
41          }
42  
43          if ( matchesTarget( node.getArtifact() ) )
44          {
45              collecting = true;
46              log.debug( "Found target. Collecting dependencies after " + node.getArtifact() );
47          }
48  
49          for ( final DependencyNode child : node.getChildren() )
50          {
51              visit( child, collecting );
52          }
53      }
54  
55      public Set<Artifact> getDependencies()
56      {
57          return Collections.unmodifiableSet( dependencies );
58      }
59  
60      private boolean matchesTarget( Artifact found )
61      {
62          return found.getGroupId().equals( target.getGroupId() )
63                  && found.getArtifactId().equals( target.getArtifactId() )
64                  && found.getVersion().equals( target.getVersion() )
65                  && found.getType().equals( target.getType() )
66                  && classifierMatch( found.getClassifier(), target.getClassifier() )
67                  ;
68      }
69  
70      private boolean classifierMatch( String classifierA, String classifierB )
71      {
72          final boolean hasClassifierA = !isNullOrEmpty( classifierA );
73          final boolean hasClassifierB = !isNullOrEmpty( classifierB );
74          if ( !hasClassifierA && !hasClassifierB )
75          {
76              return true;
77          }
78          else if ( hasClassifierA && hasClassifierB )
79          {
80              return classifierA.equals( classifierB );
81          }
82          return false;
83      }
84  
85      private boolean isNullOrEmpty( String string )
86      {
87          return ( string == null ) || string.isEmpty();
88      }
89  }