]> source.dussan.org Git - archiva.git/blob
18cef92d6c653ec2a4e984789316b3870e964350
[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 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;
38
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Properties;
44
45 /**
46  * ProjectModel300Reader 
47  *
48  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49  * @version $Id$
50  * 
51  * @plexus.component 
52  *      role="org.apache.maven.archiva.repository.project.ProjectModelReader"
53  *      role-hint="model300"
54  */
55 public class ProjectModel300Reader
56     implements ProjectModelReader
57 {
58
59     public ArchivaProjectModel read( File pomFile )
60         throws ProjectModelException
61     {
62         try
63         {
64             XMLReader xml = new XMLReader( "project", pomFile );
65
66             ArchivaProjectModel model = new ArchivaProjectModel();
67
68             xml.removeNamespaces();
69
70             Element project = xml.getElement( "//project" );
71
72             // TODO: Handle <extend /> ?? (is this even possible?)
73
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 />
84
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 ) );
100             
101             /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
102              * 
103              * model.setDependencyManagement()
104              * model.setPlugins()
105              * model.setParentProject()
106              * model.setPackaging()
107              * model.setCiManagement()
108              * model.setBuildExtensions()
109              * model.setRelocation()
110              */
111
112             return model;
113         }
114         catch ( XMLException e )
115         {
116             throw new ProjectModelException( e.getMessage(), e );
117         }
118     }
119
120     private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
121     {
122         ArtifactReference reference = new ArtifactReference();
123
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 ) );
129
130         return reference;
131     }
132
133     /**
134      * Get List of {@link ArtifactReference} objects from xpath expr.
135      */
136     private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
137         throws XMLException
138     {
139         List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
140
141         Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
142         while ( it.hasNext() )
143         {
144             Element elemPlugin = it.next();
145
146             refs.add( getArtifactReference( elemPlugin, defaultType ) );
147         }
148
149         return refs;
150     }
151
152     private List<Dependency> getDependencies( XMLReader xml )
153         throws XMLException
154     {
155         return getDependencyList( xml, new String[] { "dependencies" } );
156     }
157
158     private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
159         throws XMLException
160     {
161         List<Dependency> dependencyList = new ArrayList<Dependency>();
162
163         Element project = xml.getElement( "//project" );
164
165         Element depsParent = project;
166
167         for ( String part : parts )
168         {
169             depsParent = depsParent.element( part );
170             if ( depsParent == null )
171             {
172                 return dependencyList;
173             }
174         }
175
176         Iterator<Element> it = depsParent.elementIterator( "dependency" );
177         while ( it.hasNext() )
178         {
179             Element elemDependency = it.next();
180             Dependency dependency = new Dependency();
181
182             dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
183             dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
184             dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
185
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.
189              * 
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 ) );
193              */
194
195             dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
196
197             if ( dependencyList.contains( dependency ) )
198             {
199                 // TODO: throw into monitor as "duplicate dependency" issue.
200             }
201
202             dependencyList.add( dependency );
203         }
204
205         return dependencyList;
206     }
207
208     private List<Individual> getIndividuals( XMLReader xml )
209         throws XMLException
210     {
211         List<Individual> individuals = new ArrayList<Individual>();
212
213         individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
214         individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
215
216         return individuals;
217     }
218
219     private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
220         throws XMLException
221     {
222         List<Individual> ret = new ArrayList<Individual>();
223
224         List<Element> modelPersonList = xml.getElementList( xpathExpr );
225
226         Iterator<Element> iter = modelPersonList.iterator();
227         while ( iter.hasNext() )
228         {
229             Element elemPerson = iter.next();
230             Individual individual = new Individual();
231
232             if ( isCommitor )
233             {
234                 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
235             }
236
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" ) );
244
245             // Roles
246             Element elemRoles = elemPerson.element( "roles" );
247             if ( elemRoles != null )
248             {
249                 List<Element> roleNames = elemRoles.elements( "role" );
250                 Iterator<Element> itRole = roleNames.iterator();
251                 while ( itRole.hasNext() )
252                 {
253                     Element role = itRole.next();
254                     individual.addRole( role.getTextTrim() );
255                 }
256             }
257
258             // Properties
259             individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
260
261             ret.add( individual );
262         }
263
264         return ret;
265     }
266
267     private IssueManagement getIssueManagement( XMLReader xml )
268         throws XMLException
269     {
270         Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
271
272         if ( issueTrackingUrlElem == null )
273         {
274             return null;
275         }
276
277         String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
278         if ( StringUtils.isBlank( issueTrackingUrl ) )
279         {
280             return null;
281         }
282
283         IssueManagement issueMgmt = new IssueManagement();
284         issueMgmt.setUrl( issueTrackingUrl );
285
286         return issueMgmt;
287     }
288
289     private List<License> getLicenses( XMLReader xml )
290         throws XMLException
291     {
292         List<License> licenses = new ArrayList<License>();
293
294         Element elemLicenses = xml.getElement( "//project/licenses" );
295
296         if ( elemLicenses != null )
297         {
298             List<Element> licenseList = elemLicenses.elements( "license" );
299             for ( Element elemLicense : licenseList )
300             {
301                 License license = new License();
302
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" ) );
308
309                 licenses.add( license );
310             }
311         }
312
313         return licenses;
314     }
315
316     private List<MailingList> getMailingLists( XMLReader xml )
317         throws XMLException
318     {
319         List<MailingList> mailingLists = new ArrayList<MailingList>();
320
321         List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
322         for ( Element elemMailingList : mailingListElems )
323         {
324             MailingList mlist = new MailingList();
325
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" ) );
331
332             Element elemOtherArchives = elemMailingList.element( "otherArchives" );
333             if ( elemOtherArchives != null )
334             {
335                 List<String> otherArchives = new ArrayList<String>();
336                 List<Element> others = elemOtherArchives.elements( "otherArchive" );
337                 for ( Element other : others )
338                 {
339                     String otherArchive = other.getTextTrim();
340                     otherArchives.add( otherArchive );
341                 }
342
343                 mlist.setOtherArchives( otherArchives );
344             }
345
346             mailingLists.add( mlist );
347         }
348
349         return mailingLists;
350     }
351
352     private Organization getOrganization( XMLReader xml )
353         throws XMLException
354     {
355         Element elemOrg = xml.getElement( "//project/organization" );
356         if ( elemOrg != null )
357         {
358             Organization org = new Organization();
359
360             org.setName( elemOrg.elementTextTrim( "name" ) );
361             org.setUrl( elemOrg.elementTextTrim( "url" ) );
362             // TODO: Handle <logo />
363
364             return org;
365         }
366
367         return null;
368     }
369
370     private Properties getProperties( Element elemProperties )
371     {
372         if ( elemProperties == null )
373         {
374             return null;
375         }
376
377         Properties ret = new Properties();
378
379         Iterator<Element> itProps = elemProperties.elements().iterator();
380         while ( itProps.hasNext() )
381         {
382             Element elemProp = (Element) itProps.next();
383             ret.setProperty( elemProp.getName(), elemProp.getText() );
384         }
385
386         return ret;
387     }
388
389     private List<ArtifactReference> getReports( XMLReader xml )
390         throws XMLException
391     {
392         return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
393     }
394
395     private List<ProjectRepository> getRepositories( XMLReader xml )
396         throws XMLException
397     {
398         List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
399
400         // Repositories are not stored within the maven 1 pom.
401
402         return repos;
403     }
404
405     private Scm getSCM( XMLReader xml )
406         throws XMLException
407     {
408         Element elemScm = xml.getElement( "//project/repository" );
409
410         if ( elemScm != null )
411         {
412             Scm scm = new Scm();
413
414             scm.setConnection( elemScm.elementTextTrim( "connection" ) );
415             scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
416             scm.setUrl( elemScm.elementTextTrim( "url" ) );
417
418             return scm;
419         }
420
421         return null;
422     }
423 }