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