View Javadoc

1   package org.apache.maven.plugin.dependency.utils;
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  /**
23   * 
24   */
25  
26  import java.util.Iterator;
27  import java.util.Set;
28  import java.util.SortedSet;
29  import java.util.TreeSet;
30  
31  import org.apache.maven.artifact.Artifact;
32  
33  /**
34   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
35   * @version $Id: DependencyStatusSets.java 728546 2008-12-21 22:56:51Z bentmann $
36   */
37  public class DependencyStatusSets
38  {
39  
40      TreeSet resolvedDependencies = null;
41  
42      TreeSet unResolvedDependencies = null;
43  
44      TreeSet skippedDependencies = null;
45  
46      public DependencyStatusSets()
47      {
48  
49      }
50  
51      public DependencyStatusSets( Set resolved, Set unResolved, Set skipped )
52      {
53          if ( resolved != null )
54          {
55              this.resolvedDependencies = new TreeSet( resolved );
56          }
57          if ( unResolved != null )
58          {
59              this.unResolvedDependencies = new TreeSet( unResolved );
60          }
61          if ( skipped != null )
62          {
63              this.skippedDependencies = new TreeSet( skipped );
64          }
65      }
66  
67      /**
68       * @return Returns the resolvedDependencies.
69       */
70      public Set getResolvedDependencies()
71      {
72          return this.resolvedDependencies;
73      }
74  
75      /**
76       * @param resolvedDependencies
77       *            The resolvedDependencies to set.
78       */
79      public void setResolvedDependencies( Set resolvedDependencies )
80      {
81          if ( resolvedDependencies != null )
82          {
83              this.resolvedDependencies = new TreeSet( resolvedDependencies );
84          }
85          else
86          {
87              this.resolvedDependencies = null;
88          }
89      }
90  
91      /**
92       * @return Returns the skippedDependencies.
93       */
94      public Set getSkippedDependencies()
95      {
96          return this.skippedDependencies;
97      }
98  
99      /**
100      * @param skippedDependencies
101      *            The skippedDependencies to set.
102      */
103     public void setSkippedDependencies( Set skippedDependencies )
104     {
105         if ( skippedDependencies != null )
106         {
107             this.skippedDependencies = new TreeSet( skippedDependencies );
108         }
109         else
110         {
111             this.skippedDependencies = null;
112         }
113     }
114 
115     /**
116      * @return Returns the unResolvedDependencies.
117      */
118     public Set getUnResolvedDependencies()
119     {
120         return this.unResolvedDependencies;
121     }
122 
123     /**
124      * @param unResolvedDependencies
125      *            The unResolvedDependencies to set.
126      */
127     public void setUnResolvedDependencies( Set unResolvedDependencies )
128     {
129         if ( unResolvedDependencies != null )
130         {
131             this.unResolvedDependencies = new TreeSet( unResolvedDependencies );
132         }
133         else
134         {
135             this.unResolvedDependencies = null;
136         }
137     }
138 
139     public String getOutput(boolean outputAbsoluteArtifactFilename )
140     {
141         return getOutput(outputAbsoluteArtifactFilename, true );
142     }
143 
144     public String getOutput(boolean outputAbsoluteArtifactFilename, boolean outputScope )
145     {
146         StringBuffer sb = new StringBuffer();
147         sb.append( "\n" );
148         sb.append( "The following files have been resolved:\n" );
149         if ( this.resolvedDependencies == null || this.resolvedDependencies.isEmpty() )
150         {
151             sb.append( "   none\n" );
152         }
153         else
154         {
155             for ( Iterator i = resolvedDependencies.iterator(); i.hasNext(); )
156             {
157                 Artifact artifact = (Artifact) i.next();
158                 String artifactFilename = null;
159                 if ( outputAbsoluteArtifactFilename )
160                 {
161                     try
162                     {
163                         // we want to print the absolute file name here
164                         artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
165                     }
166                     catch ( NullPointerException e )
167                     {
168                         // ignore the null pointer, we'll output a null string
169                         artifactFilename = null;
170                     }
171                 }
172                 
173                 String id = outputScope ? artifact.toString() : artifact.getId();
174                 
175                 sb.append( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) +"\n");
176             }
177         }
178 
179         if ( this.skippedDependencies != null && !this.skippedDependencies.isEmpty() )
180         {
181             sb.append( "\n" );
182             sb.append( "The following files were skipped:\n" );
183             SortedSet sortedSkippedDependencies = new TreeSet();
184             sortedSkippedDependencies.addAll( this.skippedDependencies );
185             for ( Iterator i = sortedSkippedDependencies.iterator(); i.hasNext(); )
186             {
187                 sb.append( "   " + ( (Artifact) i.next() ).getId()+"\n" );
188             }
189         }
190 
191         if ( this.unResolvedDependencies != null && !this.unResolvedDependencies.isEmpty() )
192         {
193             sb.append( "\n" );
194             sb.append( "The following files have NOT been resolved:\n" );
195             SortedSet sortedUnResolvedDependencies = new TreeSet();
196             sortedUnResolvedDependencies.addAll( this.unResolvedDependencies );
197             for ( Iterator i = sortedUnResolvedDependencies.iterator(); i.hasNext(); )
198             {
199                 sb.append( "   " + ( (Artifact) i.next() ).getId()+"\n" );
200             }
201         }
202         sb.append("\n");
203 
204         return sb.toString();
205     }
206 }