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