|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.renderers; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.IRuleViolation; |
|
7 |
| import net.sourceforge.pmd.PMD; |
|
8 |
| import net.sourceforge.pmd.Report; |
|
9 |
| |
|
10 |
| import java.io.BufferedReader; |
|
11 |
| import java.io.File; |
|
12 |
| import java.io.FileReader; |
|
13 |
| import java.io.IOException; |
|
14 |
| import java.io.Writer; |
|
15 |
| import java.util.Iterator; |
|
16 |
| import java.util.Map; |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| public class PapariTextRenderer extends AbstractRenderer { |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| private String pwd; |
|
53 |
| |
|
54 |
| private String yellowBold = ""; |
|
55 |
| private String whiteBold = ""; |
|
56 |
| private String redBold = ""; |
|
57 |
| private String cyan = ""; |
|
58 |
| private String green = ""; |
|
59 |
| |
|
60 |
| private String colorReset = ""; |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
0
| private void initializeColorsIfSupported() {
|
|
70 |
0
| if (System.getProperty("pmd.color") != null &&
|
|
71 |
| !(System.getProperty("pmd.color").equals("0") || System.getProperty("pmd.color").equals("false"))) { |
|
72 |
0
| this.yellowBold = "\u001B[1;33m";
|
|
73 |
0
| this.whiteBold = "\u001B[1;37m";
|
|
74 |
0
| this.redBold = "\u001B[1;31m";
|
|
75 |
0
| this.green = "\u001B[0;32m";
|
|
76 |
0
| this.cyan = "\u001B[0;36m";
|
|
77 |
| |
|
78 |
0
| this.colorReset = "\u001B[0m";
|
|
79 |
| } |
|
80 |
| } |
|
81 |
| |
|
82 |
0
| public void render(Writer writer, Report report) throws IOException {
|
|
83 |
0
| StringBuffer buf = new StringBuffer(PMD.EOL);
|
|
84 |
0
| initializeColorsIfSupported();
|
|
85 |
0
| String lastFile = null;
|
|
86 |
0
| int numberOfErrors = 0;
|
|
87 |
0
| int numberOfWarnings = 0;
|
|
88 |
| |
|
89 |
0
| for (Iterator i = report.iterator(); i.hasNext();) {
|
|
90 |
0
| buf.setLength(0);
|
|
91 |
0
| numberOfWarnings++;
|
|
92 |
0
| IRuleViolation rv = (IRuleViolation) i.next();
|
|
93 |
0
| if (!rv.getFilename().equals(lastFile)) {
|
|
94 |
0
| lastFile = rv.getFilename();
|
|
95 |
0
| buf.append(this.yellowBold + "*" + this.colorReset + " file: " + this.whiteBold + this.getRelativePath(lastFile) + this.colorReset + PMD.EOL);
|
|
96 |
| } |
|
97 |
0
| buf.append(this.green + " src: " + this.cyan + lastFile.substring(lastFile.lastIndexOf(File.separator) + 1) + this.colorReset + ":" + this.cyan + rv.getBeginLine() + (rv.getEndLine() == -1 ? "" : ":" + rv.getEndLine()) + this.colorReset + PMD.EOL);
|
|
98 |
0
| buf.append(this.green + " rule: " + this.colorReset + rv.getRule().getName() + PMD.EOL);
|
|
99 |
0
| buf.append(this.green + " msg: " + this.colorReset + rv.getDescription() + PMD.EOL);
|
|
100 |
0
| buf.append(this.green + " code: " + this.colorReset + this.getLine(lastFile, rv.getBeginLine()) + PMD.EOL + PMD.EOL);
|
|
101 |
0
| writer.write(buf.toString());
|
|
102 |
| } |
|
103 |
0
| writer.write(PMD.EOL + PMD.EOL);
|
|
104 |
0
| writer.write("Summary:" + PMD.EOL + PMD.EOL);
|
|
105 |
0
| Map summary = report.getCountSummary();
|
|
106 |
0
| for (Iterator i = summary.entrySet().iterator(); i.hasNext();) {
|
|
107 |
0
| buf.setLength(0);
|
|
108 |
0
| Map.Entry entry = (Map.Entry) i.next();
|
|
109 |
0
| String key = (String) entry.getKey();
|
|
110 |
0
| buf.append(key + " : " + ((Integer) entry.getValue()).intValue() + PMD.EOL);
|
|
111 |
0
| writer.write(buf.toString());
|
|
112 |
| } |
|
113 |
| |
|
114 |
0
| for (Iterator i = report.errors(); i.hasNext();) {
|
|
115 |
0
| buf.setLength(0);
|
|
116 |
0
| numberOfErrors++;
|
|
117 |
0
| Report.ProcessingError error = (Report.ProcessingError) i.next();
|
|
118 |
0
| if (error.getFile().equals(lastFile)) {
|
|
119 |
0
| lastFile = error.getFile();
|
|
120 |
0
| buf.append(this.redBold + "*" + this.colorReset + " file: " + this.whiteBold + this.getRelativePath(lastFile) + this.colorReset + PMD.EOL);
|
|
121 |
| } |
|
122 |
0
| buf.append(this.green + " err: " + this.cyan + error.getMsg() + this.colorReset + PMD.EOL + PMD.EOL);
|
|
123 |
0
| writer.write(buf.toString());
|
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
0
| if (numberOfErrors > 0) {
|
|
128 |
0
| writer.write(this.redBold + "*" + this.colorReset + " errors: " + this.whiteBold + numberOfWarnings + this.colorReset + PMD.EOL);
|
|
129 |
| } |
|
130 |
0
| writer.write(this.yellowBold + "*" + this.colorReset + " warnings: " + this.whiteBold + numberOfWarnings + this.colorReset + PMD.EOL);
|
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
0
| private String getLine(String sourceFile, int line) {
|
|
141 |
0
| String code = null;
|
|
142 |
0
| BufferedReader br = null;
|
|
143 |
0
| try {
|
|
144 |
0
| br = new BufferedReader(new FileReader(new File(sourceFile)));
|
|
145 |
0
| for (int i = 0; line > i; i++) {
|
|
146 |
0
| code = br.readLine().trim();
|
|
147 |
| } |
|
148 |
| } catch (IOException ioErr) { |
|
149 |
0
| ioErr.printStackTrace();
|
|
150 |
| } finally { |
|
151 |
0
| if (br != null) {
|
|
152 |
0
| try {
|
|
153 |
0
| br.close();
|
|
154 |
| } catch (IOException ioErr) { |
|
155 |
0
| ioErr.printStackTrace();
|
|
156 |
| } |
|
157 |
| } |
|
158 |
| } |
|
159 |
0
| return code;
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
0
| private String getRelativePath(String fileName) {
|
|
170 |
0
| String relativePath;
|
|
171 |
| |
|
172 |
| |
|
173 |
0
| if (pwd == null) {
|
|
174 |
0
| try {
|
|
175 |
0
| this.pwd = new File(".").getCanonicalPath();
|
|
176 |
| } catch (IOException ioErr) { |
|
177 |
| |
|
178 |
0
| this.pwd = "";
|
|
179 |
| } |
|
180 |
| } |
|
181 |
| |
|
182 |
| |
|
183 |
0
| if (fileName.indexOf(this.pwd) == 0) {
|
|
184 |
0
| relativePath = "." + fileName.substring(this.pwd.length());
|
|
185 |
| |
|
186 |
| |
|
187 |
0
| if (relativePath.startsWith("." + File.separator + "." + File.separator)) {
|
|
188 |
0
| relativePath = relativePath.substring(2);
|
|
189 |
| } |
|
190 |
| } else { |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
0
| relativePath = fileName;
|
|
195 |
| } |
|
196 |
| |
|
197 |
0
| return relativePath;
|
|
198 |
| } |
|
199 |
| } |