1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.validator;
19
20 import org.w3c.dom.Node;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26
27
28
29
30
31
32 public class DefaultValidationReportBuilder implements ValidationReportBuilder {
33
34 private List<ValidationReport.Issue> issues;
35 private List<ValidationReport.RuleActivation> ruleActivations;
36 private List<ValidationReport.Error> errors;
37
38 public DefaultValidationReportBuilder() {
39
40 }
41
42 public ValidationReport getReport() {
43 return new DefaultValidationReport(issues == null ? Collections.<ValidationReport.Issue> emptyList() : issues,
44 ruleActivations == null ? Collections.<ValidationReport.RuleActivation> emptyList() : ruleActivations,
45 errors == null ? Collections.<ValidationReport.Error> emptyList() : errors);
46 }
47
48 public void reportIssue(ValidationReport.IssueLevel issueLevel, String message, Node n) {
49 if (issues == null) {
50 issues = new ArrayList<>();
51 }
52 issues.add(new ValidationReport.Issue(issueLevel, message, n));
53 }
54
55 public void reportIssue(ValidationReport.IssueLevel issueLevel, String message) {
56 reportIssue(issueLevel, message, null);
57 }
58
59 public void traceRuleActivation(Rule r) {
60 if (ruleActivations == null) {
61 ruleActivations = new ArrayList<>();
62 }
63 ruleActivations.add(new ValidationReport.RuleActivation(r));
64 }
65
66 public void reportRuleError(Rule r, Exception e, String msg) {
67 if (errors == null) {
68 errors = new ArrayList<>();
69 }
70 errors.add(new ValidationReport.RuleError(r, e, msg));
71 }
72
73 public void reportFixError(Fix f, Exception e, String msg) {
74 if (errors == null) {
75 errors = new ArrayList<>();
76 }
77 errors.add(new ValidationReport.FixError(f, e, msg));
78
79 }
80
81 @Override
82 public String toString() {
83 final StringBuilder sb = new StringBuilder();
84 if (ruleActivations != null) {
85 sb.append("Rules {\n");
86 for (ValidationReport.RuleActivation ra : ruleActivations) {
87 sb.append(ra).append('\n');
88 }
89 sb.append("}\n");
90 }
91 if (issues != null) {
92 sb.append("Issues {\n");
93 for (ValidationReport.Issue issue : issues) {
94 sb.append(issue.toString()).append('\n');
95 }
96 sb.append("}\n");
97 }
98 if (errors != null) {
99 sb.append("Errors {\n");
100 for (ValidationReport.Error error : errors) {
101 sb.append(error.toString()).append('\n');
102 }
103 sb.append("}\n");
104 }
105 return sb.toString();
106 }
107
108 }