View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    *
5    * $Id: MojoHelper.java 14410 2011-08-10 20:54:51Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/MojoHelper.java $
7    * %%
8    * Copyright (C) 2010 - 2011 Codehaus
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  package org.codehaus.mojo.license;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.model.Resource;
29  import org.apache.maven.project.MavenProject;
30  
31  import java.io.File;
32  import java.net.MalformedURLException;
33  import java.net.URL;
34  import java.text.MessageFormat;
35  import java.util.Comparator;
36  import java.util.List;
37  
38  /**
39   * Mojo helper methods.
40   *
41   * @author tchemit <chemit@codelutin.com>
42   * @since 1.0
43   */
44  public class MojoHelper
45  {
46  
47      /**
48       * Add the directory as a resource of the given project.
49       *
50       * @param dir      the directory to add
51       * @param project  the project to update
52       * @param includes the includes of the resource
53       * @return {@code true} if the resources was added (not already existing)
54       */
55      public static boolean addResourceDir( File dir, MavenProject project, String... includes )
56      {
57          List<?> resources = project.getResources();
58          return addResourceDir( dir, project, resources, includes );
59      }
60  
61      /**
62       * Add the directory as a resource in the given resource list.
63       *
64       * @param dir       the directory to add
65       * @param project   the project involved
66       * @param resources the list of existing resources
67       * @param includes  includes of the new resources
68       * @return {@code true} if the resource was added (not already existing)
69       */
70      public static boolean addResourceDir( File dir, MavenProject project, List<?> resources, String... includes )
71      {
72          String newresourceDir = dir.getAbsolutePath();
73          boolean shouldAdd = true;
74          for ( Object o : resources )
75          {
76              Resource r = (Resource) o;
77              if ( !r.getDirectory().equals( newresourceDir ) )
78              {
79                  continue;
80              }
81  
82              for ( String i : includes )
83              {
84                  if ( !r.getIncludes().contains( i ) )
85                  {
86                      r.addInclude( i );
87                  }
88              }
89              shouldAdd = false;
90              break;
91          }
92          if ( shouldAdd )
93          {
94              Resource r = new Resource();
95              r.setDirectory( newresourceDir );
96              for ( String i : includes )
97              {
98                  if ( !r.getIncludes().contains( i ) )
99                  {
100                     r.addInclude( i );
101                 }
102             }
103             project.addResource( r );
104         }
105         return shouldAdd;
106     }
107 
108     public static Comparator<MavenProject> newMavenProjectComparator()
109     {
110         return new Comparator<MavenProject>()
111         {
112             public int compare( MavenProject o1, MavenProject o2 )
113             {
114 
115                 String id1 = getArtifactId( o1.getArtifact() );
116                 String id2 = getArtifactId( o2.getArtifact() );
117                 return id1.compareTo( id2 );
118             }
119         };
120 
121     }
122 
123     static final protected double[] timeFactors = { 1000000, 1000, 60, 60, 24 };
124 
125     static final protected String[] timeUnites = { "ns", "ms", "s", "m", "h", "d" };
126 
127     static public String convertTime( long value )
128     {
129         return convert( value, timeFactors, timeUnites );
130     }
131 
132     static public String convert( long value, double[] factors, String[] unites )
133     {
134         long sign = value == 0 ? 1 : value / Math.abs( value );
135         int i = 0;
136         double tmp = Math.abs( value );
137         while ( i < factors.length && i < unites.length && tmp > factors[i] )
138         {
139             tmp = tmp / factors[i++];
140         }
141 
142         tmp *= sign;
143         String result;
144         result = MessageFormat.format( "{0,number,0.###}{1}", tmp, unites[i] );
145         return result;
146     }
147 
148     /**
149      * suffix a given {@code baseUrl} with the given {@code suffix}
150      *
151      * @param baseUrl base url to use
152      * @param suffix  suffix to add
153      * @return the new url
154      * @throws IllegalArgumentException if malformed url.
155      */
156     public static URL getUrl( URL baseUrl, String suffix )
157         throws IllegalArgumentException
158     {
159         String url = baseUrl.toString() + "/" + suffix;
160         try
161         {
162             return new URL( url );
163         }
164         catch ( MalformedURLException ex )
165         {
166             throw new IllegalArgumentException( "could not obtain url " + url, ex );
167         }
168     }
169 
170     public static String getArtifactId( Artifact artifact )
171     {
172         StringBuilder sb = new StringBuilder();
173         sb.append( artifact.getGroupId() );
174         sb.append( "--" );
175         sb.append( artifact.getArtifactId() );
176         sb.append( "--" );
177         sb.append( artifact.getVersion() );
178         return sb.toString();
179     }
180 
181     public static String getArtifactName( MavenProject project )
182     {
183         StringBuilder sb = new StringBuilder();
184         if ( project.getName().startsWith( "Unnamed -" ) )
185         {
186 
187             // as in Maven 3, let's use the artifact id
188             sb.append( project.getArtifactId() );
189         }
190         else
191         {
192             sb.append( project.getName() );
193         }
194         sb.append( " (" );
195         sb.append( project.getGroupId() );
196         sb.append( ":" );
197         sb.append( project.getArtifactId() );
198         sb.append( ":" );
199         sb.append( project.getVersion() );
200         sb.append( " - " );
201         String url = project.getUrl();
202         sb.append( url == null ? "no url defined" : url );
203         sb.append( ")" );
204 
205         return sb.toString();
206     }
207 }