Debian Codegraph @

src/ch/poole/openinghoursparser/OpeningHoursParseException.java

raw symbols SCIP
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 * Represents an exception when parsing the opening hour string
13 *
14 * @author JOSM team, Simon Legner
15 * @author Simon Poole
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; // NOSONAR
24 private String expected = null; // NOSONAR
25 private List<OpeningHoursParseException> exceptions = null; // NOSONAR
26
27 private static String EOL = System.getProperty("line.separator", "\n");
28
29 /**
30 * Construct a new exception from an optional message
31 *
32 * @param message the message or null
33 */
34 OpeningHoursParseException(@Nullable String message) {
35 this(message, -1, -1);
36 }
37
38 /**
39 * Create a new exception from a message and the token that caused this exception to be thrown
40 *
41 * @param message an optional message
42 * @param token an optional token
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 * Construct a new exception from an optional message and indication of where the error was
50 *
51 * @param message an optional message
52 * @param line the line where the exceptions was thrown
53 * @param column the column where the exception was thrown
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 * Construct a new exception from an existing ParseException
63 *
64 * @param ex a ParseException
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 * Construct a new exception from a List of existing ParseExceptions
76 *
77 * @param exceptions a List of ParseExceptions
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 * If we have multiple exceptions return the first one
96 *
97 * @return the first exception or null
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 * Returns the line number
109 *
110 * @return the line number
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 * Returns the column number
122 *
123 * @return the column number
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 * Get a list of the exceptions that caused this to be thrown
147 *
148 * @return a list of OpeningHoursParseException
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 * Create a localized message from the token information
162 */
163 private void setEncounteredExpected() {
164 // localized ch.poole.openinghoursparser.ParseException.initialise
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 }