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