View Javadoc
1   package org.codehaus.mojo.jdepend;
2   
3   /*
4    * #%L
5    * JDepend Maven Plugin
6    * %%
7    * Copyright (C) 2006 - 2014 Codehaus
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.apache.maven.doxia.sink.Sink;
24  import org.codehaus.mojo.jdepend.objects.CyclePackage;
25  import org.codehaus.mojo.jdepend.objects.JDPackage;
26  import org.codehaus.mojo.jdepend.objects.Stats;
27  
28  import java.util.List;
29  import java.util.ResourceBundle;
30  
31  public class ReportGenerator
32  {
33      private JDependXMLReportParser jdepend;
34  
35      private JDPackage jdpackage;
36  
37      private Stats stats;
38  
39      private List list;
40  
41      private CyclePackage cyclepackage;
42  
43      /**
44       * Creates a new instance of ReportGenerator.
45       */
46      public ReportGenerator()
47      {
48      }
49  
50      public void doGenerateReport( ResourceBundle bundle, Sink sink, JDependXMLReportParser jdepend )
51      {
52          this.jdepend = jdepend;
53  
54          sink.head();
55          sink.title();
56          sink.text( bundle.getString( "report.title" ) ); //$NON-NLS-1$
57          sink.title_();
58          sink.head_();
59  
60          sink.body();
61          sink.section1();
62  
63          sink.sectionTitle1();
64          sink.text( bundle.getString( "report.metricresults" ) ); //$NON-NLS-1$
65          sink.sectionTitle1_();
66  
67          doSectionLinks( bundle, sink );
68  
69          sink.lineBreak();
70          sink.lineBreak();
71  
72          sink.text( bundle.getString( "report.intro" ) ); //$NON-NLS-1$
73  
74          sink.lineBreak();
75          sink.lineBreak();
76  
77          doSummarySection( bundle, sink );
78  
79          doPackagesSection( bundle, sink );
80  
81          doCycleSection( bundle, sink );
82  
83          doExplanationSection( bundle, sink );
84  
85          sink.section1_();
86          sink.body_();
87          sink.flush();
88          sink.close();
89  
90      }
91  
92      public void doSectionLinks( ResourceBundle bundle, Sink sink )
93      {
94  
95          sink.text( bundle.getString( "report.square-open" ) ); //$NON-NLS-1$
96          sink.link( bundle.getString( "report.summary.anchor" ) ); //$NON-NLS-1$
97          sink.text( bundle.getString( "report.summary.text" ) ); //$NON-NLS-1$
98          sink.link_();
99          sink.text( bundle.getString( "report.square-close" ) ); //$NON-NLS-1$
100 
101         sink.text( bundle.getString( "report.square-open" ) ); //$NON-NLS-1$
102         sink.link( bundle.getString( "report.packages.anchor" ) ); //$NON-NLS-1$
103         sink.text( bundle.getString( "report.packages.text" ) ); //$NON-NLS-1$
104         sink.link_();
105         sink.text( bundle.getString( "report.square-close" ) ); //$NON-NLS-1$
106 
107         sink.text( bundle.getString( "report.square-open" ) ); //$NON-NLS-1$
108         sink.link( bundle.getString( "report.cycles.anchor" ) ); //$NON-NLS-1$
109         sink.text( bundle.getString( "report.cycles.text" ) ); //$NON-NLS-1$
110         sink.link_();
111         sink.text( bundle.getString( "report.square-close" ) ); //$NON-NLS-1$
112 
113         sink.text( bundle.getString( "report.square-open" ) ); //$NON-NLS-1$
114         sink.link( bundle.getString( "report.explanations.anchor" ) ); //$NON-NLS-1$
115         sink.text( bundle.getString( "report.explanations.text" ) ); //$NON-NLS-1$
116         sink.link_();
117         sink.text( bundle.getString( "report.square-close" ) ); //$NON-NLS-1$
118 
119     }
120 
121     public void doSummarySection( ResourceBundle bundle, Sink sink )
122     {
123 
124         sink.anchor( bundle.getString( "report.summary.text" ) ); //$NON-NLS-1$
125         sink.anchor_();
126 
127         sink.sectionTitle1();
128         sink.text( bundle.getString( "report.summary.title" ) ); //$NON-NLS-1$
129         sink.sectionTitle1_();
130 
131         doSectionLinks( bundle, sink );
132 
133         sink.lineBreak();
134         sink.lineBreak();
135 
136         sink.table();
137         generateHeaderRow( bundle, sink );
138 
139         list = jdepend.packages;
140 
141         for ( int i = 0; i < list.size(); i++ )
142         {
143             jdpackage = (JDPackage) list.get( i );
144 
145             stats = jdpackage.getStats();
146 
147             sink.tableRow();
148             sink.tableCell();
149             sink.link( bundle.getString( "report.dash" ) + jdpackage.getPackageName() ); //$NON-NLS-1$
150             sink.text( jdpackage.getPackageName() );
151             sink.link_();
152             sink.tableCell_();
153 
154             sink.tableCell();
155             sink.text( stats.getTotalClasses() );
156             sink.tableCell_();
157 
158             sink.tableCell();
159             sink.text( stats.getConcreteClasses() );
160             sink.tableCell_();
161 
162             sink.tableCell();
163             sink.text( stats.getAbstractClasses() );
164             sink.tableCell_();
165 
166             sink.tableCell();
167             sink.text( stats.getCa() );
168             sink.tableCell_();
169 
170             sink.tableCell();
171             sink.text( stats.getCe() );
172             sink.tableCell_();
173 
174             sink.tableCell();
175             sink.text( convertToPercent( stats.getA() ) );
176             sink.tableCell_();
177 
178             sink.tableCell();
179             sink.text( convertToPercent( stats.getI() ) );
180             sink.tableCell_();
181 
182             sink.tableCell();
183             sink.text( convertToPercent( stats.getD() ) );
184             sink.tableCell_();
185 
186             sink.tableCell();
187             sink.text( stats.getV() );
188             sink.tableCell_();
189 
190             sink.tableRow_();
191         }
192 
193         sink.table_();
194 
195     }
196 
197     private void generateHeaderRow( ResourceBundle bundle, Sink sink )
198     {
199         sink.tableRow();
200 
201         sink.tableHeaderCell();
202         sink.text( bundle.getString( "report.package.title" ) ); //$NON-NLS-1$
203         sink.tableHeaderCell_();
204 
205         sink.tableHeaderCell();
206         sink.text( bundle.getString( "report.TC" ) ); //$NON-NLS-1$
207         sink.tableHeaderCell_();
208 
209         sink.tableHeaderCell();
210         sink.text( bundle.getString( "report.CC" ) ); //$NON-NLS-1$
211         sink.tableHeaderCell_();
212 
213         sink.tableHeaderCell();
214         sink.text( bundle.getString( "report.AC" ) ); //$NON-NLS-1$
215         sink.tableHeaderCell_();
216 
217         sink.tableHeaderCell();
218         sink.text( bundle.getString( "report.Ca" ) ); //$NON-NLS-1$
219         sink.tableHeaderCell_();
220 
221         sink.tableHeaderCell();
222         sink.text( bundle.getString( "report.Ce" ) ); //$NON-NLS-1$
223         sink.tableHeaderCell_();
224 
225         sink.tableHeaderCell();
226         sink.text( bundle.getString( "report.A" ) ); //$NON-NLS-1$
227         sink.tableHeaderCell_();
228 
229         sink.tableHeaderCell();
230         sink.text( bundle.getString( "report.I" ) ); //$NON-NLS-1$
231         sink.tableHeaderCell_();
232 
233         sink.tableHeaderCell();
234         sink.text( bundle.getString( "report.D" ) ); //$NON-NLS-1$
235         sink.tableHeaderCell_();
236 
237         sink.tableHeaderCell();
238         sink.text( bundle.getString( "report.V" ) ); //$NON-NLS-1$
239         sink.tableHeaderCell_();
240 
241         sink.tableRow_();
242     }
243 
244     public void doPackagesSection( ResourceBundle bundle, Sink sink )
245     {
246 
247         sink.anchor( bundle.getString( "report.packages.text" ) ); //$NON-NLS-1$
248         sink.anchor_();
249 
250         sink.sectionTitle1();
251         sink.text( bundle.getString( "report.packages.title" ) ); //$NON-NLS-1$
252         sink.sectionTitle1_();
253 
254         doSectionLinks( bundle, sink );
255 
256         sink.lineBreak();
257 
258         doPackage( bundle, sink );
259 
260     }
261 
262     public void doPackage( ResourceBundle bundle, Sink sink )
263     {
264         list = jdepend.packages;
265 
266         if ( list.size() <= 0 )
267         {
268             sink.text( bundle.getString( "report.nopackages" ) ); //$NON-NLS-1$
269             sink.lineBreak();
270         }
271         else
272         {
273             for ( int i = 0; i < list.size(); i++ )
274             {
275                 jdpackage = (JDPackage) list.get( i );
276 
277                 stats = jdpackage.getStats();
278 
279                 sink.anchor( jdpackage.getPackageName() );
280                 sink.anchor_();
281 
282                 sink.sectionTitle2();
283                 sink.text( jdpackage.getPackageName() );
284                 sink.sectionTitle2_();
285 
286                 sink.table();
287 
288                 /* Headers */
289 
290                 sink.tableRow();
291 
292                 sink.tableHeaderCell();
293                 sink.text( bundle.getString( "report.afferentcouplings.title" ) ); //$NON-NLS-1$
294                 sink.tableHeaderCell_();
295 
296                 sink.tableHeaderCell();
297                 sink.text( bundle.getString( "report.efferentcouplings.title" ) ); //$NON-NLS-1$
298                 sink.tableHeaderCell_();
299 
300                 sink.tableHeaderCell();
301                 sink.text( bundle.getString( "report.abstractness.title" ) ); //$NON-NLS-1$
302                 sink.tableHeaderCell_();
303 
304                 sink.tableHeaderCell();
305                 sink.text( bundle.getString( "report.instability.title" ) ); //$NON-NLS-1$
306                 sink.tableHeaderCell_();
307 
308                 sink.tableHeaderCell();
309                 sink.text( bundle.getString( "report.distance.title" ) ); //$NON-NLS-1$
310                 sink.tableHeaderCell_();
311 
312                 sink.tableRow_();
313 
314                 /* Values */
315 
316                 sink.tableRow();
317 
318                 sink.tableCell();
319                 sink.text( stats.getCa() );
320                 sink.tableCell_();
321 
322                 sink.tableCell();
323                 sink.text( stats.getCe() );
324                 sink.tableCell_();
325 
326                 sink.tableCell();
327                 sink.text( convertToPercent( stats.getA() ) );
328                 sink.tableCell_();
329 
330                 sink.tableCell();
331                 sink.text( convertToPercent( stats.getI() ) );
332                 sink.tableCell_();
333 
334                 sink.tableCell();
335                 sink.text( convertToPercent( stats.getD() ) );
336                 sink.tableCell_();
337 
338                 sink.tableRow_();
339 
340                 sink.table_();
341 
342                 /* New Table */
343 
344                 sink.table();
345                 sink.tableRow();
346 
347                 /*
348                  * Headers
349                  */
350 
351                 sink.tableHeaderCell();
352                 sink.text( bundle.getString( "report.abstractclasses.title" ) ); //$NON-NLS-1$
353                 sink.tableHeaderCell_();
354 
355                 sink.tableHeaderCell();
356                 sink.text( bundle.getString( "report.concreteclasses.title" ) ); //$NON-NLS-1$
357                 sink.tableHeaderCell_();
358 
359                 sink.tableHeaderCell();
360                 sink.text( bundle.getString( "report.usedbypackages" ) ); //$NON-NLS-1$
361                 sink.tableHeaderCell_();
362 
363                 sink.tableHeaderCell();
364                 sink.text( bundle.getString( "report.usespackage" ) ); //$NON-NLS-1$
365                 sink.tableHeaderCell_();
366 
367                 sink.tableRow_();
368 
369                 /*
370                  * Values
371                  */
372 
373                 sink.tableRow();
374 
375                 /* Abstract Classes */
376                 List abstractList = jdpackage.getAbstractClasses();
377 
378                 sink.tableCell();
379 
380                 if ( abstractList.size() <= 0 )
381                 {
382                     sink.italic();
383                     sink.text( bundle.getString( "report.none" ) ); //$NON-NLS-1$
384                     sink.italic_();
385                 }
386                 else
387                 {
388                     for ( int j = 0; j < abstractList.size(); j++ )
389                     {
390                         sink.text( (String) abstractList.get( j ) );
391                         sink.lineBreak();
392                     }
393                 }
394                 sink.tableCell_();
395 
396                 /* Concrete Classes */
397                 java.util.List concreteList = jdpackage.getConcreteClasses();
398 
399                 sink.tableCell();
400 
401                 if ( concreteList.size() <= 0 )
402                 {
403                     sink.italic();
404                     sink.text( bundle.getString( "report.none" ) ); //$NON-NLS-1$
405                     sink.italic_();
406                 }
407                 else
408                 {
409                     for ( int j = 0; j < concreteList.size(); j++ )
410                     {
411                         sink.text( (String) concreteList.get( j ) );
412                         sink.lineBreak();
413                     }
414                 }
415                 sink.tableCell_();
416 
417                 /* Used By Packages */
418                 List aList = jdpackage.getUsedBy();
419 
420                 sink.tableCell();
421 
422                 if ( aList.size() <= 0 )
423                 {
424                     sink.italic();
425                     sink.text( bundle.getString( "report.none" ) ); //$NON-NLS-1$
426                     sink.italic_();
427                 }
428                 else
429                 {
430                     for ( int j = 0; j < aList.size(); j++ )
431                     {
432                         sink.text( (String) aList.get( j ) );
433                         sink.lineBreak();
434                     }
435                 }
436                 sink.tableCell_();
437 
438                 aList = null;
439 
440                 /* Uses Package */
441                 aList = jdpackage.getDependsUpon();
442 
443                 sink.tableCell();
444 
445                 if ( aList.size() <= 0 )
446                 {
447                     sink.italic();
448                     sink.text( bundle.getString( "report.none" ) ); //$NON-NLS-1$
449                     sink.italic_();
450                 }
451                 else
452                 {
453                     for ( int j = 0; j < aList.size(); j++ )
454                     {
455                         sink.text( (String) aList.get( j ) );
456                         sink.lineBreak();
457                     }
458                 }
459                 sink.tableCell_();
460 
461                 sink.tableRow_();
462 
463                 sink.table_();
464             }
465         }
466 
467     }
468 
469     public void doCycleSection( ResourceBundle bundle, Sink sink )
470     {
471 
472         sink.anchor( bundle.getString( "report.cycles.text" ) ); //$NON-NLS-1$
473         sink.anchor_();
474 
475         sink.sectionTitle1();
476         sink.text( bundle.getString( "report.cycles.title" ) ); //$NON-NLS-1$
477         sink.sectionTitle1_();
478 
479         doSectionLinks( bundle, sink );
480 
481         sink.lineBreak();
482         sink.lineBreak();
483 
484         doCycles( bundle, sink );
485 
486     }
487 
488     public void doCycles( ResourceBundle bundle, Sink sink )
489     {
490 
491         List cycleList = jdepend.cycles;
492 
493         if ( cycleList.size() <= 0 )
494         {
495             sink.text( bundle.getString( "report.nocyclicdependencies" ) ); //$NON-NLS-1$
496             sink.lineBreak();
497         }
498         else
499         {
500             sink.table();
501 
502             sink.tableRow();
503             sink.tableHeaderCell();
504             sink.text( bundle.getString( "report.package.title" ) ); //$NON-NLS-1$
505             sink.tableHeaderCell_();
506 
507             sink.tableHeaderCell();
508             sink.text( bundle.getString( "report.packagedependencies" ) ); //$NON-NLS-1$
509             sink.tableHeaderCell_();
510             sink.tableRow_();
511 
512             for ( int i = 0; i < cycleList.size(); i++ )
513             {
514                 cyclepackage = (CyclePackage) cycleList.get( i );
515 
516                 sink.tableRow();
517                 sink.tableCell();
518                 sink.text( cyclepackage.getName() );
519                 sink.tableCell_();
520 
521                 /* Package Dependencies List */
522 
523                 List packageList = cyclepackage.getPackageList();
524                 sink.tableCell();
525                 if ( packageList.size() <= 0 )
526                 {
527                     sink.italic();
528                     sink.text( bundle.getString( "report.none" ) ); //$NON-NLS-1$
529                     sink.italic_();
530                 }
531                 else
532                 {
533                     for ( int j = 0; j < packageList.size(); j++ )
534                     {
535                         sink.text( (String) packageList.get( j ) );
536                         sink.lineBreak();
537                     }
538                 }
539                 sink.tableCell_();
540                 sink.tableRow_();
541 
542             }
543             sink.table_();
544         }
545 
546     }
547 
548     public void doExplanationSection( ResourceBundle bundle, Sink sink )
549     {
550 
551         sink.anchor( bundle.getString( "report.explanations" ) ); //$NON-NLS-1$
552         sink.anchor_();
553 
554         sink.sectionTitle1();
555         sink.text( bundle.getString( "report.explanation.title" ) ); //$NON-NLS-1$
556         sink.sectionTitle1_();
557 
558         doSectionLinks( bundle, sink );
559 
560         sink.lineBreak();
561         sink.lineBreak();
562 
563         sink.text( bundle.getString( "report.explanation.description" ) ); //$NON-NLS-1$
564 
565         sink.lineBreak();
566         sink.lineBreak();
567 
568         sink.table();
569 
570         sink.tableRow();
571         sink.tableHeaderCell();
572         sink.text( bundle.getString( "report.term" ) ); //$NON-NLS-1$
573         sink.tableHeaderCell_();
574 
575         sink.tableHeaderCell();
576         sink.text( bundle.getString( "report.description" ) ); //$NON-NLS-1$
577         sink.tableHeaderCell_();
578         sink.tableRow_();
579 
580         sink.tableRow();
581         sink.tableCell();
582         sink.text( bundle.getString( "report.numberofclasses.title" ) ); //$NON-NLS-1$
583         sink.tableCell_();
584 
585         sink.tableCell();
586         sink.text( bundle.getString( "report.numberofclasses.description" ) ); //$NON-NLS-1$
587         sink.tableCell_();
588         sink.tableRow_();
589 
590         sink.tableRow();
591         sink.tableCell();
592         sink.text( bundle.getString( "report.afferentcouplings.title" ) ); //$NON-NLS-1$
593         sink.tableCell_();
594 
595         sink.tableCell();
596         sink.text( bundle.getString( "report.afferentcouplings.description" ) ); //$NON-NLS-1$
597         sink.tableCell_();
598         sink.tableRow_();
599 
600         sink.tableRow();
601         sink.tableCell();
602         sink.text( bundle.getString( "report.efferentcouplings.title" ) ); //$NON-NLS-1$
603         sink.tableCell_();
604 
605         sink.tableCell();
606         sink.text( bundle.getString( "report.efferentcouplings.description" ) ); //$NON-NLS-1$
607         sink.tableCell_();
608         sink.tableRow_();
609 
610         sink.tableRow();
611         sink.tableCell();
612         sink.text( bundle.getString( "report.abstractness.title" ) ); //$NON-NLS-1$
613         sink.tableCell_();
614 
615         sink.tableCell();
616         sink.text( bundle.getString( "report.abstractness.description" ) ); //$NON-NLS-1$
617         sink.tableCell_();
618         sink.tableRow_();
619 
620         sink.tableRow();
621         sink.tableCell();
622         sink.text( bundle.getString( "report.instability.title" ) ); //$NON-NLS-1$
623         sink.tableCell_();
624 
625         sink.tableCell();
626         sink.text( bundle.getString( "report.instability.description" ) ); //$NON-NLS-1$
627         sink.tableCell_();
628         sink.tableRow_();
629 
630         sink.tableRow();
631         sink.tableCell();
632         sink.text( bundle.getString( "report.distance.title" ) ); //$NON-NLS-1$
633         sink.tableCell_();
634 
635         sink.tableCell();
636         sink.text( bundle.getString( "report.distance.description" ) ); //$NON-NLS-1$
637         sink.tableCell_();
638         sink.tableRow_();
639 
640         sink.tableRow();
641         sink.tableCell();
642         sink.text( bundle.getString( "report.cycles.title" ) ); //$NON-NLS-1$
643         sink.tableCell_();
644 
645         sink.tableCell();
646         sink.text( bundle.getString( "report.cycles.description" ) ); //$NON-NLS-1$
647         sink.tableCell_();
648         sink.tableRow_();
649 
650         sink.table_();
651 
652     }
653 
654     private String convertToPercent( String value )
655     {
656         float ival = 0;
657 
658         ival = Float.parseFloat( value );
659         ival = ival * 100;
660         value = String.valueOf( ival ) + "%"; //$NON-NLS-1$
661 
662         return value;
663     }
664 }