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