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