1 package club.wpia.gigi.output.template;
3 import java.io.IOException;
5 import java.util.TreeSet;
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
12 public class TemplateParseException extends IOException {
14 private static final long serialVersionUID = 1L;
16 private Object templateSource;
18 private Set<ErrorMessage> errors = new TreeSet<>();
20 public TemplateParseException(Object templateSource) {
21 this.templateSource = templateSource;
24 public void addError(ErrorMessage error) {
28 public void addError(int line, int column, String message, String erroneousLine) {
29 addError(new ErrorMessage(line, column, message, erroneousLine));
32 public void append(TemplateParseException other) {
33 errors.addAll(other.errors);
37 public String toString() {
38 StringBuilder strb = new StringBuilder("Error in template \"");
39 strb.append(templateSource);
41 for (ErrorMessage errorMessage : errors) {
43 strb.append(errorMessage.toString());
45 return strb.toString();
49 public String getMessage() {
53 public boolean isEmpty() {
54 return errors.isEmpty();
57 public static class ErrorMessage implements Comparable<ErrorMessage> {
59 private final int line;
61 private final int column;
63 private final String message;
65 private final String erroneousLine;
67 public ErrorMessage(int line, int column, String message, String erroneousLine) {
70 this.message = message;
71 this.erroneousLine = erroneousLine;
75 public String toString() {
76 return String.format("Around %d:%d (after …%s…) %s", line, column, erroneousLine, message);
80 public int compareTo(ErrorMessage o) {
81 int l = Integer.compare(line, o.line);
85 return Integer.compare(column, o.column);