001    /*
002     * #%L
003     * License Maven Plugin
004     *
005     * $Id: MojoHelper.java 14410 2011-08-10 20:54:51Z tchemit $
006     * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/MojoHelper.java $
007     * %%
008     * Copyright (C) 2010 - 2011 Codehaus
009     * %%
010     * This program is free software: you can redistribute it and/or modify
011     * it under the terms of the GNU Lesser General Public License as 
012     * published by the Free Software Foundation, either version 3 of the 
013     * License, or (at your option) any later version.
014     * 
015     * This program is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Lesser Public License for more details.
019     * 
020     * You should have received a copy of the GNU General Lesser Public 
021     * License along with this program.  If not, see
022     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023     * #L%
024     */
025    package org.codehaus.mojo.license;
026    
027    import org.apache.maven.artifact.Artifact;
028    import org.apache.maven.model.Resource;
029    import org.apache.maven.project.MavenProject;
030    
031    import java.io.File;
032    import java.net.MalformedURLException;
033    import java.net.URL;
034    import java.text.MessageFormat;
035    import java.util.Comparator;
036    import java.util.List;
037    
038    /**
039     * Mojo helper methods.
040     *
041     * @author tchemit <chemit@codelutin.com>
042     * @since 1.0
043     */
044    public class MojoHelper
045    {
046    
047        /**
048         * Add the directory as a resource of the given project.
049         *
050         * @param dir      the directory to add
051         * @param project  the project to update
052         * @param includes the includes of the resource
053         * @return {@code true} if the resources was added (not already existing)
054         */
055        public static boolean addResourceDir( File dir, MavenProject project, String... includes )
056        {
057            List<?> resources = project.getResources();
058            return addResourceDir( dir, project, resources, includes );
059        }
060    
061        /**
062         * Add the directory as a resource in the given resource list.
063         *
064         * @param dir       the directory to add
065         * @param project   the project involved
066         * @param resources the list of existing resources
067         * @param includes  includes of the new resources
068         * @return {@code true} if the resource was added (not already existing)
069         */
070        public static boolean addResourceDir( File dir, MavenProject project, List<?> resources, String... includes )
071        {
072            String newresourceDir = dir.getAbsolutePath();
073            boolean shouldAdd = true;
074            for ( Object o : resources )
075            {
076                Resource r = (Resource) o;
077                if ( !r.getDirectory().equals( newresourceDir ) )
078                {
079                    continue;
080                }
081    
082                for ( String i : includes )
083                {
084                    if ( !r.getIncludes().contains( i ) )
085                    {
086                        r.addInclude( i );
087                    }
088                }
089                shouldAdd = false;
090                break;
091            }
092            if ( shouldAdd )
093            {
094                Resource r = new Resource();
095                r.setDirectory( newresourceDir );
096                for ( String i : includes )
097                {
098                    if ( !r.getIncludes().contains( i ) )
099                    {
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    }