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