Coverage Report - org.apache.maven.archiva.repository.project.writers.ProjectModel400Writer
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectModel400Writer
0%
0/241
0%
0/82
0
 
 1  
 package org.apache.maven.archiva.repository.project.writers;
 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.collections.CollectionUtils;
 23  
 import org.apache.commons.io.IOUtils;
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.apache.maven.archiva.model.ArchivaProjectModel;
 26  
 import org.apache.maven.archiva.model.ArtifactReference;
 27  
 import org.apache.maven.archiva.model.CiManagement;
 28  
 import org.apache.maven.archiva.model.Dependency;
 29  
 import org.apache.maven.archiva.model.Exclusion;
 30  
 import org.apache.maven.archiva.model.Individual;
 31  
 import org.apache.maven.archiva.model.IssueManagement;
 32  
 import org.apache.maven.archiva.model.License;
 33  
 import org.apache.maven.archiva.model.MailingList;
 34  
 import org.apache.maven.archiva.model.Organization;
 35  
 import org.apache.maven.archiva.model.ProjectRepository;
 36  
 import org.apache.maven.archiva.model.Scm;
 37  
 import org.apache.maven.archiva.model.VersionedReference;
 38  
 import org.apache.maven.archiva.repository.project.ProjectModelException;
 39  
 import org.apache.maven.archiva.repository.project.ProjectModelWriter;
 40  
 import org.apache.maven.archiva.xml.XMLException;
 41  
 import org.apache.maven.archiva.xml.XMLWriter;
 42  
 import org.dom4j.Document;
 43  
 import org.dom4j.DocumentHelper;
 44  
 import org.dom4j.Element;
 45  
 import org.dom4j.Namespace;
 46  
 import org.dom4j.Node;
 47  
 import org.dom4j.QName;
 48  
 
 49  
 import java.io.File;
 50  
 import java.io.FileWriter;
 51  
 import java.io.IOException;
 52  
 import java.io.Writer;
 53  
 import java.util.Iterator;
 54  
 import java.util.List;
 55  
 
 56  
 /**
 57  
  * ProjectModel400Writer for Maven 2 project model v4.0.0 pom files.  
 58  
  *
 59  
  * @version $Id: ProjectModel400Writer.java 778118 2009-05-24 10:58:49Z oching $
 60  
  */
 61  0
 public class ProjectModel400Writer
 62  
     implements ProjectModelWriter
 63  
 {
 64  0
     private static final Namespace DEFAULT_NAMESPACE = Namespace.get( "", "http://maven.apache.org/POM/4.0.0" );
 65  
 
 66  
     public void write( ArchivaProjectModel model, File pomFile )
 67  
         throws ProjectModelException, IOException
 68  
     {
 69  0
         FileWriter writer = null;
 70  
         try
 71  
         {
 72  0
             writer = new FileWriter( pomFile );
 73  0
             write( model, writer );
 74  0
             writer.flush();
 75  
         }
 76  
         finally
 77  
         {
 78  0
             IOUtils.closeQuietly( writer );
 79  0
         }
 80  0
     }
 81  
 
 82  
     public void write( ArchivaProjectModel model, Writer writer )
 83  
         throws ProjectModelException, IOException
 84  
     {
 85  0
         Document doc = DocumentHelper.createDocument();
 86  
 
 87  0
         Element root = DocumentHelper.createElement( "project" );
 88  
 
 89  0
         root.add( DEFAULT_NAMESPACE );
 90  0
         root.addNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
 91  0
         root.addAttribute( "xsi:schemaLocation",
 92  
                            "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" );
 93  
 
 94  0
         doc.setRootElement( root );
 95  
 
 96  0
         root.addElement( "modelVersion" ).setText( "4.0.0" );
 97  
 
 98  0
         addParent( root, model.getParentProject() );
 99  
 
 100  0
         addChildElement( root, "groupId", model.getGroupId() );
 101  0
         root.addElement( "artifactId" ).setText( model.getArtifactId() );
 102  
 
 103  0
         addChildElement( root, "version", model.getVersion() );
 104  
 
 105  0
         addChildElement( root, "packaging", model.getPackaging() );
 106  0
         addChildElement( root, "name", model.getName() );
 107  0
         addChildElement( root, "description", model.getDescription() );
 108  0
         addChildElement( root, "url", model.getUrl() );
 109  
         // TODO: add inceptionYear to ArchivaProjectModel
 110  
 
 111  0
         addOrganization( root, model.getOrganization() );
 112  
 
 113  0
         addIssueManagement( root, model.getIssueManagement() );
 114  0
         addCiManagement( root, model.getCiManagement() );
 115  0
         addMailingLists( root, model.getMailingLists() );
 116  0
         addDevelopersAndContributors( root, model.getIndividuals() );
 117  
         // TODO: add distribution management to ArchivaProjectModel
 118  
 
 119  0
         addLicenses( root, model.getLicenses() );
 120  0
         addRepositories( root, model.getRepositories() );
 121  0
         addDependencyManagement( root, model.getDependencyManagement() );
 122  0
         addDependencies( root, model.getDependencies() );
 123  
 
 124  0
         addReporting( root, model.getReports() );
 125  0
         addScm( root, model.getScm() );
 126  
 
 127  
         // <build> element
 128  0
         addPlugins( root, model.getPlugins() );
 129  0
         addBuildExtensions( root, model.getBuildExtensions() );
 130  
 
 131  
         // <distributionManagement>
 132  0
         addRelocation( root, model.getRelocation() );
 133  
 
 134  0
         fixDefaultNamespace( root );
 135  
 
 136  
         try
 137  
         {
 138  0
             XMLWriter.write( doc, writer );
 139  
         }
 140  0
         catch ( XMLException e )
 141  
         {
 142  0
             throw new ProjectModelException( "Unable to write xml contents to writer: " + e.getMessage(), e );
 143  0
         }
 144  0
     }
 145  
 
 146  
     private void addArtifactReference( Element elem, ArtifactReference ref, String defaultType )
 147  
     {
 148  0
         addChildElement( elem, "groupId", ref.getGroupId() );
 149  0
         addChildElement( elem, "artifactId", ref.getArtifactId() );
 150  0
         addChildElement( elem, "version", ref.getVersion() );
 151  0
         addChildElement( elem, "classifier", ref.getClassifier() );
 152  
 
 153  0
         if ( !StringUtils.equals( defaultType, ref.getType() ) )
 154  
         {
 155  0
             addChildElement( elem, "type", ref.getType() );
 156  
         }
 157  0
     }
 158  
 
 159  
     private void addBuildExtensions( Element root, List<ArtifactReference> buildExtensions )
 160  
     {
 161  0
         if ( CollectionUtils.isEmpty( buildExtensions ) )
 162  
         {
 163  0
             return;
 164  
         }
 165  
 
 166  0
         Element build = root.element( "build" );
 167  0
         if ( build == null )
 168  
         {
 169  0
             build = root.addElement( "build" );
 170  
         }
 171  
 
 172  0
         Element elemExtensions = build.addElement( "extensions" );
 173  
 
 174  0
         for ( ArtifactReference extension : buildExtensions )
 175  
         {
 176  0
             Element elem = elemExtensions.addElement( "extension" );
 177  
 
 178  0
             addArtifactReference( elem, extension, "jar" );
 179  0
         }
 180  0
     }
 181  
 
 182  
     private void addCiManagement( Element root, CiManagement ciManagement )
 183  
     {
 184  0
         if ( ciManagement == null )
 185  
         {
 186  0
             return;
 187  
         }
 188  
 
 189  0
         Element elem = root.addElement( "ciManagement" );
 190  0
         addChildElement( elem, "system", ciManagement.getSystem() );
 191  0
         addChildElement( elem, "url", ciManagement.getUrl() );
 192  
         // TODO: Add notifiers into ArchivaProjectModel 
 193  0
     }
 194  
 
 195  
     private void addDependencies( Element root, List<Dependency> dependencies )
 196  
     {
 197  0
         if ( CollectionUtils.isEmpty( dependencies ) )
 198  
         {
 199  0
             return;
 200  
         }
 201  
 
 202  0
         addDependencyList( root, dependencies );
 203  0
     }
 204  
 
 205  
     private void addDependencyList( Element elemParent, List<Dependency> dependencies )
 206  
     {
 207  0
         if ( CollectionUtils.isEmpty( dependencies ) )
 208  
         {
 209  0
             return;
 210  
         }
 211  
 
 212  0
         Element elemDeps = elemParent.addElement( "dependencies" );
 213  
 
 214  0
         for ( Dependency dep : dependencies )
 215  
         {
 216  0
             Element elem = elemDeps.addElement( "dependency" );
 217  
 
 218  0
             addChildElement( elem, "groupId", dep.getGroupId() );
 219  0
             addChildElement( elem, "artifactId", dep.getArtifactId() );
 220  0
             addChildElement( elem, "version", dep.getVersion() );
 221  0
             addChildElement( elem, "classifier", dep.getClassifier() );
 222  0
             addChildElement( elem, "type", dep.getType() );
 223  0
             addChildElement( elem, "scope", dep.getScope() );
 224  0
             addChildElement( elem, "systemPath", dep.getSystemPath() );
 225  
 
 226  0
             addExclusions( elem, dep.getExclusions() );
 227  0
         }
 228  0
     }
 229  
 
 230  
     private void addDependencyManagement( Element root, List<Dependency> dependencyManagement )
 231  
     {
 232  0
         if ( CollectionUtils.isEmpty( dependencyManagement ) )
 233  
         {
 234  0
             return;
 235  
         }
 236  
 
 237  0
         Element elemDepMgmt = root.addElement( "dependencyManagement" );
 238  0
         addDependencyList( elemDepMgmt, dependencyManagement );
 239  0
     }
 240  
 
 241  
     private void addDevelopersAndContributors( Element root, List<Individual> individuals )
 242  
     {
 243  0
         if ( CollectionUtils.isEmpty( individuals ) )
 244  
         {
 245  0
             return;
 246  
         }
 247  
 
 248  0
         Element developers = null;
 249  0
         Element contributors = null;
 250  
 
 251  0
         for ( Individual individual : individuals )
 252  
         {
 253  0
             if ( individual.isCommitor() )
 254  
             {
 255  0
                 if ( developers == null )
 256  
                 {
 257  0
                     developers = root.addElement( "developers" );
 258  
                 }
 259  
 
 260  0
                 Element developer = developers.addElement( "developer" );
 261  0
                 addChildElement( developer, "id", individual.getPrincipal() );
 262  0
                 addIndividual( developer, individual );
 263  0
             }
 264  
             else
 265  
             {
 266  0
                 if ( contributors == null )
 267  
                 {
 268  0
                     contributors = root.addElement( "contributors" );
 269  
                 }
 270  
 
 271  0
                 Element contributor = contributors.addElement( "contributor" );
 272  0
                 addIndividual( contributor, individual );
 273  0
             }
 274  
         }
 275  0
     }
 276  
 
 277  
     private void addExclusions( Element elemParent, List<Exclusion> exclusions )
 278  
     {
 279  0
         if ( CollectionUtils.isEmpty( exclusions ) )
 280  
         {
 281  0
             return;
 282  
         }
 283  
 
 284  0
         Element elemExclusions = elemParent.addElement( "exclusions" );
 285  
 
 286  0
         for ( Exclusion exclusion : exclusions )
 287  
         {
 288  0
             Element elem = elemExclusions.addElement( "exclusion" );
 289  
 
 290  0
             addChildElement( elem, "groupId", exclusion.getGroupId() );
 291  0
             addChildElement( elem, "artifactId", exclusion.getArtifactId() );
 292  0
         }
 293  0
     }
 294  
 
 295  
     private void addIndividual( Element elem, Individual individual )
 296  
     {
 297  0
         addChildElement( elem, "name", individual.getName() );
 298  0
         addChildElement( elem, "email", individual.getEmail() );
 299  0
         addChildElement( elem, "organization", individual.getOrganization() );
 300  0
         addChildElement( elem, "organizationUrl", individual.getOrganizationUrl() );
 301  0
         addChildElement( elem, "timezone", individual.getTimezone() );
 302  
 
 303  0
         if ( CollectionUtils.isNotEmpty( individual.getRoles() ) )
 304  
         {
 305  0
             Element roles = elem.addElement( "roles" );
 306  0
             List<String> roleList = individual.getRoles();
 307  0
             for ( String roleName : roleList )
 308  
             {
 309  0
                 addChildElement( roles, "role", roleName );
 310  
             }
 311  
         }
 312  0
     }
 313  
 
 314  
     private void addIssueManagement( Element root, IssueManagement issueManagement )
 315  
     {
 316  0
         if ( issueManagement == null )
 317  
         {
 318  0
             return;
 319  
         }
 320  
 
 321  0
         Element elem = root.addElement( "issueManagement" );
 322  0
         addChildElement( elem, "system", issueManagement.getSystem() );
 323  0
         addChildElement( elem, "url", issueManagement.getUrl() );
 324  0
     }
 325  
 
 326  
     private void addLicenses( Element root, List<License> licenses )
 327  
     {
 328  0
         if ( CollectionUtils.isEmpty( licenses ) )
 329  
         {
 330  0
             return;
 331  
         }
 332  
 
 333  0
         Element elemLicenses = root.addElement( "licenses" );
 334  
 
 335  0
         for ( License license : licenses )
 336  
         {
 337  0
             Element elem = elemLicenses.addElement( "license" );
 338  0
             addChildElement( elem, "name", license.getName() );
 339  0
             addChildElement( elem, "url", license.getUrl() );
 340  
             // TODO: research if we need <distribution> subelement.
 341  0
         }
 342  0
     }
 343  
 
 344  
     private void addMailingLists( Element root, List<MailingList> mailingLists )
 345  
     {
 346  0
         if ( CollectionUtils.isEmpty( mailingLists ) )
 347  
         {
 348  0
             return;
 349  
         }
 350  
 
 351  0
         Element mlists = root.addElement( "mailingLists" );
 352  
 
 353  0
         for ( MailingList mailingList : mailingLists )
 354  
         {
 355  0
             Element mlist = mlists.addElement( "mailingList" );
 356  0
             addChildElement( mlist, "name", mailingList.getName() );
 357  0
             addChildElement( mlist, "post", mailingList.getPostAddress() );
 358  0
             addChildElement( mlist, "subscribe", mailingList.getSubscribeAddress() );
 359  0
             addChildElement( mlist, "unsubscribe", mailingList.getUnsubscribeAddress() );
 360  0
             addChildElement( mlist, "archive", mailingList.getMainArchiveUrl() );
 361  
 
 362  0
             addOtherArchives( mlist, mailingList.getOtherArchives() );
 363  0
         }
 364  0
     }
 365  
 
 366  
     private void addOtherArchives( Element mlist, List<String> otherArchives )
 367  
     {
 368  0
         if ( CollectionUtils.isEmpty( otherArchives ) )
 369  
         {
 370  0
             return;
 371  
         }
 372  
 
 373  0
         Element elemOtherArchives = mlist.addElement( "otherArchives" );
 374  
 
 375  0
         for ( String archive : otherArchives )
 376  
         {
 377  0
             addChildElement( elemOtherArchives, "otherArchive", archive );
 378  
         }
 379  0
     }
 380  
 
 381  
     private void addOrganization( Element root, Organization organization )
 382  
     {
 383  0
         if ( organization == null )
 384  
         {
 385  0
             return;
 386  
         }
 387  
 
 388  0
         Element elem = root.addElement( "organization" );
 389  
 
 390  
         //addChildElement( elem, "name", organization.getOrganizationName() );
 391  0
         addChildElement( elem, "name", organization.getName() );
 392  0
         addChildElement( elem, "url", organization.getUrl() );
 393  0
     }
 394  
 
 395  
     private void addParent( Element root, VersionedReference parentProject )
 396  
     {
 397  0
         if ( parentProject == null )
 398  
         {
 399  0
             return;
 400  
         }
 401  
 
 402  0
         Element parent = root.addElement( "parent" );
 403  0
         parent.addElement( "groupId" ).setText( parentProject.getGroupId() );
 404  0
         parent.addElement( "artifactId" ).setText( parentProject.getArtifactId() );
 405  0
         parent.addElement( "version" ).setText( parentProject.getVersion() );
 406  0
     }
 407  
 
 408  
     private void addPlugins( Element root, List<ArtifactReference> plugins )
 409  
     {
 410  0
         if ( CollectionUtils.isEmpty( plugins ) )
 411  
         {
 412  0
             return;
 413  
         }
 414  
 
 415  0
         Element build = root.element( "build" );
 416  0
         if ( build == null )
 417  
         {
 418  0
             build = root.addElement( "build" );
 419  
         }
 420  
 
 421  0
         Element elemPlugins = build.addElement( "plugins" );
 422  
 
 423  0
         for ( ArtifactReference plugin : plugins )
 424  
         {
 425  0
             Element elem = elemPlugins.addElement( "plugin" );
 426  
 
 427  0
             addArtifactReference( elem, plugin, "maven-plugin" );
 428  0
         }
 429  0
     }
 430  
 
 431  
     private void addRelocation( Element root, VersionedReference relocation )
 432  
     {
 433  0
         if ( relocation == null )
 434  
         {
 435  0
             return;
 436  
         }
 437  
 
 438  0
         Element distribManagement = root.element( "distributionManagement" );
 439  
 
 440  0
         if ( distribManagement == null )
 441  
         {
 442  0
             distribManagement = root.addElement( "distributionManagement" );
 443  
         }
 444  
 
 445  0
         Element elem = distribManagement.addElement( "relocation" );
 446  0
         addChildElement( elem, "groupId", relocation.getGroupId() );
 447  0
         addChildElement( elem, "artifactId", relocation.getArtifactId() );
 448  0
         addChildElement( elem, "version", relocation.getVersion() );
 449  0
     }
 450  
 
 451  
     private void addReporting( Element root, List<ArtifactReference> reports )
 452  
     {
 453  0
         if ( CollectionUtils.isEmpty( reports ) )
 454  
         {
 455  0
             return;
 456  
         }
 457  
 
 458  0
         Element reporting = root.addElement( "reporting" );
 459  0
         Element plugins = reporting.addElement( "plugins" );
 460  
 
 461  0
         for ( ArtifactReference reference : reports )
 462  
         {
 463  0
             Element plugin = plugins.addElement( "plugin" );
 464  0
             addChildElement( plugin, "groupId", reference.getGroupId() );
 465  0
             addChildElement( plugin, "artifactId", reference.getArtifactId() );
 466  0
             addChildElement( plugin, "version", reference.getVersion() );
 467  0
         }
 468  0
     }
 469  
 
 470  
     private void addRepositories( Element root, List<ProjectRepository> repositories )
 471  
     {
 472  0
         if ( CollectionUtils.isEmpty( repositories ) )
 473  
         {
 474  0
             return;
 475  
         }
 476  
 
 477  0
         Element elemRepos = root.addElement( "repositories" );
 478  0
         for ( ProjectRepository repository : repositories )
 479  
         {
 480  0
             Element elem = elemRepos.addElement( "repository" );
 481  0
             addChildElement( elem, "id", repository.getId() );
 482  0
             addChildElement( elem, "name", repository.getName() );
 483  0
             addChildElement( elem, "url", repository.getUrl() );
 484  
 
 485  0
             if ( !StringUtils.equals( "default", repository.getLayout() ) )
 486  
             {
 487  0
                 addChildElement( elem, "layout", repository.getLayout() );
 488  
             }
 489  0
         }
 490  0
     }
 491  
 
 492  
     private void addScm( Element root, Scm scm )
 493  
     {
 494  0
         if ( scm == null )
 495  
         {
 496  0
             return;
 497  
         }
 498  
 
 499  0
         Element elem = root.addElement( "scm" );
 500  
 
 501  0
         addChildElement( elem, "connection", scm.getConnection() );
 502  0
         addChildElement( elem, "developerConnection", scm.getDeveloperConnection() );
 503  0
         addChildElement( elem, "url", scm.getUrl() );
 504  0
     }
 505  
 
 506  
     /**
 507  
      * Fix the default namespace on all elements recursively.
 508  
      */
 509  
     @SuppressWarnings("unchecked")
 510  
     private void fixDefaultNamespace( Element elem )
 511  
     {
 512  0
         elem.remove( elem.getNamespace() );
 513  0
         elem.setQName( QName.get( elem.getName(), DEFAULT_NAMESPACE, elem.getQualifiedName() ) );
 514  
 
 515  
         Node n;
 516  
 
 517  0
         Iterator<Node> it = elem.elementIterator();
 518  0
         while ( it.hasNext() )
 519  
         {
 520  0
             n = it.next();
 521  
 
 522  0
             switch ( n.getNodeType() )
 523  
             {
 524  
                 case Node.ELEMENT_NODE:
 525  0
                     fixDefaultNamespace( (Element) n );
 526  0
                     break;
 527  
             }
 528  
         }
 529  0
     }
 530  
 
 531  
     private static void addChildElement( Element elem, String elemName, String text )
 532  
     {
 533  0
         if ( StringUtils.isBlank( text ) )
 534  
         {
 535  0
             return;
 536  
         }
 537  
 
 538  0
         elem.addElement( elemName ).setText( text );
 539  0
     }
 540  
 }