corpus-services  1.0
ReportItem.java
Go to the documentation of this file.
1 
10 package de.uni_hamburg.corpora;
11 
13 import org.xml.sax.SAXParseException;
14 import java.io.StringWriter;
15 import java.io.PrintWriter;
16 import java.sql.Timestamp;
17 import java.util.Collection;
18 
25 public class ReportItem {
26 
44  public enum Severity {
51  UNKNOWN
52  }
53 
55  private String what;
57  private String howto;
59  private Throwable e;
61  private Severity severity = Severity.CRITICAL;
63  private String filename;
68  private String lines;
70  private String columns;
71  //name of the function that caused the error
72  private String function;
73 
78  public ReportItem() {
79  what = "Totally unknown error";
80  howto = "No known fixes";
81  e = null;
82  function = "Unknown function";
83  }
84 
85  public ReportItem(Severity s, String what) {
86  this.severity = s;
87  this.what = what;
88  this.function = "Unknown function";
89  }
90 
91  public ReportItem(Severity s, Throwable e, String what) {
92  this.severity = s;
93  this.e = e;
94  this.what = what;
95  this.function = "Unknown function";
96  }
97 
98  public ReportItem(Severity s, Throwable e, String filename, String what) {
99  this.severity = s;
100  this.e = e;
101  this.what = what;
102  this.filename = filename;
103  this.function = "Unknown function";
104  }
105 
106  public ReportItem(Severity s, String filename, String what, String function) {
107  this.severity = s;
108  this.what = what;
109  this.filename = filename;
110  this.function = function;
111  }
112 
118  public ReportItem(Severity s, SAXParseException saxpe, String what) {
119  this.severity = s;
120  this.e = saxpe;
121  this.what = what;
122  this.howto = howto;
123  this.filename = saxpe.getSystemId();
124  this.lines = "" + saxpe.getLineNumber();
125  this.columns = "" + saxpe.getColumnNumber();
126  this.function = "Unknown function";
127  }
128 
129 
134  public ReportItem(Severity s, String filename,
135  String what, String function, String howto) {
136  this.severity = s;
137  this.filename = filename;
138  this.what = what;
139  this.howto = howto;
140  this.function = function;
141  }
142 
147  return this.severity;
148  }
149 
153  public boolean isGood() {
154  if ((this.severity == Severity.CORRECT) ||
155  (this.severity == Severity.NOTE) || (this.severity == Severity.IFIXEDITFORYOU)) {
156  return true;
157  } else if ((this.severity == Severity.WARNING) ||
158  (this.severity == Severity.CRITICAL) ||
159  (this.severity == Severity.MISSING)) {
160  return false;
161  } else {
162  System.out.println("Missed a severity case in isGood :-(");
163  return false;
164  }
165  }
166 
170  public boolean isBad() {
171  if ((this.severity == Severity.CORRECT) ||
172  (this.severity == Severity.NOTE) || (this.severity == Severity.IFIXEDITFORYOU)) {
173  return false;
174  } else if ((this.severity == Severity.WARNING) ||
175  (this.severity == Severity.CRITICAL) ||
176  (this.severity == Severity.MISSING)) {
177  return true;
178  } else {
179  System.out.println("Missed a severity case in isBad :-(");
180  return true;
181  }
182  }
183 
187  public boolean isSevere() {
188  if ((this.severity == Severity.CORRECT) ||
189  (this.severity == Severity.WARNING) ||
190  (this.severity == Severity.NOTE) || (this.severity == Severity.IFIXEDITFORYOU)){
191  return false;
192  } else if ((this.severity == Severity.CRITICAL) ||
193  (this.severity == Severity.MISSING)) {
194  return true;
195  } else {
196  System.out.println("Missed a severity case in isSevere :-(");
197  return true;
198  }
199  }
200 
204  public boolean isFix() {
205  if ((this.severity == Severity.CORRECT) ||
206  (this.severity == Severity.NOTE) || (this.severity == Severity.CRITICAL) ||
207  (this.severity == Severity.MISSING) || (this.severity == Severity.WARNING)) {
208  return false;
209  } else if (this.severity == Severity.IFIXEDITFORYOU)
210  {
211  return true;
212  } else {
213  System.out.println("Missed a severity case in isFix :-(");
214  return true;
215  }
216  }
217 
221  public String getLocation() {
222  String loc = new String();
223  if (filename != null) {
224  loc = filename;
225  } else {
226  loc = "";
227  }
228  if (lines != null) {
229  if (columns != null) {
230  loc += ":" + lines + "." + columns;
231  } else {
232  loc += ":" + lines;
233  }
234  }
235  return loc;
236  }
237 
241  public String getWhat() {
242  return this.what;
243  }
244 
248  public String getHowto() {
249  if (this.howto != null) {
250  return this.howto;
251  } else {
252  return "";
253  }
254  }
255 
259  public String getFunction() {
260  if (this.function != null) {
261  return this.function;
262  } else {
263  return "";
264  }
265  }
266 
270  public String getLocalisedMessage() {
271  if (e != null) {
272  return e.getLocalizedMessage();
273  } else {
274  return "";
275  }
276  }
277 
281  public String getSummary() {
282  String s = " ";
283  if (!getLocation().equals("")) {
284  s += getLocation() + ": ";
285  }
286  s += getWhat();
287  return s;
288  }
289 
294  public String toString() {
295  return getLocation() + ": " + getWhat() + ". " + getHowto() + ". " +
296  getLocalisedMessage() + "\n" + getStackTrace();
297  }
298 
302  public String getStackTrace() {
303  if (e != null) {
304  StringWriter sw = new StringWriter();
305  PrintWriter pw = new PrintWriter(sw);
306  e.printStackTrace(pw);
307  return sw.toString();
308  } else {
309  return "";
310  }
311  }
312 
320  public static String
321  generatePlainText(Collection<ReportItem> errors,
322  boolean verbose) {
323  String report = new String();
324  int criticals = 0;
325  int warnings = 0;
326  int notes = 0;
327  int unknowns = 0;
328  for (ReportItem error : errors) {
329  switch (error.getSeverity()) {
330  case CRITICAL:
331  criticals++;
332  break;
333  case WARNING:
334  warnings++;
335  break;
336  case NOTE:
337  notes++;
338  break;
339  case UNKNOWN:
340  unknowns++;
341  break;
342  default:
343  break;
344  }
345  }
346  report += "Messages (" + errors.size() + "), of which: ";
347  if (verbose) {
348  report += criticals + " critical, " + warnings + " warnings, " +
349  notes + " notes and " + unknowns + " not classified\n";
350  } else {
351  report += criticals + " critical and " + warnings +
352  " warnings (and " + (notes + unknowns) +
353  " hidden as non-problems or unknown)\n";
354  }
355  for (ReportItem error : errors) {
356  if (verbose) {
357  report += " " + error + "\n";
358  } else if (error.getSeverity() == ReportItem.Severity.WARNING ||
359  error.getSeverity() == ReportItem.Severity.CRITICAL) {
360  report += " " + error.getLocation() + ": " +
361  error.getWhat() + "\n" +
362  " " + error.getHowto() + "\n";
363  }
364  }
365  return report;
366  }
367 
371  public static String generateSummary(Collection<ReportItem>
372  errors) {
373  String report = new String();
374  int criticals = 0;
375  int warnings = 0;
376  int notes = 0;
377  int unknowns = 0;
378  for (ReportItem error : errors) {
379  switch (error.getSeverity()) {
380  case CRITICAL:
381  criticals++;
382  break;
383  case WARNING:
384  warnings++;
385  break;
386  case NOTE:
387  notes++;
388  break;
389  case UNKNOWN:
390  unknowns++;
391  break;
392  default:
393  break;
394  }
395  }
396  report = "Total of " + (criticals + warnings + notes + unknowns) +
397  " menssages: " + criticals + " critical errors, " +
398  warnings + " warnings, " + notes + " notes and " + unknowns +
399  " others.";
400  return report;
401  }
402 
408  public static String generateHTML(Collection<ReportItem>
409  errors) {
410  int criticals = 0;
411  int warnings = 0;
412  int notes = 0;
413  int unknowns = 0;
414  for (ReportItem error : errors) {
415  switch (error.getSeverity()) {
416  case CRITICAL:
417  criticals++;
418  break;
419  case WARNING:
420  warnings++;
421  break;
422  case NOTE:
423  notes++;
424  break;
425  case UNKNOWN:
426  unknowns++;
427  break;
428  default:
429  break;
430  }
431  }
432  String report = new String();
433  //report = "<p>The following errors are from XML Schema validation only.</p>\n";
434  report += "<script type='text/javascript'>\n" +
435  "function showClick(clicksource, stuff) {\n\t" +
436  " var elems = document.getElementsByClassName(stuff);\n" +
437  " for (i = 0; i < elems.length; i++) {\n" +
438  " if (clicksource.checked) {\n" +
439  " elems[i].style.display = 'table-row';\n" +
440  " } else {\n" +
441  " elems[i].style.display = 'none';\n" +
442  " }\n" +
443  " }\n" +
444  "}\n</script>";
445  report += "<form>\n" +
446  " <input type='checkbox' id='critical' name='critical' value='show' checked='checked' onclick='showClick(this, &apos;critical&apos;)'>"
447  + "Show criticals (" + criticals + ")</input>" +
448  " <input type='checkbox' name='warning' value='show' checked='checked' onclick='showClick(this, &apos;warning&apos;)'>"
449  + "Show warnings (" + warnings + ")</input>" +
450  " <input type='checkbox' name='note' value='show' onclick='showClick(this, &apos;note&apos;)'>"
451  + "Show notes (" + notes + ")</input>" +
452  " <input type='checkbox' name='unknown' value='show' onclick='showClick(this, &apos;unknown&apos;)'>"
453  + "Show unknowns (" + unknowns + ")</input>" +
454  "</form>";
455  report += "<table>\n <thead><tr>" +
456  "<th>File:line.column</th><th>Error</th>" +
457  "<th>Fix</th><th>Original</th>" +
458  "</tr></thead>\n";
459  report += " <tbody>\n";
460  for (ReportItem error : errors) {
461  switch (error.getSeverity()) {
462  case CRITICAL:
463  report += "<tr class='critical'><td style='border-left: red solid 3px'>";
464  break;
465  case WARNING:
466  report += "<tr class='warning'><td style='border-left: yellow solid 3px'>";
467  break;
468  case NOTE:
469  report += "<tr class='note' style='display: none;'><td style='border-left: green solid 3px'>";
470  break;
471  case UNKNOWN:
472  report += "<tr class='unknown' style='display: none;'><td style='border-left: orange solid 3px'>";
473  break;
474  default:
475  report += "<tr class='other' style='display: none;'><td style='border-left: black solid 3px'>";
476  break;
477  }
478  report += error.getLocation() + "</td>";
479  report += "<td style='border: red solid 1px; white-space: pre'>" +
480  error.getWhat() +
481  "</td>";
482  report += "<td style='border: green solid 1px; white-space: pre'>" +
483  error.getHowto() +
484  "</td>";
485  report += "<td style='font-face: monospace; color: gray; border: gray solid 1px; white-space: pre'>(" +
486  error.getLocalisedMessage() +
487  ")</td>\n";
488  report += "<!-- " + error.getStackTrace() + " -->\n";
489  report += "</tr>";
490  }
491  report += " </tbody>\n </table>\n";
492  return report;
493  }
494 
495 
496 
502  public static String generateDataTableHTML(Collection<ReportItem> errors, String summarylines) {
503 
504  String report = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
505 
506  report += "<html>\n <head>\n";
507 
508  report += "<title>Corpus Check Report</title>\n";
509  report += "<meta charset=\"utf-8\"></meta>\n";
510 
511  //add JS libraries
512  report += "<script>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/js/jquery/jquery-3.1.1.min.js")) + "</script>\n";
513  report += "<script>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/js/DataTables/jquery.dataTables-1.10.12.min.js")) + "</script>\n";
514  report += "<script>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/js/DataTables/dataTables-bootstrap.min.js")) + "</script>\n";
515  report += "<script>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/js/bootstrap/bootstrap-3.3.7.min.js")) + "</script>\n";
516 
517  //add CSS
518  report += "<style>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/css/DataTables/dataTables.bootstrap.min.css")) + "</style>\n";
519  report += "<style>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/css/DataTables/buttons.dataTables.min.css")) + "</style>\n";
520  report += "<style>" + TypeConverter.InputStream2String(ReportItem.class.getResourceAsStream("/css/bootstrap/bootstrap-3.3.7.min.css")) + "</style>\n";
521 
522  //add custom CSS
523  report += "<style>"+
524  "body{padding:15px;}"+
525  "#timestamp{margin-bottom:30px;}"+
526  ".critical{ background:#ffdddd; } "+
527  ".other{ background:#ffd39e; } "+
528  ".warning{ background:#fafcc2; } "+
529  ".char_Cyrillic{ color:#b51d0d; } "+
530  ".char_Greek{ background:#022299; } "+
531  ".char_Armenian{ background:#ad7600; } "+
532  ".char_Georgian{ background:#9c026d; } "+
533  "</style>\n";
534  report += " </head>\n <body>\n";
535 
536  //add timestamp
537  report += " <div id='timestamp'>Generated: ";
538  Timestamp timestamp = new Timestamp(System.currentTimeMillis());
539  report += timestamp + "</div>\n";
540 
541  report += "<table>\n <thead><tr>" +
542  "<th>Type</th>"+
543  "<th>Function</th>"+
544  "<th>Filename:line.column</th>"+
545  "<th>Error</th>" +
546  "<th>Fix</th>"+
547  "<th>Original</th>" +
548  "</tr></thead>\n";
549  report += " <tbody>\n";
550  for (ReportItem error : errors) {
551  switch (error.getSeverity()) {
552  case CRITICAL:
553  report += "<tr class='critical'><td style='border-left: red solid 3px'>Critical</td><td>";
554  break;
555  case WARNING:
556  report += "<tr class='warning'><td style='border-left: yellow solid 3px'>Warning</td><td>";
557  break;
558  case NOTE:
559  report += "<tr class='note'><td style='border-left: green solid 3px'>Note</td><td>";
560  break;
561  case UNKNOWN:
562  report += "<tr class='unknown'><td style='border-left: orange solid 3px'>Unknown</td><td>";
563  break;
564  default:
565  report += "<tr class='other'><td style='border-left: black solid 3px'>Other</td><td>";
566  break;
567  }
568  report += error.getFunction() + "</td><td>";
569  report += error.getLocation() + "</td>";
570  report += "<td style='white-space: pre'>" +
571  error.getWhat() +
572  "</td>";
573  report += "<td style='white-space: pre'>" +
574  error.getHowto() +
575  "</td>";
576  report += "<td style='font-face: monospace; color: gray; border: gray solid 1px; white-space: pre;'>(" +
577  error.getLocalisedMessage() +
578  ")</td>\n";
579  report += "<!-- " + error.getStackTrace() + " -->\n";
580  report += "</tr>";
581  }
582  report += " </tbody>\n </table>\n";
583 
584  //initiate DataTable on <table>
585  report += "<script>$(document).ready( function () {\n" +
586  " $('table').DataTable({ 'iDisplayLength': 50 });\n" +
587  "} );</script>";
588 
589  report += " <footer style='white-space: pre'>" + summarylines + "</footer>";
590  report += " </body>\n</html>";
591 
592  return report;
593  }
594 
595 }
static String generateDataTableHTML(Collection< ReportItem > errors, String summarylines)
ReportItem(Severity s, Throwable e, String filename, String what)
Definition: ReportItem.java:98
ReportItem(Severity s, String what)
Definition: ReportItem.java:85
static String generatePlainText(Collection< ReportItem > errors, boolean verbose)
static String InputStream2String(InputStream is)
static String generateSummary(Collection< ReportItem > errors)
ReportItem(Severity s, SAXParseException saxpe, String what)
static String generateHTML(Collection< ReportItem > errors)
ReportItem(Severity s, String filename, String what, String function)
ReportItem(Severity s, String filename, String what, String function, String howto)
ReportItem(Severity s, Throwable e, String what)
Definition: ReportItem.java:91