]> WPIA git - gigi.git/blob - src/club/wpia/gigi/output/template/TemplateParseException.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / output / template / TemplateParseException.java
1 package club.wpia.gigi.output.template;
2
3 import java.io.IOException;
4 import java.util.Set;
5 import java.util.TreeSet;
6
7 /**
8  * A exception that is thrown when a template contains syntactic errors. It
9  * allows the combining of several error messages to catch more than one error
10  * in a template.
11  */
12 public class TemplateParseException extends IOException {
13
14     private static final long serialVersionUID = 1L;
15
16     private Object templateSource;
17
18     private Set<ErrorMessage> errors = new TreeSet<>();
19
20     public TemplateParseException(Object templateSource) {
21         this.templateSource = templateSource;
22     }
23
24     public void addError(ErrorMessage error) {
25         errors.add(error);
26     }
27
28     public void addError(int line, int column, String message, String erroneousLine) {
29         addError(new ErrorMessage(line, column, message, erroneousLine));
30     }
31
32     public void append(TemplateParseException other) {
33         errors.addAll(other.errors);
34     }
35
36     @Override
37     public String toString() {
38         StringBuilder strb = new StringBuilder("Error in template \"");
39         strb.append(templateSource);
40         strb.append("\":");
41         for (ErrorMessage errorMessage : errors) {
42             strb.append("\n\t");
43             strb.append(errorMessage.toString());
44         }
45         return strb.toString();
46     }
47
48     @Override
49     public String getMessage() {
50         return toString();
51     }
52
53     public boolean isEmpty() {
54         return errors.isEmpty();
55     }
56
57     public static class ErrorMessage implements Comparable<ErrorMessage> {
58
59         private final int line;
60
61         private final int column;
62
63         private final String message;
64
65         private final String erroneousLine;
66
67         public ErrorMessage(int line, int column, String message, String erroneousLine) {
68             this.line = line;
69             this.column = column;
70             this.message = message;
71             this.erroneousLine = erroneousLine;
72         }
73
74         @Override
75         public String toString() {
76             return String.format("Around %d:%d (after …%s…) %s", line, column, erroneousLine, message);
77         }
78
79         @Override
80         public int compareTo(ErrorMessage o) {
81             int l = Integer.compare(line, o.line);
82             if (l != 0) {
83                 return l;
84             }
85             return Integer.compare(column, o.column);
86         }
87     }
88
89 }