1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45
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
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
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
90
91
92
93
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
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
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
200 while (it.hasNext()) {
201 otherArchive = it.next();
202
203
204 textRow = new ArrayList<>();
205
206
207 textRow.add(" ");
208
209
210 textRow.add(" ");
211
212
213 textRow.add(" ");
214
215
216 textRow.add(" ");
217
218
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
242
243
244
245
246
247
248
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 }