]> source.dussan.org Git - archiva.git/blob
600f738ae2c1a44902965fb4d953a376f2750515
[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.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;
43
44 /**
45  * ProjectModel300Reader 
46  *
47  * @version $Id$
48  */
49 public class ProjectModel300Reader
50     implements ProjectModelReader
51 {
52
53     public ArchivaProjectModel read( File pomFile )
54         throws XMLException
55     {
56         XMLReader xml = new XMLReader( "project", pomFile );
57
58         ArchivaProjectModel model = new ArchivaProjectModel();
59
60         xml.removeNamespaces();
61
62         Element project = xml.getElement( "//project" );
63
64         // TODO: Handle <extend /> ?? (is this even possible?)
65
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 />
76
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 ) );
92
93         model.setOrigin( "filesystem" );
94
95         /*
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()
99          */
100
101         return model;
102     }
103
104     private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
105     {
106         ArtifactReference reference = new ArtifactReference();
107
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 ) );
113
114         return reference;
115     }
116
117     /**
118      * Get List of {@link ArtifactReference} objects from xpath expr.
119      */
120     private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
121         throws XMLException
122     {
123         List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
124
125         Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
126         while ( it.hasNext() )
127         {
128             Element elemPlugin = it.next();
129
130             refs.add( getArtifactReference( elemPlugin, defaultType ) );
131         }
132
133         return refs;
134     }
135
136     private List<Dependency> getDependencies( XMLReader xml )
137         throws XMLException
138     {
139         return getDependencyList( xml, new String[] { "dependencies" } );
140     }
141
142     private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
143         throws XMLException
144     {
145         List<Dependency> dependencyList = new ArrayList<Dependency>();
146
147         Element project = xml.getElement( "//project" );
148
149         Element depsParent = project;
150
151         for ( String part : parts )
152         {
153             depsParent = depsParent.element( part );
154             if ( depsParent == null )
155             {
156                 return dependencyList;
157             }
158         }
159
160         Iterator<Element> it = depsParent.elementIterator( "dependency" );
161         while ( it.hasNext() )
162         {
163             Element elemDependency = it.next();
164             Dependency dependency = new Dependency();
165
166             dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
167             dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
168             dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
169
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.
173              * 
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 ) );
177              */
178
179             dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
180
181             if ( dependencyList.contains( dependency ) )
182             {
183                 // TODO: throw into monitor as "duplicate dependency" issue.
184             }
185
186             dependencyList.add( dependency );
187         }
188
189         return dependencyList;
190     }
191
192     private List<Individual> getIndividuals( XMLReader xml )
193         throws XMLException
194     {
195         List<Individual> individuals = new ArrayList<Individual>();
196
197         individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
198         individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
199
200         return individuals;
201     }
202
203     private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
204         throws XMLException
205     {
206         List<Individual> ret = new ArrayList<Individual>();
207
208         List<Element> modelPersonList = xml.getElementList( xpathExpr );
209
210         Iterator<Element> iter = modelPersonList.iterator();
211         while ( iter.hasNext() )
212         {
213             Element elemPerson = iter.next();
214             Individual individual = new Individual();
215
216             if ( isCommitor )
217             {
218                 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
219             }
220
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" ) );
228
229             // Roles
230             Element elemRoles = elemPerson.element( "roles" );
231             if ( elemRoles != null )
232             {
233                 List<Element> roleNames = elemRoles.elements( "role" );
234                 Iterator<Element> itRole = roleNames.iterator();
235                 while ( itRole.hasNext() )
236                 {
237                     Element role = itRole.next();
238                     individual.addRole( role.getTextTrim() );
239                 }
240             }
241
242             // Properties
243             individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
244
245             ret.add( individual );
246         }
247
248         return ret;
249     }
250
251     private IssueManagement getIssueManagement( XMLReader xml )
252         throws XMLException
253     {
254         Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
255
256         if ( issueTrackingUrlElem == null )
257         {
258             return null;
259         }
260
261         String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
262         if ( StringUtils.isBlank( issueTrackingUrl ) )
263         {
264             return null;
265         }
266
267         IssueManagement issueMgmt = new IssueManagement();
268         issueMgmt.setUrl( issueTrackingUrl );
269
270         return issueMgmt;
271     }
272
273     private List<License> getLicenses( XMLReader xml )
274         throws XMLException
275     {
276         List<License> licenses = new ArrayList<License>();
277
278         Element elemLicenses = xml.getElement( "//project/licenses" );
279
280         if ( elemLicenses != null )
281         {
282             List<Element> licenseList = elemLicenses.elements( "license" );
283             for ( Element elemLicense : licenseList )
284             {
285                 License license = new License();
286
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" ) );
292
293                 licenses.add( license );
294             }
295         }
296
297         return licenses;
298     }
299
300     private List<MailingList> getMailingLists( XMLReader xml )
301         throws XMLException
302     {
303         List<MailingList> mailingLists = new ArrayList<MailingList>();
304
305         List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
306         for ( Element elemMailingList : mailingListElems )
307         {
308             MailingList mlist = new MailingList();
309
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" ) );
315
316             Element elemOtherArchives = elemMailingList.element( "otherArchives" );
317             if ( elemOtherArchives != null )
318             {
319                 List<String> otherArchives = new ArrayList<String>();
320                 List<Element> others = elemOtherArchives.elements( "otherArchive" );
321                 for ( Element other : others )
322                 {
323                     String otherArchive = other.getTextTrim();
324                     otherArchives.add( otherArchive );
325                 }
326
327                 mlist.setOtherArchives( otherArchives );
328             }
329
330             mailingLists.add( mlist );
331         }
332
333         return mailingLists;
334     }
335
336     private Organization getOrganization( XMLReader xml )
337         throws XMLException
338     {
339         Element elemOrg = xml.getElement( "//project/organization" );
340         if ( elemOrg != null )
341         {
342             Organization org = new Organization();
343
344             org.setName( elemOrg.elementTextTrim( "name" ) );
345             org.setUrl( elemOrg.elementTextTrim( "url" ) );
346             // TODO: Handle <logo />
347
348             return org;
349         }
350
351         return null;
352     }
353
354     private Properties getProperties( Element elemProperties )
355     {
356         if ( elemProperties == null )
357         {
358             return null;
359         }
360
361         Properties ret = new Properties();
362
363         Iterator<Element> itProps = elemProperties.elements().iterator();
364         while ( itProps.hasNext() )
365         {
366             Element elemProp = (Element) itProps.next();
367             ret.setProperty( elemProp.getName(), elemProp.getText() );
368         }
369
370         return ret;
371     }
372
373     private List<ArtifactReference> getReports( XMLReader xml )
374         throws XMLException
375     {
376         return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
377     }
378
379     private List<ProjectRepository> getRepositories( XMLReader xml )
380         throws XMLException
381     {
382         List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
383
384         // Repositories are not stored within the maven 1 pom.
385
386         return repos;
387     }
388
389     private Scm getSCM( XMLReader xml )
390         throws XMLException
391     {
392         Element elemScm = xml.getElement( "//project/repository" );
393
394         if ( elemScm != null )
395         {
396             Scm scm = new Scm();
397
398             scm.setConnection( elemScm.elementTextTrim( "connection" ) );
399             scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
400             scm.setUrl( elemScm.elementTextTrim( "url" ) );
401
402             return scm;
403         }
404
405         return null;
406     }
407 }