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.plugins.annotations.Mojo;
26  import org.codehaus.plexus.i18n.I18N;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Locale;
33  
34  /**
35   * Generates the Mailing Lists report.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter </a>
38   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
39   * @since 2.0
40   */
41  @Mojo( name = "mailing-lists" )
42  public class MailingListsReport
43      extends AbstractProjectInfoReport
44  {
45      // ----------------------------------------------------------------------
46      // Public methods
47      // ----------------------------------------------------------------------
48  
49      @Override
50      public boolean canGenerateReport()
51      {
52          boolean result = super.canGenerateReport();
53          if ( result && skipEmptyReport )
54          {
55              result = !isEmpty( getProject().getModel().getMailingLists() );
56          }
57  
58          return result;
59      }
60  
61      @Override
62      public void executeReport( Locale locale )
63      {
64          MailingListsRenderer r =
65              new MailingListsRenderer( getSink(), getProject().getModel(), getI18N( locale ), locale );
66  
67          r.render();
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      public String getOutputName()
74      {
75          return "mailing-lists";
76      }
77  
78      @Override
79      protected String getI18Nsection()
80      {
81          return "mailing-lists";
82      }
83  
84      // ----------------------------------------------------------------------
85      // Private
86      // ----------------------------------------------------------------------
87  
88      /**
89       * Internal renderer class
90       */
91      protected static class MailingListsRenderer
92          extends AbstractProjectInfoRenderer
93      {
94          private final Model model;
95  
96          MailingListsRenderer( Sink sink, Model model, I18N i18n, Locale locale )
97          {
98              super( sink, i18n, locale );
99              this.model = model;
100 
101         }
102 
103         @Override
104         protected String getI18Nsection()
105         {
106             return "mailing-lists";
107         }
108 
109         @Override
110         public void renderBody()
111         {
112             List<MailingList> mailingLists = model.getMailingLists();
113 
114             if ( mailingLists == null || mailingLists.isEmpty() )
115             {
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             {
135                 if ( m.getOtherArchives() != null && !m.getOtherArchives().isEmpty() )
136                 {
137                     otherArchives = true;
138                 }
139             }
140 
141             String name = getI18nString( "column.name" );
142             String subscribe = getI18nString( "column.subscribe" );
143             String unsubscribe = getI18nString( "column.unsubscribe" );
144             String post = getI18nString( "column.post" );
145             String archive = getI18nString( "column.archive" );
146             String archivesOther = getI18nString( "column.otherArchives" );
147 
148             if ( otherArchives )
149             {
150                 tableHeader( new String[] { name, subscribe, unsubscribe, post, archive, archivesOther } );
151             }
152             else
153             {
154                 tableHeader( new String[] { name, subscribe, unsubscribe, post, archive } );
155             }
156 
157             for ( MailingList mailingList : model.getMailingLists() )
158             {
159                 List<String> textRow = new ArrayList<>();
160 
161                 // Validate here subsribe/unsubsribe lists and archives?
162                 textRow.add( mailingList.getName() );
163 
164                 textRow.add( createLinkPatternedText( subscribe, mailingList.getSubscribe() ) );
165 
166                 textRow.add( createLinkPatternedText( unsubscribe, mailingList.getUnsubscribe() ) );
167 
168                 if ( mailingList.getPost() != null && mailingList.getPost().length() > 0 )
169                 {
170                     textRow.add( createLinkPatternedText( post, mailingList.getPost() ) );
171                 }
172                 else
173                 {
174                     textRow.add( "-" );
175                 }
176 
177                 if ( mailingList.getArchive() != null && mailingList.getArchive().length() > 0 )
178                 {
179                     textRow.add( createLinkPatternedText( getArchiveServer( mailingList.getArchive() ),
180                                                           mailingList.getArchive() ) );
181                 }
182                 else
183                 {
184                     textRow.add( "-" );
185                 }
186 
187                 if ( mailingList.getOtherArchives() != null && !mailingList.getOtherArchives().isEmpty() )
188                 {
189                     // For the first line
190                     Iterator<String> it = mailingList.getOtherArchives().iterator();
191                     String otherArchive = it.next();
192 
193                     textRow.add( createLinkPatternedText( getArchiveServer( otherArchive ), otherArchive ) );
194 
195                     tableRow( textRow.toArray( new String[textRow.size()] ) );
196 
197                     // Other lines...
198                     while ( it.hasNext() )
199                     {
200                         otherArchive = it.next();
201 
202                         // Reinit the list to beautify the display
203                         textRow = new ArrayList<>();
204 
205                         // Name
206                         textRow.add( " " );
207 
208                         // Subscribe
209                         textRow.add( " " );
210 
211                         // UnSubscribe
212                         textRow.add( " " );
213 
214                         // Post
215                         textRow.add( " " );
216 
217                         // Archive
218                         textRow.add( " " );
219 
220                         textRow.add( createLinkPatternedText( getArchiveServer( otherArchive ), otherArchive ) );
221 
222                         tableRow( textRow.toArray( new String[textRow.size()] ) );
223                     }
224                 }
225                 else
226                 {
227                     if ( otherArchives )
228                     {
229                         textRow.add( null );
230                     }
231 
232                     tableRow( textRow.toArray( new String[textRow.size()] ) );
233                 }
234             }
235 
236             endTable();
237 
238             endSection();
239         }
240 
241         /**
242          * Convenience method to return the name of a web-based mailing list archive server. <br>
243          * For instance, if the archive uri is <code>http://www.mail-archive.com/dev@maven.apache.org</code>, this
244          * method return <code>www.mail-archive.com</code>
245          *
246          * @param uri
247          * @return the server name of a web-based mailing list archive server
248          */
249         private static String getArchiveServer( String uri )
250         {
251             if ( StringUtils.isEmpty( uri ) )
252             {
253                 return "???UNKNOWN???";
254             }
255 
256             int at = uri.indexOf( "//" );
257             int fromIndex;
258             if ( at >= 0 )
259             {
260                 fromIndex = uri.lastIndexOf( '/', at - 1 ) >= 0 ? 0 : at + 2;
261             }
262             else
263             {
264                 fromIndex = 0;
265             }
266 
267             int from = uri.indexOf( '/', fromIndex );
268 
269             if ( from == -1 )
270             {
271                 return uri.substring( at + 2 );
272             }
273 
274             return uri.substring( at + 2, from );
275         }
276     }
277 }