Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JarIdentificationAnalysis |
|
| 4.2;4.2 |
1 | package org.apache.maven.shared.jar.identification; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * Analyze the JAR file to identify Maven artifact metadata. This class is thread safe and immutable as long as all | |
31 | * provided exposers are, as it retains no state. | |
32 | * <p/> | |
33 | * If using Plexus, the class will use all available exposers in the container. | |
34 | * <p/> | |
35 | * If not using Plexus, the exposers must be set using {@link #setExposers(java.util.List)} before calling | |
36 | * {@link #analyze(org.apache.maven.shared.jar.JarAnalyzer)} | |
37 | * <p/> | |
38 | * Note that you must first create an instance of {@link org.apache.maven.shared.jar.JarAnalyzer} - see its Javadoc for | |
39 | * a typical use. | |
40 | * | |
41 | * @plexus.component role="org.apache.maven.shared.jar.identification.JarIdentificationAnalysis" role-hint="default" | |
42 | */ | |
43 | 0 | public class JarIdentificationAnalysis |
44 | { | |
45 | /** | |
46 | * The Maven information exposers to use during identification. | |
47 | * | |
48 | * @plexus.requirement role="org.apache.maven.shared.jar.identification.JarIdentificationExposer" | |
49 | */ | |
50 | private List exposers; | |
51 | ||
52 | /** | |
53 | * Analyze a JAR and find any associated Maven metadata. Note that if the provided JAR analyzer has previously | |
54 | * analyzed the JAR, the cached results will be returned. You must obtain a new JAR analyzer to the re-read the | |
55 | * contents of the file. | |
56 | * | |
57 | * @param jarAnalyzer the JAR to analyze. This must not yet have been closed. | |
58 | * @return the Maven metadata discovered | |
59 | */ | |
60 | public JarIdentification analyze( JarAnalyzer jarAnalyzer ) | |
61 | { | |
62 | 0 | JarIdentification taxon = jarAnalyzer.getJarData().getJarIdentification(); |
63 | 0 | if ( taxon != null ) |
64 | { | |
65 | 0 | return taxon; |
66 | } | |
67 | ||
68 | 0 | taxon = new JarIdentification(); |
69 | ||
70 | 0 | for ( Iterator i = exposers.iterator(); i.hasNext(); ) |
71 | { | |
72 | 0 | JarIdentificationExposer exposer = (JarIdentificationExposer) i.next(); |
73 | 0 | exposer.expose( taxon, jarAnalyzer ); |
74 | 0 | } |
75 | ||
76 | 0 | normalize( taxon ); |
77 | ||
78 | 0 | jarAnalyzer.getJarData().setJarIdentification( taxon ); |
79 | ||
80 | 0 | return taxon; |
81 | } | |
82 | ||
83 | private void normalize( JarIdentification taxon ) | |
84 | { | |
85 | 0 | if ( StringUtils.isEmpty( taxon.getGroupId() ) ) |
86 | { | |
87 | 0 | taxon.setGroupId( pickSmallest( taxon.getPotentialGroupIds() ) ); |
88 | } | |
89 | ||
90 | 0 | if ( StringUtils.isEmpty( taxon.getArtifactId() ) ) |
91 | { | |
92 | 0 | taxon.setArtifactId( pickLargest( taxon.getPotentialArtifactIds() ) ); |
93 | } | |
94 | ||
95 | 0 | if ( StringUtils.isEmpty( taxon.getVersion() ) ) |
96 | { | |
97 | 0 | taxon.setVersion( pickSmallest( taxon.getPotentialVersions() ) ); |
98 | } | |
99 | ||
100 | 0 | if ( StringUtils.isEmpty( taxon.getName() ) ) |
101 | { | |
102 | 0 | taxon.setName( pickLargest( taxon.getPotentialNames() ) ); |
103 | } | |
104 | ||
105 | 0 | if ( StringUtils.isEmpty( taxon.getVendor() ) ) |
106 | { | |
107 | 0 | taxon.setVendor( pickLargest( taxon.getPotentialVendors() ) ); |
108 | } | |
109 | 0 | } |
110 | ||
111 | private String pickSmallest( List list ) | |
112 | { | |
113 | 0 | String smallest = null; |
114 | ||
115 | 0 | if ( !list.isEmpty() ) |
116 | { | |
117 | 0 | int size = Integer.MAX_VALUE; |
118 | 0 | Iterator it = list.iterator(); |
119 | 0 | while ( it.hasNext() ) |
120 | { | |
121 | 0 | String val = (String) it.next(); |
122 | ||
123 | 0 | if ( StringUtils.isNotEmpty( val ) ) |
124 | { | |
125 | 0 | if ( val.length() < size ) |
126 | { | |
127 | 0 | smallest = val; |
128 | 0 | size = val.length(); |
129 | } | |
130 | } | |
131 | 0 | } |
132 | } | |
133 | ||
134 | 0 | return smallest; |
135 | } | |
136 | ||
137 | private String pickLargest( List list ) | |
138 | { | |
139 | 0 | String largest = null; |
140 | 0 | if ( !list.isEmpty() ) |
141 | { | |
142 | 0 | int size = Integer.MIN_VALUE; |
143 | 0 | Iterator it = list.iterator(); |
144 | 0 | while ( it.hasNext() ) |
145 | { | |
146 | 0 | String val = (String) it.next(); |
147 | 0 | if ( StringUtils.isNotEmpty( val ) ) |
148 | { | |
149 | 0 | if ( val.length() > size ) |
150 | { | |
151 | 0 | largest = val; |
152 | 0 | size = val.length(); |
153 | } | |
154 | } | |
155 | 0 | } |
156 | } | |
157 | 0 | return largest; |
158 | } | |
159 | ||
160 | public void setExposers( List exposers ) | |
161 | { | |
162 | 0 | this.exposers = Collections.unmodifiableList( exposers ); |
163 | 0 | } |
164 | } |