View Javadoc

1   package org.apache.maven.shared.dependency.tree;
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.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
32  import org.apache.maven.artifact.resolver.ResolutionNode;
33  import org.apache.maven.project.MavenProject;
34  
35  /**
36   * Utilities for working with resolution nodes.
37   *
38   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
39   * @version $Id: ResolutionNodeUtils.java 1100703 2011-05-08 08:27:33Z hboutemy $
40   * @see ResolutionNode
41   */
42  public final class ResolutionNodeUtils
43  {
44      // constructors -----------------------------------------------------------
45      
46      private ResolutionNodeUtils()
47      {
48          // private constructor for utility class
49      }
50  
51      // public methods ---------------------------------------------------------
52      
53      public static List<ResolutionNode> getRootChildrenResolutionNodes( MavenProject project,
54                                                                         ArtifactResolutionResult resolutionResult )
55      {
56          Set<ResolutionNode> resolutionNodes = resolutionResult.getArtifactResolutionNodes();
57  
58          // obtain root children nodes
59          
60          Map<Artifact, ResolutionNode> rootChildrenResolutionNodesByArtifact = new HashMap<Artifact, ResolutionNode>();
61          
62          for ( ResolutionNode resolutionNode : resolutionNodes )
63          {
64              if ( resolutionNode.isChildOfRootNode() )
65              {
66                  rootChildrenResolutionNodesByArtifact.put( resolutionNode.getArtifact(), resolutionNode );
67              }
68          }
69          
70          // order root children by project dependencies
71          
72          List<ResolutionNode> rootChildrenResolutionNodes = new ArrayList<ResolutionNode>();
73          
74          for ( Iterator<Artifact> iterator = project.getDependencyArtifacts().iterator(); iterator.hasNext(); )
75          {
76              Artifact artifact = iterator.next();
77              ResolutionNode resolutionNode = rootChildrenResolutionNodesByArtifact.get( artifact );
78              
79              rootChildrenResolutionNodes.add( resolutionNode );
80          }
81  
82          return rootChildrenResolutionNodes;
83      }
84      
85      public static String toString( MavenProject project, ArtifactResolutionResult result )
86      {
87          StringBuffer buffer = new StringBuffer();
88          
89          append( buffer, project, result );
90          
91          return buffer.toString();
92      }
93      
94      public static StringBuffer append( StringBuffer buffer, MavenProject project, ArtifactResolutionResult result )
95      {
96          ResolutionNode rootNode = new ResolutionNode( project.getArtifact(), Collections.EMPTY_LIST );
97          append( buffer, rootNode, 0 );
98  
99          List<ResolutionNode> rootChildrenNodes = getRootChildrenResolutionNodes( project, result );
100         append( buffer, rootChildrenNodes.iterator(), 1 );
101         
102         return buffer;
103     }
104     
105     // private methods --------------------------------------------------------
106     
107     private static StringBuffer append( StringBuffer buffer, Iterator<ResolutionNode> nodesIterator, int depth )
108     {
109         while ( nodesIterator.hasNext() )
110         {
111             ResolutionNode node = nodesIterator.next();
112             
113             append( buffer, node, depth );
114         }
115         
116         return buffer;
117     }
118 
119     private static StringBuffer append( StringBuffer buffer, ResolutionNode node, int depth )
120     {
121         for ( int i = 0; i < depth; i++ )
122         {
123             buffer.append( "   " );
124         }
125         
126         buffer.append( node );
127         buffer.append( System.getProperty( "line.separator" ) );
128         
129         if ( node != null && node.isResolved() )
130         {
131             append( buffer, node.getChildrenIterator(), depth + 1 );
132         }
133         
134         return buffer;
135     }
136 }