1 package org.apache.maven.shared.jar.identification;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.jar.JarAnalyzer;
23 import org.codehaus.plexus.util.StringUtils;
24
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class JarIdentificationAnalysis
44 {
45
46
47
48
49
50 private List exposers;
51
52
53
54
55
56
57
58
59
60 public JarIdentification analyze( JarAnalyzer jarAnalyzer )
61 {
62 JarIdentification taxon = jarAnalyzer.getJarData().getJarIdentification();
63 if ( taxon != null )
64 {
65 return taxon;
66 }
67
68 taxon = new JarIdentification();
69
70 for ( Iterator i = exposers.iterator(); i.hasNext(); )
71 {
72 JarIdentificationExposer exposer = (JarIdentificationExposer) i.next();
73 exposer.expose( taxon, jarAnalyzer );
74 }
75
76 normalize( taxon );
77
78 jarAnalyzer.getJarData().setJarIdentification( taxon );
79
80 return taxon;
81 }
82
83 private void normalize( JarIdentification taxon )
84 {
85 if ( StringUtils.isEmpty( taxon.getGroupId() ) )
86 {
87 taxon.setGroupId( pickSmallest( taxon.getPotentialGroupIds() ) );
88 }
89
90 if ( StringUtils.isEmpty( taxon.getArtifactId() ) )
91 {
92 taxon.setArtifactId( pickLargest( taxon.getPotentialArtifactIds() ) );
93 }
94
95 if ( StringUtils.isEmpty( taxon.getVersion() ) )
96 {
97 taxon.setVersion( pickSmallest( taxon.getPotentialVersions() ) );
98 }
99
100 if ( StringUtils.isEmpty( taxon.getName() ) )
101 {
102 taxon.setName( pickLargest( taxon.getPotentialNames() ) );
103 }
104
105 if ( StringUtils.isEmpty( taxon.getVendor() ) )
106 {
107 taxon.setVendor( pickLargest( taxon.getPotentialVendors() ) );
108 }
109 }
110
111 private String pickSmallest( List list )
112 {
113 String smallest = null;
114
115 if ( !list.isEmpty() )
116 {
117 int size = Integer.MAX_VALUE;
118 Iterator it = list.iterator();
119 while ( it.hasNext() )
120 {
121 String val = (String) it.next();
122
123 if ( StringUtils.isNotEmpty( val ) )
124 {
125 if ( val.length() < size )
126 {
127 smallest = val;
128 size = val.length();
129 }
130 }
131 }
132 }
133
134 return smallest;
135 }
136
137 private String pickLargest( List list )
138 {
139 String largest = null;
140 if ( !list.isEmpty() )
141 {
142 int size = Integer.MIN_VALUE;
143 Iterator it = list.iterator();
144 while ( it.hasNext() )
145 {
146 String val = (String) it.next();
147 if ( StringUtils.isNotEmpty( val ) )
148 {
149 if ( val.length() > size )
150 {
151 largest = val;
152 size = val.length();
153 }
154 }
155 }
156 }
157 return largest;
158 }
159
160 public void setExposers( List exposers )
161 {
162 this.exposers = Collections.unmodifiableList( exposers );
163 }
164 }