1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.jar.identification;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.List;
26
27 import org.apache.maven.shared.jar.JarAnalyzer;
28 import org.codehaus.plexus.util.StringUtils;
29
30 import static java.util.Objects.requireNonNull;
31
32
33
34
35
36
37
38
39
40
41 @Singleton
42 @Named
43 public class JarIdentificationAnalysis {
44
45
46
47 private final List<JarIdentificationExposer> exposers;
48
49 @Inject
50 public JarIdentificationAnalysis(List<JarIdentificationExposer> exposers) {
51 this.exposers = requireNonNull(exposers);
52 }
53
54
55
56
57
58
59
60
61
62 public JarIdentification analyze(JarAnalyzer jarAnalyzer) {
63 JarIdentification taxon = jarAnalyzer.getJarData().getJarIdentification();
64 if (taxon != null) {
65 return taxon;
66 }
67
68 taxon = new JarIdentification();
69
70 for (JarIdentificationExposer exposer : exposers) {
71 exposer.expose(taxon, jarAnalyzer);
72 }
73
74 normalize(taxon);
75
76 jarAnalyzer.getJarData().setJarIdentification(taxon);
77
78 return taxon;
79 }
80
81 private void normalize(JarIdentification taxon) {
82 if (StringUtils.isEmpty(taxon.getGroupId())) {
83 taxon.setGroupId(pickSmallest(taxon.getPotentialGroupIds()));
84 }
85
86 if (StringUtils.isEmpty(taxon.getArtifactId())) {
87 taxon.setArtifactId(pickLargest(taxon.getPotentialArtifactIds()));
88 }
89
90 if (StringUtils.isEmpty(taxon.getVersion())) {
91 taxon.setVersion(pickSmallest(taxon.getPotentialVersions()));
92 }
93
94 if (StringUtils.isEmpty(taxon.getName())) {
95 taxon.setName(pickLargest(taxon.getPotentialNames()));
96 }
97
98 if (StringUtils.isEmpty(taxon.getVendor())) {
99 taxon.setVendor(pickLargest(taxon.getPotentialVendors()));
100 }
101 }
102
103 private String pickSmallest(List<String> list) {
104 String smallest = null;
105
106 int size = Integer.MAX_VALUE;
107 for (String val : list) {
108 if (val != null && !val.isEmpty()) {
109 if (val.length() < size) {
110 smallest = val;
111 size = val.length();
112 }
113 }
114 }
115
116 return smallest;
117 }
118
119 private String pickLargest(List<String> list) {
120 String largest = null;
121 int size = Integer.MIN_VALUE;
122 for (String val : list) {
123 if (val != null && !val.isEmpty()) {
124 if (val.length() > size) {
125 largest = val;
126 size = val.length();
127 }
128 }
129 }
130 return largest;
131 }
132 }