18
18
*/
19
19
package org .apache .maven .plugins .pmd ;
20
20
21
+ import javax .swing .text .html .HTML .Attribute ;
22
+
21
23
import java .io .File ;
22
- import java .util .List ;
24
+ import java .util .Collection ;
25
+ import java .util .Locale ;
23
26
import java .util .Map ;
24
- import java .util .ResourceBundle ;
25
27
26
28
import org .apache .maven .doxia .sink .Sink ;
29
+ import org .apache .maven .doxia .sink .SinkEventAttributes ;
30
+ import org .apache .maven .doxia .sink .impl .SinkEventAttributeSet ;
27
31
import org .apache .maven .plugins .pmd .model .CpdFile ;
28
32
import org .apache .maven .plugins .pmd .model .Duplication ;
29
33
import org .apache .maven .project .MavenProject ;
34
+ import org .apache .maven .reporting .AbstractMavenReportRenderer ;
35
+ import org .codehaus .plexus .i18n .I18N ;
30
36
import org .codehaus .plexus .util .StringUtils ;
31
37
32
38
/**
35
41
* @author mperham
36
42
* @version $Id$
37
43
*/
38
- public class CpdReportGenerator {
39
- private Sink sink ;
44
+ public class CpdReportRenderer extends AbstractMavenReportRenderer {
45
+ private final I18N i18n ;
46
+
47
+ private final Locale locale ;
40
48
41
- private Map <File , PmdFileInfo > fileMap ;
49
+ private final Map <File , PmdFileInfo > files ;
42
50
43
- private ResourceBundle bundle ;
51
+ private final Collection < Duplication > duplications ;
44
52
45
- private boolean aggregate ;
53
+ private final boolean aggregate ;
46
54
47
- public CpdReportGenerator (Sink sink , Map <File , PmdFileInfo > fileMap , ResourceBundle bundle , boolean aggregate ) {
48
- this .sink = sink ;
49
- this .fileMap = fileMap ;
50
- this .bundle = bundle ;
55
+ public CpdReportRenderer (
56
+ Sink sink ,
57
+ I18N i18n ,
58
+ Locale locale ,
59
+ Map <File , PmdFileInfo > files ,
60
+ Collection <Duplication > duplications ,
61
+ boolean aggregate ) {
62
+ super (sink );
63
+ this .i18n = i18n ;
64
+ this .locale = locale ;
65
+ this .files = files ;
66
+ this .duplications = duplications ;
51
67
this .aggregate = aggregate ;
52
68
}
53
69
54
- /**
55
- * Method that returns the title of the CPD Report
56
- *
57
- * @return a String that contains the title
58
- */
59
- private String getTitle () {
60
- return bundle .getString ("report.cpd.title" );
70
+ @ Override
71
+ public String getTitle () {
72
+ return getI18nString ("title" );
61
73
}
62
74
63
75
/**
64
- * Method that generates the start of the CPD report.
76
+ * @param key The key.
77
+ * @return The translated string.
65
78
*/
66
- public void beginDocument () {
67
- sink .head ();
68
- sink .title ();
69
- sink .text (getTitle ());
70
- sink .title_ ();
71
- sink .head_ ();
72
-
73
- sink .body ();
79
+ private String getI18nString (String key ) {
80
+ return i18n .getString ("cpd-report" , locale , "report.cpd." + key );
81
+ }
74
82
75
- sink .section1 ();
76
- sink .sectionTitle1 ();
77
- sink .text (getTitle ());
78
- sink .sectionTitle1_ ();
83
+ @ Override
84
+ protected void renderBody () {
85
+ startSection (getTitle ());
79
86
80
87
sink .paragraph ();
81
- sink .text (bundle .getString ("report.cpd.cpdlink" ) + " " );
82
- sink .link ("https://pmd.github.io/latest/pmd_userdocs_cpd.html" );
83
- sink .text ("CPD" );
84
- sink .link_ ();
88
+ sink .text (getI18nString ("cpdlink" ) + " " );
89
+ link ("https://pmd.github.io/latest/pmd_userdocs_cpd.html" , "CPD" );
85
90
sink .text (" " + AbstractPmdReport .getPmdVersion () + "." );
86
91
sink .paragraph_ ();
87
92
88
- sink .section1_ ();
89
-
90
93
// TODO overall summary
91
94
92
- sink .section1 ();
93
- sink .sectionTitle1 ();
94
- sink .text (bundle .getString ("report.cpd.dupes" ));
95
- sink .sectionTitle1_ ();
95
+ if (!duplications .isEmpty ()) {
96
+ renderDuplications ();
97
+ } else {
98
+ paragraph (getI18nString ("noProblems" ));
99
+ }
96
100
97
101
// TODO files summary
102
+
103
+ endSection ();
98
104
}
99
105
100
106
/**
@@ -104,7 +110,7 @@ private void generateFileLine(CpdFile duplicationMark) {
104
110
// Get information for report generation
105
111
String filename = duplicationMark .getPath ();
106
112
File file = new File (filename );
107
- PmdFileInfo fileInfo = fileMap .get (file );
113
+ PmdFileInfo fileInfo = files .get (file );
108
114
File sourceDirectory = fileInfo .getSourceDirectory ();
109
115
filename = StringUtils .substring (
110
116
filename , sourceDirectory .getAbsolutePath ().length () + 1 );
@@ -113,13 +119,9 @@ private void generateFileLine(CpdFile duplicationMark) {
113
119
int line = duplicationMark .getLine ();
114
120
115
121
sink .tableRow ();
116
- sink .tableCell ();
117
- sink .text (filename );
118
- sink .tableCell_ ();
122
+ tableCell (filename );
119
123
if (aggregate ) {
120
- sink .tableCell ();
121
- sink .text (projectFile .getName ());
122
- sink .tableCell_ ();
124
+ tableCell (projectFile .getName ());
123
125
}
124
126
sink .tableCell ();
125
127
@@ -136,37 +138,19 @@ private void generateFileLine(CpdFile duplicationMark) {
136
138
sink .tableRow_ ();
137
139
}
138
140
139
- /**
140
- * Method that generates the contents of the CPD report
141
- *
142
- * @param duplications the found duplications
143
- */
144
- public void generate (List <Duplication > duplications ) {
145
- beginDocument ();
146
-
147
- if (duplications .isEmpty ()) {
148
- sink .paragraph ();
149
- sink .text (bundle .getString ("report.cpd.noProblems" ));
150
- sink .paragraph_ ();
151
- }
141
+ private void renderDuplications () {
142
+ startSection (getI18nString ("dupes" ));
152
143
153
144
for (Duplication duplication : duplications ) {
154
145
String code = duplication .getCodefragment ();
155
146
156
- sink .table ();
157
- sink .tableRows (null , false );
147
+ startTable ();
158
148
sink .tableRow ();
159
- sink .tableHeaderCell ();
160
- sink .text (bundle .getString ("report.cpd.column.file" ));
161
- sink .tableHeaderCell_ ();
149
+ tableHeaderCell (getI18nString ("column.file" ));
162
150
if (aggregate ) {
163
- sink .tableHeaderCell ();
164
- sink .text (bundle .getString ("report.cpd.column.project" ));
165
- sink .tableHeaderCell_ ();
151
+ tableHeaderCell (getI18nString ("column.project" ));
166
152
}
167
- sink .tableHeaderCell ();
168
- sink .text (bundle .getString ("report.cpd.column.line" ));
169
- sink .tableHeaderCell_ ();
153
+ tableHeaderCell (getI18nString ("column.line" ));
170
154
sink .tableRow_ ();
171
155
172
156
// Iterating on every token entry
@@ -179,22 +163,17 @@ public void generate(List<Duplication> duplications) {
179
163
180
164
int colspan = 2 ;
181
165
if (aggregate ) {
182
- ++ colspan ;
166
+ colspan = 3 ;
183
167
}
184
- // TODO Cleaner way to do this?
185
- sink .rawText ("<td colspan='" + colspan + "'>" );
186
- sink .verbatim (null );
187
- sink .text (code );
188
- sink .verbatim_ ();
189
- sink .rawText ("</td>" );
168
+ SinkEventAttributes att = new SinkEventAttributeSet ();
169
+ att .addAttribute (Attribute .COLSPAN , colspan );
170
+ sink .tableCell (att );
171
+ verbatimText (code );
172
+ sink .tableCell_ ();
190
173
sink .tableRow_ ();
191
- sink .tableRows_ ();
192
- sink .table_ ();
174
+ endTable ();
193
175
}
194
176
195
- sink .section1_ ();
196
- sink .body_ ();
197
- sink .flush ();
198
- sink .close ();
177
+ endSection ();
199
178
}
200
179
}
0 commit comments