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