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                 if ( StringUtils.isNotEmpty( mailingList.getName() ) )
162                 {
163                     textRow.add( mailingList.getName() );
164                 }
165                 else
166                 {
167                     textRow.add( "-" );
168                 }
169 
170                 if ( StringUtils.isNotEmpty( mailingList.getSubscribe() ) )
171                 {
172                     textRow.add( createEmailLinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
173                 }
174                 else
175                 {
176                     textRow.add( "-" );
177                 }
178 
179                 if ( StringUtils.isNotEmpty( mailingList.getUnsubscribe() ) )
180                 {
181                     textRow.add( createEmailLinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
182                 }
183                 else
184                 {
185                     textRow.add( "-" );
186                 }
187 
188                 if ( StringUtils.isNotEmpty( mailingList.getPost() ) )
189                 {
190                     textRow.add( createEmailLinkPatternedText( post, mailingList.getPost(), null ) );
191                 }
192                 else
193                 {
194                     textRow.add( "-" );
195                 }
196 
197                 if ( mailingList.getArchive() != null && !mailingList.getArchive().isEmpty() )
198                 {
199                     textRow.add( createLinkPatternedText(
200                             ProjectInfoReportUtils.getArchiveServer( mailingList.getArchive() ),
201                             mailingList.getArchive() ) );
202                 }
203                 else
204                 {
205                     textRow.add( "-" );
206                 }
207 
208                 if ( mailingList.getOtherArchives() != null && !mailingList.getOtherArchives().isEmpty() )
209                 {
210                     // For the first line
211                     Iterator<String> it = mailingList.getOtherArchives().iterator();
212                     String otherArchive = it.next();
213 
214                     textRow.add( createLinkPatternedText(
215                             ProjectInfoReportUtils.getArchiveServer( otherArchive ), otherArchive ) );
216 
217                     tableRow( textRow.toArray( new String[textRow.size()] ) );
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<>();
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(
243                                 ProjectInfoReportUtils.getArchiveServer( otherArchive ), otherArchive ) );
244 
245                         tableRow( textRow.toArray( new String[textRow.size()] ) );
246                     }
247                 }
248                 else
249                 {
250                     if ( otherArchives )
251                     {
252                         textRow.add( null );
253                     }
254 
255                     tableRow( textRow.toArray( new String[textRow.size()] ) );
256                 }
257             }
258 
259             endTable();
260 
261             endSection();
262         }
263 
264         /**
265          * Create a link pattern text for email addresses defined by <code>{text, mailto:href}</code>. If href is null,
266          * then <code>defaultHref</code> is used instead.
267          *
268          * @param text a text.
269          * @param href the email address to use.
270          * @param defaultHref the String to use in case href is null.
271          * @return an email link pattern.
272          * @see #createLinkPatternedText(String,String)
273          */
274         private String createEmailLinkPatternedText( String text, String href, String defaultHref )
275         {
276             if ( href == null || href.isEmpty() )
277             {
278                 return createLinkPatternedText( text, defaultHref );
279             }
280             return createLinkPatternedText( text,
281                     href.toLowerCase( Locale.ENGLISH ).startsWith( "mailto:" ) ? href : "mailto:" + href );
282         }
283     }
284 }