|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
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 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| package org.w3c.tidy.servlet.reports; |
|
56 |
| |
|
57 |
| import java.io.IOException; |
|
58 |
| import java.io.InputStream; |
|
59 |
| import java.io.LineNumberReader; |
|
60 |
| import java.io.InputStreamReader; |
|
61 |
| import java.io.Writer; |
|
62 |
| import java.io.StringReader; |
|
63 |
| import java.util.Vector; |
|
64 |
| import java.util.Iterator; |
|
65 |
| import java.util.HashMap; |
|
66 |
| |
|
67 |
| import javax.servlet.http.HttpSession; |
|
68 |
| |
|
69 |
| import org.apache.commons.logging.Log; |
|
70 |
| import org.apache.commons.logging.LogFactory; |
|
71 |
| |
|
72 |
| import org.w3c.tidy.TidyMessage; |
|
73 |
| import org.w3c.tidy.servlet.ResponseRecord; |
|
74 |
| import org.w3c.tidy.servlet.ResponseRecordRepository; |
|
75 |
| import org.w3c.tidy.servlet.properties.JTidyServletProperties; |
|
76 |
| import org.w3c.tidy.servlet.util.HTMLEncode; |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| public class Report |
|
85 |
| { |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| private boolean completePage = true; |
|
91 |
| |
|
92 |
| private boolean view; |
|
93 |
| |
|
94 |
| private boolean printSource; |
|
95 |
| |
|
96 |
| private boolean wrapSource = true; |
|
97 |
| |
|
98 |
| private boolean printHtmlResult = false; |
|
99 |
| |
|
100 |
| private int wrapLen = 0; |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| private boolean xhtml; |
|
106 |
| |
|
107 |
| private static final String RESOURCE_JAVASCRIPT = "JTidyServletReport.js"; |
|
108 |
| |
|
109 |
| private static final String RESOURCE_STYLESHEET = "JTidyServletReport.css"; |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| private Log log = LogFactory.getLog(Report.class); |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| StringBuffer out; |
|
120 |
| |
|
121 |
| ResponseRecordRepository responseRecordRepository; |
|
122 |
| |
|
123 |
0
| public Report(ResponseRecordRepository responseRecordRepository)
|
|
124 |
| { |
|
125 |
0
| this.out = new StringBuffer(1024);
|
|
126 |
0
| this.responseRecordRepository = responseRecordRepository;
|
|
127 |
0
| init();
|
|
128 |
| } |
|
129 |
| |
|
130 |
9
| public Report(HttpSession httpSession)
|
|
131 |
| { |
|
132 |
9
| this.out = new StringBuffer(1024);
|
|
133 |
9
| this.responseRecordRepository = JTidyServletProperties.getInstance().getRepositoryInstance(httpSession);
|
|
134 |
9
| init();
|
|
135 |
| } |
|
136 |
| |
|
137 |
9
| private void init()
|
|
138 |
| { |
|
139 |
9
| JTidyServletProperties properties = JTidyServletProperties.getInstance();
|
|
140 |
9
| this.xhtml = properties.getBooleanProperty(JTidyServletProperties.PROPERTY_BOOLEAN_XHTML, true);
|
|
141 |
| } |
|
142 |
| |
|
143 |
9
| public void print(Writer writer, String key) throws IOException
|
|
144 |
| { |
|
145 |
9
| format(key);
|
|
146 |
9
| writer.write(this.out.toString());
|
|
147 |
| } |
|
148 |
| |
|
149 |
0
| void td(String str1, String str2)
|
|
150 |
| { |
|
151 |
0
| td(str1);
|
|
152 |
0
| td(str2);
|
|
153 |
| } |
|
154 |
| |
|
155 |
28
| void td(String str)
|
|
156 |
| { |
|
157 |
28
| this.out.append("<td>").append(str).append("</td>\n");
|
|
158 |
| } |
|
159 |
| |
|
160 |
21
| void tr()
|
|
161 |
| { |
|
162 |
21
| this.out.append("</tr><tr>\n");
|
|
163 |
| } |
|
164 |
| |
|
165 |
98
| void identSpace(int ln)
|
|
166 |
| { |
|
167 |
98
| if (ln < 10)
|
|
168 |
| { |
|
169 |
53
| this.out.append(" ");
|
|
170 |
| } |
|
171 |
45
| else if (ln < 100)
|
|
172 |
| { |
|
173 |
45
| this.out.append(" ");
|
|
174 |
| } |
|
175 |
0
| else if (ln < 1000)
|
|
176 |
| { |
|
177 |
0
| this.out.append(" ");
|
|
178 |
| } |
|
179 |
| } |
|
180 |
| |
|
181 |
9
| void format(String keyString) throws IOException
|
|
182 |
| { |
|
183 |
| |
|
184 |
9
| ResponseRecord record = null;
|
|
185 |
| |
|
186 |
9
| if (this.responseRecordRepository == null)
|
|
187 |
| { |
|
188 |
0
| log.info("No ResponseRecordRepository");
|
|
189 |
| } |
|
190 |
| else |
|
191 |
| { |
|
192 |
9
| Object key = this.responseRecordRepository.getResponseID(keyString);
|
|
193 |
9
| record = this.responseRecordRepository.getRecord(key);
|
|
194 |
| } |
|
195 |
| |
|
196 |
9
| if (record == null)
|
|
197 |
| { |
|
198 |
2
| this.out.append("No data for ").append(keyString);
|
|
199 |
2
| return;
|
|
200 |
| } |
|
201 |
| |
|
202 |
7
| if (this.view)
|
|
203 |
| { |
|
204 |
0
| formatView(record);
|
|
205 |
| } |
|
206 |
| else |
|
207 |
| { |
|
208 |
7
| formatReport(record);
|
|
209 |
| } |
|
210 |
| } |
|
211 |
| |
|
212 |
0
| void formatView(ResponseRecord record)
|
|
213 |
| { |
|
214 |
0
| if (printHtmlResult)
|
|
215 |
| { |
|
216 |
0
| this.out.append(record.getHtmlOutput());
|
|
217 |
| } |
|
218 |
| else |
|
219 |
| { |
|
220 |
0
| this.out.append(record.getHtmlInput());
|
|
221 |
| } |
|
222 |
| } |
|
223 |
| |
|
224 |
7
| void printScript()
|
|
225 |
| { |
|
226 |
7
| appendResource(RESOURCE_JAVASCRIPT);
|
|
227 |
| } |
|
228 |
| |
|
229 |
5
| void printStylesheet()
|
|
230 |
| { |
|
231 |
5
| this.out.append("<style type=\"text/css\">\n");
|
|
232 |
5
| appendResource(RESOURCE_STYLESHEET);
|
|
233 |
5
| this.out.append("</style>\n");
|
|
234 |
| } |
|
235 |
| |
|
236 |
12
| void appendResource(String resourceName)
|
|
237 |
| { |
|
238 |
12
| InputStream in = this.getClass().getClassLoader().getResourceAsStream(resourceName);
|
|
239 |
12
| if (in == null)
|
|
240 |
| { |
|
241 |
0
| log.warn("resource not found:" + resourceName);
|
|
242 |
0
| return;
|
|
243 |
| } |
|
244 |
| |
|
245 |
12
| LineNumberReader lnr = new LineNumberReader(new InputStreamReader(in));
|
|
246 |
12
| String strSource;
|
|
247 |
12
| try
|
|
248 |
| { |
|
249 |
?
| while ((strSource = lnr.readLine()) != null)
|
|
250 |
| { |
|
251 |
281
| this.out.append(strSource).append("\n");
|
|
252 |
| } |
|
253 |
12
| lnr.close();
|
|
254 |
| } |
|
255 |
| catch (IOException e) |
|
256 |
| { |
|
257 |
0
| log.error("Error Reading resource " + resourceName, e);
|
|
258 |
| } |
|
259 |
| } |
|
260 |
| |
|
261 |
7
| void formatReport(ResponseRecord record) throws IOException
|
|
262 |
| { |
|
263 |
7
| if (completePage)
|
|
264 |
| { |
|
265 |
5
| if (this.xhtml)
|
|
266 |
| { |
|
267 |
5
| out.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
|
|
268 |
5
| out.append(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
|
|
269 |
| } |
|
270 |
| else |
|
271 |
| { |
|
272 |
0
| out.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
|
|
273 |
0
| out.append(" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
|
|
274 |
| } |
|
275 |
5
| out.append("<html><head><title>JTidy Messages</title>");
|
|
276 |
5
| printStylesheet();
|
|
277 |
5
| out.append("</head><body>\n");
|
|
278 |
| } |
|
279 |
| |
|
280 |
7
| printScript();
|
|
281 |
| |
|
282 |
7
| out.append("<table id=\"JTidyMessagesTable\" summary=\"\"><tr>");
|
|
283 |
7
| out.append("<td colspan=\"4\">JTidy Messages for request:" + record.getRequestID());
|
|
284 |
7
| out.append(" processed in " + record.getParsTime() + " milliseconds</td>");
|
|
285 |
7
| tr();
|
|
286 |
7
| out.append("<td colspan=\"4\">Validation Errors " + record.getParseErrors() + "</td>\n");
|
|
287 |
7
| tr();
|
|
288 |
7
| out.append("<td colspan=\"4\">Validation Warnings " + record.getParseWarnings() + "</td>\n");
|
|
289 |
| |
|
290 |
7
| HashMap map = new HashMap();
|
|
291 |
| |
|
292 |
| |
|
293 |
7
| Vector source = new Vector();
|
|
294 |
7
| LineNumberReader lnr = new LineNumberReader(new StringReader(record.getHtmlInput()));
|
|
295 |
7
| String strSource;
|
|
296 |
?
| while ((strSource = lnr.readLine()) != null)
|
|
297 |
| { |
|
298 |
| |
|
299 |
88
| source.add(strSource);
|
|
300 |
| } |
|
301 |
| |
|
302 |
7
| for (Iterator i = record.getMessages().iterator(); i.hasNext();)
|
|
303 |
| { |
|
304 |
7
| TidyMessage message = (TidyMessage) i.next();
|
|
305 |
7
| tr();
|
|
306 |
7
| Integer ln = new Integer(message.getLine());
|
|
307 |
| |
|
308 |
7
| StringBuffer lineStr = new StringBuffer(300);
|
|
309 |
7
| lineStr.append("Line ");
|
|
310 |
7
| if (printSource)
|
|
311 |
| { |
|
312 |
7
| lineStr.append("<a href=\"#line").append(ln).append("\" ");
|
|
313 |
7
| lineStr.append(" onclick=\"jTidyReportHighlight('").append(ln).append("')\" ");
|
|
314 |
7
| lineStr.append(">").append(ln).append("</a>");
|
|
315 |
| } |
|
316 |
| else |
|
317 |
| { |
|
318 |
0
| lineStr.append(ln);
|
|
319 |
| } |
|
320 |
| |
|
321 |
7
| if (map.get(ln) == null)
|
|
322 |
| { |
|
323 |
| |
|
324 |
3
| lineStr.append("<a name=\"errline").append(ln).append("\"></a>");
|
|
325 |
| } |
|
326 |
7
| lineStr.append(", ");
|
|
327 |
| |
|
328 |
7
| td(lineStr.toString());
|
|
329 |
| |
|
330 |
7
| td("column " + message.getColumn());
|
|
331 |
7
| td(message.getLevel().toString() + ":");
|
|
332 |
| |
|
333 |
7
| StringBuffer messageStr = new StringBuffer(300);
|
|
334 |
| |
|
335 |
7
| messageStr.append("<a title=\"");
|
|
336 |
7
| messageStr.append(HTMLEncode.encode((String) source.get(message.getLine() - 1)));
|
|
337 |
7
| messageStr.append("\">");
|
|
338 |
| |
|
339 |
7
| messageStr.append(HTMLEncode.encode(message.getMessage()));
|
|
340 |
| |
|
341 |
7
| messageStr.append("</a>");
|
|
342 |
| |
|
343 |
7
| td(messageStr.toString());
|
|
344 |
| |
|
345 |
7
| map.put(ln, message);
|
|
346 |
| } |
|
347 |
| |
|
348 |
7
| out.append("</tr>\n");
|
|
349 |
7
| out.append("</table>\n");
|
|
350 |
| |
|
351 |
7
| if (printSource)
|
|
352 |
| { |
|
353 |
6
| out.append("<div>");
|
|
354 |
6
| if (this.xhtml)
|
|
355 |
| { |
|
356 |
6
| out.append("<br/>");
|
|
357 |
| } |
|
358 |
| else |
|
359 |
| { |
|
360 |
0
| out.append("<br>");
|
|
361 |
| } |
|
362 |
6
| out.append("Below is the source used for this validation:");
|
|
363 |
| |
|
364 |
6
| out.append("<pre>");
|
|
365 |
6
| out.append("<a name=\"JTidyOriginalSource\"></a>");
|
|
366 |
| |
|
367 |
6
| int ln = 0;
|
|
368 |
6
| for (int lnIdx = 0; lnIdx < source.size(); lnIdx++)
|
|
369 |
| { |
|
370 |
72
| ln = lnIdx + 1;
|
|
371 |
72
| String str = (String) source.get(lnIdx);
|
|
372 |
72
| TidyMessage message = (TidyMessage) map.get(new Integer(ln));
|
|
373 |
| |
|
374 |
72
| out.append("<span id=\"srcline").append(ln).append("\"");
|
|
375 |
| |
|
376 |
72
| if (message != null)
|
|
377 |
| { |
|
378 |
| |
|
379 |
| |
|
380 |
3
| out.append("class = \"JTidyReportSrcLineError\" ");
|
|
381 |
| } |
|
382 |
72
| out.append(">");
|
|
383 |
| |
|
384 |
72
| identSpace(ln);
|
|
385 |
| |
|
386 |
72
| out.append("<a name=\"line");
|
|
387 |
72
| out.append(ln);
|
|
388 |
72
| out.append("\"></a><strong>");
|
|
389 |
| |
|
390 |
72
| if (message == null)
|
|
391 |
| { |
|
392 |
69
| out.append(ln);
|
|
393 |
| } |
|
394 |
| else |
|
395 |
| { |
|
396 |
| |
|
397 |
3
| out.append("<a href=\"#errline" + ln + "\">");
|
|
398 |
3
| out.append(ln);
|
|
399 |
3
| out.append("</a>");
|
|
400 |
| } |
|
401 |
| |
|
402 |
72
| out.append("</strong>");
|
|
403 |
| |
|
404 |
72
| if ((this.wrapSource) || (this.wrapLen != 0))
|
|
405 |
| { |
|
406 |
72
| int useWrapLen = this.wrapLen;
|
|
407 |
72
| if (useWrapLen == 0)
|
|
408 |
| { |
|
409 |
56
| useWrapLen = 100;
|
|
410 |
| } |
|
411 |
72
| str = wrap(str, useWrapLen);
|
|
412 |
| } |
|
413 |
| |
|
414 |
72
| if (message != null)
|
|
415 |
| { |
|
416 |
| |
|
417 |
3
| out.append("<a title=\"");
|
|
418 |
3
| out.append(message.getLevel().toString() + " :");
|
|
419 |
3
| out.append(HTMLEncode.encode(message.getMessage()));
|
|
420 |
3
| out.append("\">");
|
|
421 |
| } |
|
422 |
| |
|
423 |
72
| if (str.length() > 0)
|
|
424 |
| { |
|
425 |
60
| out.append("<code class=\"html\"> ");
|
|
426 |
60
| out.append(HTMLEncode.encode(str));
|
|
427 |
60
| out.append("</code>");
|
|
428 |
| } |
|
429 |
| |
|
430 |
72
| if (message != null)
|
|
431 |
| { |
|
432 |
3
| out.append("</a>");
|
|
433 |
| } |
|
434 |
72
| out.append("</span>");
|
|
435 |
72
| out.append("\n");
|
|
436 |
| } |
|
437 |
| |
|
438 |
| |
|
439 |
6
| out.append("<a name=\"line");
|
|
440 |
6
| out.append((ln + 1));
|
|
441 |
6
| out.append("\"></a>");
|
|
442 |
6
| out.append("EOF");
|
|
443 |
6
| out.append("</pre>");
|
|
444 |
6
| out.append("</div>");
|
|
445 |
| } |
|
446 |
| |
|
447 |
7
| if (printHtmlResult)
|
|
448 |
| { |
|
449 |
1
| out.append("<div>");
|
|
450 |
1
| if (this.xhtml) {
|
|
451 |
1
| out.append("<br/>");
|
|
452 |
| } else { |
|
453 |
0
| out.append("<br>");
|
|
454 |
| } |
|
455 |
1
| out.append("Below is the generated html code:");
|
|
456 |
| |
|
457 |
1
| LineNumberReader lnrr = new LineNumberReader(new StringReader(record.getHtmlOutput()));
|
|
458 |
| |
|
459 |
1
| out.append("<pre>");
|
|
460 |
1
| out.append("<a name=\"JTidyHtmlResult\"></a>");
|
|
461 |
| |
|
462 |
1
| String str;
|
|
463 |
1
| int ln = 0;
|
|
464 |
?
| while ((str = lnrr.readLine()) != null)
|
|
465 |
| { |
|
466 |
26
| ln = lnrr.getLineNumber();
|
|
467 |
| |
|
468 |
26
| identSpace(ln);
|
|
469 |
26
| out.append("<a name=\"resultLine");
|
|
470 |
26
| out.append(ln);
|
|
471 |
26
| out.append("\"></a><strong>");
|
|
472 |
26
| out.append(ln);
|
|
473 |
26
| out.append("</strong>");
|
|
474 |
| |
|
475 |
26
| if ((this.wrapSource) || (this.wrapLen != 0))
|
|
476 |
| { |
|
477 |
26
| int useWrapLen = this.wrapLen;
|
|
478 |
26
| if (useWrapLen == 0)
|
|
479 |
| { |
|
480 |
0
| useWrapLen = 100;
|
|
481 |
| } |
|
482 |
26
| str = wrap(str, useWrapLen);
|
|
483 |
| } |
|
484 |
| |
|
485 |
| |
|
486 |
26
| if (str.length() > 0)
|
|
487 |
| { |
|
488 |
23
| out.append("<code> ");
|
|
489 |
23
| out.append(HTMLEncode.encode(str));
|
|
490 |
23
| out.append("</code>");
|
|
491 |
| } |
|
492 |
| |
|
493 |
26
| out.append("\n");
|
|
494 |
| } |
|
495 |
1
| out.append("<a name=\"resultLine");
|
|
496 |
1
| out.append((ln + 1));
|
|
497 |
1
| out.append("\"></a>");
|
|
498 |
1
| out.append("EOF");
|
|
499 |
1
| out.append("</pre>");
|
|
500 |
1
| out.append("</div>");
|
|
501 |
| } |
|
502 |
| |
|
503 |
7
| if (completePage)
|
|
504 |
| { |
|
505 |
5
| out.append("</body></html>");
|
|
506 |
| } |
|
507 |
| } |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
98
| private String wrap(String str, int wrapLen)
|
|
516 |
| { |
|
517 |
98
| final String identLine = " ";
|
|
518 |
98
| int strLen = str.length();
|
|
519 |
98
| StringBuffer buffer = new StringBuffer(strLen + strLen / wrapLen + 3);
|
|
520 |
| |
|
521 |
98
| char[] charAry = str.toCharArray();
|
|
522 |
98
| int idx = 0;
|
|
523 |
98
| int lineLen = 0;
|
|
524 |
98
| boolean inString = false;
|
|
525 |
98
| while (idx < charAry.length)
|
|
526 |
| { |
|
527 |
3525
| char c = charAry[idx];
|
|
528 |
3525
| idx++;
|
|
529 |
3525
| lineLen++;
|
|
530 |
3525
| buffer.append(c);
|
|
531 |
3525
| if ((lineLen >= wrapLen) && (c == '>'))
|
|
532 |
| { |
|
533 |
4
| buffer.append('\n').append(identLine);
|
|
534 |
4
| lineLen = 0;
|
|
535 |
| } |
|
536 |
| |
|
537 |
3525
| if (c == '\"')
|
|
538 |
| { |
|
539 |
158
| if (inString && (lineLen >= (wrapLen + 10)))
|
|
540 |
| { |
|
541 |
14
| buffer.append('\n').append(identLine);
|
|
542 |
14
| lineLen = 0;
|
|
543 |
| } |
|
544 |
158
| inString = !inString;
|
|
545 |
| } |
|
546 |
| } |
|
547 |
| |
|
548 |
98
| return buffer.toString();
|
|
549 |
| } |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
9
| public void setCompletePage(boolean completePage)
|
|
555 |
| { |
|
556 |
9
| this.completePage = completePage;
|
|
557 |
| } |
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
5
| public void setView(boolean view)
|
|
563 |
| { |
|
564 |
5
| this.view = view;
|
|
565 |
| } |
|
566 |
| |
|
567 |
| |
|
568 |
| |
|
569 |
| |
|
570 |
9
| public void setPrintSource(boolean printSource)
|
|
571 |
| { |
|
572 |
9
| this.printSource = printSource;
|
|
573 |
| } |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
| |
|
578 |
4
| public void setWrapSource(boolean wrapSource)
|
|
579 |
| { |
|
580 |
4
| this.wrapSource = wrapSource;
|
|
581 |
| } |
|
582 |
| |
|
583 |
| |
|
584 |
| |
|
585 |
| |
|
586 |
4
| public void setPrintHtmlResult(boolean printHtmlResult)
|
|
587 |
| { |
|
588 |
4
| this.printHtmlResult = printHtmlResult;
|
|
589 |
| } |
|
590 |
| |
|
591 |
| |
|
592 |
| |
|
593 |
| |
|
594 |
4
| public void setWrapLen(int wrapLen)
|
|
595 |
| { |
|
596 |
4
| this.wrapLen = wrapLen;
|
|
597 |
| } |
|
598 |
| } |