]> source.dussan.org Git - archiva.git/blob
74ebf1be86f9f02f6c0d94dfd0e4dfd05b0a5cf6
[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     @SuppressWarnings("unchecked")
143     private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
144         throws XMLException
145     {
146         List<Dependency> dependencyList = new ArrayList<Dependency>();
147
148         Element project = xml.getElement( "//project" );
149
150         Element depsParent = project;
151
152         for ( String part : parts )
153         {
154             depsParent = depsParent.element( part );
155             if ( depsParent == null )
156             {
157                 return dependencyList;
158             }
159         }
160
161         Iterator<Element> it = depsParent.elementIterator( "dependency" );
162         while ( it.hasNext() )
163         {
164             Element elemDependency = it.next();
165             Dependency dependency = new Dependency();
166
167             dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
168             dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
169             dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
170
171             dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
172             dependency.setUrl( elemDependency.elementTextTrim( "url" ) );
173             /* Following are not valid for <pomVersion>3</pomVersion> / Maven 1 pom files.
174              * 
175              * dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
176              * dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
177              * dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
178              */
179
180             dependency.setSystemPath( elemDependency.elementTextTrim( "jar" ) );
181
182             if ( dependencyList.contains( dependency ) )
183             {
184                 // TODO: throw into monitor as "duplicate dependency" issue.
185             }
186
187             dependencyList.add( dependency );
188         }
189
190         return dependencyList;
191     }
192
193     private List<Individual> getIndividuals( XMLReader xml )
194         throws XMLException
195     {
196         List<Individual> individuals = new ArrayList<Individual>();
197
198         individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
199         individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
200
201         return individuals;
202     }
203
204     @SuppressWarnings("unchecked")
205     private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
206         throws XMLException
207     {
208         List<Individual> ret = new ArrayList<Individual>();
209
210         List<Element> modelPersonList = xml.getElementList( xpathExpr );
211
212         Iterator<Element> iter = modelPersonList.iterator();
213         while ( iter.hasNext() )
214         {
215             Element elemPerson = iter.next();
216             Individual individual = new Individual();
217
218             if ( isCommitor )
219             {
220                 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
221             }
222
223             individual.setCommitor( isCommitor );
224             individual.setEmail( elemPerson.elementTextTrim( "email" ) );
225             individual.setName( elemPerson.elementTextTrim( "name" ) );
226             individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
227             individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
228             individual.setUrl( elemPerson.elementTextTrim( "url" ) );
229             individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
230
231             // Roles
232             Element elemRoles = elemPerson.element( "roles" );
233             if ( elemRoles != null )
234             {
235                 List<Element> roleNames = elemRoles.elements( "role" );
236                 Iterator<Element> itRole = roleNames.iterator();
237                 while ( itRole.hasNext() )
238                 {
239                     Element role = itRole.next();
240                     individual.addRole( role.getTextTrim() );
241                 }
242             }
243
244             // Properties
245             individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
246
247             ret.add( individual );
248         }
249
250         return ret;
251     }
252
253     private IssueManagement getIssueManagement( XMLReader xml )
254         throws XMLException
255     {
256         Element issueTrackingUrlElem = xml.getElement( "//project/issueTrackingUrl" );
257
258         if ( issueTrackingUrlElem == null )
259         {
260             return null;
261         }
262
263         String issueTrackingUrl = issueTrackingUrlElem.getTextTrim();
264         if ( StringUtils.isBlank( issueTrackingUrl ) )
265         {
266             return null;
267         }
268
269         IssueManagement issueMgmt = new IssueManagement();
270         issueMgmt.setUrl( issueTrackingUrl );
271
272         return issueMgmt;
273     }
274
275     @SuppressWarnings("unchecked")
276     private List<License> getLicenses( XMLReader xml )
277         throws XMLException
278     {
279         List<License> licenses = new ArrayList<License>();
280
281         Element elemLicenses = xml.getElement( "//project/licenses" );
282
283         if ( elemLicenses != null )
284         {
285             List<Element> licenseList = elemLicenses.elements( "license" );
286             for ( Element elemLicense : licenseList )
287             {
288                 License license = new License();
289
290                 // TODO: Create LicenseIdentity class to managed license ids.
291                 // license.setId( elemLicense.elementTextTrim("id") );
292                 license.setName( elemLicense.elementTextTrim( "name" ) );
293                 license.setUrl( elemLicense.elementTextTrim( "url" ) );
294                 license.setComments( elemLicense.elementTextTrim( "comments" ) );
295
296                 licenses.add( license );
297             }
298         }
299
300         return licenses;
301     }
302
303     @SuppressWarnings("unchecked")
304     private List<MailingList> getMailingLists( XMLReader xml )
305         throws XMLException
306     {
307         List<MailingList> mailingLists = new ArrayList<MailingList>();
308
309         List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
310         for ( Element elemMailingList : mailingListElems )
311         {
312             MailingList mlist = new MailingList();
313
314             mlist.setName( elemMailingList.elementTextTrim( "name" ) );
315             mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
316             mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
317             mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
318             mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
319
320             Element elemOtherArchives = elemMailingList.element( "otherArchives" );
321             if ( elemOtherArchives != null )
322             {
323                 List<String> otherArchives = new ArrayList<String>();
324                 List<Element> others = elemOtherArchives.elements( "otherArchive" );
325                 for ( Element other : others )
326                 {
327                     String otherArchive = other.getTextTrim();
328                     otherArchives.add( otherArchive );
329                 }
330
331                 mlist.setOtherArchives( otherArchives );
332             }
333
334             mailingLists.add( mlist );
335         }
336
337         return mailingLists;
338     }
339
340     private Organization getOrganization( XMLReader xml )
341         throws XMLException
342     {
343         Element elemOrg = xml.getElement( "//project/organization" );
344         if ( elemOrg != null )
345         {
346             Organization org = new Organization();
347
348             org.setName( elemOrg.elementTextTrim( "name" ) );
349             org.setUrl( elemOrg.elementTextTrim( "url" ) );
350             // TODO: Handle <logo />
351
352             return org;
353         }
354
355         return null;
356     }
357
358     @SuppressWarnings("unchecked")
359     private Properties getProperties( Element elemProperties )
360     {
361         if ( elemProperties == null )
362         {
363             return null;
364         }
365
366         Properties ret = new Properties();
367
368         Iterator<Element> itProps = elemProperties.elements().iterator();
369         while ( itProps.hasNext() )
370         {
371             Element elemProp = (Element) itProps.next();
372             ret.setProperty( elemProp.getName(), elemProp.getText() );
373         }
374
375         return ret;
376     }
377
378     private List<ArtifactReference> getReports( XMLReader xml )
379         throws XMLException
380     {
381         return getArtifactReferenceList( xml, "//project/reports/plugins/plugin", "maven-plugin" );
382     }
383
384     private List<ProjectRepository> getRepositories( XMLReader xml )
385         throws XMLException
386     {
387         List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
388
389         // Repositories are not stored within the maven 1 pom.
390
391         return repos;
392     }
393
394     private Scm getSCM( XMLReader xml )
395         throws XMLException
396     {
397         Element elemScm = xml.getElement( "//project/repository" );
398
399         if ( elemScm != null )
400         {
401             Scm scm = new Scm();
402
403             scm.setConnection( elemScm.elementTextTrim( "connection" ) );
404             scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
405             scm.setUrl( elemScm.elementTextTrim( "url" ) );
406
407             return scm;
408         }
409
410         return null;
411     }
412 }