1 /**
2 * The Maven License Verifier Plugin
3 *
4 * Copyright (c) 2009, 2010, 2011 by SoftwareEntwicklung Beratung Schulung (SoEBeS)
5 * Copyright (c) 2009, 2010, 2011 by Karl Heinz Marbaise
6 *
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * this work for additional information regarding copyright ownership.
10 * The ASF licenses this file to You under the Apache License, Version 2.0
11 * (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package com.soebes.maven.plugins.mlv.licenses;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.maven.model.License;
28
29 import com.soebes.maven.plugins.mlv.model.LicenseItem;
30 import com.soebes.maven.plugins.mlv.model.LicensesContainer;
31
32 /**
33 * This class offers you methods to categorize licenses into the different
34 * categories.
35 *
36 * @author <a href="mailto:kama@soebes.de">Karl Heinz Marbaise</a>
37 */
38 public class LicenseValidator {
39
40 /**
41 * Where the licenses are stored.
42 */
43 private final LicensesContainer licensesContainer;
44
45 private boolean strictChecking;
46
47 public LicenseValidator(LicensesContainer licensesContainer) {
48 strictChecking = false;
49 this.licensesContainer = licensesContainer;
50 }
51
52 /**
53 * This will return the valid licenses or an empty List if no such entry in
54 * the license.xml file exist.
55 *
56 * @return The list of LicenseItem
57 */
58 public List<LicenseItem> getValid() {
59 if (licensesContainer.getValid() == null) {
60 return new ArrayList<LicenseItem>();
61 } else {
62 return licensesContainer.getValid().getLicenses();
63 }
64 }
65
66 public void setValid(List<LicenseItem> valid) {
67 this.licensesContainer.getInvalid().setLicenses(valid);
68 }
69
70 /**
71 * This will return the Invalid license list from the license.xml file or an
72 * empty list if no such area is defined in the license.xml file.
73 *
74 * @return The List of Invalid licenses.
75 */
76 public List<LicenseItem> getInvalid() {
77 if (licensesContainer.getInvalid() == null) {
78 return new ArrayList<LicenseItem>();
79 } else {
80 return licensesContainer.getInvalid().getLicenses();
81 }
82 }
83
84 public void setInvalid(ArrayList<LicenseItem> invalid) {
85 this.licensesContainer.getInvalid().setLicenses(invalid);
86 }
87
88 /**
89 * This will return the list of licenses which are defined in the warning
90 * area of the license.xml file or an empty list if no such area has been
91 * defined in the license.xml file.
92 *
93 * @return The list of Warning licenses.
94 */
95 public List<LicenseItem> getWarning() {
96 if (licensesContainer.getWarning() == null) {
97 return new ArrayList<LicenseItem>();
98 } else {
99 return licensesContainer.getWarning().getLicenses();
100 }
101 }
102
103 public void setWarning(ArrayList<LicenseItem> warning) {
104 this.licensesContainer.getWarning().setLicenses(warning);
105 }
106
107 /**
108 * This method will check if the given License name and URL is existing in
109 * the given CheckLicense class which contains a single License which can be
110 * described with multiple names and multiple URL's.
111 *
112 * @param item
113 * The Instance of the CheckLicense class.
114 * @param cl
115 * The instance of the License to check
116 * @return true if the given license URL and Name are
117 *
118 */
119 private boolean checkNamesAndURLs(LicenseItem item, License cl) {
120 boolean result = false;
121 if (checkNames(item, cl) && checkUrls(item, cl)) {
122 result = true;
123 }
124 return result;
125 }
126
127 /**
128 * This method will check if the given License name or url is existing in
129 * the given CheckLicense class which contains a single License which can be
130 * described with multiple names and multiple URL's. This will
131 *
132 * @param item
133 * The Instance of the CheckLicense class.
134 * @param cl
135 * The instance of the License to check
136 * @return true if the given license URL and Name are
137 */
138 private boolean checkNamesOrURLs(LicenseItem item, License cl) {
139 boolean result = false;
140 if (checkNames(item, cl)) {
141 result = true;
142 }
143 if (checkUrls(item, cl)) {
144 result = true;
145 }
146 return result;
147 }
148
149 /**
150 * This will check the given License against the given list of licenses.
151 *
152 * @param checkList
153 * The list of licenses which will be used to check against.
154 * @param cl
155 * The license which will be check to be part of the above list.
156 * @return true if the license has an equal name and/or equal URL false
157 * otherwise.
158 */
159 private boolean check(List<LicenseItem> checkList, License cl) {
160 boolean result = false;
161 for (LicenseItem item : checkList) {
162 if (isStrictChecking()) {
163 if (checkNamesAndURLs(item, cl)) {
164 result = true;
165 }
166 } else {
167 if (checkNamesOrURLs(item, cl)) {
168 result = true;
169 }
170 }
171 }
172 return result;
173 }
174
175 private String checkId(List<LicenseItem> checkList, License cl) {
176 String result = null;
177 for (LicenseItem item : checkList) {
178 if (isStrictChecking()) {
179 if (checkNamesAndURLs(item, cl)) {
180 result = item.getId();
181 }
182 } else {
183 if (checkNamesOrURLs(item, cl)) {
184 result = item.getId();
185 }
186 }
187 }
188 return result;
189 }
190
191 /**
192 * This will check if the given license is part of the <code>valid</code>
193 * licenses or not.
194 *
195 * @param cl
196 * License which will be checked.
197 * @return true if License is part of the valid licenses category, false
198 * otherwise.
199 */
200 public boolean isValid(License cl) {
201 if (check(getValid(), cl)) {
202 return true;
203 } else {
204 return false;
205 }
206 }
207
208 /**
209 * This will return the Id of the License.
210 *
211 * @param cl
212 * @return null if not found or the id of the found license.
213 */
214 public String getValidId(License cl) {
215 return checkId(getValid(), cl);
216 }
217
218 /**
219 * The given list of licenses has to be part of the <code>valid</code>
220 * section of the configuration file which means in other word an
221 * conjunction (logical and).
222 *
223 * @param licenses
224 * @return
225 */
226 public boolean isValid(ArrayList<License> licenses) {
227 boolean result = true;
228 if (licenses.isEmpty()) {
229 result = false;
230 } else {
231 for (License license : licenses) {
232 if (!isValid(license)) {
233 result = false;
234 }
235 }
236 }
237 return result;
238 }
239
240 /**
241 * This method will get the Id's from the valid area of the licenses.xml
242 * file. //FIXME: More docs. analyze..
243 *
244 * @param licenses
245 * @return
246 */
247 public ArrayList<String> getValidIds(ArrayList<License> licenses) {
248 ArrayList<String> result = new ArrayList<String>();
249 if (licenses.isEmpty()) {
250 return result;
251 } else {
252 for (License license : licenses) {
253 String t = getValidId(license);
254 if (t != null) {
255 if (!result.contains(t)) {
256 result.add(t);
257 }
258 }
259 }
260 }
261 return result;
262 }
263
264 public ArrayList<String> getInvalidIds(ArrayList<License> licenses) {
265 ArrayList<String> result = new ArrayList<String>();
266 if (licenses.isEmpty()) {
267 return result;
268 } else {
269 for (License license : licenses) {
270 String t = getInvalidId(license);
271 if (t != null) {
272 if (!result.contains(t)) {
273 result.add(t);
274 }
275 }
276 }
277 }
278 return result;
279 }
280
281 public ArrayList<String> getWarningIds(ArrayList<License> licenses) {
282 ArrayList<String> result = new ArrayList<String>();
283 if (licenses.isEmpty()) {
284 return result;
285 } else {
286 for (License license : licenses) {
287 String t = getWarningId(license);
288 if (t != null) {
289 if (!result.contains(t)) {
290 result.add(t);
291 }
292 }
293 }
294 }
295 return result;
296 }
297
298 /**
299 * The given list of licenses has to be part of the <code>invalid</code>
300 * section of the configuration file which means in other word an
301 * conjunction (logical and).
302 *
303 * @param licenses
304 * @return
305 */
306 public boolean isInvalid(ArrayList<License> licenses) {
307 boolean result = true;
308 if (licenses.isEmpty()) {
309 result = false;
310 } else {
311 for (License license : licenses) {
312 if (!isInvalid(license)) {
313 result = false;
314 }
315 }
316 }
317 return result;
318 }
319
320 /**
321 * The given list of licenses has to be part of the <code>warning</code>
322 * section of the configuration file which means in other word an
323 * conjunction (logical and).
324 *
325 * @param licenses
326 * @return
327 */
328 public boolean isWarning(ArrayList<License> licenses) {
329 boolean result = true;
330 if (licenses.isEmpty()) {
331 result = false;
332 } else {
333 for (License license : licenses) {
334 if (!isWarning(license)) {
335 result = false;
336 }
337 }
338 }
339 return result;
340 }
341
342 /**
343 * TBD: Enhance description
344 *
345 * A list of licenses will be categorized as <code>Unknown</code> if one or
346 * more licenses can be categorized into different categories.
347 *
348 * For example if you have a list with three licenses
349 *
350 * isValid isWarning isInvalid isUnknown A X B X C X
351 *
352 * @param licenses
353 * The list with the licenses which will be checked.
354 * @return true if a license is from no category.
355 *
356 */
357 public boolean isUnknown(ArrayList<License> licenses) {
358 boolean result = false;
359 if (licenses.isEmpty()) {
360 result = true;
361 } else {
362 int changeValid = 0;
363 int changeInvalid = 0;
364 int changeWarning = 0;
365 for (License license : licenses) {
366 if (isValid(license)) {
367 changeValid++;
368 }
369 if (isInvalid(license)) {
370 changeInvalid++;
371 }
372 if (isWarning(license)) {
373 changeWarning++;
374 }
375 }
376
377 if (changeValid == licenses.size()) {
378 result = false;
379 } else if (changeInvalid == licenses.size()) {
380 result = false;
381 } else if (changeWarning == licenses.size()) {
382 result = false;
383 } else {
384 result = true;
385 }
386 }
387 return result;
388 }
389
390 /**
391 * Check if a given license is not part of one the categories
392 * <b>invalid</b>, <b>valid</b> or <b>warning</b>.
393 *
394 * @param license
395 * @return true not part of any category false otherwise.
396 */
397 public boolean isUnknown(License license) {
398 boolean result = true;
399 if (isValid(license)) {
400 result = false;
401 } else if (isInvalid(license)) {
402 result = false;
403 } else if (isWarning(license)) {
404 result = false;
405 }
406
407 return result;
408 }
409
410 /**
411 * This will check if the given license is part of the <code>invalid</code>
412 * category or not.
413 *
414 * @param cl
415 * License which will be checked.
416 * @return true if License part of the invalid licenses false otherwise.
417 */
418 public boolean isInvalid(License cl) {
419 return check(getInvalid(), cl);
420 }
421
422 public String getInvalidId(License cl) {
423 return checkId(getInvalid(), cl);
424 }
425
426 /**
427 * This will check if the given license is part of the <code>warning</code>
428 * category or not.
429 *
430 * @param cl
431 * License which will be checked.
432 * @return true if License part of the warning licenses false otherwise.
433 */
434 public boolean isWarning(License cl) {
435 return check(getWarning(), cl);
436 }
437
438 public String getWarningId(License cl) {
439 return checkId(getWarning(), cl);
440 }
441
442 /**
443 * Check the given License URL against the CheckLicense which can contain
444 * more than one.
445 *
446 * @param item
447 * The CheckLicense instance which is used to check.
448 * @param cl
449 * The license which will be checked
450 * @return true if the License <b>URL</b> is equal to one of the URLs in
451 * CheckLicense.
452 */
453 private boolean checkUrls(LicenseItem item, License cl) {
454 boolean result = false;
455 for (String checkUrl : item.getUrls()) {
456 if (checkUrl.equals(cl.getUrl())) {
457 result = true;
458 }
459 }
460 return result;
461 }
462
463 /**
464 * Check the given License Name against the CheckLicense which can contain
465 * more than one.
466 *
467 * @param item
468 * The CheckLicense instance which is used to check.
469 * @param cl
470 * The license which will be checked
471 * @return true if the License <b>name</b> is equal to one of the Names in
472 * CheckLicense.
473 */
474 private boolean checkNames(LicenseItem item, License cl) {
475 boolean result = false;
476 for (String checkName : item.getNames()) {
477 if (checkName.equals(cl.getName())) {
478 result = true;
479 }
480 }
481 return result;
482 }
483
484 public enum Category {
485 VALID,
486 INVALID,
487 WARNING,
488 UNKNOWN
489 }
490 public boolean isCategory(ArrayList<License> licenses, Category category) {
491 boolean result = false;
492 switch(category) {
493 case INVALID:
494 result = isInvalid(licenses);
495 break;
496 case VALID:
497 result = isValid(licenses);
498 break;
499 case UNKNOWN:
500 result = isUnknown(licenses);
501 break;
502 case WARNING:
503 result = isWarning(licenses);
504 break;
505 }
506 return result;
507 }
508 public void setStrictChecking(boolean strictChecking) {
509 this.strictChecking = strictChecking;
510 }
511
512 public boolean isStrictChecking() {
513 return strictChecking;
514 }
515 }