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 javax.inject.Inject;
22  
23  import java.net.URI;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  
29  import org.apache.maven.doxia.sink.Sink;
30  import org.apache.maven.model.MailingList;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.plugin.logging.Log;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.project.ProjectBuilder;
35  import org.apache.maven.reporting.MavenReportException;
36  import org.apache.maven.repository.RepositorySystem;
37  import org.codehaus.plexus.i18n.I18N;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * Generates the Mailing Lists report.
42   *
43   * @author <a href="mailto:brett@apache.org">Brett Porter </a>
44   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
45   * @since 2.0
46   */
47  @Mojo(name = "mailing-lists")
48  public class MailingListsReport extends AbstractProjectInfoReport {
49  
50      @Inject
51      public MailingListsReport(RepositorySystem repositorySystem, I18N i18n, ProjectBuilder projectBuilder) {
52          super(repositorySystem, i18n, projectBuilder);
53      }
54      // ----------------------------------------------------------------------
55      // Public methods
56      // ----------------------------------------------------------------------
57  
58      @Override
59      public boolean canGenerateReport() throws MavenReportException {
60          boolean result = super.canGenerateReport();
61          if (result && skipEmptyReport) {
62              result = !isEmpty(getProject().getModel().getMailingLists());
63          }
64  
65          return result;
66      }
67  
68      @Override
69      public void executeReport(Locale locale) {
70          MailingListsRenderer r =
71                  new MailingListsRenderer(getLog(), getSink(), getProject().getModel(), getI18N(locale), locale);
72  
73          r.render();
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      public String getOutputName() {
80          return "mailing-lists";
81      }
82  
83      @Override
84      protected String getI18Nsection() {
85          return "mailing-lists";
86      }
87  
88      // ----------------------------------------------------------------------
89      // Private
90      // ----------------------------------------------------------------------
91  
92      /**
93       * Internal renderer class
94       */
95      protected static class MailingListsRenderer extends AbstractProjectInfoRenderer {
96  
97          private final Log log;
98          private final Model model;
99  
100         MailingListsRenderer(Log log, Sink sink, Model model, I18N i18n, Locale locale) {
101             super(sink, i18n, locale);
102             this.model = model;
103             this.log = log;
104         }
105 
106         @Override
107         protected String getI18Nsection() {
108             return "mailing-lists";
109         }
110 
111         @Override
112         protected void renderBody() {
113             List<MailingList> mailingLists = model.getMailingLists();
114 
115             if (mailingLists == null || mailingLists.isEmpty()) {
116                 startSection(getTitle());
117 
118                 paragraph(getI18nString("nolist"));
119 
120                 endSection();
121 
122                 return;
123             }
124 
125             startSection(getTitle());
126 
127             paragraph(getI18nString("intro"));
128 
129             startTable();
130 
131             // To beautify the display with other archives
132             boolean otherArchives = false;
133             for (MailingList m : mailingLists) {
134                 if (m.getOtherArchives() != null && !m.getOtherArchives().isEmpty()) {
135                     otherArchives = true;
136                 }
137             }
138 
139             String name = getI18nString("column.name");
140             String subscribe = getI18nString("column.subscribe");
141             String unsubscribe = getI18nString("column.unsubscribe");
142             String post = getI18nString("column.post");
143             String archive = getI18nString("column.archive");
144             String archivesOther = getI18nString("column.otherArchives");
145 
146             if (otherArchives) {
147                 tableHeader(new String[] {name, subscribe, unsubscribe, post, archive, archivesOther});
148             } else {
149                 tableHeader(new String[] {name, subscribe, unsubscribe, post, archive});
150             }
151 
152             for (MailingList mailingList : model.getMailingLists()) {
153                 List<String> textRow = new ArrayList<>();
154 
155                 if (StringUtils.isNotEmpty(mailingList.getName())) {
156                     textRow.add(mailingList.getName());
157                 } else {
158                     textRow.add("-");
159                 }
160 
161                 if (StringUtils.isNotEmpty(mailingList.getSubscribe())) {
162                     textRow.add(createURILinkPatternedText(subscribe, mailingList.getSubscribe(), null));
163                 } else {
164                     textRow.add("-");
165                 }
166 
167                 if (StringUtils.isNotEmpty(mailingList.getUnsubscribe())) {
168                     textRow.add(createURILinkPatternedText(unsubscribe, mailingList.getUnsubscribe(), null));
169                 } else {
170                     textRow.add("-");
171                 }
172 
173                 if (StringUtils.isNotEmpty(mailingList.getPost())) {
174                     textRow.add(createURILinkPatternedText(post, mailingList.getPost(), null));
175                 } else {
176                     textRow.add("-");
177                 }
178 
179                 if (mailingList.getArchive() != null
180                         && !mailingList.getArchive().isEmpty()) {
181                     textRow.add(createLinkPatternedText(
182                             ProjectInfoReportUtils.getArchiveServer(mailingList.getArchive()),
183                             mailingList.getArchive()));
184                 } else {
185                     textRow.add("-");
186                 }
187 
188                 if (mailingList.getOtherArchives() != null
189                         && !mailingList.getOtherArchives().isEmpty()) {
190                     // For the first line
191                     Iterator<String> it = mailingList.getOtherArchives().iterator();
192                     String otherArchive = it.next();
193 
194                     textRow.add(createLinkPatternedText(
195                             ProjectInfoReportUtils.getArchiveServer(otherArchive), otherArchive));
196 
197                     tableRow(textRow.toArray(new String[textRow.size()]));
198 
199                     // Other lines...
200                     while (it.hasNext()) {
201                         otherArchive = it.next();
202 
203                         // Reinit the list to beautify the display
204                         textRow = new ArrayList<>();
205 
206                         // Name
207                         textRow.add(" ");
208 
209                         // Subscribe
210                         textRow.add(" ");
211 
212                         // UnSubscribe
213                         textRow.add(" ");
214 
215                         // Post
216                         textRow.add(" ");
217 
218                         // Archive
219                         textRow.add(" ");
220 
221                         textRow.add(createLinkPatternedText(
222                                 ProjectInfoReportUtils.getArchiveServer(otherArchive), otherArchive));
223 
224                         tableRow(textRow.toArray(new String[textRow.size()]));
225                     }
226                 } else {
227                     if (otherArchives) {
228                         textRow.add(null);
229                     }
230 
231                     tableRow(textRow.toArray(new String[textRow.size()]));
232                 }
233             }
234 
235             endTable();
236 
237             endSection();
238         }
239 
240         /**
241          * Create a URI link pattern text for a mailing list. If no scheme is provided {@code mailto:}
242          * will be prepended by default. If href is null, then <code>defaultHref</code> is used instead.
243          *
244          * @param text a text.
245          * @param href the potential URI to use.
246          * @param defaultHref the String to use in case href is null.
247          * @return a link pattern.
248          * @see #createLinkPatternedText(String,String)
249          */
250         private String createURILinkPatternedText(String text, String href, String defaultHref) {
251             if (href == null || href.isEmpty()) {
252                 return createLinkPatternedText(text, defaultHref);
253             }
254 
255             try {
256                 URI hrefUri = URI.create(href);
257                 if (StringUtils.isNotEmpty(hrefUri.getScheme())) {
258                     return createLinkPatternedText(text, href);
259                 } else {
260                     return createLinkPatternedText(text, "mailto:" + href);
261                 }
262             } catch (IllegalArgumentException e) {
263                 log.warn("Invalid mailing list link provided '" + href + "': " + e.getMessage());
264                 return href;
265             }
266         }
267     }
268 }