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