View Javadoc

1   package org.apache.maven.plugin.changelog;
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.scm.ChangeFile;
24  import org.apache.maven.scm.ChangeSet;
25  import org.apache.maven.scm.command.changelog.ChangeLogSet;
26  
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.LinkedList;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.Map;
35  import java.util.ResourceBundle;
36  
37  /**
38   * Generate a file activity report.
39   *
40   * @version $Id: FileActivityReport.java 803834 2009-08-13 10:32:13Z vsiveton $
41   * @goal file-activity
42   */
43  public class FileActivityReport
44      extends ChangeLogReport
45  {
46      /** {@inheritDoc} */
47      public String getDescription( Locale locale )
48      {
49          return getBundle( locale ).getString( "report.file-activity.description" );
50      }
51  
52      /** {@inheritDoc} */
53      public String getName( Locale locale )
54      {
55          return getBundle( locale ).getString( "report.file-activity.name" );
56      }
57  
58      /** {@inheritDoc} */
59      public String getOutputName()
60      {
61          return "file-activity";
62      }
63  
64      /** {@inheritDoc} */
65      protected void doGenerateEmptyReport( ResourceBundle bundle, Sink sink )
66      {
67          sink.head();
68          sink.title();
69          sink.text( bundle.getString( "report.file-activity.header" ) );
70          sink.title_();
71          sink.head_();
72  
73          sink.body();
74          sink.section1();
75  
76          sink.sectionTitle1();
77          sink.text( bundle.getString( "report.file-activity.mainTitle" ) );
78          sink.sectionTitle1_();
79  
80          sink.paragraph();
81          sink.text( "No sources found to create a report." );
82          sink.paragraph_();
83  
84          sink.section1_();
85  
86          sink.body_();
87          sink.flush();
88          sink.close();
89      }
90  
91      /** {@inheritDoc} */
92      protected void doGenerateReport( List changeLogSets, ResourceBundle bundle, Sink sink )
93      {
94          sink.head();
95          sink.title();
96          sink.text( bundle.getString( "report.file-activity.header" ) );
97          sink.title_();
98          sink.head_();
99  
100         sink.body();
101         sink.section1();
102         sink.sectionTitle1();
103         sink.text( bundle.getString( "report.file-activity.mainTitle" ) );
104         sink.sectionTitle1_();
105 
106         for ( Iterator sets = changeLogSets.iterator(); sets.hasNext(); )
107         {
108             ChangeLogSet set = (ChangeLogSet) sets.next();
109             doChangedSets( set, bundle, sink );
110         }
111 
112         sink.section1_();
113         sink.body_();
114 
115         sink.flush();
116         sink.close();
117     }
118 
119     /**
120      * generates a section of the report referring to a changeset
121      *
122      * @param set    the current ChangeSet to generate this section of the report
123      * @param bundle the resource bundle to retrieve report phrases from
124      * @param sink   the report formatting tool
125      */
126     private void doChangedSets( ChangeLogSet set, ResourceBundle bundle, Sink sink )
127     {
128         sink.section2();
129 
130         doChangeSetTitle( set, bundle, sink );
131 
132         doSummary( set, bundle, sink );
133 
134         sink.table();
135 
136         sink.tableRow();
137         sink.tableHeaderCell();
138         sink.text( bundle.getString( "report.file-activity.filename" ) );
139         sink.tableHeaderCell_();
140         sink.tableHeaderCell();
141         sink.text( bundle.getString( "report.file-activity.timesChanged" ) );
142         sink.tableHeaderCell_();
143         sink.tableRow_();
144 
145         doRows( set, sink );
146 
147         sink.table_();
148 
149         sink.section2_();
150     }
151 
152     /**
153      * generates the row details for the file activity report
154      *
155      * @param set  the changelog set to generate a report with
156      * @param sink the report formatting tool
157      */
158     private void doRows( ChangeLogSet set, Sink sink )
159     {
160         List list = getOrderedFileList( set.getChangeSets() );
161 
162         initReportUrls();
163 
164         for ( Iterator i = list.iterator(); i.hasNext(); )
165         {
166             List revision = (List) i.next();
167             ChangeFile file = (ChangeFile) revision.get( 0 );
168 
169             sink.tableRow();
170             sink.tableCell();
171 
172             try
173             {
174                 generateLinks( getConnection(), file.getName(), sink );
175             }
176             catch ( Exception e )
177             {
178                 if ( getLog().isDebugEnabled() )
179                 {
180                     getLog().error( e.getMessage(), e );
181                 }
182                 else
183                 {
184                     getLog().error( e.getMessage() );
185                 }
186 
187                 sink.text( file.getName() );
188             }
189             sink.tableCell_();
190 
191             sink.tableCell();
192             sink.text( "" + revision.size() );
193 
194             sink.tableCell_();
195             sink.tableRow_();
196         }
197     }
198 
199     /**
200      * reads the change log entries and generates a list of files changed order by the number of times edited. This
201      * used the FileActivityComparator Object to sort the list
202      *
203      * @param entries the changelog entries to generate the report
204      * @return list of changed files within the SCM with the number of times changed in descending order
205      */
206     private List getOrderedFileList( Collection entries )
207     {
208         List list = new LinkedList();
209 
210         Map map = new HashMap();
211 
212         for ( Iterator i = entries.iterator(); i.hasNext(); )
213         {
214             ChangeSet entry = (ChangeSet) i.next();
215 
216             for ( Iterator j = entry.getFiles().iterator(); j.hasNext(); )
217             {
218                 ChangeFile file = (ChangeFile) j.next();
219 
220                 List revisions;
221 
222                 if ( map.containsKey( file.getName() ) )
223                 {
224                     revisions = (List) map.get( file.getName() );
225                 }
226                 else
227                 {
228                     revisions = new LinkedList();
229                 }
230 
231                 revisions.add( file );
232 
233                 map.put( file.getName(), revisions );
234             }
235         }
236 
237         list.addAll( map.values() );
238 
239         Collections.sort( list, new FileActivityComparator() );
240 
241         return list;
242     }
243 }