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 java.io.File;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.text.MessageFormat;
22  import java.util.Comparator;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.model.Resource;
27  import org.apache.maven.project.MavenProject;
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       * Add the directory as a resource of the given project.
39       *
40       * @param dir
41       *            the directory to add
42       * @param project
43       *            the project to update
44       * @param includes
45       *            the includes of the resource
46       * @return {@code true} if the resources was added (not already existing)
47       */
48      public static boolean addResourceDir(File dir, MavenProject project, String... includes) {
49          List<?> resources = project.getResources();
50          return addResourceDir(dir, project, resources, includes);
51      }
52  
53      /**
54       * Add the directory as a resource in the given resource list.
55       *
56       * @param dir
57       *            the directory to add
58       * @param project
59       *            the project involved
60       * @param resources
61       *            the list of existing resources
62       * @param includes
63       *            includes of the new resources
64       * @return {@code true} if the resource was added (not already existing)
65       */
66      public static boolean addResourceDir(File dir, MavenProject project, List<?> resources, String... includes) {
67          String newresourceDir = dir.getAbsolutePath();
68          boolean shouldAdd = true;
69          for (Object o : resources) {
70              Resource r = (Resource) o;
71              if (!r.getDirectory().equals(newresourceDir)) {
72                  continue;
73              }
74  
75              for (String i : includes) {
76                  if (!r.getIncludes().contains(i)) {
77                      r.addInclude(i);
78                  }
79              }
80              shouldAdd = false;
81              break;
82          }
83          if (shouldAdd) {
84              Resource r = new Resource();
85              r.setDirectory(newresourceDir);
86              for (String i : includes) {
87                  if (!r.getIncludes().contains(i)) {
88                      r.addInclude(i);
89                  }
90              }
91              project.addResource(r);
92          }
93          return shouldAdd;
94      }
95  
96      public static Comparator<MavenProject> newMavenProjectComparator() {
97          return new Comparator<MavenProject>() {
98              @Override
99              public int compare(MavenProject o1, MavenProject o2) {
100 
101                 String id1 = getArtifactId(o1.getArtifact());
102                 String id2 = getArtifactId(o2.getArtifact());
103                 return id1.compareTo(id2);
104             }
105         };
106 
107     }
108 
109     static final protected double[] timeFactors = { 1000000, 1000, 60, 60, 24 };
110 
111     static final protected String[] timeUnites = { "ns", "ms", "s", "m", "h", "d" };
112 
113     static public String convertTime(long value) {
114         return convert(value, timeFactors, timeUnites);
115     }
116 
117     static public String convert(long value, double[] factors, String[] unites) {
118         long sign = value == 0 ? 1 : value / Math.abs(value);
119         int i = 0;
120         double tmp = Math.abs(value);
121         while (i < factors.length && i < unites.length && tmp > factors[i]) {
122             tmp = tmp / factors[i++];
123         }
124 
125         tmp *= sign;
126         String result;
127         result = MessageFormat.format("{0,number,0.###}{1}", tmp, unites[i]);
128         return result;
129     }
130 
131     /**
132      * suffix a given {@code baseUrl} with the given {@code suffix}
133      *
134      * @param baseUrl
135      *            base url to use
136      * @param suffix
137      *            suffix to add
138      * @return the new url
139      * @throws IllegalArgumentException
140      *             if malformed url.
141      */
142     public static URL getUrl(URL baseUrl, String suffix) throws IllegalArgumentException {
143         String url = baseUrl.toString() + "/" + suffix;
144         try {
145             return new URL(url);
146         } catch (MalformedURLException ex) {
147             throw new IllegalArgumentException("could not obtain url " + url, ex);
148         }
149     }
150 
151     public static String getArtifactId(Artifact artifact) {
152         StringBuilder sb = new StringBuilder();
153         sb.append(artifact.getGroupId());
154         sb.append("--");
155         sb.append(artifact.getArtifactId());
156         sb.append("--");
157         sb.append(artifact.getVersion());
158         return sb.toString();
159     }
160 
161     public static String getArtifactName(MavenProject project) {
162         StringBuilder sb = new StringBuilder();
163         if (project.getName().startsWith("Unnamed -")) {
164 
165             // as in Maven 3, let's use the artifact id
166             sb.append(project.getArtifactId());
167         } else {
168             sb.append(project.getName());
169         }
170         sb.append(" (");
171         sb.append(project.getGroupId());
172         sb.append(":");
173         sb.append(project.getArtifactId());
174         sb.append(":");
175         sb.append(project.getVersion());
176         sb.append(" - ");
177         String url = project.getUrl();
178         sb.append(url == null ? "no url defined" : url);
179         sb.append(")");
180 
181         return sb.toString();
182     }
183 }