1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34
35
36
37
38 public class LicenseValidator {
39
40
41
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
54
55
56
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
72
73
74
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
90
91
92
93
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
109
110
111
112
113
114
115
116
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
129
130
131
132
133
134
135
136
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
151
152
153
154
155
156
157
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
193
194
195
196
197
198
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
210
211
212
213
214 public String getValidId(License cl) {
215 return checkId(getValid(), cl);
216 }
217
218
219
220
221
222
223
224
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
242
243
244
245
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
300
301
302
303
304
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
322
323
324
325
326
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
344
345
346
347
348
349
350
351
352
353
354
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
392
393
394
395
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
412
413
414
415
416
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
428
429
430
431
432
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
444
445
446
447
448
449
450
451
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
465
466
467
468
469
470
471
472
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 }