View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo;
20  
21  import java.util.Locale;
22  
23  import org.apache.maven.doxia.sink.Sink;
24  import org.apache.maven.model.DistributionManagement;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.i18n.I18N;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  /**
31   * Generates the Project Distribution Management report.
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
34   * @since 2.3
35   */
36  @Mojo(name = "distribution-management")
37  public class DistributionManagementReport extends AbstractProjectInfoReport {
38      // ----------------------------------------------------------------------
39      // Public methods
40      // ----------------------------------------------------------------------
41  
42      @Override
43      public boolean canGenerateReport() {
44          boolean result = super.canGenerateReport();
45          if (result && skipEmptyReport) {
46              result = getProject().getDistributionManagement() != null;
47          }
48  
49          return result;
50      }
51  
52      @Override
53      public void executeReport(Locale locale) {
54          DistributionManagementRenderer r =
55                  new DistributionManagementRenderer(getSink(), getProject(), getI18N(locale), locale);
56  
57          r.render();
58      }
59  
60      /** {@inheritDoc} */
61      public String getOutputName() {
62          return "distribution-management";
63      }
64  
65      @Override
66      protected String getI18Nsection() {
67          return "distribution-management";
68      }
69  
70      // ----------------------------------------------------------------------
71      // Private
72      // ----------------------------------------------------------------------
73  
74      /**
75       * Internal renderer class
76       */
77      private static class DistributionManagementRenderer extends AbstractProjectInfoRenderer {
78          private final MavenProject project;
79  
80          DistributionManagementRenderer(Sink sink, MavenProject project, I18N i18n, Locale locale) {
81              super(sink, i18n, locale);
82  
83              this.project = project;
84          }
85  
86          @Override
87          protected String getI18Nsection() {
88              return "distribution-management";
89          }
90  
91          @Override
92          protected void renderBody() {
93              DistributionManagement distributionManagement = project.getDistributionManagement();
94              if (distributionManagement == null) {
95                  startSection(getI18nString("overview.title"));
96  
97                  paragraph(getI18nString("nodistributionmanagement"));
98  
99                  endSection();
100 
101                 return;
102             }
103 
104             startSection(getI18nString("overview.title"));
105             paragraph(getI18nString("overview.intro"));
106 
107             if (StringUtils.isNotEmpty(distributionManagement.getDownloadUrl())) {
108                 startSection(getI18nString("downloadURL"));
109                 internalLink(distributionManagement.getDownloadUrl());
110                 endSection();
111             }
112 
113             if (distributionManagement.getRelocation() != null) {
114                 startSection(getI18nString("relocation"));
115                 startTable();
116                 tableHeader(new String[] {getI18nString("field"), getI18nString("value")});
117                 tableRow(new String[] {
118                     getI18nString("relocation.groupid"),
119                     distributionManagement.getRelocation().getGroupId()
120                 });
121                 tableRow(new String[] {
122                     getI18nString("relocation.artifactid"),
123                     distributionManagement.getRelocation().getArtifactId()
124                 });
125                 tableRow(new String[] {
126                     getI18nString("relocation.version"),
127                     distributionManagement.getRelocation().getVersion()
128                 });
129                 tableRow(new String[] {
130                     getI18nString("relocation.message"),
131                     distributionManagement.getRelocation().getMessage()
132                 });
133                 endTable();
134                 endSection();
135             }
136 
137             if (distributionManagement.getRepository() != null
138                     && StringUtils.isNotEmpty(
139                             distributionManagement.getRepository().getUrl())) {
140                 startSection(getI18nString("repository")
141                         + getRepoName(distributionManagement.getRepository().getId()));
142                 internalLink(distributionManagement.getRepository().getUrl());
143                 endSection();
144             }
145 
146             if (distributionManagement.getSnapshotRepository() != null
147                     && StringUtils.isNotEmpty(
148                             distributionManagement.getSnapshotRepository().getUrl())) {
149                 startSection(getI18nString("snapshotRepository")
150                         + getRepoName(
151                                 distributionManagement.getSnapshotRepository().getId()));
152                 internalLink(distributionManagement.getSnapshotRepository().getUrl());
153                 endSection();
154             }
155 
156             if (distributionManagement.getSite() != null
157                     && StringUtils.isNotEmpty(distributionManagement.getSite().getUrl())) {
158                 startSection(getI18nString("site")
159                         + getRepoName(distributionManagement.getSite().getId()));
160                 internalLink(distributionManagement.getSite().getUrl());
161                 endSection();
162             }
163 
164             endSection();
165         }
166 
167         private void internalLink(String url) {
168             if (url == null || url.isEmpty()) {
169                 return;
170             }
171 
172             String urlLowerCase = url.trim().toLowerCase(Locale.ENGLISH);
173             if (urlLowerCase.startsWith("http") || urlLowerCase.startsWith("https") || urlLowerCase.startsWith("ftp")) {
174                 link(url, url);
175             } else {
176                 paragraph(url);
177             }
178         }
179 
180         private String getRepoName(String name) {
181             if (name != null && !name.isEmpty()) {
182                 return " - " + name;
183             }
184 
185             return "";
186         }
187     }
188 }