View Javadoc

1   package net.sf.statcvs.pages.xml;
2   
3   /**
4    * @author Nilendra Weerasinghe (nilendraw@gmail.com)
5    * @version $Id: FilesXml.java,v 1.5 2008/04/02 11:52:02 benoitx Exp $
6    */
7   
8   import java.text.DecimalFormat;
9   import java.text.NumberFormat;
10  import java.util.Iterator;
11  
12  import net.sf.statcvs.model.Repository;
13  import net.sf.statcvs.model.VersionedFile;
14  import net.sf.statcvs.output.ReportConfig;
15  import net.sf.statcvs.renderer.XMLRenderer;
16  import net.sf.statcvs.reportmodel.Column;
17  import net.sf.statcvs.reportmodel.Table;
18  import net.sf.statcvs.reports.FileTypeReport;
19  import net.sf.statcvs.reports.FilesWithMostRevisionsTableReport;
20  import net.sf.statcvs.reports.LargestFilesTableReport;
21  import net.sf.statcvs.reports.TableReport;
22  
23  import org.jdom.Element;
24  
25  public class FilesXml {
26      private final static NumberFormat[] DOUBLE_FORMATS = { new DecimalFormat("0"), new DecimalFormat("0.0"), new DecimalFormat("0.00"),
27              new DecimalFormat("0.000"), new DecimalFormat("0.0000") };
28      private static final int NO_OF_COLS_IN_EXT_TABLE = 4;
29      private static final int NO_OF_COLS_IN_LARG_TABLE = 4;
30      private static final int MAX_LARGEST_FILES = 40;
31      private Table table;
32      private final XMLRenderer renderer = new XMLRenderer();
33  
34      private final ReportConfig config;
35      private final Repository repository;
36  
37      /**
38       * @param config Configuration and data for the report suite
39       */
40      public FilesXml(final ReportConfig config) {
41          this.config = config;
42          this.repository = config.getRepository();
43      }
44  
45      /**
46       * returns jdom element which contains data regarding the files of the repository
47       *
48       * @returns Element
49       */
50      public Element toFile() {
51          final Element fileStats = new Element("FileStats");
52          final Element summ = new Element(XmlTags.TAG_SUMMARY);
53          summ.addContent(new Element(XmlTags.TAG_TOTAL_FILES).setText(Integer.toString(getCurrentFileCount())));
54          summ.addContent(new Element(XmlTags.TAG_AVG_FILE_SIZE).setText(DOUBLE_FORMATS[1].format(getCurrentAverageFileSize()) + " lines"));
55          summ.addContent(new Element(XmlTags.TAG_AVG_REVISIONS_PER_FILE).setText(Double.toString(getCurrentAverageRevisionCount())));
56          fileStats.addContent(summ);
57          fileStats.addContent(fileExts());
58          fileStats.addContent(largestFiles());
59          fileStats.addContent(mostRevs());
60  
61          return fileStats;
62      }
63  
64      /**
65       * returns jdom element which contains data about files grouped by extension
66       *
67       * @returns Element
68       */
69      private Element fileExts() {
70          final Element ele = new Element(XmlTags.TAG_EXTENSIONS);
71          final TableReport tr = new FileTypeReport(this.config);
72          tr.calculate();
73          this.table = tr.getTable();
74          final String[] str = new String[NO_OF_COLS_IN_EXT_TABLE];
75          for (int j = 0; j < table.getRowCount(); j++) {
76              Element col = null;
77              int i = 0;
78              final Iterator it = table.getColumnIterator();
79              final Iterator itr = table.getColumnIterator();
80              while (it.hasNext()) {
81                  final Column column = (Column) it.next();
82                  column.renderHead(renderer);
83                  str[i] = renderer.getColumnHead();
84                  if (i == 0) {
85                      col = new Element(str[i]);
86                  } else {
87                      col.addContent(new Element(str[i]));
88                  }
89                  i++;
90              }
91              boolean isFirstColumn = true;
92              int k = 0;
93              while (itr.hasNext()) {
94                  final Column column = (Column) itr.next();
95                  column.renderCell(j, renderer);
96                  if (isFirstColumn && table.hasKeysInFirstColumn()) {
97                      col.setAttribute("ext", renderer.getRowHead().toLowerCase());
98                      isFirstColumn = false;
99                  } else {
100                     col.getChild(str[k]).setText(renderer.getTableCell());
101                 }
102                 k++;
103             }
104             ele.addContent(col);
105         }
106         return ele;
107     }
108 
109     /**
110      * returns jdom element which contains data of the largest files in terms of LOC of the repository
111      *
112      * @returns Element
113      */
114     public Element largestFiles() {
115         final Element larg = new Element(XmlTags.TAG_LARGEST_FILES);
116         final TableReport largestFilesTable = new LargestFilesTableReport(this.config, this.repository.getFiles(), MAX_LARGEST_FILES);
117         largestFilesTable.calculate();
118         this.table = largestFilesTable.getTable();
119         final String[] str = new String[NO_OF_COLS_IN_LARG_TABLE];
120         for (int j = 0; j < table.getRowCount(); j++) {
121             Element col = null;
122             int i = 0;
123             final Iterator it = table.getColumnIterator();
124             final Iterator itr = table.getColumnIterator();
125             while (it.hasNext()) {
126                 final Column column = (Column) it.next();
127                 column.renderHead(renderer);
128                 str[i] = renderer.getColumnHead();
129                 if (i == 0) {
130                     col = new Element(str[i]);
131                 } else {
132                     str[i] = renderer.getColumnHead().replaceAll(" ", "_");
133                     //col.addContent(new Element (str[i]));
134                 }
135                 i++;
136             }
137 
138             boolean isFirstColumn = true;
139             int k = 0;
140             while (itr.hasNext()) {
141                 final Column column = (Column) itr.next();
142                 column.renderCell(j, renderer);
143                 if (isFirstColumn && table.hasKeysInFirstColumn()) {
144                     col.setText(/*removeHTML(*/renderer.getRowHead()/*)*/);
145                     isFirstColumn = false;
146                 } else {
147                     col.setAttribute(str[k].toLowerCase(), renderer.getTableCell());
148                 }
149                 k++;
150             }
151             larg.addContent(col);
152         }
153         return larg;
154     }
155 
156     /**
157      * returns jdom element which contains data of the files with most revisions
158      *
159      * @returns Element
160      */
161     public Element mostRevs() {
162         final Element revs = new Element(XmlTags.TAG_MOST_REVISIONS);
163         final TableReport mostRevs = new FilesWithMostRevisionsTableReport(this.config, this.repository.getFiles(), MAX_LARGEST_FILES);
164         mostRevs.calculate();
165         this.table = mostRevs.getTable();
166         final String[] str = new String[NO_OF_COLS_IN_LARG_TABLE];
167         for (int j = 0; j < table.getRowCount(); j++) {
168             Element col = null;
169             int i = 0;
170             final Iterator it = table.getColumnIterator();
171             final Iterator itr = table.getColumnIterator();
172             while (it.hasNext()) {
173                 final Column column = (Column) it.next();
174                 column.renderHead(renderer);
175                 if (i == 0) {
176                     str[i] = renderer.getColumnHead();
177                     col = new Element(str[i]);
178                 } else {
179                     str[i] = renderer.getColumnHead().replaceAll(" ", "_");
180                     //col.addContent(new Element (str[i]));
181                 }
182                 i++;
183             }
184 
185             boolean isFirstColumn = true;
186             int k = 0;
187             while (itr.hasNext()) {
188                 final Column column = (Column) itr.next();
189                 column.renderCell(j, renderer);
190                 if (isFirstColumn && table.hasKeysInFirstColumn()) {
191                     col.setText(/*removeHTML(*/renderer.getRowHead());
192                     isFirstColumn = false;
193                 } else {
194                     col.setAttribute(str[k].toLowerCase(), renderer.getTableCell());
195                 }
196                 k++;
197             }
198             revs.addContent(col);
199         }
200         return revs;
201     }
202 
203     /**
204      * returns Integer of the number of files
205      *
206      * @returns int
207      */
208     private int getCurrentFileCount() {
209         int result = 0;
210         final Iterator it = this.repository.getFiles().iterator();
211         while (it.hasNext()) {
212             final VersionedFile file = (VersionedFile) it.next();
213             if (!file.isDead()) {
214                 result++;
215             }
216         }
217         return result;
218     }
219 
220     /**
221      * returns the average size of a file in terms of LOC
222      *
223      * @returns double
224      */
225     private double getCurrentAverageFileSize() {
226         return ((double) this.repository.getCurrentLOC()) / getCurrentFileCount();
227     }
228 
229     /**
230      * returns number of revisions done on average
231      *
232      * @returns double
233      */
234     private double getCurrentAverageRevisionCount() {
235         int revisions = 0;
236         final Iterator it = this.repository.getFiles().iterator();
237         while (it.hasNext()) {
238             final VersionedFile file = (VersionedFile) it.next();
239             if (!file.isDead()) {
240                 revisions += file.getRevisions().size();
241             }
242         }
243         return ((double) revisions) / getCurrentFileCount();
244     }
245 }