Coverage Report - org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectModel400Reader
0%
0/229
0%
0/58
0
 
 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.CiManagement;
 32  
 import org.apache.maven.archiva.model.Dependency;
 33  
 import org.apache.maven.archiva.model.DependencyScope;
 34  
 import org.apache.maven.archiva.model.Exclusion;
 35  
 import org.apache.maven.archiva.model.Individual;
 36  
 import org.apache.maven.archiva.model.IssueManagement;
 37  
 import org.apache.maven.archiva.model.License;
 38  
 import org.apache.maven.archiva.model.MailingList;
 39  
 import org.apache.maven.archiva.model.Organization;
 40  
 import org.apache.maven.archiva.model.ProjectRepository;
 41  
 import org.apache.maven.archiva.model.Scm;
 42  
 import org.apache.maven.archiva.model.VersionedReference;
 43  
 import org.apache.maven.archiva.repository.project.ProjectModelReader;
 44  
 import org.apache.maven.archiva.xml.XMLException;
 45  
 import org.apache.maven.archiva.xml.XMLReader;
 46  
 import org.dom4j.Element;
 47  
 
 48  
 /**
 49  
  * ProjectModel400Reader - read in modelVersion 4.0.0 pom files into archiva-model structures.
 50  
  *
 51  
  * @version $Id: ProjectModel400Reader.java 778118 2009-05-24 10:58:49Z oching $
 52  
  */
 53  
 @SuppressWarnings("unchecked")
 54  0
 public class ProjectModel400Reader
 55  
     implements ProjectModelReader
 56  
 {
 57  
     public ArchivaProjectModel read( File pomFile )
 58  
         throws XMLException
 59  
     {
 60  0
         XMLReader xml = new XMLReader( "project", pomFile );
 61  
 
 62  0
         ArchivaProjectModel model = new ArchivaProjectModel();
 63  
 
 64  0
         if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
 65  
         {
 66  
             // No namespace defined
 67  
             // TODO: Output to monitor the problem with the Namespace.
 68  
         }
 69  
 
 70  0
         xml.removeNamespaces();
 71  
 
 72  0
         Element project = xml.getElement( "//project" );
 73  
 
 74  0
         model.setGroupId( project.elementTextTrim( "groupId" ) );
 75  0
         model.setArtifactId( project.elementTextTrim( "artifactId" ) );
 76  0
         model.setVersion( project.elementTextTrim( "version" ) );
 77  0
         model.setName( project.elementTextTrim( "name" ) );
 78  0
         model.setDescription( project.elementTextTrim( "description" ) );
 79  0
         model.setUrl( project.elementTextTrim( "url" ) );
 80  
 
 81  0
         model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
 82  
 
 83  0
         model.setParentProject( getParentProject( xml ) );
 84  
 
 85  0
         model.setMailingLists( getMailingLists( xml ) );
 86  0
         model.setCiManagement( getCiManagement( xml ) );
 87  0
         model.setIndividuals( getIndividuals( xml ) );
 88  0
         model.setIssueManagement( getIssueManagement( xml ) );
 89  0
         model.setLicenses( getLicenses( xml ) );
 90  0
         model.setOrganization( getOrganization( xml ) );
 91  0
         model.setScm( getSCM( xml ) );
 92  0
         model.setRepositories( getRepositories( xml ) );
 93  
 
 94  0
         model.setDependencies( getDependencies( xml ) );
 95  0
         model.setDependencyManagement( getDependencyManagement( xml ) );
 96  0
         model.setPlugins( getPlugins( xml ) );
 97  0
         model.setReports( getReports( xml ) );
 98  0
         model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
 99  
 
 100  0
         model.setBuildExtensions( getBuildExtensions( xml ) );
 101  
 
 102  0
         model.setRelocation( getRelocation( xml ) );
 103  
 
 104  0
         model.setOrigin( "filesystem" );
 105  
 
 106  0
         return model;
 107  
     }
 108  
 
 109  
     private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
 110  
     {
 111  0
         ArtifactReference reference = new ArtifactReference();
 112  
 
 113  0
         reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
 114  0
         reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
 115  0
         reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
 116  0
         reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
 117  0
         reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
 118  
 
 119  0
         return reference;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Get List of {@link ArtifactReference} objects from xpath expr.
 124  
      */
 125  
     private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
 126  
         throws XMLException
 127  
     {
 128  0
         List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
 129  
 
 130  0
         Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
 131  0
         while ( it.hasNext() )
 132  
         {
 133  0
             Element elemPlugin = it.next();
 134  
 
 135  0
             refs.add( getArtifactReference( elemPlugin, defaultType ) );
 136  0
         }
 137  
 
 138  0
         return refs;
 139  
     }
 140  
 
 141  
     private List<ArtifactReference> getBuildExtensions( XMLReader xml )
 142  
         throws XMLException
 143  
     {
 144  0
         return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
 145  
     }
 146  
 
 147  
     private CiManagement getCiManagement( XMLReader xml )
 148  
         throws XMLException
 149  
     {
 150  0
         Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
 151  0
         if ( elemCiMgmt != null )
 152  
         {
 153  0
             CiManagement ciManagement = new CiManagement();
 154  0
             ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
 155  0
             ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
 156  0
             ciManagement.setCiUrl( elemCiMgmt.elementTextTrim( "url" ) );
 157  0
             return ciManagement;
 158  
         }
 159  
 
 160  0
         return null;
 161  
     }
 162  
 
 163  
     private List<Dependency> getDependencies( XMLReader xml )
 164  
         throws XMLException
 165  
     {
 166  0
         return getDependencyList( xml, new String[] { "dependencies" } );
 167  
     }
 168  
 
 169  
     private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
 170  
         throws XMLException
 171  
     {
 172  0
         List<Dependency> dependencyList = new ArrayList<Dependency>();
 173  
 
 174  0
         Element project = xml.getElement( "//project" );
 175  
 
 176  0
         Element depsParent = project;
 177  
 
 178  0
         for ( String part : parts )
 179  
         {
 180  0
             depsParent = depsParent.element( part );
 181  0
             if ( depsParent == null )
 182  
             {
 183  0
                 return dependencyList;
 184  
             }
 185  
         }
 186  
 
 187  0
         Iterator<Element> it = depsParent.elementIterator( "dependency" );
 188  0
         while ( it.hasNext() )
 189  
         {
 190  0
             Element elemDependency = it.next();
 191  0
             Dependency dependency = new Dependency();
 192  
 
 193  0
             dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
 194  0
             dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
 195  0
             dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
 196  
 
 197  0
             dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
 198  0
             dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
 199  0
             dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
 200  
             // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
 201  0
             dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
 202  0
             if ( DependencyScope.isSystemScoped( dependency ) )
 203  
             {
 204  0
                 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
 205  
             }
 206  
 
 207  0
             dependency.setExclusions( getExclusions( elemDependency ) );
 208  
 
 209  0
             if ( dependencyList.contains( dependency ) )
 210  
             {
 211  
                 // TODO: throw into monitor as "duplicate dependency" issue.
 212  
             }
 213  
 
 214  0
             dependencyList.add( dependency );
 215  0
         }
 216  
 
 217  0
         return dependencyList;
 218  
     }
 219  
 
 220  
     private List<Dependency> getDependencyManagement( XMLReader xml )
 221  
         throws XMLException
 222  
     {
 223  0
         return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
 224  
     }
 225  
 
 226  
     private List<Exclusion> getExclusions( Element elemDependency )
 227  
     {
 228  0
         List<Exclusion> exclusions = new ArrayList<Exclusion>();
 229  
 
 230  0
         Element elemExclusions = elemDependency.element( "exclusions" );
 231  
 
 232  0
         if ( elemExclusions != null )
 233  
         {
 234  0
             Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
 235  0
             while ( it.hasNext() )
 236  
             {
 237  0
                 Element elemExclusion = it.next();
 238  0
                 Exclusion exclusion = new Exclusion();
 239  
 
 240  0
                 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
 241  0
                 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
 242  
 
 243  0
                 exclusions.add( exclusion );
 244  0
             }
 245  
         }
 246  
 
 247  0
         return exclusions;
 248  
     }
 249  
 
 250  
     private List<Individual> getIndividuals( XMLReader xml )
 251  
         throws XMLException
 252  
     {
 253  0
         List<Individual> individuals = new ArrayList<Individual>();
 254  
 
 255  0
         individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
 256  0
         individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
 257  
 
 258  0
         return individuals;
 259  
     }
 260  
 
 261  
     private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
 262  
         throws XMLException
 263  
     {
 264  0
         List<Individual> ret = new ArrayList<Individual>();
 265  
 
 266  0
         List<Element> modelPersonList = xml.getElementList( xpathExpr );
 267  
 
 268  0
         Iterator<Element> iter = modelPersonList.iterator();
 269  0
         while ( iter.hasNext() )
 270  
         {
 271  0
             Element elemPerson = iter.next();
 272  0
             Individual individual = new Individual();
 273  
 
 274  0
             if ( isCommitor )
 275  
             {
 276  0
                 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
 277  
             }
 278  
 
 279  0
             individual.setCommitor( isCommitor );
 280  0
             individual.setEmail( elemPerson.elementTextTrim( "email" ) );
 281  0
             individual.setName( elemPerson.elementTextTrim( "name" ) );
 282  0
             individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
 283  0
             individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
 284  0
             individual.setUrl( elemPerson.elementTextTrim( "url" ) );
 285  0
             individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
 286  0
             individual.setIndividualEmail( elemPerson.elementTextTrim( "email" ) );
 287  
 
 288  
             // Roles
 289  0
             Element elemRoles = elemPerson.element( "roles" );
 290  0
             if ( elemRoles != null )
 291  
             {
 292  0
                 List<Element> roleNames = elemRoles.elements( "role" );
 293  0
                 Iterator<Element> itRole = roleNames.iterator();
 294  0
                 while ( itRole.hasNext() )
 295  
                 {
 296  0
                     Element role = itRole.next();
 297  0
                     individual.addRole( role.getTextTrim() );
 298  0
                 }
 299  
             }
 300  
 
 301  
             // Properties
 302  0
             individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
 303  
 
 304  0
             ret.add( individual );
 305  0
         }
 306  
 
 307  0
         return ret;
 308  
     }
 309  
 
 310  
     private IssueManagement getIssueManagement( XMLReader xml )
 311  
         throws XMLException
 312  
     {
 313  0
         Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
 314  0
         if ( elemIssueMgmt != null )
 315  
         {
 316  0
             IssueManagement issueMgmt = new IssueManagement();
 317  
 
 318  0
             issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
 319  0
             issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
 320  0
             issueMgmt.setIssueManagementUrl( elemIssueMgmt.elementTextTrim( "url" ) );
 321  
             
 322  0
             return issueMgmt;
 323  
         }
 324  
 
 325  0
         return null;
 326  
     }
 327  
 
 328  
     private List<License> getLicenses( XMLReader xml )
 329  
         throws XMLException
 330  
     {
 331  0
         List<License> licenses = new ArrayList<License>();
 332  
 
 333  0
         Element elemLicenses = xml.getElement( "//project/licenses" );
 334  
 
 335  0
         if ( elemLicenses != null )
 336  
         {
 337  0
             List<Element> licenseList = elemLicenses.elements( "license" );
 338  0
             for ( Element elemLicense : licenseList )
 339  
             {
 340  0
                 License license = new License();
 341  
 
 342  
                 // TODO: Create LicenseIdentity class to managed license ids.
 343  
                 // license.setId( elemLicense.elementTextTrim("id") );
 344  0
                 license.setName( elemLicense.elementTextTrim( "name" ) );
 345  0
                 license.setUrl( elemLicense.elementTextTrim( "url" ) );
 346  0
                 license.setComments( elemLicense.elementTextTrim( "comments" ) );
 347  
 
 348  0
                 licenses.add( license );
 349  0
             }
 350  
         }
 351  
 
 352  0
         return licenses;
 353  
     }
 354  
 
 355  
     private List<MailingList> getMailingLists( XMLReader xml )
 356  
         throws XMLException
 357  
     {
 358  0
         List<MailingList> mailingLists = new ArrayList<MailingList>();
 359  
 
 360  0
         List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
 361  0
         for ( Element elemMailingList : mailingListElems )
 362  
         {
 363  0
             MailingList mlist = new MailingList();
 364  
 
 365  0
             mlist.setName( elemMailingList.elementTextTrim( "name" ) );
 366  0
             mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
 367  0
             mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
 368  0
             mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
 369  0
             mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
 370  
 
 371  0
             Element elemOtherArchives = elemMailingList.element( "otherArchives" );
 372  0
             if ( elemOtherArchives != null )
 373  
             {
 374  0
                 List<String> otherArchives = new ArrayList<String>();
 375  0
                 List<Element> others = elemOtherArchives.elements( "otherArchive" );
 376  0
                 for ( Element other : others )
 377  
                 {
 378  0
                     String otherArchive = other.getTextTrim();
 379  0
                     otherArchives.add( otherArchive );
 380  0
                 }
 381  
 
 382  0
                 mlist.setOtherArchives( otherArchives );
 383  
             }
 384  
 
 385  0
             mailingLists.add( mlist );
 386  0
         }
 387  
 
 388  0
         return mailingLists;
 389  
     }
 390  
 
 391  
     private Organization getOrganization( XMLReader xml )
 392  
         throws XMLException
 393  
     {
 394  0
         Element elemOrg = xml.getElement( "//project/organization" );
 395  0
         if ( elemOrg != null )
 396  
         {
 397  0
             Organization org = new Organization();
 398  
 
 399  0
             org.setOrganizationName( elemOrg.elementTextTrim( "name" ) );
 400  0
             org.setName( elemOrg.elementTextTrim( "name" ) );
 401  0
             org.setUrl( elemOrg.elementTextTrim( "url" ) );
 402  
 
 403  0
             return org;
 404  
         }
 405  
 
 406  0
         return null;
 407  
     }
 408  
 
 409  
     private VersionedReference getParentProject( XMLReader xml )
 410  
         throws XMLException
 411  
     {
 412  0
         Element elemParent = xml.getElement( "//project/parent" );
 413  
 
 414  0
         if ( elemParent != null )
 415  
         {
 416  0
             return getVersionedReference( elemParent );
 417  
         }
 418  
 
 419  0
         return null;
 420  
     }
 421  
 
 422  
     private List<ArtifactReference> getPlugins( XMLReader xml )
 423  
         throws XMLException
 424  
     {
 425  0
         return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
 426  
     }
 427  
 
 428  
     private Properties getProperties( Element elemProperties )
 429  
     {
 430  0
         if ( elemProperties == null )
 431  
         {
 432  0
             return null;
 433  
         }
 434  
 
 435  0
         Properties ret = new Properties();
 436  
 
 437  0
         Iterator<Element> itProps = elemProperties.elements().iterator();
 438  0
         while ( itProps.hasNext() )
 439  
         {
 440  0
             Element elemProp = (Element) itProps.next();
 441  0
             ret.setProperty( elemProp.getName(), elemProp.getText() );
 442  0
         }
 443  
 
 444  0
         return ret;
 445  
     }
 446  
 
 447  
     private VersionedReference getRelocation( XMLReader xml )
 448  
         throws XMLException
 449  
     {
 450  0
         Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
 451  
 
 452  0
         if ( elemRelocation != null )
 453  
         {
 454  0
             return getVersionedReference( elemRelocation );
 455  
         }
 456  
 
 457  0
         return null;
 458  
     }
 459  
 
 460  
     private List<ArtifactReference> getReports( XMLReader xml )
 461  
         throws XMLException
 462  
     {
 463  0
         return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
 464  
     }
 465  
 
 466  
     private List<ProjectRepository> getRepositories( XMLReader xml )
 467  
         throws XMLException
 468  
     {
 469  0
         List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
 470  
 
 471  0
         repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
 472  0
         repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
 473  
 
 474  0
         return repos;
 475  
     }
 476  
 
 477  
     private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
 478  
         throws XMLException
 479  
     {
 480  0
         List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
 481  
 
 482  0
         List<Element> repositoriesList = xml.getElementList( xpathExpr );
 483  
 
 484  0
         for ( Element elemRepo : repositoriesList )
 485  
         {
 486  0
             ProjectRepository repo = new ProjectRepository();
 487  
 
 488  0
             repo.setId( elemRepo.elementTextTrim( "id" ) );
 489  0
             repo.setName( elemRepo.elementTextTrim( "name" ) );
 490  0
             repo.setUrl( elemRepo.elementTextTrim( "url" ) );
 491  0
             repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
 492  0
             repo.setPlugins( isPluginRepo );
 493  
 
 494  0
             repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
 495  0
             repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
 496  
 
 497  0
             ret.add( repo );
 498  0
         }
 499  
 
 500  0
         return ret;
 501  
     }
 502  
 
 503  
     private Scm getSCM( XMLReader xml )
 504  
         throws XMLException
 505  
     {
 506  0
         Element elemScm = xml.getElement( "//project/scm" );
 507  
 
 508  0
         if ( elemScm != null )
 509  
         {
 510  0
             Scm scm = new Scm();
 511  
 
 512  0
             scm.setConnection( elemScm.elementTextTrim( "connection" ) );
 513  0
             scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
 514  0
             scm.setUrl( elemScm.elementTextTrim( "url" ) );
 515  
 
 516  0
             return scm;
 517  
         }
 518  
 
 519  0
         return null;
 520  
     }
 521  
 
 522  
     private VersionedReference getVersionedReference( Element elem )
 523  
     {
 524  0
         VersionedReference reference = new VersionedReference();
 525  
 
 526  0
         reference.setGroupId( elem.elementTextTrim( "groupId" ) );
 527  0
         reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
 528  0
         reference.setVersion( elem.elementTextTrim( "version" ) );
 529  
 
 530  0
         return reference;
 531  
     }
 532  
 
 533  
     private boolean toBoolean( String value, boolean defaultValue )
 534  
     {
 535  0
         if ( StringUtils.equalsIgnoreCase( value, "true" ) )
 536  
         {
 537  0
             return true;
 538  
         }
 539  0
         else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
 540  
         {
 541  0
             return false;
 542  
         }
 543  
         else
 544  
         {
 545  
             // If unset, or not "true" or "false".
 546  0
             return defaultValue;
 547  
         }
 548  
     }
 549  
 }