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 | 0 | public DefaultValidationReportBuilder() {} |
39 | |
|
40 | |
public ValidationReport getReport() { |
41 | 0 | return new DefaultValidationReport( |
42 | |
issues == null ? Collections.<ValidationReport.Issue>emptyList() : issues, |
43 | |
ruleActivations == null ? Collections.<ValidationReport.RuleActivation>emptyList() : ruleActivations, |
44 | |
errors == null ? Collections.<ValidationReport.Error>emptyList() : errors |
45 | |
); |
46 | |
} |
47 | |
|
48 | |
public void reportIssue(ValidationReport.IssueLevel issueLevel, String message, Node n) { |
49 | 0 | if(issues == null) { |
50 | 0 | issues = new ArrayList<ValidationReport.Issue>(); |
51 | |
} |
52 | 0 | issues.add( new ValidationReport.Issue(issueLevel, message, n) ); |
53 | 0 | } |
54 | |
|
55 | |
public void reportIssue(ValidationReport.IssueLevel issueLevel, String message) { |
56 | 0 | reportIssue(issueLevel, message, null); |
57 | 0 | } |
58 | |
|
59 | |
public void traceRuleActivation(Rule r) { |
60 | 0 | if(ruleActivations == null) { |
61 | 0 | ruleActivations = new ArrayList<ValidationReport.RuleActivation>(); |
62 | |
} |
63 | 0 | ruleActivations.add( new ValidationReport.RuleActivation(r) ); |
64 | 0 | } |
65 | |
|
66 | |
public void reportRuleError(Rule r, Exception e, String msg) { |
67 | 0 | if(errors == null) { |
68 | 0 | errors = new ArrayList<ValidationReport.Error>(); |
69 | |
} |
70 | 0 | errors.add( new ValidationReport.RuleError(r, e, msg) ); |
71 | 0 | } |
72 | |
|
73 | |
public void reportFixError(Fix f, Exception e, String msg) { |
74 | 0 | if(errors == null) { |
75 | 0 | errors = new ArrayList<ValidationReport.Error>(); |
76 | |
} |
77 | 0 | errors.add( new ValidationReport.FixError(f, e, msg) ); |
78 | |
|
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public String toString() { |
83 | 0 | final StringBuilder sb = new StringBuilder(); |
84 | 0 | if(ruleActivations != null) { |
85 | 0 | sb.append("Rules {\n"); |
86 | 0 | for(ValidationReport.RuleActivation ra :ruleActivations) { |
87 | 0 | sb.append(ra).append('\n'); |
88 | |
} |
89 | 0 | sb.append("}\n"); |
90 | |
} |
91 | 0 | if(issues != null) { |
92 | 0 | sb.append("Issues {\n"); |
93 | 0 | for(ValidationReport.Issue issue : issues) { |
94 | 0 | sb.append( issue.toString() ).append('\n'); |
95 | |
} |
96 | 0 | sb.append("}\n"); |
97 | |
} |
98 | 0 | if (errors != null) { |
99 | 0 | sb.append("Errors {\n"); |
100 | 0 | for (ValidationReport.Error error : errors) { |
101 | 0 | sb.append( error.toString() ).append('\n'); |
102 | |
} |
103 | 0 | sb.append("}\n"); |
104 | |
} |
105 | 0 | return sb.toString(); |
106 | |
} |
107 | |
|
108 | |
} |