1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package com.puppycrawl.tools.checkstyle; |
20 | |
|
21 | |
import java.io.OutputStream; |
22 | |
import java.io.OutputStreamWriter; |
23 | |
import java.io.PrintWriter; |
24 | |
import java.io.StringWriter; |
25 | |
import java.io.UnsupportedEncodingException; |
26 | |
import java.util.ResourceBundle; |
27 | |
|
28 | |
import com.puppycrawl.tools.checkstyle.api.AuditEvent; |
29 | |
import com.puppycrawl.tools.checkstyle.api.AuditListener; |
30 | |
import com.puppycrawl.tools.checkstyle.api.AutomaticBean; |
31 | |
import com.puppycrawl.tools.checkstyle.api.SeverityLevel; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class XMLLogger |
42 | |
extends AutomaticBean |
43 | |
implements AuditListener |
44 | |
{ |
45 | |
|
46 | |
private static final int BASE_10 = 10; |
47 | |
|
48 | |
|
49 | |
private static final int BASE_16 = 16; |
50 | |
|
51 | |
|
52 | |
private boolean mCloseStream; |
53 | |
|
54 | |
|
55 | |
private PrintWriter mWriter; |
56 | |
|
57 | |
|
58 | 1 | private static final String[] ENTITIES = {"gt", "amp", "lt", "apos", |
59 | |
"quot", }; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public XMLLogger(OutputStream aOS, boolean aCloseStream) |
68 | 8 | { |
69 | 8 | setOutputStream(aOS); |
70 | 8 | mCloseStream = aCloseStream; |
71 | 8 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private void setOutputStream(OutputStream aOS) |
78 | |
{ |
79 | |
try { |
80 | 8 | final OutputStreamWriter osw = new OutputStreamWriter(aOS, "UTF-8"); |
81 | 8 | mWriter = new PrintWriter(osw); |
82 | |
} |
83 | 0 | catch (final UnsupportedEncodingException e) { |
84 | |
|
85 | 0 | throw new ExceptionInInitializerError(e); |
86 | 8 | } |
87 | 8 | } |
88 | |
|
89 | |
|
90 | |
public void auditStarted(AuditEvent aEvt) |
91 | |
{ |
92 | 6 | mWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
93 | |
|
94 | 6 | final ResourceBundle compilationProperties = |
95 | |
ResourceBundle.getBundle("checkstylecompilation"); |
96 | 6 | final String version = |
97 | |
compilationProperties.getString("checkstyle.compile.version"); |
98 | |
|
99 | 6 | mWriter.println("<checkstyle version=\"" + version + "\">"); |
100 | 6 | } |
101 | |
|
102 | |
|
103 | |
public void auditFinished(AuditEvent aEvt) |
104 | |
{ |
105 | 6 | mWriter.println("</checkstyle>"); |
106 | 6 | if (mCloseStream) { |
107 | 5 | mWriter.close(); |
108 | |
} |
109 | |
else { |
110 | 1 | mWriter.flush(); |
111 | |
} |
112 | 6 | } |
113 | |
|
114 | |
|
115 | |
public void fileStarted(AuditEvent aEvt) |
116 | |
{ |
117 | 1 | mWriter.println("<file name=\"" + encode(aEvt.getFileName()) + "\">"); |
118 | 1 | } |
119 | |
|
120 | |
|
121 | |
public void fileFinished(AuditEvent aEvt) |
122 | |
{ |
123 | 1 | mWriter.println("</file>"); |
124 | 1 | } |
125 | |
|
126 | |
|
127 | |
public void addError(AuditEvent aEvt) |
128 | |
{ |
129 | 1 | if (!SeverityLevel.IGNORE.equals(aEvt.getSeverityLevel())) { |
130 | 1 | mWriter.print("<error" + " line=\"" + aEvt.getLine() + "\""); |
131 | 1 | if (aEvt.getColumn() > 0) { |
132 | 1 | mWriter.print(" column=\"" + aEvt.getColumn() + "\""); |
133 | |
} |
134 | 1 | mWriter.print(" severity=\"" |
135 | |
+ aEvt.getSeverityLevel().getName() |
136 | |
+ "\""); |
137 | 1 | mWriter.print(" message=\"" |
138 | |
+ encode(aEvt.getMessage()) |
139 | |
+ "\""); |
140 | 1 | mWriter.println(" source=\"" |
141 | |
+ encode(aEvt.getSourceName()) |
142 | |
+ "\"/>"); |
143 | |
} |
144 | 1 | } |
145 | |
|
146 | |
|
147 | |
public void addException(AuditEvent aEvt, Throwable aThrowable) |
148 | |
{ |
149 | 1 | final StringWriter sw = new StringWriter(); |
150 | 1 | final PrintWriter pw = new PrintWriter(sw); |
151 | 1 | pw.println("<exception>"); |
152 | 1 | pw.println("<![CDATA["); |
153 | 1 | aThrowable.printStackTrace(pw); |
154 | 1 | pw.println("]]>"); |
155 | 1 | pw.println("</exception>"); |
156 | 1 | pw.flush(); |
157 | 1 | mWriter.println(encode(sw.toString())); |
158 | 1 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public String encode(String aValue) |
166 | |
{ |
167 | 11 | final StringBuffer sb = new StringBuffer(); |
168 | 130 | for (int i = 0; i < aValue.length(); i++) { |
169 | 119 | final char c = aValue.charAt(i); |
170 | 119 | switch (c) { |
171 | |
case '<': |
172 | 4 | sb.append("<"); |
173 | 4 | break; |
174 | |
case '>': |
175 | 4 | sb.append(">"); |
176 | 4 | break; |
177 | |
case '\'': |
178 | 1 | sb.append("'"); |
179 | 1 | break; |
180 | |
case '\"': |
181 | 1 | sb.append("""); |
182 | 1 | break; |
183 | |
case '&': |
184 | 2 | final int nextSemi = aValue.indexOf(";", i); |
185 | 2 | if ((nextSemi < 0) |
186 | |
|| !isReference(aValue.substring(i, nextSemi + 1))) |
187 | |
{ |
188 | 1 | sb.append("&"); |
189 | |
} |
190 | |
else { |
191 | 1 | sb.append('&'); |
192 | |
} |
193 | 1 | break; |
194 | |
default: |
195 | 107 | sb.append(c); |
196 | |
break; |
197 | |
} |
198 | |
} |
199 | 11 | return sb.toString(); |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public boolean isReference(String aEnt) |
207 | |
{ |
208 | 10 | if (!(aEnt.charAt(0) == '&') || !aEnt.endsWith(";")) { |
209 | 1 | return false; |
210 | |
} |
211 | |
|
212 | 9 | if (aEnt.charAt(1) == '#') { |
213 | 7 | int prefixLength = 2; |
214 | 7 | int radix = BASE_10; |
215 | 7 | if (aEnt.charAt(2) == 'x') { |
216 | 3 | prefixLength++; |
217 | 3 | radix = BASE_16; |
218 | |
} |
219 | |
try { |
220 | 7 | Integer.parseInt( |
221 | |
aEnt.substring(prefixLength, aEnt.length() - 1), radix); |
222 | 2 | return true; |
223 | |
} |
224 | 5 | catch (final NumberFormatException nfe) { |
225 | 5 | return false; |
226 | |
} |
227 | |
} |
228 | |
|
229 | 2 | final String name = aEnt.substring(1, aEnt.length() - 1); |
230 | 9 | for (String element : ENTITIES) { |
231 | 8 | if (name.equals(element)) { |
232 | 1 | return true; |
233 | |
} |
234 | |
} |
235 | 1 | return false; |
236 | |
} |
237 | |
} |