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>
51 public class ProjectModel300Reader
52 implements ProjectModelReader
55 public ArchivaProjectModel read( File pomFile )
56 throws ProjectModelException
60 XMLReader xml = new XMLReader( "project", pomFile );
62 ArchivaProjectModel model = new ArchivaProjectModel();
64 xml.removeNamespaces();
66 Element project = xml.getElement( "//project" );
68 // TODO: Handle <extend /> ?? (is this even possible?)
70 model.setGroupId( project.elementTextTrim( "groupId" ) );
71 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
72 // TODO: Handle <id />
73 model.setVersion( project.elementTextTrim( "currentVersion" ) );
74 model.setName( project.elementTextTrim( "name" ) );
75 model.setDescription( project.elementTextTrim( "description" ) );
76 // TODO: what to do with <shortDescription /> ?
77 model.setUrl( project.elementTextTrim( "url" ) );
78 // TODO: Handle <logo />
79 // TODO: Handle <inceptionYear />
81 model.setIssueManagement( getIssueManagement( xml ) );
82 // TODO: What to do with <gumpRepositoryId /> ?
83 // TODO: Handle <siteAddress />
84 // TODO: Handle <siteDirectory /> ?
85 // TODO: Handle <distributionSite />
86 // TODO: Handle <distributionDirectory />
87 model.setMailingLists( getMailingLists( xml ) );
88 model.setIndividuals( getIndividuals( xml ) );
89 model.setLicenses( getLicenses( xml ) );
90 model.setReports( getReports( xml ) );
91 model.setRepositories( getRepositories( xml ) );
92 model.setScm( getSCM( xml ) );
93 model.setOrganization( getOrganization( xml ) );
94 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
95 model.setDependencies( getDependencies( xml ) );
97 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
99 * model.setDependencyManagement()
101 * model.setParentProject()
102 * model.setPackaging()
103 * model.setCiManagement()
104 * model.setBuildExtensions()
105 * model.setRelocation()
110 catch ( XMLException e )
112 throw new ProjectModelException( e.getMessage(), e );
116 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
118 ArtifactReference reference = new ArtifactReference();
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 ) );
130 * Get List of {@link ArtifactReference} objects from xpath expr.
132 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
135 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
137 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
138 while ( it.hasNext() )
140 Element elemPlugin = it.next();
142 refs.add( getArtifactReference( elemPlugin, defaultType ) );
148 private List<Dependency> getDependencies( XMLReader xml )
151 return getDependencyList( xml, new String[] { "dependencies" } );
154 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
157 List<Dependency> dependencyList = new ArrayList<Dependency>();
159 Element project = xml.getElement( "//project" );
161 Element depsParent = project;
163 for ( String part : parts )
165 depsParent = depsParent.element( part );
166 if ( depsParent == null )
168 return dependencyList;
172 Iterator<Element> it = depsParent.elementIterator( "dependency" );
173 while ( it.hasNext() )
175 Element elemDependency = it.next();
176 Dependency dependency = new Dependency();
178 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
179 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
180 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
182 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
183 dependency.setUrl( elemDependency.elementTextTrim( "url" ) );
184 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
186 * dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
187 * dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
188 * dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
191 dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
193 if ( dependencyList.contains( dependency ) )
195 // TODO: throw into monitor as "duplicate dependency" issue.
198 dependencyList.add( dependency );
201 return dependencyList;
204 private List<Individual> getIndividuals( XMLReader xml )
207 List<Individual> individuals = new ArrayList<Individual>();
209 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
210 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
215 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
218 List<Individual> ret = new ArrayList<Individual>();
220 List<Element> modelPersonList = xml.getElementList( xpathExpr );
222 Iterator<Element> iter = modelPersonList.iterator();
223 while ( iter.hasNext() )
225 Element elemPerson = iter.next();
226 Individual individual = new Individual();
230 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
233 individual.setCommitor( isCommitor );
234 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
235 individual.setName( elemPerson.elementTextTrim( "name" ) );
236 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
237 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
238 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
239 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
242 Element elemRoles = elemPerson.element( "roles" );
243 if ( elemRoles != null )
245 List<Element> roleNames = elemRoles.elements( "role" );
246 Iterator<Element> itRole = roleNames.iterator();
247 while ( itRole.hasNext() )
249 Element role = itRole.next();
250 individual.addRole( role.getTextTrim() );
255 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
257 ret.add( individual );
263 private IssueManagement getIssueManagement( XMLReader xml )
266 Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
268 if ( issueTrackingUrlElem == null )
273 String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
274 if ( StringUtils.isBlank( issueTrackingUrl ) )
279 IssueManagement issueMgmt = new IssueManagement();
280 issueMgmt.setUrl( issueTrackingUrl );
285 private List<License> getLicenses( XMLReader xml )
288 List<License> licenses = new ArrayList<License>();
290 Element elemLicenses = xml.getElement( "//project/licenses" );
292 if ( elemLicenses != null )
294 List<Element> licenseList = elemLicenses.elements( "license" );
295 for ( Element elemLicense : licenseList )
297 License license = new License();
299 // TODO: Create LicenseIdentity class to managed license ids.
300 // license.setId( elemLicense.elementTextTrim("id") );
301 license.setName( elemLicense.elementTextTrim( "name" ) );
302 license.setUrl( elemLicense.elementTextTrim( "url" ) );
303 license.setComments( elemLicense.elementTextTrim( "comments" ) );
305 licenses.add( license );
312 private List<MailingList> getMailingLists( XMLReader xml )
315 List<MailingList> mailingLists = new ArrayList<MailingList>();
317 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
318 for ( Element elemMailingList : mailingListElems )
320 MailingList mlist = new MailingList();
322 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
323 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
324 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
325 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
326 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
328 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
329 if ( elemOtherArchives != null )
331 List<String> otherArchives = new ArrayList<String>();
332 List<Element> others = elemOtherArchives.elements( "otherArchive" );
333 for ( Element other : others )
335 String otherArchive = other.getTextTrim();
336 otherArchives.add( otherArchive );
339 mlist.setOtherArchives( otherArchives );
342 mailingLists.add( mlist );
348 private Organization getOrganization( XMLReader xml )
351 Element elemOrg = xml.getElement( "//project/organization" );
352 if ( elemOrg != null )
354 Organization org = new Organization();
356 org.setName( elemOrg.elementTextTrim( "name" ) );
357 org.setUrl( elemOrg.elementTextTrim( "url" ) );
358 // TODO: Handle <logo />
366 private Properties getProperties( Element elemProperties )
368 if ( elemProperties == null )
373 Properties ret = new Properties();
375 Iterator<Element> itProps = elemProperties.elements().iterator();
376 while ( itProps.hasNext() )
378 Element elemProp = (Element) itProps.next();
379 ret.setProperty( elemProp.getName(), elemProp.getText() );
385 private List<ArtifactReference> getReports( XMLReader xml )
388 return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
391 private List<ProjectRepository> getRepositories( XMLReader xml )
394 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
396 // Repositories are not stored within the maven 1 pom.
401 private Scm getSCM( XMLReader xml )
404 Element elemScm = xml.getElement( "//project/repository" );
406 if ( elemScm != null )
410 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
411 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
412 scm.setUrl( elemScm.elementTextTrim( "url" ) );