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