001 /**
002 * Copyright 2010-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.license;
017
018 import org.apache.maven.artifact.Artifact;
019 import org.apache.maven.model.Resource;
020 import org.apache.maven.project.MavenProject;
021
022 import java.io.File;
023 import java.net.MalformedURLException;
024 import java.net.URL;
025 import java.text.MessageFormat;
026 import java.util.Comparator;
027 import java.util.List;
028
029 /**
030 * Mojo helper methods.
031 *
032 * @author tchemit <chemit@codelutin.com>
033 * @since 1.0
034 */
035 public class MojoHelper
036 {
037
038 /**
039 * Add the directory as a resource of the given project.
040 *
041 * @param dir the directory to add
042 * @param project the project to update
043 * @param includes the includes of the resource
044 * @return {@code true} if the resources was added (not already existing)
045 */
046 public static boolean addResourceDir( File dir, MavenProject project, String... includes )
047 {
048 List<?> resources = project.getResources();
049 return addResourceDir( dir, project, resources, includes );
050 }
051
052 /**
053 * Add the directory as a resource in the given resource list.
054 *
055 * @param dir the directory to add
056 * @param project the project involved
057 * @param resources the list of existing resources
058 * @param includes includes of the new resources
059 * @return {@code true} if the resource was added (not already existing)
060 */
061 public static boolean addResourceDir( File dir, MavenProject project, List<?> resources, String... includes )
062 {
063 String newresourceDir = dir.getAbsolutePath();
064 boolean shouldAdd = true;
065 for ( Object o : resources )
066 {
067 Resource r = (Resource) o;
068 if ( !r.getDirectory().equals( newresourceDir ) )
069 {
070 continue;
071 }
072
073 for ( String i : includes )
074 {
075 if ( !r.getIncludes().contains( i ) )
076 {
077 r.addInclude( i );
078 }
079 }
080 shouldAdd = false;
081 break;
082 }
083 if ( shouldAdd )
084 {
085 Resource r = new Resource();
086 r.setDirectory( newresourceDir );
087 for ( String i : includes )
088 {
089 if ( !r.getIncludes().contains( i ) )
090 {
091 r.addInclude( i );
092 }
093 }
094 project.addResource( r );
095 }
096 return shouldAdd;
097 }
098
099 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 }