1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.swid.plugin.entry.resource;
28
29 import gov.nist.secauto.swid.plugin.entry.AbstractFileEntryProcessor;
30 import gov.nist.secauto.swid.plugin.entry.FileEntry;
31 import gov.nist.secauto.swid.plugin.entry.FileFileEntry;
32 import gov.nist.secauto.swid.plugin.entry.FileSelectorPredicate;
33
34 import org.apache.maven.model.Resource;
35 import org.apache.maven.plugin.logging.Log;
36 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
37
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.List;
46 import java.util.Objects;
47 import java.util.stream.Collectors;
48
49 public class ResourceFileEntryProcessor extends AbstractFileEntryProcessor<Resource> {
50
51 private IncludeExcludeFileSelector selector = new IncludeExcludeFileSelector();
52 private final String buildOutputDirectory;
53
54
55
56
57
58
59
60
61
62 public ResourceFileEntryProcessor(String buildOutputDirectory, Log log) {
63 super(log);
64 Objects.requireNonNull(buildOutputDirectory);
65 this.buildOutputDirectory = buildOutputDirectory;
66 }
67
68 public void setIncludes(String[] includes) {
69 selector.setIncludes(includes);
70 }
71
72 public void setExcludes(String[] excludes) {
73 selector.setExcludes(excludes);
74 }
75
76 @Override
77 public List<FileEntry> process(List<? extends Resource> resources) throws IOException {
78 List<FileEntry> processedResources = super.process(resources);
79 List<FileEntry> processedClasses = generateClassFileEntries();
80
81 int size = processedResources.size() + processedClasses.size();
82 List<FileEntry> retval;
83 if (size == 0) {
84 retval = Collections.emptyList();
85 } else {
86 retval = new ArrayList<>(size);
87 retval.addAll(processedResources);
88 retval.addAll(processedClasses);
89 }
90 return retval;
91 }
92
93 private List<FileEntry> generateClassFileEntries() throws IOException {
94 getLog().debug("Processing classes: " + buildOutputDirectory);
95
96 Path path = Paths.get(buildOutputDirectory);
97
98 List<FileEntry> retval;
99 if (path.toFile().exists()) {
100 retval = Files.walk(path).filter(Files::isRegularFile).map(p -> new FileFileEntry(p, path))
101 .filter(new FileSelectorPredicate(selector)).collect(Collectors.toList());
102 } else {
103 retval = Collections.emptyList();
104 }
105 return retval;
106 }
107
108 @Override
109 protected Collection<? extends FileEntry> generateFileEntries(Resource resource) throws IOException {
110 getLog().debug("Processing resource: " + resource.getDirectory());
111
112 Path path = Paths.get(resource.getDirectory());
113
114 List<FileEntry> retval;
115 if (path.toFile().exists()) {
116 retval = Files.walk(path).filter(Files::isRegularFile).map(p -> new ResourceFileEntry(resource, p))
117 .filter(new FileSelectorPredicate(selector)).collect(Collectors.toList());
118 } else {
119 retval = Collections.emptyList();
120 }
121 return retval;
122 }
123
124 }