1 | |
package org.apache.maven.archiva.repository.project.readers; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Properties; |
27 | |
|
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.apache.maven.archiva.model.ArchivaProjectModel; |
30 | |
import org.apache.maven.archiva.model.ArtifactReference; |
31 | |
import org.apache.maven.archiva.model.Dependency; |
32 | |
import org.apache.maven.archiva.model.Individual; |
33 | |
import org.apache.maven.archiva.model.IssueManagement; |
34 | |
import org.apache.maven.archiva.model.License; |
35 | |
import org.apache.maven.archiva.model.MailingList; |
36 | |
import org.apache.maven.archiva.model.Organization; |
37 | |
import org.apache.maven.archiva.model.ProjectRepository; |
38 | |
import org.apache.maven.archiva.model.Scm; |
39 | |
import org.apache.maven.archiva.repository.project.ProjectModelReader; |
40 | |
import org.apache.maven.archiva.xml.XMLException; |
41 | |
import org.apache.maven.archiva.xml.XMLReader; |
42 | |
import org.dom4j.Element; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class ProjectModel300Reader |
50 | |
implements ProjectModelReader |
51 | |
{ |
52 | |
|
53 | |
public ArchivaProjectModel read( File pomFile ) |
54 | |
throws XMLException |
55 | |
{ |
56 | 0 | XMLReader xml = new XMLReader( "project", pomFile ); |
57 | |
|
58 | 0 | ArchivaProjectModel model = new ArchivaProjectModel(); |
59 | |
|
60 | 0 | xml.removeNamespaces(); |
61 | |
|
62 | 0 | Element project = xml.getElement( "//project" ); |
63 | |
|
64 | |
|
65 | |
|
66 | 0 | model.setGroupId( project.elementTextTrim( "groupId" ) ); |
67 | 0 | model.setArtifactId( project.elementTextTrim( "artifactId" ) ); |
68 | |
|
69 | 0 | model.setVersion( project.elementTextTrim( "currentVersion" ) ); |
70 | 0 | model.setName( project.elementTextTrim( "name" ) ); |
71 | 0 | model.setDescription( project.elementTextTrim( "description" ) ); |
72 | |
|
73 | 0 | model.setUrl( project.elementTextTrim( "url" ) ); |
74 | |
|
75 | |
|
76 | |
|
77 | 0 | model.setIssueManagement( getIssueManagement( xml ) ); |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 0 | model.setMailingLists( getMailingLists( xml ) ); |
84 | 0 | model.setIndividuals( getIndividuals( xml ) ); |
85 | 0 | model.setLicenses( getLicenses( xml ) ); |
86 | 0 | model.setReports( getReports( xml ) ); |
87 | 0 | model.setRepositories( getRepositories( xml ) ); |
88 | 0 | model.setScm( getSCM( xml ) ); |
89 | 0 | model.setOrganization( getOrganization( xml ) ); |
90 | 0 | model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) ); |
91 | 0 | model.setDependencies( getDependencies( xml ) ); |
92 | |
|
93 | 0 | model.setOrigin( "filesystem" ); |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 0 | return model; |
102 | |
} |
103 | |
|
104 | |
private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType ) |
105 | |
{ |
106 | 0 | ArtifactReference reference = new ArtifactReference(); |
107 | |
|
108 | 0 | reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) ); |
109 | 0 | reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) ); |
110 | 0 | reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) ); |
111 | 0 | reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) ); |
112 | 0 | reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) ); |
113 | |
|
114 | 0 | return reference; |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType ) |
121 | |
throws XMLException |
122 | |
{ |
123 | 0 | List<ArtifactReference> refs = new ArrayList<ArtifactReference>(); |
124 | |
|
125 | 0 | Iterator<Element> it = xml.getElementList( xpathExpr ).iterator(); |
126 | 0 | while ( it.hasNext() ) |
127 | |
{ |
128 | 0 | Element elemPlugin = it.next(); |
129 | |
|
130 | 0 | refs.add( getArtifactReference( elemPlugin, defaultType ) ); |
131 | 0 | } |
132 | |
|
133 | 0 | return refs; |
134 | |
} |
135 | |
|
136 | |
private List<Dependency> getDependencies( XMLReader xml ) |
137 | |
throws XMLException |
138 | |
{ |
139 | 0 | return getDependencyList( xml, new String[] { "dependencies" } ); |
140 | |
} |
141 | |
|
142 | |
@SuppressWarnings("unchecked") |
143 | |
private List<Dependency> getDependencyList( XMLReader xml, String parts[] ) |
144 | |
throws XMLException |
145 | |
{ |
146 | 0 | List<Dependency> dependencyList = new ArrayList<Dependency>(); |
147 | |
|
148 | 0 | Element project = xml.getElement( "//project" ); |
149 | |
|
150 | 0 | Element depsParent = project; |
151 | |
|
152 | 0 | for ( String part : parts ) |
153 | |
{ |
154 | 0 | depsParent = depsParent.element( part ); |
155 | 0 | if ( depsParent == null ) |
156 | |
{ |
157 | 0 | return dependencyList; |
158 | |
} |
159 | |
} |
160 | |
|
161 | 0 | Iterator<Element> it = depsParent.elementIterator( "dependency" ); |
162 | 0 | while ( it.hasNext() ) |
163 | |
{ |
164 | 0 | Element elemDependency = it.next(); |
165 | 0 | Dependency dependency = new Dependency(); |
166 | |
|
167 | 0 | dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) ); |
168 | 0 | dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) ); |
169 | 0 | dependency.setVersion( elemDependency.elementTextTrim( "version" ) ); |
170 | |
|
171 | 0 | dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) ); |
172 | 0 | dependency.setUrl( elemDependency.elementTextTrim( "url" ) ); |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | 0 | dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) ); |
181 | |
|
182 | 0 | if ( dependencyList.contains( dependency ) ) |
183 | |
{ |
184 | |
|
185 | |
} |
186 | |
|
187 | 0 | dependencyList.add( dependency ); |
188 | 0 | } |
189 | |
|
190 | 0 | return dependencyList; |
191 | |
} |
192 | |
|
193 | |
private List<Individual> getIndividuals( XMLReader xml ) |
194 | |
throws XMLException |
195 | |
{ |
196 | 0 | List<Individual> individuals = new ArrayList<Individual>(); |
197 | |
|
198 | 0 | individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) ); |
199 | 0 | individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) ); |
200 | |
|
201 | 0 | return individuals; |
202 | |
} |
203 | |
|
204 | |
@SuppressWarnings("unchecked") |
205 | |
private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr ) |
206 | |
throws XMLException |
207 | |
{ |
208 | 0 | List<Individual> ret = new ArrayList<Individual>(); |
209 | |
|
210 | 0 | List<Element> modelPersonList = xml.getElementList( xpathExpr ); |
211 | |
|
212 | 0 | Iterator<Element> iter = modelPersonList.iterator(); |
213 | 0 | while ( iter.hasNext() ) |
214 | |
{ |
215 | 0 | Element elemPerson = iter.next(); |
216 | 0 | Individual individual = new Individual(); |
217 | |
|
218 | 0 | if ( isCommitor ) |
219 | |
{ |
220 | 0 | individual.setPrincipal( elemPerson.elementTextTrim( "id" ) ); |
221 | |
} |
222 | |
|
223 | 0 | individual.setCommitor( isCommitor ); |
224 | 0 | individual.setEmail( elemPerson.elementTextTrim( "email" ) ); |
225 | 0 | individual.setName( elemPerson.elementTextTrim( "name" ) ); |
226 | 0 | individual.setOrganization( elemPerson.elementTextTrim( "organization" ) ); |
227 | 0 | individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) ); |
228 | 0 | individual.setUrl( elemPerson.elementTextTrim( "url" ) ); |
229 | 0 | individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) ); |
230 | 0 | individual.setIndividualEmail( elemPerson.elementTextTrim( "email" ) ); |
231 | |
|
232 | |
|
233 | 0 | Element elemRoles = elemPerson.element( "roles" ); |
234 | 0 | if ( elemRoles != null ) |
235 | |
{ |
236 | 0 | List<Element> roleNames = elemRoles.elements( "role" ); |
237 | 0 | Iterator<Element> itRole = roleNames.iterator(); |
238 | 0 | while ( itRole.hasNext() ) |
239 | |
{ |
240 | 0 | Element role = itRole.next(); |
241 | 0 | individual.addRole( role.getTextTrim() ); |
242 | 0 | } |
243 | |
} |
244 | |
|
245 | |
|
246 | 0 | individual.setProperties( getProperties( elemPerson.element( "properties" ) ) ); |
247 | |
|
248 | 0 | ret.add( individual ); |
249 | 0 | } |
250 | |
|
251 | 0 | return ret; |
252 | |
} |
253 | |
|
254 | |
private IssueManagement getIssueManagement( XMLReader xml ) |
255 | |
throws XMLException |
256 | |
{ |
257 | 0 | Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" ); |
258 | |
|
259 | 0 | if ( issueTrackingUrlElem == null ) |
260 | |
{ |
261 | 0 | return null; |
262 | |
} |
263 | |
|
264 | 0 | String issueTrackingUrl = issueTrackingUrlElem.getTextTrim(); |
265 | 0 | if ( StringUtils.isBlank( issueTrackingUrl ) ) |
266 | |
{ |
267 | 0 | return null; |
268 | |
} |
269 | |
|
270 | 0 | IssueManagement issueMgmt = new IssueManagement(); |
271 | 0 | issueMgmt.setUrl( issueTrackingUrl ); |
272 | 0 | issueMgmt.setIssueManagementUrl( issueTrackingUrl ); |
273 | |
|
274 | 0 | return issueMgmt; |
275 | |
} |
276 | |
|
277 | |
@SuppressWarnings("unchecked") |
278 | |
private List<License> getLicenses( XMLReader xml ) |
279 | |
throws XMLException |
280 | |
{ |
281 | 0 | List<License> licenses = new ArrayList<License>(); |
282 | |
|
283 | 0 | Element elemLicenses = xml.getElement( "//project/licenses" ); |
284 | |
|
285 | 0 | if ( elemLicenses != null ) |
286 | |
{ |
287 | 0 | List<Element> licenseList = elemLicenses.elements( "license" ); |
288 | 0 | for ( Element elemLicense : licenseList ) |
289 | |
{ |
290 | 0 | License license = new License(); |
291 | |
|
292 | |
|
293 | |
|
294 | 0 | license.setName( elemLicense.elementTextTrim( "name" ) ); |
295 | 0 | license.setUrl( elemLicense.elementTextTrim( "url" ) ); |
296 | 0 | license.setComments( elemLicense.elementTextTrim( "comments" ) ); |
297 | |
|
298 | 0 | licenses.add( license ); |
299 | 0 | } |
300 | |
} |
301 | |
|
302 | 0 | return licenses; |
303 | |
} |
304 | |
|
305 | |
@SuppressWarnings("unchecked") |
306 | |
private List<MailingList> getMailingLists( XMLReader xml ) |
307 | |
throws XMLException |
308 | |
{ |
309 | 0 | List<MailingList> mailingLists = new ArrayList<MailingList>(); |
310 | |
|
311 | 0 | List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" ); |
312 | 0 | for ( Element elemMailingList : mailingListElems ) |
313 | |
{ |
314 | 0 | MailingList mlist = new MailingList(); |
315 | |
|
316 | 0 | mlist.setName( elemMailingList.elementTextTrim( "name" ) ); |
317 | 0 | mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) ); |
318 | 0 | mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) ); |
319 | 0 | mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) ); |
320 | 0 | mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) ); |
321 | |
|
322 | 0 | Element elemOtherArchives = elemMailingList.element( "otherArchives" ); |
323 | 0 | if ( elemOtherArchives != null ) |
324 | |
{ |
325 | 0 | List<String> otherArchives = new ArrayList<String>(); |
326 | 0 | List<Element> others = elemOtherArchives.elements( "otherArchive" ); |
327 | 0 | for ( Element other : others ) |
328 | |
{ |
329 | 0 | String otherArchive = other.getTextTrim(); |
330 | 0 | otherArchives.add( otherArchive ); |
331 | 0 | } |
332 | |
|
333 | 0 | mlist.setOtherArchives( otherArchives ); |
334 | |
} |
335 | |
|
336 | 0 | mailingLists.add( mlist ); |
337 | 0 | } |
338 | |
|
339 | 0 | return mailingLists; |
340 | |
} |
341 | |
|
342 | |
private Organization getOrganization( XMLReader xml ) |
343 | |
throws XMLException |
344 | |
{ |
345 | 0 | Element elemOrg = xml.getElement( "//project/organization" ); |
346 | 0 | if ( elemOrg != null ) |
347 | |
{ |
348 | 0 | Organization org = new Organization(); |
349 | |
|
350 | 0 | org.setOrganizationName( elemOrg.elementTextTrim( "name" ) ); |
351 | 0 | org.setName( elemOrg.elementTextTrim( "name" ) ); |
352 | 0 | org.setUrl( elemOrg.elementTextTrim( "url" ) ); |
353 | |
|
354 | |
|
355 | 0 | return org; |
356 | |
} |
357 | |
|
358 | 0 | return null; |
359 | |
} |
360 | |
|
361 | |
@SuppressWarnings("unchecked") |
362 | |
private Properties getProperties( Element elemProperties ) |
363 | |
{ |
364 | 0 | if ( elemProperties == null ) |
365 | |
{ |
366 | 0 | return null; |
367 | |
} |
368 | |
|
369 | 0 | Properties ret = new Properties(); |
370 | |
|
371 | 0 | Iterator<Element> itProps = elemProperties.elements().iterator(); |
372 | 0 | while ( itProps.hasNext() ) |
373 | |
{ |
374 | 0 | Element elemProp = (Element) itProps.next(); |
375 | 0 | ret.setProperty( elemProp.getName(), elemProp.getText() ); |
376 | 0 | } |
377 | |
|
378 | 0 | return ret; |
379 | |
} |
380 | |
|
381 | |
private List<ArtifactReference> getReports( XMLReader xml ) |
382 | |
throws XMLException |
383 | |
{ |
384 | 0 | return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" ); |
385 | |
} |
386 | |
|
387 | |
private List<ProjectRepository> getRepositories( XMLReader xml ) |
388 | |
throws XMLException |
389 | |
{ |
390 | 0 | List<ProjectRepository> repos = new ArrayList<ProjectRepository>(); |
391 | |
|
392 | |
|
393 | |
|
394 | 0 | return repos; |
395 | |
} |
396 | |
|
397 | |
private Scm getSCM( XMLReader xml ) |
398 | |
throws XMLException |
399 | |
{ |
400 | 0 | Element elemScm = xml.getElement( "//project/repository" ); |
401 | |
|
402 | 0 | if ( elemScm != null ) |
403 | |
{ |
404 | 0 | Scm scm = new Scm(); |
405 | |
|
406 | 0 | scm.setConnection( elemScm.elementTextTrim( "connection" ) ); |
407 | 0 | scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) ); |
408 | 0 | scm.setUrl( elemScm.elementTextTrim( "url" ) ); |
409 | |
|
410 | 0 | return scm; |
411 | |
} |
412 | |
|
413 | 0 | return null; |
414 | |
} |
415 | |
} |