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.apache.maven.plugins.annotations.Parameter;
28  import org.codehaus.plexus.i18n.I18N;
29  import org.codehaus.plexus.util.StringUtils;
30  
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 List 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   * @version $Id: MailingListsReport.html 935177 2015-01-05 21:05:55Z michaelo $
42   * @since 2.0
43   */
44  @Mojo( name = "mailing-list" )
45  public class MailingListsReport
46      extends AbstractProjectInfoReport
47  {
48  
49      /**
50       * This can override the header text of the mailing list(s) report
51       *
52       * @since 2.2
53       * @deprecated since 2.3, you should use a custom bundle.
54       */
55      @Parameter
56      protected String introduction;
57  
58      // ----------------------------------------------------------------------
59      // Public methods
60      // ----------------------------------------------------------------------
61  
62      @Override
63      public boolean canGenerateReport()
64      {
65          boolean result = super.canGenerateReport();
66          if ( result && skipEmptyReport )
67          {
68              result = !isEmpty( getProject().getModel().getMailingLists() );
69          }
70  
71          return result;
72      }
73  
74      @Override
75      public void executeReport( Locale locale )
76      {
77          MailingListsRenderer r =
78              new MailingListsRenderer( getSink(), getProject().getModel(), getI18N( locale ), locale, introduction,
79                                        getLog() );
80  
81          r.render();
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      public String getOutputName()
88      {
89          return "mail-lists";
90      }
91  
92      @Override
93      protected String getI18Nsection()
94      {
95          return "mailing-lists";
96      }
97  
98      // ----------------------------------------------------------------------
99      // Private
100     // ----------------------------------------------------------------------
101 
102     /**
103      * Internal renderer class
104      */
105     protected static class MailingListsRenderer
106         extends AbstractProjectInfoRenderer
107     {
108         private static final String[] EMPTY_STRING_ARRAY = new String[0];
109 
110         private final Model model;
111 
112         private final String introduction;
113 
114         private final Log log;
115 
116         MailingListsRenderer( Sink sink, Model model, I18N i18n, Locale locale, String introduction, Log log )
117         {
118             super( sink, i18n, locale );
119 
120             this.model = model;
121 
122             this.introduction = introduction;
123 
124             this.log = log;
125         }
126 
127         @Override
128         protected String getI18Nsection()
129         {
130             return "mailing-lists";
131         }
132 
133         @Override
134         public void renderBody()
135         {
136             List<MailingList> mailingLists = model.getMailingLists();
137 
138             if ( mailingLists == null || mailingLists.isEmpty() )
139             {
140                 startSection( getTitle() );
141 
142                 paragraph( getI18nString( "nolist" ) );
143 
144                 endSection();
145 
146                 return;
147             }
148 
149             startSection( getTitle() );
150 
151             if ( StringUtils.isNotBlank( introduction ) )
152             {
153                 log.warn( "Since 2.3, the <introduction/> parameter is deprecated. Please use a <customBundle/>"
154                     + " parameter to configure a custom bundle." );
155                 paragraph( introduction );
156             }
157             else
158             {
159                 paragraph( getI18nString( "intro" ) );
160             }
161 
162             startTable();
163 
164             // To beautify the display with other archives
165             boolean otherArchives = false;
166             for ( MailingList m : mailingLists )
167             {
168                 if ( m.getOtherArchives() != null && !m.getOtherArchives().isEmpty() )
169                 {
170                     otherArchives = true;
171                 }
172             }
173 
174             String name = getI18nString( "column.name" );
175             String subscribe = getI18nString( "column.subscribe" );
176             String unsubscribe = getI18nString( "column.unsubscribe" );
177             String post = getI18nString( "column.post" );
178             String archive = getI18nString( "column.archive" );
179             String archivesOther = getI18nString( "column.otherArchives" );
180 
181             if ( otherArchives )
182             {
183                 tableHeader( new String[] { name, subscribe, unsubscribe, post, archive, archivesOther } );
184             }
185             else
186             {
187                 tableHeader( new String[] { name, subscribe, unsubscribe, post, archive } );
188             }
189 
190             for ( MailingList mailingList : model.getMailingLists() )
191             {
192                 List<String> textRow = new ArrayList<String>();
193 
194                 // Validate here subsribe/unsubsribe lists and archives?
195                 textRow.add( mailingList.getName() );
196 
197                 textRow.add( createLinkPatternedText( subscribe, mailingList.getSubscribe() ) );
198 
199                 textRow.add( createLinkPatternedText( unsubscribe, mailingList.getUnsubscribe() ) );
200 
201                 if ( mailingList.getPost() != null && mailingList.getPost().length() > 0 )
202                 {
203                     textRow.add( createLinkPatternedText( post, mailingList.getPost() ) );
204                 }
205                 else
206                 {
207                     textRow.add( "-" );
208                 }
209 
210                 if ( mailingList.getArchive() != null && mailingList.getArchive().length() > 0 )
211                 {
212                     textRow.add( createLinkPatternedText( getArchiveServer( mailingList.getArchive() ),
213                                                           mailingList.getArchive() ) );
214                 }
215                 else
216                 {
217                     textRow.add( "-" );
218                 }
219 
220                 if ( mailingList.getOtherArchives() != null && !mailingList.getOtherArchives().isEmpty() )
221                 {
222                     // For the first line
223                     Iterator<String> it = mailingList.getOtherArchives().iterator();
224                     String otherArchive = it.next();
225 
226                     textRow.add( createLinkPatternedText( getArchiveServer( otherArchive ), otherArchive ) );
227 
228                     tableRow( textRow.toArray( new String[textRow.size()] ) );
229 
230                     // Other lines...
231                     while ( it.hasNext() )
232                     {
233                         otherArchive = it.next();
234 
235                         // Reinit the list to beautify the display
236                         textRow = new ArrayList<String>();
237 
238                         // Name
239                         textRow.add( " " );
240 
241                         // Subscribe
242                         textRow.add( " " );
243 
244                         // UnSubscribe
245                         textRow.add( " " );
246 
247                         // Post
248                         textRow.add( " " );
249 
250                         // Archive
251                         textRow.add( " " );
252 
253                         textRow.add( createLinkPatternedText( getArchiveServer( otherArchive ), otherArchive ) );
254 
255                         tableRow( textRow.toArray( new String[textRow.size()] ) );
256                     }
257                 }
258                 else
259                 {
260                     if ( otherArchives )
261                     {
262                         textRow.add( null );
263                     }
264 
265                     tableRow( textRow.toArray( new String[textRow.size()] ) );
266                 }
267             }
268 
269             endTable();
270 
271             endSection();
272         }
273 
274         /**
275          * Convenience method to return the name of a web-based mailing list archive server. <br>
276          * For instance, if the archive uri is <code>http://www.mail-archive.com/dev@maven.apache.org</code>, this
277          * method return <code>www.mail-archive.com</code>
278          *
279          * @param uri
280          * @return the server name of a web-based mailing list archive server
281          */
282         private static String getArchiveServer( String uri )
283         {
284             if ( StringUtils.isEmpty( uri ) )
285             {
286                 return "???UNKNOWN???";
287             }
288 
289             int at = uri.indexOf( "//" );
290             int fromIndex;
291             if ( at >= 0 )
292             {
293                 fromIndex = uri.lastIndexOf( "/", at - 1 ) >= 0 ? 0 : at + 2;
294             }
295             else
296             {
297                 fromIndex = 0;
298             }
299 
300             int from = uri.indexOf( "/", fromIndex );
301 
302             if ( from == -1 )
303             {
304                 return uri.substring( at + 2 );
305             }
306 
307             return uri.substring( at + 2, from );
308         }
309     }
310 }