View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.license;
17  
18  import org.apache.maven.artifact.Artifact;
19  import org.apache.maven.model.Resource;
20  import org.apache.maven.project.MavenProject;
21  
22  import java.io.File;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.text.MessageFormat;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  /**
30   * Mojo helper methods.
31   *
32   * @author tchemit <chemit@codelutin.com>
33   * @since 1.0
34   */
35  public class MojoHelper
36  {
37  
38      /**
39       * Add the directory as a resource of the given project.
40       *
41       * @param dir      the directory to add
42       * @param project  the project to update
43       * @param includes the includes of the resource
44       * @return {@code true} if the resources was added (not already existing)
45       */
46      public static boolean addResourceDir( File dir, MavenProject project, String... includes )
47      {
48          List<?> resources = project.getResources();
49          return addResourceDir( dir, project, resources, includes );
50      }
51  
52      /**
53       * Add the directory as a resource in the given resource list.
54       *
55       * @param dir       the directory to add
56       * @param project   the project involved
57       * @param resources the list of existing resources
58       * @param includes  includes of the new resources
59       * @return {@code true} if the resource was added (not already existing)
60       */
61      public static boolean addResourceDir( File dir, MavenProject project, List<?> resources, String... includes )
62      {
63          String newresourceDir = dir.getAbsolutePath();
64          boolean shouldAdd = true;
65          for ( Object o : resources )
66          {
67              Resource r = (Resource) o;
68              if ( !r.getDirectory().equals( newresourceDir ) )
69              {
70                  continue;
71              }
72  
73              for ( String i : includes )
74              {
75                  if ( !r.getIncludes().contains( i ) )
76                  {
77                      r.addInclude( i );
78                  }
79              }
80              shouldAdd = false;
81              break;
82          }
83          if ( shouldAdd )
84          {
85              Resource r = new Resource();
86              r.setDirectory( newresourceDir );
87              for ( String i : includes )
88              {
89                  if ( !r.getIncludes().contains( i ) )
90                  {
91                      r.addInclude( i );
92                  }
93              }
94              project.addResource( r );
95          }
96          return shouldAdd;
97      }
98  
99      public static Comparator<MavenProject> newMavenProjectComparator()
100     {
101         return new Comparator<MavenProject>()
102         {
103             public int compare( MavenProject o1, MavenProject o2 )
104             {
105 
106                 String id1 = getArtifactId( o1.getArtifact() );
107                 String id2 = getArtifactId( o2.getArtifact() );
108                 return id1.compareTo( id2 );
109             }
110         };
111 
112     }
113 
114     static final protected double[] timeFactors = { 1000000, 1000, 60, 60, 24 };
115 
116     static final protected String[] timeUnites = { "ns", "ms", "s", "m", "h", "d" };
117 
118     static public String convertTime( long value )
119     {
120         return convert( value, timeFactors, timeUnites );
121     }
122 
123     static public String convert( long value, double[] factors, String[] unites )
124     {
125         long sign = value == 0 ? 1 : value / Math.abs( value );
126         int i = 0;
127         double tmp = Math.abs( value );
128         while ( i < factors.length && i < unites.length && tmp > factors[i] )
129         {
130             tmp = tmp / factors[i++];
131         }
132 
133         tmp *= sign;
134         String result;
135         result = MessageFormat.format( "{0,number,0.###}{1}", tmp, unites[i] );
136         return result;
137     }
138 
139     /**
140      * suffix a given {@code baseUrl} with the given {@code suffix}
141      *
142      * @param baseUrl base url to use
143      * @param suffix  suffix to add
144      * @return the new url
145      * @throws IllegalArgumentException if malformed url.
146      */
147     public static URL getUrl( URL baseUrl, String suffix )
148         throws IllegalArgumentException
149     {
150         String url = baseUrl.toString() + "/" + suffix;
151         try
152         {
153             return new URL( url );
154         }
155         catch ( MalformedURLException ex )
156         {
157             throw new IllegalArgumentException( "could not obtain url " + url, ex );
158         }
159     }
160 
161     public static String getArtifactId( Artifact artifact )
162     {
163         StringBuilder sb = new StringBuilder();
164         sb.append( artifact.getGroupId() );
165         sb.append( "--" );
166         sb.append( artifact.getArtifactId() );
167         sb.append( "--" );
168         sb.append( artifact.getVersion() );
169         return sb.toString();
170     }
171 
172     public static String getArtifactName( MavenProject project )
173     {
174         StringBuilder sb = new StringBuilder();
175         if ( project.getName().startsWith( "Unnamed -" ) )
176         {
177 
178             // as in Maven 3, let's use the artifact id
179             sb.append( project.getArtifactId() );
180         }
181         else
182         {
183             sb.append( project.getName() );
184         }
185         sb.append( " (" );
186         sb.append( project.getGroupId() );
187         sb.append( ":" );
188         sb.append( project.getArtifactId() );
189         sb.append( ":" );
190         sb.append( project.getVersion() );
191         sb.append( " - " );
192         String url = project.getUrl();
193         sb.append( url == null ? "no url defined" : url );
194         sb.append( ")" );
195 
196         return sb.toString();
197     }
198 }