1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.creadur.whisker.app.analysis; |
20 | |
|
21 | |
import java.util.Collection; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
import java.util.TreeSet; |
25 | |
|
26 | |
|
27 | |
import org.apache.commons.lang3.tuple.Pair; |
28 | |
import org.apache.creadur.whisker.model.Resource; |
29 | |
import org.apache.creadur.whisker.model.ResourceNamesCollator; |
30 | |
import org.apache.creadur.whisker.model.Descriptor; |
31 | |
import org.apache.creadur.whisker.scan.Directory; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class LicenseAnalyst { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
private static Map<ResourceDefinitionError, |
43 | |
Collection<ResourceDescription>> buildIssueMap() { |
44 | |
final Map<ResourceDefinitionError, |
45 | 0 | Collection<ResourceDescription>> results = |
46 | |
new HashMap<ResourceDefinitionError, |
47 | |
Collection<ResourceDescription>>(); |
48 | |
for (final ResourceDefinitionError error: |
49 | 0 | ResourceDefinitionError.values()) { |
50 | 0 | initIssues(results, error); |
51 | |
} |
52 | 0 | return results; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private static void initIssues( |
61 | |
final Map< |
62 | |
ResourceDefinitionError, |
63 | |
Collection<ResourceDescription>> results, |
64 | |
ResourceDefinitionError error) { |
65 | 0 | results.put(error, new TreeSet<ResourceDescription>()); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
private final Collection<Directory> directories; |
70 | |
|
71 | |
private final Map<ResourceDefinitionError, |
72 | |
Collection<ResourceDescription>> issues; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public LicenseAnalyst() { |
78 | 0 | this(null); |
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public LicenseAnalyst(final Collection<Directory> directories) { |
86 | 0 | super(); |
87 | 0 | this.directories = directories; |
88 | 0 | issues = buildIssueMap(); |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public LicenseAnalyst analyse(final Descriptor work) { |
97 | 0 | if (directories == null) { |
98 | 0 | final ResourceNamesCollator collator = |
99 | |
new ResourceNamesCollator(); |
100 | 0 | work.traverse(collator); |
101 | 0 | analyseDuplicates(collator); |
102 | |
|
103 | 0 | final ResourceSourceAuditor sourceAuditor = |
104 | |
new ResourceSourceAuditor(); |
105 | 0 | work.traverse(sourceAuditor); |
106 | 0 | analyse(sourceAuditor); |
107 | 0 | } else { |
108 | 0 | for (final Directory directory: directories) { |
109 | 0 | final ResourceNamesCollator collator = |
110 | |
new ResourceNamesCollator(); |
111 | 0 | work.traverseDirectory(collator, directory.getName()); |
112 | 0 | analyseLicenses(directory, collator); |
113 | 0 | analyseDuplicates(collator); |
114 | |
|
115 | 0 | final ResourceSourceAuditor sourceAuditor = new |
116 | |
ResourceSourceAuditor(); |
117 | 0 | work.traverseDirectory(sourceAuditor, directory.getName()); |
118 | 0 | analyse(sourceAuditor); |
119 | 0 | } |
120 | |
} |
121 | 0 | return this; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private void analyse(final ResourceSourceAuditor sourceAuditor) { |
129 | 0 | addIssues(sourceAuditor.getResourcesMissingSource(), |
130 | |
ResourceDefinitionError.MISSING_SOURCE); |
131 | 0 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public boolean isValid() { |
139 | |
for (final ResourceDefinitionError error: |
140 | 0 | ResourceDefinitionError.values()) { |
141 | 0 | if (!getIssues(error).isEmpty()) { |
142 | 0 | return false; |
143 | |
} |
144 | |
} |
145 | 0 | return true; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public Descriptor validate(final Descriptor work) |
155 | |
throws ResourceDefinitionException { |
156 | 0 | analyse(work); |
157 | 0 | if (isValid()) { |
158 | 0 | return work; |
159 | |
} else { |
160 | 0 | throw new ResourceDefinitionException(issues); |
161 | |
} |
162 | |
|
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
private void analyseDuplicates( |
170 | |
final ResourceNamesCollator collator) { |
171 | 0 | addIssues(collator.getDuplicates(), |
172 | |
ResourceDefinitionError.DUPLICATE); |
173 | 0 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
private void addIssues( |
181 | |
final Collection<Pair< |
182 | |
org.apache.creadur.whisker.model.WithinDirectory, |
183 | |
Resource>> resources, |
184 | |
final ResourceDefinitionError error) { |
185 | |
for (Pair< |
186 | |
org.apache.creadur.whisker.model.WithinDirectory, |
187 | 0 | Resource> duplicate: resources) { |
188 | 0 | getIssues(error).add( |
189 | |
new ResourceDescription( |
190 | |
duplicate.getLeft().getName(), |
191 | |
duplicate.getRight().getName())); |
192 | |
} |
193 | 0 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
private void analyseLicenses(final Directory directory, |
201 | |
final ResourceNamesCollator collator) { |
202 | 0 | analyseExtraLicenses(directory, collator); |
203 | 0 | analyseMissingLicenses(directory, collator); |
204 | 0 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
@SuppressWarnings("PMD.EmptyIfStmt") |
213 | |
private void analyseExtraLicenses(final Directory directory, |
214 | |
final ResourceNamesCollator collator) { |
215 | 0 | final Collection<String> actualResources = directory.getContents(); |
216 | 0 | for (final String resourceLicense: collator.getNames()) { |
217 | 0 | if (actualResources.contains(resourceLicense)) { |
218 | |
|
219 | |
} else { |
220 | 0 | getExtraLicenses().add( |
221 | |
new ResourceDescription( |
222 | |
directory.getName(), |
223 | |
resourceLicense)); |
224 | |
} |
225 | |
} |
226 | 0 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
@SuppressWarnings("PMD.EmptyIfStmt") |
234 | |
private void analyseMissingLicenses(final Directory directory, |
235 | |
final ResourceNamesCollator collator) { |
236 | 0 | final Collection<String> licensedResources = collator.getNames(); |
237 | 0 | for (final String actualResource: directory.getContents()) { |
238 | 0 | if (licensedResources.contains(actualResource)) { |
239 | |
|
240 | |
} else { |
241 | 0 | getMissingLicenses().add( |
242 | |
new ResourceDescription( |
243 | |
directory.getName(), |
244 | |
actualResource)); |
245 | |
} |
246 | |
} |
247 | 0 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public Collection<ResourceDescription> getResourcesMissingSources() { |
254 | 0 | return getIssues(ResourceDefinitionError.MISSING_SOURCE); |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public Collection<ResourceDescription> getExtraLicenses() { |
262 | 0 | return getIssues(ResourceDefinitionError.EXTRA_LICENSE); |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public Collection<ResourceDescription> getMissingLicenses() { |
270 | 0 | return getIssues(ResourceDefinitionError.MISSING_LICENSE); |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
public Collection<ResourceDescription> getDuplicates() { |
278 | 0 | return getIssues(ResourceDefinitionError.DUPLICATE); |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public Collection<ResourceDescription> getIssues( |
287 | |
ResourceDefinitionError ofType) { |
288 | 0 | return issues.get(ofType); |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
@Override |
297 | |
public String toString() { |
298 | 0 | return "LicenseAnalyst [directories=" + directories + "]"; |
299 | |
} |
300 | |
} |