1 package org.apache.maven.archiva.repository.project.readers;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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.Dependency;
26 import org.apache.maven.archiva.model.Individual;
27 import org.apache.maven.archiva.model.IssueManagement;
28 import org.apache.maven.archiva.model.License;
29 import org.apache.maven.archiva.model.MailingList;
30 import org.apache.maven.archiva.model.Organization;
31 import org.apache.maven.archiva.model.ProjectRepository;
32 import org.apache.maven.archiva.model.Scm;
33 import org.apache.maven.archiva.repository.project.ProjectModelException;
34 import org.apache.maven.archiva.repository.project.ProjectModelReader;
35 import org.apache.maven.archiva.xml.XMLException;
36 import org.apache.maven.archiva.xml.XMLReader;
37 import org.dom4j.Element;
40 import java.util.ArrayList;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Properties;
46 * ProjectModel300Reader
50 public class ProjectModel300Reader
51 implements ProjectModelReader
54 public ArchivaProjectModel read( File pomFile )
55 throws ProjectModelException
59 XMLReader xml = new XMLReader( "project", pomFile );
61 ArchivaProjectModel model = new ArchivaProjectModel();
63 xml.removeNamespaces();
65 Element project = xml.getElement( "//project" );
67 // TODO: Handle <extend /> ?? (is this even possible?)
69 model.setGroupId( project.elementTextTrim( "groupId" ) );
70 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
71 // TODO: Handle <id />
72 model.setVersion( project.elementTextTrim( "currentVersion" ) );
73 model.setName( project.elementTextTrim( "name" ) );
74 model.setDescription( project.elementTextTrim( "description" ) );
75 // TODO: what to do with <shortDescription /> ?
76 model.setUrl( project.elementTextTrim( "url" ) );
77 // TODO: Handle <logo />
78 // TODO: Handle <inceptionYear />
80 model.setIssueManagement( getIssueManagement( xml ) );
81 // TODO: What to do with <gumpRepositoryId /> ?
82 // TODO: Handle <siteAddress />
83 // TODO: Handle <siteDirectory /> ?
84 // TODO: Handle <distributionSite />
85 // TODO: Handle <distributionDirectory />
86 model.setMailingLists( getMailingLists( xml ) );
87 model.setIndividuals( getIndividuals( xml ) );
88 model.setLicenses( getLicenses( xml ) );
89 model.setReports( getReports( xml ) );
90 model.setRepositories( getRepositories( xml ) );
91 model.setScm( getSCM( xml ) );
92 model.setOrganization( getOrganization( xml ) );
93 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
94 model.setDependencies( getDependencies( xml ) );
96 model.setOrigin("filesystem");
98 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
100 * model.setDependencyManagement()
102 * model.setParentProject()
103 * model.setPackaging()
104 * model.setCiManagement()
105 * model.setBuildExtensions()
106 * model.setRelocation()
111 catch ( XMLException e )
113 throw new ProjectModelException( e.getMessage(), e );
117 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
119 ArtifactReference reference = new ArtifactReference();
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 ) );
131 * Get List of {@link ArtifactReference} objects from xpath expr.
133 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
136 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
138 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
139 while ( it.hasNext() )
141 Element elemPlugin = it.next();
143 refs.add( getArtifactReference( elemPlugin, defaultType ) );
149 private List<Dependency> getDependencies( XMLReader xml )
152 return getDependencyList( xml, new String[] { "dependencies" } );
155 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
158 List<Dependency> dependencyList = new ArrayList<Dependency>();
160 Element project = xml.getElement( "//project" );
162 Element depsParent = project;
164 for ( String part : parts )
166 depsParent = depsParent.element( part );
167 if ( depsParent == null )
169 return dependencyList;
173 Iterator<Element> it = depsParent.elementIterator( "dependency" );
174 while ( it.hasNext() )
176 Element elemDependency = it.next();
177 Dependency dependency = new Dependency();
179 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
180 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
181 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
183 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
184 dependency.setUrl( elemDependency.elementTextTrim( "url" ) );
185 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
187 * dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
188 * dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
189 * dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
192 dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
194 if ( dependencyList.contains( dependency ) )
196 // TODO: throw into monitor as "duplicate dependency" issue.
199 dependencyList.add( dependency );
202 return dependencyList;
205 private List<Individual> getIndividuals( XMLReader xml )
208 List<Individual> individuals = new ArrayList<Individual>();
210 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
211 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
216 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
219 List<Individual> ret = new ArrayList<Individual>();
221 List<Element> modelPersonList = xml.getElementList( xpathExpr );
223 Iterator<Element> iter = modelPersonList.iterator();
224 while ( iter.hasNext() )
226 Element elemPerson = iter.next();
227 Individual individual = new Individual();
231 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
234 individual.setCommitor( isCommitor );
235 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
236 individual.setName( elemPerson.elementTextTrim( "name" ) );
237 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
238 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
239 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
240 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
243 Element elemRoles = elemPerson.element( "roles" );
244 if ( elemRoles != null )
246 List<Element> roleNames = elemRoles.elements( "role" );
247 Iterator<Element> itRole = roleNames.iterator();
248 while ( itRole.hasNext() )
250 Element role = itRole.next();
251 individual.addRole( role.getTextTrim() );
256 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
258 ret.add( individual );
264 private IssueManagement getIssueManagement( XMLReader xml )
267 Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
269 if ( issueTrackingUrlElem == null )
274 String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
275 if ( StringUtils.isBlank( issueTrackingUrl ) )
280 IssueManagement issueMgmt = new IssueManagement();
281 issueMgmt.setUrl( issueTrackingUrl );
286 private List<License> getLicenses( XMLReader xml )
289 List<License> licenses = new ArrayList<License>();
291 Element elemLicenses = xml.getElement( "//project/licenses" );
293 if ( elemLicenses != null )
295 List<Element> licenseList = elemLicenses.elements( "license" );
296 for ( Element elemLicense : licenseList )
298 License license = new License();
300 // TODO: Create LicenseIdentity class to managed license ids.
301 // license.setId( elemLicense.elementTextTrim("id") );
302 license.setName( elemLicense.elementTextTrim( "name" ) );
303 license.setUrl( elemLicense.elementTextTrim( "url" ) );
304 license.setComments( elemLicense.elementTextTrim( "comments" ) );
306 licenses.add( license );
313 private List<MailingList> getMailingLists( XMLReader xml )
316 List<MailingList> mailingLists = new ArrayList<MailingList>();
318 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
319 for ( Element elemMailingList : mailingListElems )
321 MailingList mlist = new MailingList();
323 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
324 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
325 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
326 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
327 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
329 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
330 if ( elemOtherArchives != null )
332 List<String> otherArchives = new ArrayList<String>();
333 List<Element> others = elemOtherArchives.elements( "otherArchive" );
334 for ( Element other : others )
336 String otherArchive = other.getTextTrim();
337 otherArchives.add( otherArchive );
340 mlist.setOtherArchives( otherArchives );
343 mailingLists.add( mlist );
349 private Organization getOrganization( XMLReader xml )
352 Element elemOrg = xml.getElement( "//project/organization" );
353 if ( elemOrg != null )
355 Organization org = new Organization();
357 org.setName( elemOrg.elementTextTrim( "name" ) );
358 org.setUrl( elemOrg.elementTextTrim( "url" ) );
359 // TODO: Handle <logo />
367 private Properties getProperties( Element elemProperties )
369 if ( elemProperties == null )
374 Properties ret = new Properties();
376 Iterator<Element> itProps = elemProperties.elements().iterator();
377 while ( itProps.hasNext() )
379 Element elemProp = (Element) itProps.next();
380 ret.setProperty( elemProp.getName(), elemProp.getText() );
386 private List<ArtifactReference> getReports( XMLReader xml )
389 return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
392 private List<ProjectRepository> getRepositories( XMLReader xml )
395 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
397 // Repositories are not stored within the maven 1 pom.
402 private Scm getSCM( XMLReader xml )
405 Element elemScm = xml.getElement( "//project/repository" );
407 if ( elemScm != null )
411 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
412 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
413 scm.setUrl( elemScm.elementTextTrim( "url" ) );