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