001    /**
002     * Copyright 2010-2013 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 java.io.File;
019    import java.net.MalformedURLException;
020    import java.net.URL;
021    import java.text.MessageFormat;
022    import java.util.Comparator;
023    import java.util.List;
024    
025    import org.apache.maven.artifact.Artifact;
026    import org.apache.maven.model.Resource;
027    import org.apache.maven.project.MavenProject;
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         * Add the directory as a resource of the given project.
039         *
040         * @param dir
041         *            the directory to add
042         * @param project
043         *            the project to update
044         * @param includes
045         *            the includes of the resource
046         * @return {@code true} if the resources was added (not already existing)
047         */
048        public static boolean addResourceDir(File dir, MavenProject project, String... includes) {
049            List<?> resources = project.getResources();
050            return addResourceDir(dir, project, resources, includes);
051        }
052    
053        /**
054         * Add the directory as a resource in the given resource list.
055         *
056         * @param dir
057         *            the directory to add
058         * @param project
059         *            the project involved
060         * @param resources
061         *            the list of existing resources
062         * @param includes
063         *            includes of the new resources
064         * @return {@code true} if the resource was added (not already existing)
065         */
066        public static boolean addResourceDir(File dir, MavenProject project, List<?> resources, String... includes) {
067            String newresourceDir = dir.getAbsolutePath();
068            boolean shouldAdd = true;
069            for (Object o : resources) {
070                Resource r = (Resource) o;
071                if (!r.getDirectory().equals(newresourceDir)) {
072                    continue;
073                }
074    
075                for (String i : includes) {
076                    if (!r.getIncludes().contains(i)) {
077                        r.addInclude(i);
078                    }
079                }
080                shouldAdd = false;
081                break;
082            }
083            if (shouldAdd) {
084                Resource r = new Resource();
085                r.setDirectory(newresourceDir);
086                for (String i : includes) {
087                    if (!r.getIncludes().contains(i)) {
088                        r.addInclude(i);
089                    }
090                }
091                project.addResource(r);
092            }
093            return shouldAdd;
094        }
095    
096        public static Comparator<MavenProject> newMavenProjectComparator() {
097            return new Comparator<MavenProject>() {
098                @Override
099                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    }