/
XmlException.cs
296 lines (255 loc) · 11 KB
/
XmlException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Resources;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Runtime.Serialization;
namespace System.Xml
{
/// <devdoc>
/// <para>Returns detailed information about the last parse error, including the error
/// number, line number, character position, and a text description.</para>
/// </devdoc>
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class XmlException : SystemException
{
private readonly string _res;
private readonly string?[]? _args; // this field is not used, it's here just V1.1 serialization compatibility
private readonly int _lineNumber;
private readonly int _linePosition;
private readonly string? _sourceUri;
// message != null for V1 exceptions deserialized in Whidbey
// message == null for V2 or higher exceptions; the exception message is stored on the base class (Exception._message)
private readonly string? _message;
protected XmlException(SerializationInfo info, StreamingContext context) : base(info, context)
{
_res = (string)info.GetValue("res", typeof(string))!;
_args = (string?[])info.GetValue("args", typeof(string[]))!;
_lineNumber = (int)info.GetValue("lineNumber", typeof(int))!;
_linePosition = (int)info.GetValue("linePosition", typeof(int))!;
// deserialize optional members
_sourceUri = string.Empty;
string? version = null;
foreach (SerializationEntry e in info)
{
switch (e.Name)
{
case "sourceUri":
_sourceUri = (string?)e.Value;
break;
case "version":
version = (string?)e.Value;
break;
}
}
if (version == null)
{
// deserializing V1 exception
_message = CreateMessage(_res, _args, _lineNumber, _linePosition);
}
else
{
// deserializing V2 or higher exception -> exception message is serialized by the base class (Exception._message)
_message = null;
}
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("res", _res);
info.AddValue("args", _args);
info.AddValue("lineNumber", _lineNumber);
info.AddValue("linePosition", _linePosition);
info.AddValue("sourceUri", _sourceUri);
info.AddValue("version", "2.0");
}
//provided to meet the ECMA standards
public XmlException() : this(null)
{
}
//provided to meet the ECMA standards
public XmlException(string? message) : this(message, ((Exception?)null), 0, 0)
{
Debug.Assert(message == null || !message.StartsWith("Xml_", StringComparison.Ordinal), "Do not pass a resource here!");
}
//provided to meet ECMA standards
public XmlException(string? message, Exception? innerException) : this(message, innerException, 0, 0)
{
}
//provided to meet ECMA standards
public XmlException(string? message, Exception? innerException, int lineNumber, int linePosition) :
this(message, innerException, lineNumber, linePosition, null)
{
}
internal XmlException(string? message, Exception? innerException, int lineNumber, int linePosition, string? sourceUri) :
base(FormatUserMessage(message, lineNumber, linePosition), innerException)
{
HResult = HResults.Xml;
_res = (message == null ? SR.Xml_DefaultException : SR.Xml_UserException);
_args = new string?[] { message };
_sourceUri = sourceUri;
_lineNumber = lineNumber;
_linePosition = linePosition;
}
internal XmlException(string res, string?[]? args) :
this(res, args, null, 0, 0, null)
{ }
internal XmlException(string res, string? arg) :
this(res, new string?[] { arg }, null, 0, 0, null)
{ }
internal XmlException(string res, string? arg, string? sourceUri) :
this(res, new string?[] { arg }, null, 0, 0, sourceUri)
{ }
internal XmlException(string res, string? arg, IXmlLineInfo? lineInfo) :
this(res, new string?[] { arg }, lineInfo, null)
{ }
internal XmlException(string res, string? arg, Exception? innerException, IXmlLineInfo? lineInfo) :
this(res, new string?[] { arg }, innerException, (lineInfo == null ? 0 : lineInfo.LineNumber), (lineInfo == null ? 0 : lineInfo.LinePosition), null)
{ }
internal XmlException(string res, string?[]? args, IXmlLineInfo? lineInfo) :
this(res, args, lineInfo, null)
{ }
internal XmlException(string res, string?[]? args, IXmlLineInfo? lineInfo, string? sourceUri) :
this(res, args, null, (lineInfo == null ? 0 : lineInfo.LineNumber), (lineInfo == null ? 0 : lineInfo.LinePosition), sourceUri)
{
}
internal XmlException(string res, string? arg, int lineNumber, int linePosition) :
this(res, new string?[] { arg }, null, lineNumber, linePosition, null)
{ }
internal XmlException(string res, string? arg, int lineNumber, int linePosition, string? sourceUri) :
this(res, new string?[] { arg }, null, lineNumber, linePosition, sourceUri)
{ }
internal XmlException(string res, string?[]? args, int lineNumber, int linePosition) :
this(res, args, null, lineNumber, linePosition, null)
{ }
internal XmlException(string res, string?[]? args, int lineNumber, int linePosition, string? sourceUri) :
this(res, args, null, lineNumber, linePosition, sourceUri)
{ }
internal XmlException(string res, string?[]? args, Exception? innerException, int lineNumber, int linePosition) :
this(res, args, innerException, lineNumber, linePosition, null)
{ }
internal XmlException(string res, string?[]? args, Exception? innerException, int lineNumber, int linePosition, string? sourceUri) :
base(CreateMessage(res, args, lineNumber, linePosition), innerException)
{
HResult = HResults.Xml;
_res = res;
_args = args;
_sourceUri = sourceUri;
_lineNumber = lineNumber;
_linePosition = linePosition;
}
private static string FormatUserMessage(string? message, int lineNumber, int linePosition)
{
if (message == null)
{
return CreateMessage(SR.Xml_DefaultException, null, lineNumber, linePosition);
}
else
{
if (lineNumber == 0 && linePosition == 0)
{
// do not reformat the message when not needed
return message;
}
else
{
// add line information
return CreateMessage(SR.Xml_UserException, new string[] { message }, lineNumber, linePosition);
}
}
}
private static string CreateMessage(string res, string?[]? args, int lineNumber, int linePosition)
{
try
{
string message = (args == null) ? res : string.Format(res, args);
// Line information is available -> we need to append it to the error message
if (lineNumber != 0)
{
string lineNumberStr = lineNumber.ToString(CultureInfo.InvariantCulture);
string linePositionStr = linePosition.ToString(CultureInfo.InvariantCulture);
message = SR.Format(SR.Xml_MessageWithErrorPosition, new string[] { message, lineNumberStr, linePositionStr });
}
return message;
}
catch (MissingManifestResourceException)
{
return $"UNKNOWN({res})";
}
}
internal static string[] BuildCharExceptionArgs(string data, int invCharIndex)
{
return BuildCharExceptionArgs(data[invCharIndex], invCharIndex + 1 < data.Length ? data[invCharIndex + 1] : '\0');
}
internal static string[] BuildCharExceptionArgs(char[] data, int length, int invCharIndex)
{
Debug.Assert(invCharIndex < data.Length);
Debug.Assert(invCharIndex < length);
Debug.Assert(length <= data.Length);
return BuildCharExceptionArgs(data[invCharIndex], invCharIndex + 1 < length ? data[invCharIndex + 1] : '\0');
}
internal static string[] BuildCharExceptionArgs(char invChar, char nextChar)
{
string[] aStringList = new string[2];
// for surrogate characters include both high and low char in the message so that a full character is displayed
if (XmlCharType.IsHighSurrogate(invChar) && nextChar != 0)
{
int combinedChar = XmlCharType.CombineSurrogateChar(nextChar, invChar);
aStringList[0] = new string(stackalloc char[] { invChar, nextChar });
aStringList[1] = $"0x{combinedChar:X2}";
}
else
{
// don't include 0 character in the string - in means eof-of-string in native code, where this may bubble up to
if ((int)invChar == 0)
{
aStringList[0] = ".";
}
else
{
aStringList[0] = invChar.ToString();
}
aStringList[1] = $"0x{(int)invChar:X2}";
}
return aStringList;
}
public int LineNumber
{
get { return _lineNumber; }
}
public int LinePosition
{
get { return _linePosition; }
}
public string? SourceUri
{
get { return _sourceUri; }
}
public override string Message
{
get
{
return _message ?? base.Message;
}
}
internal string ResString
{
get
{
return _res;
}
}
internal static bool IsCatchableException(Exception e)
{
Debug.Assert(e != null, "Unexpected null exception");
return !(
e is OutOfMemoryException ||
e is NullReferenceException
);
}
};
}