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