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
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 * role="org.apache.maven.archiva.repository.project.ProjectModelReader"
53 * role-hint="model300"
55 public class ProjectModel300Reader
56 implements ProjectModelReader
59 public ArchivaProjectModel read( File pomFile )
60 throws ProjectModelException
64 XMLReader xml = new XMLReader( "project", pomFile );
66 ArchivaProjectModel model = new ArchivaProjectModel();
68 xml.removeNamespaces();
70 Element project = xml.getElement( "//project" );
72 // TODO: Handle <extend /> ?? (is this even possible?)
74 model.setGroupId( project.elementTextTrim( "groupId" ) );
75 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
76 // TODO: Handle <id />
77 model.setVersion( project.elementTextTrim( "currentVersion" ) );
78 model.setName( project.elementTextTrim( "name" ) );
79 model.setDescription( project.elementTextTrim( "description" ) );
80 // TODO: what to do with <shortDescription /> ?
81 model.setUrl( project.elementTextTrim( "url" ) );
82 // TODO: Handle <logo />
83 // TODO: Handle <inceptionYear />
85 model.setIssueManagement( getIssueManagement( xml ) );
86 // TODO: What to do with <gumpRepositoryId /> ?
87 // TODO: Handle <siteAddress />
88 // TODO: Handle <siteDirectory /> ?
89 // TODO: Handle <distributionSite />
90 // TODO: Handle <distributionDirectory />
91 model.setMailingLists( getMailingLists( xml ) );
92 model.setIndividuals( getIndividuals( xml ) );
93 model.setLicenses( getLicenses( xml ) );
94 model.setReports( getReports( xml ) );
95 model.setRepositories( getRepositories( xml ) );
96 model.setScm( getSCM( xml ) );
97 model.setOrganization( getOrganization( xml ) );
98 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
99 model.setDependencies( getDependencies( xml ) );
101 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
103 * model.setDependencyManagement()
105 * model.setParentProject()
106 * model.setPackaging()
107 * model.setCiManagement()
108 * model.setBuildExtensions()
109 * model.setRelocation()
114 catch ( XMLException e )
116 throw new ProjectModelException( e.getMessage(), e );
120 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
122 ArtifactReference reference = new ArtifactReference();
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 ) );
134 * Get List of {@link ArtifactReference} objects from xpath expr.
136 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
139 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
141 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
142 while ( it.hasNext() )
144 Element elemPlugin = it.next();
146 refs.add( getArtifactReference( elemPlugin, defaultType ) );
152 private List<Dependency> getDependencies( XMLReader xml )
155 return getDependencyList( xml, new String[] { "dependencies" } );
158 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
161 List<Dependency> dependencyList = new ArrayList<Dependency>();
163 Element project = xml.getElement( "//project" );
165 Element depsParent = project;
167 for ( String part : parts )
169 depsParent = depsParent.element( part );
170 if ( depsParent == null )
172 return dependencyList;
176 Iterator<Element> it = depsParent.elementIterator( "dependency" );
177 while ( it.hasNext() )
179 Element elemDependency = it.next();
180 Dependency dependency = new Dependency();
182 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
183 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
184 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
186 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
187 dependency.setUrl( elemDependency.elementTextTrim( "url" ) );
188 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
190 * dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
191 * dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
192 * dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
195 dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
197 if ( dependencyList.contains( dependency ) )
199 // TODO: throw into monitor as "duplicate dependency" issue.
202 dependencyList.add( dependency );
205 return dependencyList;
208 private List<Individual> getIndividuals( XMLReader xml )
211 List<Individual> individuals = new ArrayList<Individual>();
213 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
214 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
219 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
222 List<Individual> ret = new ArrayList<Individual>();
224 List<Element> modelPersonList = xml.getElementList( xpathExpr );
226 Iterator<Element> iter = modelPersonList.iterator();
227 while ( iter.hasNext() )
229 Element elemPerson = iter.next();
230 Individual individual = new Individual();
234 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
237 individual.setCommitor( isCommitor );
238 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
239 individual.setName( elemPerson.elementTextTrim( "name" ) );
240 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
241 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
242 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
243 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
246 Element elemRoles = elemPerson.element( "roles" );
247 if ( elemRoles != null )
249 List<Element> roleNames = elemRoles.elements( "role" );
250 Iterator<Element> itRole = roleNames.iterator();
251 while ( itRole.hasNext() )
253 Element role = itRole.next();
254 individual.addRole( role.getTextTrim() );
259 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
261 ret.add( individual );
267 private IssueManagement getIssueManagement( XMLReader xml )
270 Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
272 if ( issueTrackingUrlElem == null )
277 String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
278 if ( StringUtils.isBlank( issueTrackingUrl ) )
283 IssueManagement issueMgmt = new IssueManagement();
284 issueMgmt.setUrl( issueTrackingUrl );
289 private List<License> getLicenses( XMLReader xml )
292 List<License> licenses = new ArrayList<License>();
294 Element elemLicenses = xml.getElement( "//project/licenses" );
296 if ( elemLicenses != null )
298 List<Element> licenseList = elemLicenses.elements( "license" );
299 for ( Element elemLicense : licenseList )
301 License license = new License();
303 // TODO: Create LicenseIdentity class to managed license ids.
304 // license.setId( elemLicense.elementTextTrim("id") );
305 license.setName( elemLicense.elementTextTrim( "name" ) );
306 license.setUrl( elemLicense.elementTextTrim( "url" ) );
307 license.setComments( elemLicense.elementTextTrim( "comments" ) );
309 licenses.add( license );
316 private List<MailingList> getMailingLists( XMLReader xml )
319 List<MailingList> mailingLists = new ArrayList<MailingList>();
321 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
322 for ( Element elemMailingList : mailingListElems )
324 MailingList mlist = new MailingList();
326 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
327 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
328 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
329 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
330 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
332 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
333 if ( elemOtherArchives != null )
335 List<String> otherArchives = new ArrayList<String>();
336 List<Element> others = elemOtherArchives.elements( "otherArchive" );
337 for ( Element other : others )
339 String otherArchive = other.getTextTrim();
340 otherArchives.add( otherArchive );
343 mlist.setOtherArchives( otherArchives );
346 mailingLists.add( mlist );
352 private Organization getOrganization( XMLReader xml )
355 Element elemOrg = xml.getElement( "//project/organization" );
356 if ( elemOrg != null )
358 Organization org = new Organization();
360 org.setName( elemOrg.elementTextTrim( "name" ) );
361 org.setUrl( elemOrg.elementTextTrim( "url" ) );
362 // TODO: Handle <logo />
370 private Properties getProperties( Element elemProperties )
372 if ( elemProperties == null )
377 Properties ret = new Properties();
379 Iterator<Element> itProps = elemProperties.elements().iterator();
380 while ( itProps.hasNext() )
382 Element elemProp = (Element) itProps.next();
383 ret.setProperty( elemProp.getName(), elemProp.getText() );
389 private List<ArtifactReference> getReports( XMLReader xml )
392 return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
395 private List<ProjectRepository> getRepositories( XMLReader xml )
398 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
400 // Repositories are not stored within the maven 1 pom.
405 private Scm getSCM( XMLReader xml )
408 Element elemScm = xml.getElement( "//project/repository" );
410 if ( elemScm != null )
414 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
415 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
416 scm.setUrl( elemScm.elementTextTrim( "url" ) );