1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.htmlunit.javascript.HtmlUnitScriptable;
24 import org.htmlunit.javascript.JavaScriptEngine;
25 import org.htmlunit.javascript.configuration.JsxClass;
26 import org.htmlunit.javascript.configuration.JsxConstant;
27 import org.htmlunit.javascript.configuration.JsxConstructor;
28 import org.htmlunit.javascript.configuration.JsxGetter;
29
30
31
32
33
34
35
36
37
38
39
40
41 @JsxClass
42 public class DOMException extends HtmlUnitScriptable {
43
44
45 @JsxConstant
46 public static final int DOMSTRING_SIZE_ERR = org.w3c.dom.DOMException.DOMSTRING_SIZE_ERR;
47
48
49 @JsxConstant
50 public static final int HIERARCHY_REQUEST_ERR = org.w3c.dom.DOMException.HIERARCHY_REQUEST_ERR;
51
52
53 @JsxConstant
54 public static final int INDEX_SIZE_ERR = org.w3c.dom.DOMException.INDEX_SIZE_ERR;
55
56
57 @JsxConstant
58 public static final int INUSE_ATTRIBUTE_ERR = org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR;
59
60
61 @JsxConstant
62 public static final int INVALID_ACCESS_ERR = org.w3c.dom.DOMException.INVALID_ACCESS_ERR;
63
64
65 @JsxConstant
66 public static final int INVALID_CHARACTER_ERR = org.w3c.dom.DOMException.INVALID_CHARACTER_ERR;
67
68
69 @JsxConstant
70 public static final int INVALID_MODIFICATION_ERR = org.w3c.dom.DOMException.INVALID_MODIFICATION_ERR;
71
72
73 @JsxConstant
74 public static final int INVALID_STATE_ERR = org.w3c.dom.DOMException.INVALID_STATE_ERR;
75
76
77 @JsxConstant
78 public static final int NAMESPACE_ERR = org.w3c.dom.DOMException.NAMESPACE_ERR;
79
80
81 @JsxConstant
82 public static final int NO_DATA_ALLOWED_ERR = org.w3c.dom.DOMException.NO_DATA_ALLOWED_ERR;
83
84
85 @JsxConstant
86 public static final int NO_MODIFICATION_ALLOWED_ERR = org.w3c.dom.DOMException.NO_MODIFICATION_ALLOWED_ERR;
87
88
89 @JsxConstant
90 public static final int NOT_FOUND_ERR = org.w3c.dom.DOMException.NOT_FOUND_ERR;
91
92
93 @JsxConstant
94 public static final int NOT_SUPPORTED_ERR = org.w3c.dom.DOMException.NOT_SUPPORTED_ERR;
95
96
97 @JsxConstant
98 public static final int SYNTAX_ERR = org.w3c.dom.DOMException.SYNTAX_ERR;
99
100
101 @JsxConstant
102 public static final int WRONG_DOCUMENT_ERR = org.w3c.dom.DOMException.WRONG_DOCUMENT_ERR;
103
104
105 @JsxConstant
106 public static final int VALIDATION_ERR = org.w3c.dom.DOMException.VALIDATION_ERR;
107
108
109 @JsxConstant
110 public static final int TYPE_MISMATCH_ERR = org.w3c.dom.DOMException.TYPE_MISMATCH_ERR;
111
112
113 @JsxConstant
114 public static final int SECURITY_ERR = 18;
115
116
117 @JsxConstant
118 public static final int NETWORK_ERR = 19;
119
120
121 @JsxConstant
122 public static final int ABORT_ERR = 20;
123
124
125 @JsxConstant
126 public static final int URL_MISMATCH_ERR = 21;
127
128
129 @JsxConstant
130 public static final int QUOTA_EXCEEDED_ERR = 22;
131
132
133 @JsxConstant
134 public static final int TIMEOUT_ERR = 23;
135
136
137 @JsxConstant
138 public static final int INVALID_NODE_TYPE_ERR = 24;
139
140
141 @JsxConstant
142 public static final int DATA_CLONE_ERR = 25;
143
144
145
146
147
148 public static final int ENCODING_ERR = 26;
149
150
151 public static final int NOT_READABLE_ERR = 27;
152
153
154 public static final int UNKNOWN_ERR = 28;
155
156
157 public static final int CONSTRAINT_ERR = 29;
158
159
160 public static final int DATA_ERR = 30;
161
162
163 public static final int TRANSACTION_INACTIVE_ERR = 31;
164
165
166 public static final int READ_ONLY_ERR = 32;
167
168
169 public static final int VERSION_ERR = 33;
170
171
172 public static final int OPERATION_ERR = 34;
173
174
175 public static final int NOT_ALLOWED_ERR = 35;
176
177
178 public static final int OPT_OUT_ERR = 36;
179
180 private static final List<String> COMMON_ERROR_NAMES = Arrays.asList(
181 "IndexSizeError",
182 "DOMStringSizeError",
183 "HierarchyRequestError",
184 "WrongDocumentError",
185 "InvalidCharacterError",
186 "NoDataAllowedError",
187 "NoModificationAllowedError",
188 "NotFoundError",
189 "NotSupportedError",
190 "InUseAttributeError",
191 "InvalidStateError",
192 "SyntaxError",
193 "InvalidModificationError",
194 "NamespaceError",
195 "InvalidAccessError",
196 "ValidationError",
197 "TypeMismatchError",
198 "SecurityError",
199 "NetworkError",
200 "AbortError",
201 "URLMismatchError",
202 "QuotaExceededError",
203 "TimeoutError",
204 "InvalidNodeTypeError",
205 "DataCloneError",
206 "EncodingError",
207 "NotReadableError",
208 "UnknownError",
209 "ConstraintError",
210 "DataError",
211 "TransactionInactiveError",
212 "ReadOnlyError",
213 "VersionError",
214 "OperationError",
215 "NotAllowedError",
216 "OptOutError");
217
218 private int code_;
219 private String name_;
220 private String message_;
221 private int lineNumber_;
222 private String fileName_;
223
224
225
226
227 public DOMException() {
228 super();
229 }
230
231
232
233
234
235
236 public DOMException(final String message, final int error) {
237 message_ = message;
238 code_ = toLegacyCode(error);
239 name_ = COMMON_ERROR_NAMES.get(error - 1);
240 }
241
242
243
244
245
246
247
248
249
250 @JsxConstructor
251 public void jsConstructor(final String message, final Object error) {
252 message_ = message;
253
254 if (error == null) {
255 code_ = 0;
256 name_ = null;
257 return;
258 }
259
260 if (JavaScriptEngine.isUndefined(error)) {
261 code_ = 0;
262 name_ = "Error";
263 return;
264 }
265
266 name_ = JavaScriptEngine.toString(error);
267
268 final int idx = COMMON_ERROR_NAMES.indexOf(name_);
269 if (idx != -1) {
270 code_ = toLegacyCode(idx + 1);
271 return;
272 }
273
274 code_ = 0;
275 }
276
277 private static int toLegacyCode(final int error) {
278 return error > DATA_CLONE_ERR ? 0 : error;
279 }
280
281
282
283
284
285 @JsxGetter
286 public Object getCode() {
287 if (code_ == -1) {
288 return JavaScriptEngine.UNDEFINED;
289 }
290 return code_;
291 }
292
293
294
295
296
297 @JsxGetter
298 public String getName() {
299 return name_;
300 }
301
302
303
304
305
306 @JsxGetter
307 public Object getMessage() {
308 if (message_ == null) {
309 return JavaScriptEngine.UNDEFINED;
310 }
311 return message_;
312 }
313
314
315
316
317
318 @JsxGetter({FF, FF_ESR})
319 public Object getLineNumber() {
320 if (lineNumber_ == -1) {
321 return JavaScriptEngine.UNDEFINED;
322 }
323 return lineNumber_;
324 }
325
326
327
328
329
330 @JsxGetter({FF, FF_ESR})
331 public Object getFilename() {
332 if (fileName_ == null) {
333 return JavaScriptEngine.UNDEFINED;
334 }
335 return fileName_;
336 }
337
338
339
340
341
342
343 public void setLocation(final String fileName, final int lineNumber) {
344 fileName_ = fileName;
345 lineNumber_ = lineNumber;
346 }
347 }