1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.qedeq.kernel.common; |
19 |
| |
20 |
| import java.io.File; |
21 |
| import java.io.IOException; |
22 |
| import java.net.URL; |
23 |
| |
24 |
| import org.qedeq.kernel.utility.IoUtility; |
25 |
| import org.qedeq.kernel.utility.ReplaceUtility; |
26 |
| import org.qedeq.kernel.utility.TextInput; |
27 |
| import org.xml.sax.SAXException; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public final class XmlFileException extends Exception { |
37 |
| |
38 |
| |
39 |
| private static final long serialVersionUID = -4109767904038020052L; |
40 |
| |
41 |
| |
42 |
| private final int errorCode; |
43 |
| |
44 |
| |
45 |
| private final SourceArea errorArea; |
46 |
| |
47 |
| |
48 |
| private final SourceArea referenceArea; |
49 |
| |
50 |
| |
51 |
| private String line; |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
5
| public XmlFileException(final QedeqException exception, final SourceArea errorArea,
|
61 |
| final SourceArea referenceArea) { |
62 |
5
| super(exception);
|
63 |
5
| this.errorCode = exception.getErrorCode();
|
64 |
5
| this.errorArea = errorArea;
|
65 |
5
| this.referenceArea = referenceArea;
|
66 |
| } |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
10
| public XmlFileException(final SyntaxException exception) {
|
75 |
10
| super(exception);
|
76 |
10
| errorCode = exception.getErrorCode();
|
77 |
10
| errorArea = new SourceArea(exception.getErrorPosition().getAddress(),
|
78 |
| exception.getErrorPosition().getLocalAddress(), exception.getErrorPosition(), null); |
79 |
10
| referenceArea = null;
|
80 |
| } |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
0
| public XmlFileException(final URL url, final Exception exception) {
|
89 |
0
| super(exception);
|
90 |
0
| this.errorCode = 9997;
|
91 |
0
| errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
|
92 |
0
| referenceArea = null;
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
0
| public XmlFileException(final File file, final Exception exception) {
|
102 |
0
| super(exception);
|
103 |
0
| this.errorCode = 9998;
|
104 |
0
| final URL url = IoUtility.toUrl(file);
|
105 |
0
| errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
|
106 |
0
| referenceArea = null;
|
107 |
| } |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
1
| public XmlFileException(final Exception exception) {
|
115 |
1
| super(exception);
|
116 |
1
| this.errorCode = 9999;
|
117 |
1
| errorArea = null;
|
118 |
1
| referenceArea = null;
|
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
0
| public XmlFileException(final IOException exception) {
|
127 |
0
| super(exception);
|
128 |
0
| this.errorCode = 9997;
|
129 |
0
| errorArea = null;
|
130 |
0
| referenceArea = null;
|
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
0
| public XmlFileException(final SAXException exception) {
|
139 |
0
| super(exception);
|
140 |
0
| this.errorCode = SyntaxException.SAX_PARSER_EXCEPTION;
|
141 |
0
| errorArea = null;
|
142 |
0
| referenceArea = null;
|
143 |
| } |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
26
| public final SourceArea getSourceArea() {
|
151 |
26
| return errorArea;
|
152 |
| } |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
0
| public final SourceArea getReferenceArea() {
|
160 |
0
| return referenceArea;
|
161 |
| } |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
18
| public final int getErrorCode() {
|
169 |
18
| return errorCode;
|
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
4
| public final String getLine() {
|
178 |
4
| if (line == null) {
|
179 |
4
| line = "";
|
180 |
4
| try {
|
181 |
4
| final TextInput input = new TextInput(errorArea.getLocalAddress());
|
182 |
4
| input.setRow(errorArea.getStartPosition().getLine());
|
183 |
4
| input.setColumn(errorArea.getStartPosition().getColumn());
|
184 |
4
| line = input.getLine();
|
185 |
| } catch (Exception e) { |
186 |
| |
187 |
| } |
188 |
| } |
189 |
4
| return line;
|
190 |
| } |
191 |
| |
192 |
5
| public final String getMessage() {
|
193 |
5
| if (getCause() != null) {
|
194 |
5
| if (getCause().getCause() != null) {
|
195 |
3
| return getCause().getCause().getMessage();
|
196 |
| } |
197 |
2
| return getCause().getMessage();
|
198 |
| } |
199 |
0
| return "";
|
200 |
| } |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
5
| public final String getDescription() {
|
214 |
5
| final StringBuffer buffer = new StringBuffer();
|
215 |
5
| buffer.append(getErrorCode() + ": " + getMessage());
|
216 |
5
| if (errorArea != null && errorArea.getStartPosition() != null) {
|
217 |
4
| final SourcePosition start = errorArea.getStartPosition();
|
218 |
4
| buffer.append("\n");
|
219 |
4
| buffer.append(start.getLocalAddress() + ":" + start.getLine() + ":"
|
220 |
| + start.getColumn()); |
221 |
4
| buffer.append("\n");
|
222 |
4
| buffer.append(ReplaceUtility.replace(getLine(), "\t", " "));
|
223 |
4
| buffer.append("\n");
|
224 |
4
| final StringBuffer whitespace = IoUtility.getSpaces(start.getColumn() - 1);
|
225 |
4
| buffer.append(whitespace);
|
226 |
4
| buffer.append("^");
|
227 |
| } |
228 |
5
| return buffer.toString();
|
229 |
| } |
230 |
| |
231 |
4
| public final String toString() {
|
232 |
4
| return getDescription();
|
233 |
| } |
234 |
| } |