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
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Properties;
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.Dependency;
32 import org.apache.maven.archiva.model.Individual;
33 import org.apache.maven.archiva.model.IssueManagement;
34 import org.apache.maven.archiva.model.License;
35 import org.apache.maven.archiva.model.MailingList;
36 import org.apache.maven.archiva.model.Organization;
37 import org.apache.maven.archiva.model.ProjectRepository;
38 import org.apache.maven.archiva.model.Scm;
39 import org.apache.maven.archiva.repository.project.ProjectModelReader;
40 import org.apache.maven.archiva.xml.XMLException;
41 import org.apache.maven.archiva.xml.XMLReader;
42 import org.dom4j.Element;
45 * ProjectModel300Reader
49 public class ProjectModel300Reader
50 implements ProjectModelReader
53 public ArchivaProjectModel read( File pomFile )
56 XMLReader xml = new XMLReader( "project", pomFile );
58 ArchivaProjectModel model = new ArchivaProjectModel();
60 xml.removeNamespaces();
62 Element project = xml.getElement( "//project" );
64 // TODO: Handle <extend /> ?? (is this even possible?)
66 model.setGroupId( project.elementTextTrim( "groupId" ) );
67 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
68 // TODO: Handle <id />
69 model.setVersion( project.elementTextTrim( "currentVersion" ) );
70 model.setName( project.elementTextTrim( "name" ) );
71 model.setDescription( project.elementTextTrim( "description" ) );
72 // TODO: what to do with <shortDescription /> ?
73 model.setUrl( project.elementTextTrim( "url" ) );
74 // TODO: Handle <logo />
75 // TODO: Handle <inceptionYear />
77 model.setIssueManagement( getIssueManagement( xml ) );
78 // TODO: What to do with <gumpRepositoryId /> ?
79 // TODO: Handle <siteAddress />
80 // TODO: Handle <siteDirectory /> ?
81 // TODO: Handle <distributionSite />
82 // TODO: Handle <distributionDirectory />
83 model.setMailingLists( getMailingLists( xml ) );
84 model.setIndividuals( getIndividuals( xml ) );
85 model.setLicenses( getLicenses( xml ) );
86 model.setReports( getReports( xml ) );
87 model.setRepositories( getRepositories( xml ) );
88 model.setScm( getSCM( xml ) );
89 model.setOrganization( getOrganization( xml ) );
90 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
91 model.setDependencies( getDependencies( xml ) );
93 model.setOrigin( "filesystem" );
96 * Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files. model.setDependencyManagement()
97 * model.setPlugins() model.setParentProject() model.setPackaging() model.setCiManagement()
98 * model.setBuildExtensions() model.setRelocation()
104 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
106 ArtifactReference reference = new ArtifactReference();
108 reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
109 reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
110 reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
111 reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
112 reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
118 * Get List of {@link ArtifactReference} objects from xpath expr.
120 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
123 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
125 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
126 while ( it.hasNext() )
128 Element elemPlugin = it.next();
130 refs.add( getArtifactReference( elemPlugin, defaultType ) );
136 private List<Dependency> getDependencies( XMLReader xml )
139 return getDependencyList( xml, new String[] { "dependencies" } );
142 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
145 List<Dependency> dependencyList = new ArrayList<Dependency>();
147 Element project = xml.getElement( "//project" );
149 Element depsParent = project;
151 for ( String part : parts )
153 depsParent = depsParent.element( part );
154 if ( depsParent == null )
156 return dependencyList;
160 Iterator<Element> it = depsParent.elementIterator( "dependency" );
161 while ( it.hasNext() )
163 Element elemDependency = it.next();
164 Dependency dependency = new Dependency();
166 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
167 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
168 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
170 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
171 dependency.setUrl( elemDependency.elementTextTrim( "url" ) );
172 /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
174 * dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
175 * dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
176 * dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
179 dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
181 if ( dependencyList.contains( dependency ) )
183 // TODO: throw into monitor as "duplicate dependency" issue.
186 dependencyList.add( dependency );
189 return dependencyList;
192 private List<Individual> getIndividuals( XMLReader xml )
195 List<Individual> individuals = new ArrayList<Individual>();
197 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
198 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
203 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
206 List<Individual> ret = new ArrayList<Individual>();
208 List<Element> modelPersonList = xml.getElementList( xpathExpr );
210 Iterator<Element> iter = modelPersonList.iterator();
211 while ( iter.hasNext() )
213 Element elemPerson = iter.next();
214 Individual individual = new Individual();
218 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
221 individual.setCommitor( isCommitor );
222 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
223 individual.setName( elemPerson.elementTextTrim( "name" ) );
224 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
225 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
226 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
227 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
230 Element elemRoles = elemPerson.element( "roles" );
231 if ( elemRoles != null )
233 List<Element> roleNames = elemRoles.elements( "role" );
234 Iterator<Element> itRole = roleNames.iterator();
235 while ( itRole.hasNext() )
237 Element role = itRole.next();
238 individual.addRole( role.getTextTrim() );
243 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
245 ret.add( individual );
251 private IssueManagement getIssueManagement( XMLReader xml )
254 Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
256 if ( issueTrackingUrlElem == null )
261 String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
262 if ( StringUtils.isBlank( issueTrackingUrl ) )
267 IssueManagement issueMgmt = new IssueManagement();
268 issueMgmt.setUrl( issueTrackingUrl );
273 private List<License> getLicenses( XMLReader xml )
276 List<License> licenses = new ArrayList<License>();
278 Element elemLicenses = xml.getElement( "//project/licenses" );
280 if ( elemLicenses != null )
282 List<Element> licenseList = elemLicenses.elements( "license" );
283 for ( Element elemLicense : licenseList )
285 License license = new License();
287 // TODO: Create LicenseIdentity class to managed license ids.
288 // license.setId( elemLicense.elementTextTrim("id") );
289 license.setName( elemLicense.elementTextTrim( "name" ) );
290 license.setUrl( elemLicense.elementTextTrim( "url" ) );
291 license.setComments( elemLicense.elementTextTrim( "comments" ) );
293 licenses.add( license );
300 private List<MailingList> getMailingLists( XMLReader xml )
303 List<MailingList> mailingLists = new ArrayList<MailingList>();
305 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
306 for ( Element elemMailingList : mailingListElems )
308 MailingList mlist = new MailingList();
310 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
311 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
312 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
313 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
314 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
316 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
317 if ( elemOtherArchives != null )
319 List<String> otherArchives = new ArrayList<String>();
320 List<Element> others = elemOtherArchives.elements( "otherArchive" );
321 for ( Element other : others )
323 String otherArchive = other.getTextTrim();
324 otherArchives.add( otherArchive );
327 mlist.setOtherArchives( otherArchives );
330 mailingLists.add( mlist );
336 private Organization getOrganization( XMLReader xml )
339 Element elemOrg = xml.getElement( "//project/organization" );
340 if ( elemOrg != null )
342 Organization org = new Organization();
344 org.setName( elemOrg.elementTextTrim( "name" ) );
345 org.setUrl( elemOrg.elementTextTrim( "url" ) );
346 // TODO: Handle <logo />
354 private Properties getProperties( Element elemProperties )
356 if ( elemProperties == null )
361 Properties ret = new Properties();
363 Iterator<Element> itProps = elemProperties.elements().iterator();
364 while ( itProps.hasNext() )
366 Element elemProp = (Element) itProps.next();
367 ret.setProperty( elemProp.getName(), elemProp.getText() );
373 private List<ArtifactReference> getReports( XMLReader xml )
376 return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
379 private List<ProjectRepository> getRepositories( XMLReader xml )
382 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
384 // Repositories are not stored within the maven 1 pom.
389 private Scm getSCM( XMLReader xml )
392 Element elemScm = xml.getElement( "//project/repository" );
394 if ( elemScm != null )
398 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
399 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
400 scm.setUrl( elemScm.elementTextTrim( "url" ) );