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