Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ValidateNames.cs
Go to the documentation of this file.
2
3namespace System.Xml;
4
5internal static class ValidateNames
6{
7 internal enum Flags
8 {
9 NCNames = 1,
12 All = 7,
15 }
16
17 internal static int ParseNmtoken(string s, int offset)
18 {
19 int i;
20 for (i = offset; i < s.Length && XmlCharType.IsNCNameSingleChar(s[i]); i++)
21 {
22 }
23 return i - offset;
24 }
25
26 internal static int ParseNmtokenNoNamespaces(string s, int offset)
27 {
28 int i;
29 for (i = offset; i < s.Length && (XmlCharType.IsNameSingleChar(s[i]) || s[i] == ':'); i++)
30 {
31 }
32 return i - offset;
33 }
34
35 internal static bool IsNmtokenNoNamespaces(string s)
36 {
37 int num = ParseNmtokenNoNamespaces(s, 0);
38 if (num > 0)
39 {
40 return num == s.Length;
41 }
42 return false;
43 }
44
45 internal static int ParseNameNoNamespaces(string s, int offset)
46 {
47 int i = offset;
48 if (i < s.Length)
49 {
50 if (!XmlCharType.IsStartNCNameSingleChar(s[i]) && s[i] != ':')
51 {
52 return 0;
53 }
54 for (i++; i < s.Length && (XmlCharType.IsNCNameSingleChar(s[i]) || s[i] == ':'); i++)
55 {
56 }
57 }
58 return i - offset;
59 }
60
61 internal static bool IsNameNoNamespaces(string s)
62 {
63 int num = ParseNameNoNamespaces(s, 0);
64 if (num > 0)
65 {
66 return num == s.Length;
67 }
68 return false;
69 }
70
71 internal static int ParseNCName(string s, int offset)
72 {
73 int i = offset;
74 if (i < s.Length)
75 {
77 {
78 return 0;
79 }
80 for (i++; i < s.Length && XmlCharType.IsNCNameSingleChar(s[i]); i++)
81 {
82 }
83 }
84 return i - offset;
85 }
86
87 internal static int ParseNCName(string s)
88 {
89 return ParseNCName(s, 0);
90 }
91
92 internal static string ParseNCNameThrow(string s)
93 {
95 return s;
96 }
97
98 private static bool ParseNCNameInternal(string s, bool throwOnError)
99 {
100 int num = ParseNCName(s, 0);
101 if (num == 0 || num != s.Length)
102 {
103 if (throwOnError)
104 {
105 ThrowInvalidName(s, 0, num);
106 }
107 return false;
108 }
109 return true;
110 }
111
112 internal static int ParseQName(string s, int offset, out int colonOffset)
113 {
114 colonOffset = 0;
115 int num = ParseNCName(s, offset);
116 if (num != 0)
117 {
118 offset += num;
119 if (offset < s.Length && s[offset] == ':')
120 {
121 int num2 = ParseNCName(s, offset + 1);
122 if (num2 != 0)
123 {
125 num += num2 + 1;
126 }
127 }
128 }
129 return num;
130 }
131
132 internal static int ParseQNameThrow(string s)
133 {
134 int colonOffset;
135 int num = ParseQName(s, 0, out colonOffset);
136 if (num == 0 || num != s.Length)
137 {
138 ThrowInvalidName(s, 0, num);
139 }
140 return colonOffset;
141 }
142
143 internal static void ParseQNameThrow(string s, out string prefix, out string localName)
144 {
145 int num = ParseQNameThrow(s);
146 if (num != 0)
147 {
148 prefix = s.Substring(0, num);
149 localName = s.Substring(num + 1);
150 }
151 else
152 {
153 prefix = "";
154 localName = s;
155 }
156 }
157
158 internal static void ParseNameTestThrow(string s, out string prefix, out string localName)
159 {
160 int num;
161 if (s.Length != 0 && s[0] == '*')
162 {
163 prefix = (localName = null);
164 num = 1;
165 }
166 else
167 {
168 num = ParseNCName(s, 0);
169 if (num != 0)
170 {
171 localName = s.Substring(0, num);
172 if (num < s.Length && s[num] == ':')
173 {
174 prefix = localName;
175 int num2 = num + 1;
176 if (num2 < s.Length && s[num2] == '*')
177 {
178 localName = null;
179 num += 2;
180 }
181 else
182 {
183 int num3 = ParseNCName(s, num2);
184 if (num3 != 0)
185 {
186 localName = s.Substring(num2, num3);
187 num += num3 + 1;
188 }
189 }
190 }
191 else
192 {
193 prefix = string.Empty;
194 }
195 }
196 else
197 {
198 prefix = (localName = null);
199 }
200 }
201 if (num == 0 || num != s.Length)
202 {
203 ThrowInvalidName(s, 0, num);
204 }
205 }
206
219
232
233 internal static bool StartsWithXml(string s)
234 {
235 if (s.Length < 3)
236 {
237 return false;
238 }
239 if (s[0] != 'x' && s[0] != 'X')
240 {
241 return false;
242 }
243 if (s[1] != 'm' && s[1] != 'M')
244 {
245 return false;
246 }
247 if (s[2] != 'l' && s[2] != 'L')
248 {
249 return false;
250 }
251 return true;
252 }
253
254 internal static bool IsReservedNamespace(string s)
255 {
256 if (!s.Equals("http://www.w3.org/XML/1998/namespace"))
257 {
258 return s.Equals("http://www.w3.org/2000/xmlns/");
259 }
260 return true;
261 }
262
263 internal static void ValidateNameThrow(string prefix, string localName, string ns, XPathNodeType nodeKind, Flags flags)
264 {
265 ValidateNameInternal(prefix, localName, ns, nodeKind, flags, throwOnError: true);
266 }
267
268 internal static bool ValidateName(string prefix, string localName, string ns, XPathNodeType nodeKind, Flags flags)
269 {
270 return ValidateNameInternal(prefix, localName, ns, nodeKind, flags, throwOnError: false);
271 }
272
273 private static bool ValidateNameInternal(string prefix, string localName, string ns, XPathNodeType nodeKind, Flags flags, bool throwOnError)
274 {
275 if ((flags & Flags.NCNames) != 0)
276 {
277 if (prefix.Length != 0 && !ParseNCNameInternal(prefix, throwOnError))
278 {
279 return false;
280 }
281 if (localName.Length != 0 && !ParseNCNameInternal(localName, throwOnError))
282 {
283 return false;
284 }
285 }
286 if ((flags & Flags.CheckLocalName) != 0)
287 {
288 if (nodeKind != XPathNodeType.Element)
289 {
290 if (nodeKind != XPathNodeType.Attribute)
291 {
292 if (nodeKind == XPathNodeType.ProcessingInstruction)
293 {
294 if (localName.Length == 0 || (localName.Length == 3 && StartsWithXml(localName)))
295 {
296 if (throwOnError)
297 {
298 throw new XmlException(System.SR.Xml_InvalidPIName, localName);
299 }
300 return false;
301 }
302 }
303 else if (localName.Length != 0)
304 {
305 if (throwOnError)
306 {
307 throw new XmlException(System.SR.XmlNoNameAllowed, nodeKind.ToString());
308 }
309 return false;
310 }
311 goto IL_00fa;
312 }
313 if (ns.Length == 0 && localName.Equals("xmlns"))
314 {
315 if (throwOnError)
316 {
317 throw new XmlException(System.SR.XmlBadName, new string[2]
318 {
319 nodeKind.ToString(),
320 localName
321 });
322 }
323 return false;
324 }
325 }
326 if (localName.Length == 0)
327 {
328 if (throwOnError)
329 {
330 throw new XmlException(System.SR.Xdom_Empty_LocalName, string.Empty);
331 }
332 return false;
333 }
334 }
335 goto IL_00fa;
336 IL_00fa:
337 if ((flags & Flags.CheckPrefixMapping) != 0)
338 {
339 switch (nodeKind)
340 {
341 case XPathNodeType.Element:
342 case XPathNodeType.Attribute:
343 case XPathNodeType.Namespace:
344 if (ns.Length == 0)
345 {
346 if (prefix.Length != 0)
347 {
348 if (throwOnError)
349 {
350 throw new XmlException(System.SR.Xml_PrefixForEmptyNs, string.Empty);
351 }
352 return false;
353 }
354 break;
355 }
356 if (prefix.Length == 0 && nodeKind == XPathNodeType.Attribute)
357 {
358 if (throwOnError)
359 {
360 throw new XmlException(System.SR.XmlBadName, new string[2]
361 {
362 nodeKind.ToString(),
363 localName
364 });
365 }
366 return false;
367 }
368 if (prefix.Equals("xml"))
369 {
370 if (!ns.Equals("http://www.w3.org/XML/1998/namespace"))
371 {
372 if (throwOnError)
373 {
374 throw new XmlException(System.SR.Xml_XmlPrefix, string.Empty);
375 }
376 return false;
377 }
378 break;
379 }
380 if (prefix.Equals("xmlns"))
381 {
382 if (throwOnError)
383 {
384 throw new XmlException(System.SR.Xml_XmlnsPrefix, string.Empty);
385 }
386 return false;
387 }
388 if (IsReservedNamespace(ns))
389 {
390 if (throwOnError)
391 {
392 throw new XmlException(System.SR.Xml_NamespaceDeclXmlXmlns, string.Empty);
393 }
394 return false;
395 }
396 break;
397 case XPathNodeType.ProcessingInstruction:
398 if (prefix.Length != 0 || ns.Length != 0)
399 {
400 if (throwOnError)
401 {
403 }
404 return false;
405 }
406 break;
407 default:
408 if (prefix.Length != 0 || ns.Length != 0)
409 {
410 if (throwOnError)
411 {
412 throw new XmlException(System.SR.XmlNoNameAllowed, nodeKind.ToString());
413 }
414 return false;
415 }
416 break;
417 }
418 }
419 return true;
420 }
421
422 private static string CreateName(string prefix, string localName)
423 {
424 if (prefix.Length == 0)
425 {
426 return localName;
427 }
428 return prefix + ":" + localName;
429 }
430
431 internal static void SplitQName(string name, out string prefix, out string lname)
432 {
433 int num = name.IndexOf(':');
434 if (-1 == num)
435 {
436 prefix = string.Empty;
437 lname = name;
438 return;
439 }
440 if (num == 0 || name.Length - 1 == num)
441 {
443 object[] args = XmlException.BuildCharExceptionArgs(':', '\0');
445 }
446 prefix = name.Substring(0, num);
447 num++;
448 lname = name.Substring(num, name.Length - num);
449 }
450}
static string Xml_PrefixForEmptyNs
Definition SR.cs:286
static string Xml_EmptyName
Definition SR.cs:292
static string Xml_BadStartNameChar
Definition SR.cs:42
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string XmlBadName
Definition SR.cs:364
static string Xml_InvalidPIName
Definition SR.cs:90
static string Xml_XmlPrefix
Definition SR.cs:304
static string Xml_NamespaceDeclXmlXmlns
Definition SR.cs:308
static string Xml_BadNameChar
Definition SR.cs:44
static string XmlNoNameAllowed
Definition SR.cs:366
static string Xdom_Empty_LocalName
Definition SR.cs:1300
static string Xml_XmlnsPrefix
Definition SR.cs:306
Definition SR.cs:7
static int ParseNCName(string s)
static bool IsNameNoNamespaces(string s)
static int ParseNameNoNamespaces(string s, int offset)
static void ThrowInvalidName(string s, int offsetStartChar, int offsetBadChar)
static Exception GetInvalidNameException(string s, int offsetStartChar, int offsetBadChar)
static void ParseQNameThrow(string s, out string prefix, out string localName)
static int ParseQName(string s, int offset, out int colonOffset)
static string ParseNCNameThrow(string s)
static int ParseNmtoken(string s, int offset)
static bool ValidateNameInternal(string prefix, string localName, string ns, XPathNodeType nodeKind, Flags flags, bool throwOnError)
static bool ValidateName(string prefix, string localName, string ns, XPathNodeType nodeKind, Flags flags)
static void ValidateNameThrow(string prefix, string localName, string ns, XPathNodeType nodeKind, Flags flags)
static int ParseNmtokenNoNamespaces(string s, int offset)
static int ParseQNameThrow(string s)
static bool IsNmtokenNoNamespaces(string s)
static int ParseNCName(string s, int offset)
static bool ParseNCNameInternal(string s, bool throwOnError)
static void ParseNameTestThrow(string s, out string prefix, out string localName)
static bool IsReservedNamespace(string s)
static bool StartsWithXml(string s)
static string CreateName(string prefix, string localName)
static void SplitQName(string name, out string prefix, out string lname)
static bool IsStartNCNameSingleChar(char ch)
static bool IsNCNameSingleChar(char ch)
static bool IsNameSingleChar(char ch)
static string[] BuildCharExceptionArgs(string data, int invCharIndex)