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