1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat;
20
21 import java.io.File;
22 import java.io.FileFilter;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.PrintWriter;
29 import java.net.MalformedURLException;
30 import java.net.URI;
31 import java.net.URL;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.Objects;
38 import java.util.SortedSet;
39 import java.util.function.Consumer;
40
41 import org.apache.commons.io.function.IOSupplier;
42 import org.apache.rat.analysis.IHeaderMatcher;
43 import org.apache.rat.commandline.StyleSheets;
44 import org.apache.rat.config.AddLicenseHeaders;
45 import org.apache.rat.config.exclusion.ExclusionProcessor;
46 import org.apache.rat.config.exclusion.StandardCollection;
47 import org.apache.rat.config.results.ClaimValidator;
48 import org.apache.rat.configuration.builders.AnyBuilder;
49 import org.apache.rat.document.DocumentName;
50 import org.apache.rat.document.DocumentNameMatcher;
51 import org.apache.rat.document.FileDocument;
52 import org.apache.rat.license.ILicense;
53 import org.apache.rat.license.ILicenseFamily;
54 import org.apache.rat.license.LicenseSetFactory;
55 import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
56 import org.apache.rat.report.IReportable;
57 import org.apache.rat.utils.DefaultLog;
58 import org.apache.rat.utils.Log.Level;
59 import org.apache.rat.utils.ReportingSet;
60 import org.apache.rat.walker.FileListWalker;
61 import org.apache.rat.walker.IReportableListWalker;
62
63
64
65
66
67
68 public class ReportConfiguration {
69
70
71
72
73 public enum Processing {
74
75 NOTIFICATION("List file as present"),
76
77 PRESENCE("List any licenses found"),
78
79 ABSENCE("List licenses found and any unknown licences");
80
81
82
83
84 private final String description;
85
86
87 Processing(final String description) {
88 this.description = description;
89 }
90
91
92
93
94
95 public String desc() {
96 return description;
97 }
98 }
99
100
101 private final LicenseSetFactory licenseSetFactory;
102
103
104
105
106 private boolean addingLicenses;
107
108
109
110 private boolean addingLicensesForced;
111
112
113
114
115 private String copyrightMessage;
116
117
118
119 private IOSupplier<OutputStream> out;
120
121
122
123 private IOSupplier<InputStream> styleSheet;
124
125
126
127
128 private final List<File> sources;
129
130
131
132
133 private final List<IReportable> reportables;
134
135
136
137
138 private final ExclusionProcessor exclusionProcessor;
139
140
141
142
143 private LicenseFilter listFamilies;
144
145
146
147 private LicenseFilter listLicenses;
148
149
150
151 private boolean dryRun;
152
153
154
155 private Processing archiveProcessing;
156
157
158
159 private Processing standardProcessing;
160
161
162
163 private final ClaimValidator claimValidator;
164
165
166
167 public ReportConfiguration() {
168 licenseSetFactory = new LicenseSetFactory();
169 listFamilies = Defaults.LIST_FAMILIES;
170 listLicenses = Defaults.LIST_LICENSES;
171 dryRun = false;
172 exclusionProcessor = new ExclusionProcessor();
173 claimValidator = new ClaimValidator();
174 sources = new ArrayList<>();
175 reportables = new ArrayList<>();
176 }
177
178
179
180
181
182
183
184
185 public void addSource(final File file) {
186 notNull(file, "File may not be null.");
187 sources.add(file);
188 }
189
190 private void notNull(final Object o, final String msg) {
191 if (o == null) {
192 throw new ConfigurationException(msg);
193 }
194 }
195
196
197
198
199
200 public void addSource(final IReportable reportable) {
201 notNull(reportable, "Reportable may not be null.");
202 reportables.add(reportable);
203 }
204
205
206
207
208
209 public boolean hasSource() {
210 return !reportables.isEmpty() || !sources.isEmpty();
211 }
212
213
214
215
216
217 public IReportableListWalker.Builder getSources() {
218 DocumentName name = DocumentName.builder(new File(".")).build();
219 IReportableListWalker.Builder builder = IReportableListWalker.builder(name);
220 sources.forEach(file -> builder.addReportable(new FileListWalker(new FileDocument(file, DocumentNameMatcher.MATCHES_ALL))));
221 reportables.forEach(builder::addReportable);
222 return builder;
223 }
224
225
226
227
228
229 public IHeaderMatcher getGeneratedMatcher() {
230 return new AnyBuilder().setResource("/org/apache/rat/generation-keywords.txt").build();
231 }
232
233
234
235
236
237 public Processing getArchiveProcessing() {
238 return archiveProcessing == null ? Defaults.ARCHIVE_PROCESSING : archiveProcessing;
239 }
240
241
242
243
244
245 public void setArchiveProcessing(final Processing archiveProcessing) {
246 this.archiveProcessing = archiveProcessing;
247 }
248
249
250
251
252
253 public Processing getStandardProcessing() {
254 return standardProcessing == null ? Defaults.STANDARD_PROCESSING : standardProcessing;
255 }
256
257
258
259
260
261 public void setStandardProcessing(final Processing standardProcessing) {
262 this.standardProcessing = standardProcessing;
263 }
264
265
266
267
268
269
270 public void logFamilyCollisions(final Level level) {
271 licenseSetFactory.logFamilyCollisions(level);
272 }
273
274
275
276
277
278 public void familyDuplicateOption(final ReportingSet.Options state) {
279 licenseSetFactory.familyDuplicateOption(state);
280 }
281
282
283
284
285
286 public void logLicenseCollisions(final Level level) {
287 licenseSetFactory.logLicenseCollisions(level);
288 }
289
290
291
292
293
294 public void licenseDuplicateOption(final ReportingSet.Options state) {
295 licenseSetFactory.licenseDuplicateOption(state);
296 }
297
298
299
300
301
302 public void listFamilies(final LicenseFilter filter) {
303 listFamilies = filter;
304 }
305
306
307
308
309
310 public LicenseFilter listFamilies() {
311 return listFamilies;
312 }
313
314
315
316
317
318 public void listLicenses(final LicenseFilter filter) {
319 listLicenses = filter;
320 }
321
322
323
324
325
326 public LicenseFilter listLicenses() {
327 return listLicenses;
328 }
329
330
331
332
333
334 public void setDryRun(final boolean state) {
335 dryRun = state;
336 }
337
338
339
340
341
342 public boolean isDryRun() {
343 return dryRun;
344 }
345
346
347
348
349
350
351 public void addExcludedCollection(final StandardCollection collection) {
352 exclusionProcessor.addExcludedCollection(collection);
353 }
354
355
356
357
358
359
360 public void addExcludedFileProcessor(final StandardCollection collection) {
361 exclusionProcessor.addFileProcessor(collection);
362 }
363
364
365
366
367
368 public void addExcludedFilter(final FileFilter fileFilter) {
369 exclusionProcessor.addExcludedMatcher(new DocumentNameMatcher(fileFilter));
370 }
371
372
373
374
375
376 public void addExcludedMatcher(final DocumentNameMatcher matcher) {
377 exclusionProcessor.addExcludedMatcher(matcher);
378 }
379
380
381
382
383
384
385
386 public void addExcludedPatterns(final Iterable<String> patterns) {
387 exclusionProcessor.addExcludedPatterns(patterns);
388 }
389
390
391
392
393
394 public void addIncludedCollection(final StandardCollection collection) {
395 exclusionProcessor.addIncludedCollection(collection);
396 }
397
398
399
400
401
402
403 public void addIncludedFilter(final FileFilter fileFilter) {
404 exclusionProcessor.addIncludedMatcher(new DocumentNameMatcher(fileFilter));
405 }
406
407
408
409
410
411
412 public void addIncludedPatterns(final Iterable<String> patterns) {
413 exclusionProcessor.addIncludedPatterns(patterns);
414 }
415
416
417
418
419
420
421 public DocumentNameMatcher getDocumentExcluder(final DocumentName baseDir) {
422 return exclusionProcessor.getNameMatcher(baseDir);
423 }
424
425
426
427
428
429
430 public IOSupplier<InputStream> getStyleSheet() {
431 return styleSheet;
432 }
433
434
435
436
437
438
439
440 public void setStyleSheet(final IOSupplier<InputStream> styleSheet) {
441 this.styleSheet = styleSheet;
442 }
443
444
445
446
447
448
449
450 public void setFrom(final Defaults defaults) {
451 licenseSetFactory.add(defaults.getLicenseSetFactory());
452 if (getStyleSheet() == null) {
453 setStyleSheet(StyleSheets.PLAIN.getStyleSheet());
454 }
455 defaults.getStandardExclusion().forEach(this::addExcludedCollection);
456 }
457
458
459
460
461
462 public void setStyleSheet(final File styleSheet) {
463 Objects.requireNonNull(styleSheet, "styleSheet file should not be null");
464 setStyleSheet(styleSheet.toURI());
465 }
466
467
468
469
470
471
472 public void setStyleSheet(final URI styleSheet) {
473 Objects.requireNonNull(styleSheet, "Stylesheet file must not be null");
474 try {
475 setStyleSheet(styleSheet.toURL());
476 } catch (MalformedURLException e) {
477 throw new ConfigurationException("Unable to process stylesheet", e);
478 }
479 }
480
481
482
483
484
485
486 public void setStyleSheet(final URL styleSheet) {
487 Objects.requireNonNull(styleSheet, "Stylesheet file must not be null");
488 setStyleSheet(styleSheet::openStream);
489 }
490
491
492
493
494
495
496
497
498
499
500 public void setOut(final IOSupplier<OutputStream> out) {
501 this.out = out;
502 }
503
504
505
506
507
508
509
510
511 public void setOut(final File file) {
512 Objects.requireNonNull(file, "output file should not be null");
513 if (file.exists()) {
514 try {
515 Files.delete(file.toPath());
516 } catch (IOException e) {
517 DefaultLog.getInstance().warn("Unable to delete file: " + file);
518 }
519 }
520 File parent = file.getParentFile();
521 if (!parent.mkdirs() && !parent.isDirectory()) {
522 DefaultLog.getInstance().warn("Unable to create directory: " + file.getParentFile());
523 }
524 setOut(() -> new FileOutputStream(file, true));
525 }
526
527
528
529
530
531
532 public IOSupplier<OutputStream> getOutput() {
533 return out == null ? () -> new NoCloseOutputStream(System.out) : out;
534 }
535
536
537
538
539
540
541 public IOSupplier<PrintWriter> getWriter() {
542 return () -> new PrintWriter(new OutputStreamWriter(getOutput().get(), StandardCharsets.UTF_8));
543 }
544
545
546
547
548
549
550 public void addLicense(final ILicense license) {
551 licenseSetFactory.addLicense(license);
552 }
553
554
555
556
557
558
559
560 public ILicense addLicense(final ILicense.Builder builder) {
561 return licenseSetFactory.addLicense(builder);
562 }
563
564
565
566
567
568
569 public void addLicenses(final Collection<ILicense> licenses) {
570 licenseSetFactory.addLicenses(licenses);
571 }
572
573
574
575
576
577
578 public void addFamily(final ILicenseFamily family) {
579 licenseSetFactory.addFamily(family);
580 }
581
582
583
584
585
586
587
588 public void addFamily(final ILicenseFamily.Builder builder) {
589 licenseSetFactory.addFamily(builder);
590 }
591
592
593
594
595
596
597 public void addFamilies(final Collection<ILicenseFamily> families) {
598 families.forEach(this::addApprovedLicenseCategory);
599 }
600
601
602
603
604
605 public void addApprovedLicenseCategory(final ILicenseFamily approvedILicenseFamily) {
606 addApprovedLicenseCategory(approvedILicenseFamily.getFamilyCategory());
607 }
608
609
610
611
612
613 public void addApprovedLicenseCategory(final String familyCategory) {
614 licenseSetFactory.approveLicenseCategory(familyCategory);
615 }
616
617
618
619
620
621
622 public void addApprovedLicenseCategories(final Collection<String> approvedLicenseCategories) {
623 approvedLicenseCategories.forEach(this::addApprovedLicenseCategory);
624 }
625
626
627
628
629
630
631 public void removeApprovedLicenseCategory(final String familyCategory) {
632 licenseSetFactory.removeLicenseCategory(ILicenseFamily.makeCategory(familyCategory));
633 }
634
635
636
637
638
639
640
641 public void removeApprovedLicenseCategories(final Collection<String> familyCategory) {
642 familyCategory.forEach(this::removeApprovedLicenseCategory);
643 }
644
645
646
647
648
649
650
651 public SortedSet<String> getLicenseCategories(final LicenseFilter filter) {
652 return licenseSetFactory.getLicenseCategories(filter);
653 }
654
655
656
657
658
659
660
661 public SortedSet<ILicense> getLicenses(final LicenseFilter filter) {
662 return licenseSetFactory.getLicenses(filter);
663 }
664
665
666
667
668
669
670
671 public SortedSet<String> getLicenseIds(final LicenseFilter filter) {
672 return licenseSetFactory.getLicenseIds(filter);
673 }
674
675
676
677
678
679 public void addApprovedLicenseId(final ILicense approvedLicense) {
680 addApprovedLicenseId(approvedLicense.getId());
681 }
682
683
684
685
686
687 public void addApprovedLicenseId(final String licenseId) {
688 licenseSetFactory.approveLicenseId(licenseId);
689 }
690
691
692
693
694
695
696 public void addApprovedLicenseIds(final Collection<String> approvedLicenseIds) {
697 approvedLicenseIds.forEach(this::addApprovedLicenseId);
698 }
699
700
701
702
703
704
705 public void removeApprovedLicenseId(final String licenseId) {
706 licenseSetFactory.removeLicenseId(licenseId);
707 }
708
709
710
711
712
713
714
715 public void removeApprovedLicenseIds(final Collection<String> licenseIds) {
716 licenseIds.forEach(this::removeApprovedLicenseId);
717 }
718
719
720
721
722
723
724
725 public String getCopyrightMessage() {
726 return copyrightMessage;
727 }
728
729
730
731
732
733
734
735 public void setCopyrightMessage(final String copyrightMessage) {
736 this.copyrightMessage = copyrightMessage;
737 }
738
739
740
741
742
743
744
745 public boolean isAddingLicensesForced() {
746 return addingLicensesForced;
747 }
748
749
750
751
752
753
754
755 public boolean isAddingLicenses() {
756 return addingLicenses;
757 }
758
759
760
761
762
763
764
765
766
767 public void setAddLicenseHeaders(final AddLicenseHeaders addLicenseHeaders) {
768 addingLicenses = false;
769 addingLicensesForced = false;
770 switch (addLicenseHeaders) {
771 case FALSE:
772
773 break;
774 case FORCED:
775 addingLicensesForced = true;
776 addingLicenses = true;
777 break;
778 case TRUE:
779 addingLicenses = true;
780 break;
781 }
782 }
783
784
785
786
787
788
789
790
791
792
793
794
795 public SortedSet<ILicenseFamily> getLicenseFamilies(final LicenseFilter filter) {
796 return licenseSetFactory.getLicenseFamilies(filter);
797 }
798
799
800
801
802
803 public ClaimValidator getClaimValidator() {
804 return claimValidator;
805 }
806
807
808
809
810
811 public LicenseSetFactory getLicenseSetFactory() {
812 return licenseSetFactory;
813 }
814
815
816
817
818
819
820 public void validate(final Consumer<String> logger) {
821 if (!hasSource()) {
822 String msg = "At least one source must be specified";
823 logger.accept(msg);
824 throw new ConfigurationException(msg);
825 }
826 if (licenseSetFactory.getLicenses(LicenseFilter.ALL).isEmpty()) {
827 String msg = "You must specify at least one license";
828 logger.accept(msg);
829 throw new ConfigurationException(msg);
830 }
831 }
832
833
834
835
836 public static class NoCloseOutputStream extends OutputStream {
837
838 private final OutputStream delegate;
839
840
841
842
843
844 public NoCloseOutputStream(final OutputStream delegate) {
845 this.delegate = delegate;
846 }
847
848 @Override
849 public void write(final int arg0) throws IOException {
850 delegate.write(arg0);
851 }
852
853
854
855
856
857 @Override
858 public void close() throws IOException {
859 this.delegate.flush();
860 }
861
862 @Override
863 public boolean equals(final Object obj) {
864 return delegate.equals(obj);
865 }
866
867 @Override
868 public void flush() throws IOException {
869 delegate.flush();
870 }
871
872 @Override
873 public int hashCode() {
874 return delegate.hashCode();
875 }
876
877 @Override
878 public String toString() {
879 return delegate.toString();
880 }
881
882 @Override
883 public void write(final byte[] arg0, final int arg1, final int arg2) throws IOException {
884 delegate.write(arg0, arg1, arg2);
885 }
886
887 @Override
888 public void write(final byte[] b) throws IOException {
889 delegate.write(b);
890 }
891 }
892 }