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