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