| 1 |
package ch.poole.openinghoursparser; |
| 2 |
|
| 3 |
import org.jetbrains.annotations.NotNull; |
| 4 |
import org.jetbrains.annotations.Nullable; |
| 5 |
|
| 6 |
import static ch.poole.openinghoursparser.I18n.tr; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.List; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class OpeningHoursParseException extends ParseException { |
| 18 |
|
| 19 |
private static final long serialVersionUID = 1L; |
| 20 |
|
| 21 |
private final int line; |
| 22 |
private final int column; |
| 23 |
private String encountered = null; |
| 24 |
private String expected = null; |
| 25 |
private List<OpeningHoursParseException> exceptions = null; |
| 26 |
|
| 27 |
private static String EOL = System.getProperty("line.separator", "\n"); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
OpeningHoursParseException(@Nullable String message) { |
| 35 |
this(message, -1, -1); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
OpeningHoursParseException(@Nullable String message, @Nullable Token token) { |
| 45 |
this(message, token != null ? token.beginLine : -1, token != null ? token.beginColumn : -1); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private OpeningHoursParseException(@Nullable String message, int line, int column) { |
| 56 |
super(message); |
| 57 |
this.line = line; |
| 58 |
this.column = column; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
OpeningHoursParseException(@NotNull ParseException ex) { |
| 67 |
this(null, ex.currentToken); |
| 68 |
this.currentToken = ex.currentToken; |
| 69 |
this.expectedTokenSequences = ex.expectedTokenSequences; |
| 70 |
this.tokenImage = ex.tokenImage; |
| 71 |
setEncounteredExpected(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
OpeningHoursParseException(@NotNull List<ParseException> exceptionList) { |
| 80 |
this(!exceptionList.isEmpty() ? exceptionList.get(0).getMessage() : null, null); |
| 81 |
if (exceptionList.isEmpty()) { |
| 82 |
throw new IllegalArgumentException("exceptions is empty"); |
| 83 |
} |
| 84 |
exceptions = new ArrayList<>(); |
| 85 |
for (ParseException ex : exceptionList) { |
| 86 |
if (ex instanceof OpeningHoursParseException) { |
| 87 |
exceptions.add((OpeningHoursParseException) ex); |
| 88 |
} else { |
| 89 |
exceptions.add(new OpeningHoursParseException(ex)); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@Nullable |
| 100 |
private OpeningHoursParseException getFirstException() { |
| 101 |
if (exceptions != null && !exceptions.isEmpty()) { |
| 102 |
return exceptions.get(0); |
| 103 |
} |
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public int getLine() { |
| 113 |
OpeningHoursParseException first = getFirstException(); |
| 114 |
if (first != null) { |
| 115 |
return first.getLine(); |
| 116 |
} |
| 117 |
return line; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public int getColumn() { |
| 126 |
OpeningHoursParseException first = getFirstException(); |
| 127 |
if (first != null) { |
| 128 |
return first.getColumn(); |
| 129 |
} |
| 130 |
return column; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public String getMessage() { |
| 135 |
OpeningHoursParseException first = getFirstException(); |
| 136 |
if (first != null) { |
| 137 |
return first.getMessage(); |
| 138 |
} |
| 139 |
final String message = encountered == null || encountered.isEmpty() ? super.getMessage() : tr("exception_encountered", encountered); |
| 140 |
final String string = line >= 0 && column >= 0 ? tr("exception_line_column", message, line, column) : message; |
| 141 |
final String appendix = expected == null || expected.isEmpty() ? "" : EOL + tr("exception_expecting", expected); |
| 142 |
return string + appendix; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@NotNull |
| 151 |
public List<OpeningHoursParseException> getExceptions() { |
| 152 |
if (exceptions != null) { |
| 153 |
return exceptions; |
| 154 |
} |
| 155 |
List<OpeningHoursParseException> ex = new ArrayList<>(); |
| 156 |
ex.add(this); |
| 157 |
return ex; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private void setEncounteredExpected() { |
| 164 |
|
| 165 |
if (expectedTokenSequences == null) { |
| 166 |
return; |
| 167 |
} |
| 168 |
StringBuilder expectedBuilder = new StringBuilder(); |
| 169 |
int maxSize = 0; |
| 170 |
for (int[] expectedTokenSequence : expectedTokenSequences) { |
| 171 |
if (maxSize < expectedTokenSequence.length) { |
| 172 |
maxSize = expectedTokenSequence.length; |
| 173 |
} |
| 174 |
for (int i : expectedTokenSequence) { |
| 175 |
expectedBuilder.append(tokenImage[i]).append(' '); |
| 176 |
} |
| 177 |
if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) { |
| 178 |
expectedBuilder.append("..."); |
| 179 |
} |
| 180 |
expectedBuilder.append(EOL).append(" "); |
| 181 |
} |
| 182 |
expected = expectedBuilder.toString().trim(); |
| 183 |
|
| 184 |
StringBuilder encounteredBuilder = new StringBuilder(); |
| 185 |
Token tok = currentToken.next; |
| 186 |
for (int i = 0; i < maxSize; i++) { |
| 187 |
if (i != 0) |
| 188 |
encounteredBuilder.append(" "); |
| 189 |
if (tok.kind == 0) { |
| 190 |
encounteredBuilder.append(tokenImage[0]); |
| 191 |
break; |
| 192 |
} |
| 193 |
encounteredBuilder.append(" ").append(tokenImage[tok.kind]); |
| 194 |
encounteredBuilder.append(" \""); |
| 195 |
encounteredBuilder.append(add_escapes(tok.image)); |
| 196 |
encounteredBuilder.append(" \""); |
| 197 |
tok = tok.next; |
| 198 |
} |
| 199 |
encountered = encounteredBuilder.toString(); |
| 200 |
} |
| 201 |
} |