]> source.dussan.org Git - archiva.git/blob
d946ef31770ca4da9a673326c758dcb23f021886
[archiva.git] /
1 package org.apache.maven.archiva.repository.project.readers;
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 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.CiManagement;
32 import org.apache.maven.archiva.model.Dependency;
33 import org.apache.maven.archiva.model.DependencyScope;
34 import org.apache.maven.archiva.model.Exclusion;
35 import org.apache.maven.archiva.model.Individual;
36 import org.apache.maven.archiva.model.IssueManagement;
37 import org.apache.maven.archiva.model.License;
38 import org.apache.maven.archiva.model.MailingList;
39 import org.apache.maven.archiva.model.Organization;
40 import org.apache.maven.archiva.model.ProjectRepository;
41 import org.apache.maven.archiva.model.Scm;
42 import org.apache.maven.archiva.model.VersionedReference;
43 import org.apache.maven.archiva.repository.project.ProjectModelReader;
44 import org.apache.maven.archiva.xml.XMLException;
45 import org.apache.maven.archiva.xml.XMLReader;
46 import org.dom4j.Element;
47
48 /**
49  * ProjectModel400Reader - read in modelVersion 4.0.0 pom files into archiva-model structures.
50  *
51  * @version $Id$
52  */
53 @SuppressWarnings("unchecked")
54 public class ProjectModel400Reader
55     implements ProjectModelReader
56 {
57     public ArchivaProjectModel read( File pomFile )
58         throws XMLException
59     {
60         XMLReader xml = new XMLReader( "project", pomFile );
61
62         ArchivaProjectModel model = new ArchivaProjectModel();
63
64         if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
65         {
66             // No namespace defined
67             // TODO: Output to monitor the problem with the Namespace.
68         }
69
70         xml.removeNamespaces();
71
72         Element project = xml.getElement( "//project" );
73
74         model.setGroupId( project.elementTextTrim( "groupId" ) );
75         model.setArtifactId( project.elementTextTrim( "artifactId" ) );
76         model.setVersion( project.elementTextTrim( "version" ) );
77         model.setName( project.elementTextTrim( "name" ) );
78         model.setDescription( project.elementTextTrim( "description" ) );
79         model.setUrl( project.elementTextTrim( "url" ) );
80
81         model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
82
83         model.setParentProject( getParentProject( xml ) );
84
85         model.setMailingLists( getMailingLists( xml ) );
86         model.setCiManagement( getCiManagement( xml ) );
87         model.setIndividuals( getIndividuals( xml ) );
88         model.setIssueManagement( getIssueManagement( xml ) );
89         model.setLicenses( getLicenses( xml ) );
90         model.setOrganization( getOrganization( xml ) );
91         model.setScm( getSCM( xml ) );
92         model.setRepositories( getRepositories( xml ) );
93
94         model.setDependencies( getDependencies( xml ) );
95         model.setDependencyManagement( getDependencyManagement( xml ) );
96         model.setPlugins( getPlugins( xml ) );
97         model.setReports( getReports( xml ) );
98         model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
99
100         model.setBuildExtensions( getBuildExtensions( xml ) );
101
102         model.setRelocation( getRelocation( xml ) );
103
104         model.setOrigin( "filesystem" );
105
106         return model;
107     }
108
109     private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
110     {
111         ArtifactReference reference = new ArtifactReference();
112
113         reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
114         reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
115         reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
116         reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
117         reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
118
119         return reference;
120     }
121
122     /**
123      * Get List of {@link ArtifactReference} objects from xpath expr.
124      */
125     private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
126         throws XMLException
127     {
128         List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
129
130         Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
131         while ( it.hasNext() )
132         {
133             Element elemPlugin = it.next();
134
135             refs.add( getArtifactReference( elemPlugin, defaultType ) );
136         }
137
138         return refs;
139     }
140
141     private List<ArtifactReference> getBuildExtensions( XMLReader xml )
142         throws XMLException
143     {
144         return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
145     }
146
147     private CiManagement getCiManagement( XMLReader xml )
148         throws XMLException
149     {
150         Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
151         if ( elemCiMgmt != null )
152         {
153             CiManagement ciManagement = new CiManagement();
154             ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
155             ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
156             return ciManagement;
157         }
158
159         return null;
160     }
161
162     private List<Dependency> getDependencies( XMLReader xml )
163         throws XMLException
164     {
165         return getDependencyList( xml, new String[] { "dependencies" } );
166     }
167
168     private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
169         throws XMLException
170     {
171         List<Dependency> dependencyList = new ArrayList<Dependency>();
172
173         Element project = xml.getElement( "//project" );
174
175         Element depsParent = project;
176
177         for ( String part : parts )
178         {
179             depsParent = depsParent.element( part );
180             if ( depsParent == null )
181             {
182                 return dependencyList;
183             }
184         }
185
186         Iterator<Element> it = depsParent.elementIterator( "dependency" );
187         while ( it.hasNext() )
188         {
189             Element elemDependency = it.next();
190             Dependency dependency = new Dependency();
191
192             dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
193             dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
194             dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
195
196             dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
197             dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
198             dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
199             // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
200             dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
201             if ( DependencyScope.isSystemScoped( dependency ) )
202             {
203                 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
204             }
205
206             dependency.setExclusions( getExclusions( elemDependency ) );
207
208             if ( dependencyList.contains( dependency ) )
209             {
210                 // TODO: throw into monitor as "duplicate dependency" issue.
211             }
212
213             dependencyList.add( dependency );
214         }
215
216         return dependencyList;
217     }
218
219     private List<Dependency> getDependencyManagement( XMLReader xml )
220         throws XMLException
221     {
222         return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
223     }
224
225     private List<Exclusion> getExclusions( Element elemDependency )
226     {
227         List<Exclusion> exclusions = new ArrayList<Exclusion>();
228
229         Element elemExclusions = elemDependency.element( "exclusions" );
230
231         if ( elemExclusions != null )
232         {
233             Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
234             while ( it.hasNext() )
235             {
236                 Element elemExclusion = it.next();
237                 Exclusion exclusion = new Exclusion();
238
239                 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
240                 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
241
242                 exclusions.add( exclusion );
243             }
244         }
245
246         return exclusions;
247     }
248
249     private List<Individual> getIndividuals( XMLReader xml )
250         throws XMLException
251     {
252         List<Individual> individuals = new ArrayList<Individual>();
253
254         individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
255         individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
256
257         return individuals;
258     }
259
260     private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
261         throws XMLException
262     {
263         List<Individual> ret = new ArrayList<Individual>();
264
265         List<Element> modelPersonList = xml.getElementList( xpathExpr );
266
267         Iterator<Element> iter = modelPersonList.iterator();
268         while ( iter.hasNext() )
269         {
270             Element elemPerson = iter.next();
271             Individual individual = new Individual();
272
273             if ( isCommitor )
274             {
275                 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
276             }
277
278             individual.setCommitor( isCommitor );
279             individual.setEmail( elemPerson.elementTextTrim( "email" ) );
280             individual.setName( elemPerson.elementTextTrim( "name" ) );
281             individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
282             individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
283             individual.setUrl( elemPerson.elementTextTrim( "url" ) );
284             individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
285
286             // Roles
287             Element elemRoles = elemPerson.element( "roles" );
288             if ( elemRoles != null )
289             {
290                 List<Element> roleNames = elemRoles.elements( "role" );
291                 Iterator<Element> itRole = roleNames.iterator();
292                 while ( itRole.hasNext() )
293                 {
294                     Element role = itRole.next();
295                     individual.addRole( role.getTextTrim() );
296                 }
297             }
298
299             // Properties
300             individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
301
302             ret.add( individual );
303         }
304
305         return ret;
306     }
307
308     private IssueManagement getIssueManagement( XMLReader xml )
309         throws XMLException
310     {
311         Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
312         if ( elemIssueMgmt != null )
313         {
314             IssueManagement issueMgmt = new IssueManagement();
315
316             issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
317             issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
318
319             return issueMgmt;
320         }
321
322         return null;
323     }
324
325     private List<License> getLicenses( XMLReader xml )
326         throws XMLException
327     {
328         List<License> licenses = new ArrayList<License>();
329
330         Element elemLicenses = xml.getElement( "//project/licenses" );
331
332         if ( elemLicenses != null )
333         {
334             List<Element> licenseList = elemLicenses.elements( "license" );
335             for ( Element elemLicense : licenseList )
336             {
337                 License license = new License();
338
339                 // TODO: Create LicenseIdentity class to managed license ids.
340                 // license.setId( elemLicense.elementTextTrim("id") );
341                 license.setName( elemLicense.elementTextTrim( "name" ) );
342                 license.setUrl( elemLicense.elementTextTrim( "url" ) );
343                 license.setComments( elemLicense.elementTextTrim( "comments" ) );
344
345                 licenses.add( license );
346             }
347         }
348
349         return licenses;
350     }
351
352     private List<MailingList> getMailingLists( XMLReader xml )
353         throws XMLException
354     {
355         List<MailingList> mailingLists = new ArrayList<MailingList>();
356
357         List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
358         for ( Element elemMailingList : mailingListElems )
359         {
360             MailingList mlist = new MailingList();
361
362             mlist.setName( elemMailingList.elementTextTrim( "name" ) );
363             mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
364             mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
365             mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
366             mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
367
368             Element elemOtherArchives = elemMailingList.element( "otherArchives" );
369             if ( elemOtherArchives != null )
370             {
371                 List<String> otherArchives = new ArrayList<String>();
372                 List<Element> others = elemOtherArchives.elements( "otherArchive" );
373                 for ( Element other : others )
374                 {
375                     String otherArchive = other.getTextTrim();
376                     otherArchives.add( otherArchive );
377                 }
378
379                 mlist.setOtherArchives( otherArchives );
380             }
381
382             mailingLists.add( mlist );
383         }
384
385         return mailingLists;
386     }
387
388     private Organization getOrganization( XMLReader xml )
389         throws XMLException
390     {
391         Element elemOrg = xml.getElement( "//project/organization" );
392         if ( elemOrg != null )
393         {
394             Organization org = new Organization();
395
396             org.setName( elemOrg.elementTextTrim( "name" ) );
397             org.setUrl( elemOrg.elementTextTrim( "url" ) );
398
399             return org;
400         }
401
402         return null;
403     }
404
405     private VersionedReference getParentProject( XMLReader xml )
406         throws XMLException
407     {
408         Element elemParent = xml.getElement( "//project/parent" );
409
410         if ( elemParent != null )
411         {
412             return getVersionedReference( elemParent );
413         }
414
415         return null;
416     }
417
418     private List<ArtifactReference> getPlugins( XMLReader xml )
419         throws XMLException
420     {
421         return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
422     }
423
424     private Properties getProperties( Element elemProperties )
425     {
426         if ( elemProperties == null )
427         {
428             return null;
429         }
430
431         Properties ret = new Properties();
432
433         Iterator<Element> itProps = elemProperties.elements().iterator();
434         while ( itProps.hasNext() )
435         {
436             Element elemProp = (Element) itProps.next();
437             ret.setProperty( elemProp.getName(), elemProp.getText() );
438         }
439
440         return ret;
441     }
442
443     private VersionedReference getRelocation( XMLReader xml )
444         throws XMLException
445     {
446         Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
447
448         if ( elemRelocation != null )
449         {
450             return getVersionedReference( elemRelocation );
451         }
452
453         return null;
454     }
455
456     private List<ArtifactReference> getReports( XMLReader xml )
457         throws XMLException
458     {
459         return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
460     }
461
462     private List<ProjectRepository> getRepositories( XMLReader xml )
463         throws XMLException
464     {
465         List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
466
467         repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
468         repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
469
470         return repos;
471     }
472
473     private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
474         throws XMLException
475     {
476         List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
477
478         List<Element> repositoriesList = xml.getElementList( xpathExpr );
479
480         for ( Element elemRepo : repositoriesList )
481         {
482             ProjectRepository repo = new ProjectRepository();
483
484             repo.setId( elemRepo.elementTextTrim( "id" ) );
485             repo.setName( elemRepo.elementTextTrim( "name" ) );
486             repo.setUrl( elemRepo.elementTextTrim( "url" ) );
487             repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
488             repo.setPlugins( isPluginRepo );
489
490             repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
491             repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
492
493             ret.add( repo );
494         }
495
496         return ret;
497     }
498
499     private Scm getSCM( XMLReader xml )
500         throws XMLException
501     {
502         Element elemScm = xml.getElement( "//project/scm" );
503
504         if ( elemScm != null )
505         {
506             Scm scm = new Scm();
507
508             scm.setConnection( elemScm.elementTextTrim( "connection" ) );
509             scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
510             scm.setUrl( elemScm.elementTextTrim( "url" ) );
511
512             return scm;
513         }
514
515         return null;
516     }
517
518     private VersionedReference getVersionedReference( Element elem )
519     {
520         VersionedReference reference = new VersionedReference();
521
522         reference.setGroupId( elem.elementTextTrim( "groupId" ) );
523         reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
524         reference.setVersion( elem.elementTextTrim( "version" ) );
525
526         return reference;
527     }
528
529     private boolean toBoolean( String value, boolean defaultValue )
530     {
531         if ( StringUtils.equalsIgnoreCase( value, "true" ) )
532         {
533             return true;
534         }
535         else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
536         {
537             return false;
538         }
539         else
540         {
541             // If unset, or not "true" or "false".
542             return defaultValue;
543         }
544     }
545 }