Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlCharCheckingWriter.cs
Go to the documentation of this file.
2using System.Text;
4
5namespace System.Xml;
6
8{
9 private readonly bool _checkValues;
10
11 private readonly bool _checkNames;
12
13 private readonly bool _replaceNewLines;
14
15 private readonly string _newLineChars;
16
18 {
19 get
20 {
22 settings = ((settings != null) ? settings.Clone() : new XmlWriterSettings());
23 if (_checkValues)
24 {
26 }
28 {
31 }
32 settings.ReadOnly = true;
33 return settings;
34 }
35 }
36
45
46 public override void WriteDocType(string name, string pubid, string sysid, string subset)
47 {
48 if (_checkNames)
49 {
50 ValidateQName(name);
51 }
52 if (_checkValues)
53 {
54 int invCharPos;
55 if (pubid != null && (invCharPos = XmlCharType.IsPublicId(pubid)) >= 0)
56 {
58 }
59 if (sysid != null)
60 {
62 }
63 if (subset != null)
64 {
66 }
67 }
69 {
73 }
75 }
76
77 public override void WriteStartElement(string prefix, string localName, string ns)
78 {
79 if (_checkNames)
80 {
81 if (localName == null || localName.Length == 0)
82 {
84 }
85 ValidateNCName(localName);
86 if (prefix != null && prefix.Length > 0)
87 {
89 }
90 }
91 writer.WriteStartElement(prefix, localName, ns);
92 }
93
94 public override void WriteStartAttribute(string prefix, string localName, string ns)
95 {
96 if (_checkNames)
97 {
98 if (localName == null || localName.Length == 0)
99 {
101 }
102 ValidateNCName(localName);
103 if (prefix != null && prefix.Length > 0)
104 {
106 }
107 }
108 writer.WriteStartAttribute(prefix, localName, ns);
109 }
110
111 public override void WriteCData(string text)
112 {
113 if (text != null)
114 {
115 if (_checkValues)
116 {
118 }
120 {
122 }
123 int num;
124 while ((num = text.IndexOf("]]>", StringComparison.Ordinal)) >= 0)
125 {
126 writer.WriteCData(text.Substring(0, num + 2));
127 text = text.Substring(num + 2);
128 }
129 }
131 }
132
133 public override void WriteComment(string text)
134 {
135 if (text != null)
136 {
137 if (_checkValues)
138 {
140 text = InterleaveInvalidChars(text, '-', '-');
141 }
143 {
145 }
146 }
148 }
149
150 public override void WriteProcessingInstruction(string name, string text)
151 {
152 if (_checkNames)
153 {
154 ValidateNCName(name);
155 }
156 if (text != null)
157 {
158 if (_checkValues)
159 {
161 text = InterleaveInvalidChars(text, '?', '>');
162 }
164 {
166 }
167 }
169 }
170
171 public override void WriteEntityRef(string name)
172 {
173 if (_checkNames)
174 {
175 ValidateQName(name);
176 }
178 }
179
180 public override void WriteWhitespace(string ws)
181 {
182 if (ws == null)
183 {
184 ws = string.Empty;
185 }
186 int invCharIndex;
188 {
192 }
194 {
196 }
198 }
199
200 public override void WriteString(string text)
201 {
202 if (text != null)
203 {
204 if (_checkValues)
205 {
207 }
208 if (_replaceNewLines && WriteState != WriteState.Attribute)
209 {
211 }
212 }
214 }
215
216 public override void WriteSurrogateCharEntity(char lowChar, char highChar)
217 {
219 }
220
221 public override void WriteChars(char[] buffer, int index, int count)
222 {
223 if (buffer == null)
224 {
225 throw new ArgumentNullException("buffer");
226 }
227 if (index < 0)
228 {
229 throw new ArgumentOutOfRangeException("index");
230 }
231 if (count < 0)
232 {
233 throw new ArgumentOutOfRangeException("count");
234 }
235 if (count > buffer.Length - index)
236 {
237 throw new ArgumentOutOfRangeException("count");
238 }
239 if (_checkValues)
240 {
242 }
243 if (_replaceNewLines && WriteState != WriteState.Attribute)
244 {
246 if (text != null)
247 {
249 return;
250 }
251 }
253 }
254
255 public override void WriteNmToken(string name)
256 {
257 if (_checkNames)
258 {
259 if (name == null || name.Length == 0)
260 {
262 }
264 }
265 writer.WriteNmToken(name);
266 }
267
268 public override void WriteName(string name)
269 {
270 if (_checkNames)
271 {
272 XmlConvert.VerifyQName(name, ExceptionType.XmlException);
273 }
274 writer.WriteName(name);
275 }
276
277 public override void WriteQualifiedName(string localName, string ns)
278 {
279 if (_checkNames)
280 {
281 ValidateNCName(localName);
282 }
283 writer.WriteQualifiedName(localName, ns);
284 }
285
286 private void CheckCharacters(string str)
287 {
288 XmlConvert.VerifyCharData(str, ExceptionType.ArgumentException);
289 }
290
291 private void CheckCharacters(char[] data, int offset, int len)
292 {
293 XmlConvert.VerifyCharData(data, offset, len, ExceptionType.ArgumentException);
294 }
295
296 private void ValidateNCName(string ncname)
297 {
298 if (ncname.Length == 0)
299 {
301 }
302 int num = ValidateNames.ParseNCName(ncname, 0);
303 if (num != ncname.Length)
304 {
308 }
309 }
310
311 private void ValidateQName(string name)
312 {
313 if (name.Length == 0)
314 {
316 }
317 int colonOffset;
318 int num = ValidateNames.ParseQName(name, 0, out colonOffset);
319 if (num != name.Length)
320 {
321 string format = ((num == 0 || (colonOffset > -1 && num == colonOffset + 1)) ? System.SR.Xml_BadStartNameChar : System.SR.Xml_BadNameChar);
322 object[] args = XmlException.BuildCharExceptionArgs(name, num);
323 throw new ArgumentException(string.Format(format, args));
324 }
325 }
326
327 [return: NotNullIfNotNull("str")]
328 private string ReplaceNewLines(string str)
329 {
330 if (str == null)
331 {
332 return null;
333 }
335 int num = 0;
336 int i;
337 for (i = 0; i < str.Length; i++)
338 {
339 char c;
340 if ((c = str[i]) >= ' ')
341 {
342 continue;
343 }
344 if (c == '\n')
345 {
346 if (_newLineChars == "\n")
347 {
348 continue;
349 }
350 if (stringBuilder == null)
351 {
352 stringBuilder = new StringBuilder(str.Length + 5);
353 }
354 stringBuilder.Append(str, num, i - num);
355 }
356 else
357 {
358 if (c != '\r')
359 {
360 continue;
361 }
362 if (i + 1 < str.Length && str[i + 1] == '\n')
363 {
364 if (_newLineChars == "\r\n")
365 {
366 i++;
367 continue;
368 }
369 if (stringBuilder == null)
370 {
371 stringBuilder = new StringBuilder(str.Length + 5);
372 }
373 stringBuilder.Append(str, num, i - num);
374 i++;
375 }
376 else
377 {
378 if (_newLineChars == "\r")
379 {
380 continue;
381 }
382 if (stringBuilder == null)
383 {
384 stringBuilder = new StringBuilder(str.Length + 5);
385 }
386 stringBuilder.Append(str, num, i - num);
387 }
388 }
390 num = i + 1;
391 }
392 if (stringBuilder == null)
393 {
394 return str;
395 }
396 stringBuilder.Append(str, num, i - num);
397 return stringBuilder.ToString();
398 }
399
400 private string ReplaceNewLines(char[] data, int offset, int len)
401 {
402 if (data == null)
403 {
404 return null;
405 }
407 int num = offset;
408 int num2 = offset + len;
409 int i;
410 for (i = offset; i < num2; i++)
411 {
412 char c;
413 if ((c = data[i]) >= ' ')
414 {
415 continue;
416 }
417 if (c == '\n')
418 {
419 if (_newLineChars == "\n")
420 {
421 continue;
422 }
423 if (stringBuilder == null)
424 {
426 }
427 stringBuilder.Append(data, num, i - num);
428 }
429 else
430 {
431 if (c != '\r')
432 {
433 continue;
434 }
435 if (i + 1 < num2 && data[i + 1] == '\n')
436 {
437 if (_newLineChars == "\r\n")
438 {
439 i++;
440 continue;
441 }
442 if (stringBuilder == null)
443 {
445 }
446 stringBuilder.Append(data, num, i - num);
447 i++;
448 }
449 else
450 {
451 if (_newLineChars == "\r")
452 {
453 continue;
454 }
455 if (stringBuilder == null)
456 {
458 }
459 stringBuilder.Append(data, num, i - num);
460 }
461 }
463 num = i + 1;
464 }
465 if (stringBuilder == null)
466 {
467 return null;
468 }
469 stringBuilder.Append(data, num, i - num);
470 return stringBuilder.ToString();
471 }
472
473 private string InterleaveInvalidChars(string text, char invChar1, char invChar2)
474 {
476 int num = 0;
477 int i;
478 for (i = 0; i < text.Length; i++)
479 {
480 if (text[i] == invChar2 && i > 0 && text[i - 1] == invChar1)
481 {
482 if (stringBuilder == null)
483 {
484 stringBuilder = new StringBuilder(text.Length + 5);
485 }
486 stringBuilder.Append(text, num, i - num);
487 stringBuilder.Append(' ');
488 num = i;
489 }
490 }
491 if (stringBuilder == null)
492 {
493 if (i != 0 && text[i - 1] == invChar1)
494 {
495 return text + " ";
496 }
497 return text;
498 }
499 stringBuilder.Append(text, num, i - num);
500 if (i > 0 && text[i - 1] == invChar1)
501 {
502 stringBuilder.Append(' ');
503 }
504 return stringBuilder.ToString();
505 }
506
507 public override Task WriteDocTypeAsync(string name, string pubid, string sysid, string subset)
508 {
509 if (_checkNames)
510 {
511 ValidateQName(name);
512 }
513 if (_checkValues)
514 {
515 int invCharPos;
516 if (pubid != null && (invCharPos = XmlCharType.IsPublicId(pubid)) >= 0)
517 {
519 }
520 if (sysid != null)
521 {
523 }
524 if (subset != null)
525 {
527 }
528 }
530 {
534 }
535 return writer.WriteDocTypeAsync(name, pubid, sysid, subset);
536 }
537
538 public override Task WriteStartElementAsync(string prefix, string localName, string ns)
539 {
540 if (_checkNames)
541 {
542 if (localName == null || localName.Length == 0)
543 {
545 }
546 ValidateNCName(localName);
547 if (prefix != null && prefix.Length > 0)
548 {
550 }
551 }
552 return writer.WriteStartElementAsync(prefix, localName, ns);
553 }
554
555 protected internal override Task WriteStartAttributeAsync(string prefix, string localName, string ns)
556 {
557 if (_checkNames)
558 {
559 if (localName == null || localName.Length == 0)
560 {
562 }
563 ValidateNCName(localName);
564 if (prefix != null && prefix.Length > 0)
565 {
567 }
568 }
569 return writer.WriteStartAttributeAsync(prefix, localName, ns);
570 }
571
572 public override async Task WriteCDataAsync(string text)
573 {
574 if (text != null)
575 {
576 if (_checkValues)
577 {
579 }
581 {
583 }
584 while (true)
585 {
586 int num;
587 int i = (num = text.IndexOf("]]>", StringComparison.Ordinal));
588 if (num < 0)
589 {
590 break;
591 }
592 await writer.WriteCDataAsync(text.Substring(0, i + 2)).ConfigureAwait(continueOnCapturedContext: false);
593 text = text.Substring(i + 2);
594 }
595 }
597 }
598
599 public override Task WriteCommentAsync(string text)
600 {
601 if (text != null)
602 {
603 if (_checkValues)
604 {
606 text = InterleaveInvalidChars(text, '-', '-');
607 }
609 {
611 }
612 }
614 }
615
616 public override Task WriteProcessingInstructionAsync(string name, string text)
617 {
618 if (_checkNames)
619 {
620 ValidateNCName(name);
621 }
622 if (text != null)
623 {
624 if (_checkValues)
625 {
627 text = InterleaveInvalidChars(text, '?', '>');
628 }
630 {
632 }
633 }
635 }
636
637 public override Task WriteEntityRefAsync(string name)
638 {
639 if (_checkNames)
640 {
641 ValidateQName(name);
642 }
643 return writer.WriteEntityRefAsync(name);
644 }
645
646 public override Task WriteWhitespaceAsync(string ws)
647 {
648 if (ws == null)
649 {
650 ws = string.Empty;
651 }
652 int invCharIndex;
654 {
658 }
660 {
662 }
664 }
665
666 public override Task WriteStringAsync(string text)
667 {
668 if (text != null)
669 {
670 if (_checkValues)
671 {
673 }
674 if (_replaceNewLines && WriteState != WriteState.Attribute)
675 {
677 }
678 }
680 }
681
686
687 public override Task WriteCharsAsync(char[] buffer, int index, int count)
688 {
689 if (buffer == null)
690 {
691 throw new ArgumentNullException("buffer");
692 }
693 if (index < 0)
694 {
695 throw new ArgumentOutOfRangeException("index");
696 }
697 if (count < 0)
698 {
699 throw new ArgumentOutOfRangeException("count");
700 }
701 if (count > buffer.Length - index)
702 {
703 throw new ArgumentOutOfRangeException("count");
704 }
705 if (_checkValues)
706 {
708 }
709 if (_replaceNewLines && WriteState != WriteState.Attribute)
710 {
712 if (text != null)
713 {
714 return WriteStringAsync(text);
715 }
716 }
718 }
719
720 public override Task WriteNmTokenAsync(string name)
721 {
722 if (_checkNames)
723 {
724 if (name == null || name.Length == 0)
725 {
727 }
729 }
730 return writer.WriteNmTokenAsync(name);
731 }
732
733 public override Task WriteNameAsync(string name)
734 {
735 if (_checkNames)
736 {
737 XmlConvert.VerifyQName(name, ExceptionType.XmlException);
738 }
739 return writer.WriteNameAsync(name);
740 }
741
742 public override Task WriteQualifiedNameAsync(string localName, string ns)
743 {
744 if (_checkNames)
745 {
746 ValidateNCName(localName);
747 }
748 return writer.WriteQualifiedNameAsync(localName, ns);
749 }
750}
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 Xml_EmptyLocalName
Definition SR.cs:294
static string Xml_InvalidWhitespaceCharacter
Definition SR.cs:200
static string Xml_BadNameChar
Definition SR.cs:44
Definition SR.cs:7
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static int ParseQName(string s, int offset, out int colonOffset)
static int ParseNCName(string s, int offset)
override Task WriteStringAsync(string text)
override void WriteProcessingInstruction(string name, string text)
override void WriteChars(char[] buffer, int index, int count)
override Task WriteNmTokenAsync(string name)
override Task WriteNameAsync(string name)
override Task WriteCharsAsync(char[] buffer, int index, int count)
override void WriteComment(string text)
string ReplaceNewLines(char[] data, int offset, int len)
override Task WriteDocTypeAsync(string name, string pubid, string sysid, string subset)
override void WriteName(string name)
override Task WriteQualifiedNameAsync(string localName, string ns)
override Task WriteEntityRefAsync(string name)
override Task WriteCommentAsync(string text)
override void WriteWhitespace(string ws)
override Task WriteStartElementAsync(string prefix, string localName, string ns)
override Task WriteSurrogateCharEntityAsync(char lowChar, char highChar)
void CheckCharacters(char[] data, int offset, int len)
string InterleaveInvalidChars(string text, char invChar1, char invChar2)
override void WriteEntityRef(string name)
override Task WriteProcessingInstructionAsync(string name, string text)
override void WriteStartAttribute(string prefix, string localName, string ns)
override void WriteQualifiedName(string localName, string ns)
override Task WriteStartAttributeAsync(string prefix, string localName, string ns)
override void WriteString(string text)
override async Task WriteCDataAsync(string text)
override void WriteSurrogateCharEntity(char lowChar, char highChar)
override void WriteNmToken(string name)
override void WriteCData(string text)
override void WriteDocType(string name, string pubid, string sysid, string subset)
override Task WriteWhitespaceAsync(string ws)
XmlCharCheckingWriter(XmlWriter baseWriter, bool checkValues, bool checkNames, bool replaceNewLines, string newLineChars)
override void WriteStartElement(string prefix, string localName, string ns)
override XmlWriterSettings Settings
static int IsOnlyWhitespaceWithPos(string str)
static int IsPublicId(string str)
static void VerifyCharData(string data, ExceptionType exceptionType)
static Exception CreateInvalidCharException(char[] data, int length, int invCharPos, ExceptionType exceptionType)
static string VerifyNMTOKEN(string name)
static string VerifyQName(string name, ExceptionType exceptionType)
static string[] BuildCharExceptionArgs(string data, int invCharIndex)
virtual void WriteName(string name)
Definition XmlWriter.cs:127
void WriteCData(string? text)
void WriteComment(string? text)
void WriteProcessingInstruction(string name, string? text)
virtual Task WriteProcessingInstructionAsync(string name, string? text)
Definition XmlWriter.cs:648
virtual void WriteNmToken(string name)
Definition XmlWriter.cs:118
virtual Task WriteSurrogateCharEntityAsync(char lowChar, char highChar)
Definition XmlWriter.cs:673
void WriteSurrogateCharEntity(char lowChar, char highChar)
virtual Task WriteNameAsync(string name)
Definition XmlWriter.cs:717
virtual Task WriteWhitespaceAsync(string? ws)
Definition XmlWriter.cs:663
void WriteEntityRef(string name)
void WriteString(string? text)
void WriteChars(char[] buffer, int index, int count)
virtual Task WriteEntityRefAsync(string name)
Definition XmlWriter.cs:653
virtual Task WriteStringAsync(string? text)
Definition XmlWriter.cs:668
virtual void WriteQualifiedName(string localName, string? ns)
Definition XmlWriter.cs:132
virtual Task WriteCommentAsync(string? text)
Definition XmlWriter.cs:643
virtual Task WriteNmTokenAsync(string name)
Definition XmlWriter.cs:708
virtual Task WriteCDataAsync(string? text)
Definition XmlWriter.cs:638
virtual Task WriteCharsAsync(char[] buffer, int index, int count)
Definition XmlWriter.cs:678
virtual Task WriteStartElementAsync(string? prefix, string localName, string? ns)
Definition XmlWriter.cs:596
virtual Task WriteStartAttributeAsync(string? prefix, string localName, string? ns)
Definition XmlWriter.cs:628
virtual Task WriteDocTypeAsync(string name, string? pubid, string? sysid, string? subset)
Definition XmlWriter.cs:591
void WriteStartAttribute(string localName, string? ns)
Definition XmlWriter.cs:67
void WriteWhitespace(string? ws)
void WriteDocType(string name, string? pubid, string? sysid, string? subset)
virtual ? XmlWriterSettings Settings
Definition XmlWriter.cs:14
virtual async Task WriteQualifiedNameAsync(string localName, string? ns)
Definition XmlWriter.cs:722
void WriteStartElement(string localName, string? ns)
Definition XmlWriter.cs:30