Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DocumentSchemaValidator.cs
Go to the documentation of this file.
5
6namespace System.Xml;
7
9{
11
12 private readonly XmlSchemaSet _schemas;
13
15
16 private readonly XmlNameTable _nameTable;
17
19
20 private readonly XmlValueGetter _nodeValueGetter;
21
23
25
26 private readonly ValidationEventHandler _eventHandler;
27
28 private readonly ValidationEventHandler _internalEventHandler;
29
31
33
34 private readonly XmlDocument _document;
35
37
38 private bool _isPartialTreeValid;
39
40 private bool _psviAugmentation;
41
42 private bool _isValid;
43
44 private readonly string _nsXmlNs;
45
46 private readonly string _nsXsi;
47
48 private readonly string _xsiType;
49
50 private readonly string _xsiNil;
51
52 public bool PsviAugmentation
53 {
54 set
55 {
57 }
58 }
59
61 {
62 get
63 {
64 if (_startNode == _document)
65 {
66 return _nsManager;
67 }
68 return this;
69 }
70 }
71
73 {
74 _schemas = schemas;
81 _psviAugmentation = true;
82 _nsXmlNs = _nameTable.Add("http://www.w3.org/2000/xmlns/");
83 _nsXsi = _nameTable.Add("http://www.w3.org/2001/XMLSchema-instance");
84 _xsiType = _nameTable.Add("type");
85 _xsiNil = _nameTable.Add("nil");
86 }
87
89 {
93 XmlNodeType nodeType = nodeToValidate.NodeType;
94 if (nodeType <= XmlNodeType.Attribute)
95 {
96 if (nodeType != XmlNodeType.Element)
97 {
98 if (nodeType != XmlNodeType.Attribute || nodeToValidate.XPNodeType == XPathNodeType.Namespace)
99 {
100 goto IL_00fe;
101 }
102 xmlSchemaObject = nodeToValidate.SchemaInfo.SchemaAttribute;
103 if (xmlSchemaObject == null)
104 {
106 if (xmlSchemaObject == null)
107 {
109 }
110 }
111 }
112 else
113 {
114 IXmlSchemaInfo schemaInfo = nodeToValidate.SchemaInfo;
116 if (schemaElement != null)
117 {
119 }
120 else
121 {
122 xmlSchemaObject = schemaInfo.SchemaType;
123 if (xmlSchemaObject == null)
124 {
125 if (nodeToValidate.ParentNode.NodeType == XmlNodeType.Document)
126 {
127 nodeToValidate = nodeToValidate.ParentNode;
128 }
129 else
130 {
132 if (xmlSchemaObject == null)
133 {
135 }
136 }
137 }
138 }
139 }
140 }
141 else if (nodeType != XmlNodeType.Document)
142 {
143 if (nodeType != XmlNodeType.DocumentFragment)
144 {
145 goto IL_00fe;
146 }
147 }
148 else
149 {
150 xmlSchemaValidationFlags |= XmlSchemaValidationFlags.ProcessIdentityConstraints;
151 }
152 _isValid = true;
155 {
156 if (_schemaInfo == null)
157 {
159 }
161 }
164 return _isValid;
165 IL_00fe:
167 }
168
170 {
172 if (scope != XmlNamespaceScope.Local)
173 {
175 while (xmlNode != null)
176 {
177 switch (xmlNode.NodeType)
178 {
179 case XmlNodeType.Element:
180 {
182 if (xmlElement.HasAttributes)
183 {
184 XmlAttributeCollection attributes = xmlElement.Attributes;
185 for (int i = 0; i < attributes.Count; i++)
186 {
187 XmlAttribute xmlAttribute = attributes[i];
188 if (!Ref.Equal(xmlAttribute.NamespaceURI, _document.strReservedXmlns))
189 {
190 continue;
191 }
192 if (xmlAttribute.Prefix.Length == 0)
193 {
194 if (!namespacesInScope.ContainsKey(string.Empty))
195 {
197 }
198 }
199 else if (!namespacesInScope.ContainsKey(xmlAttribute.LocalName))
200 {
202 }
203 }
204 }
205 xmlNode = xmlNode.ParentNode;
206 break;
207 }
208 case XmlNodeType.Attribute:
209 xmlNode = ((XmlAttribute)xmlNode).OwnerElement;
210 break;
211 default:
212 xmlNode = xmlNode.ParentNode;
213 break;
214 }
215 }
216 }
217 return namespacesInScope;
218 }
219
220 public string LookupNamespace(string prefix)
221 {
223 if (text == null)
224 {
226 }
227 return text;
228 }
229
230 public string LookupPrefix(string namespaceName)
231 {
233 if (text == null)
234 {
236 }
237 return text;
238 }
239
256
258 {
260 switch (_currentNode.NodeType)
261 {
263 {
264 XmlElement documentElement = ((XmlDocument)node).DocumentElement;
265 if (documentElement == null)
266 {
268 }
270 break;
271 }
272 case XmlNodeType.EntityReference:
273 case XmlNodeType.DocumentFragment:
274 {
275 for (XmlNode xmlNode = node.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
276 {
278 }
279 break;
280 }
281 case XmlNodeType.Element:
283 break;
284 case XmlNodeType.Attribute:
285 {
289 {
291 }
292 break;
293 }
294 case XmlNodeType.Text:
296 break;
297 case XmlNodeType.CDATA:
299 break;
300 case XmlNodeType.Whitespace:
301 case XmlNodeType.SignificantWhitespace:
303 break;
304 default:
306 case XmlNodeType.ProcessingInstruction:
307 case XmlNodeType.Comment:
308 break;
309 }
310 }
311
312 private void ValidateElement()
313 {
316 XmlAttributeCollection attributes = xmlElement.Attributes;
318 string xsiNil = null;
319 string xsiType = null;
320 for (int i = 0; i < attributes.Count; i++)
321 {
322 xmlAttribute = attributes[i];
323 string namespaceURI = xmlAttribute.NamespaceURI;
324 string localName = xmlAttribute.LocalName;
326 {
327 if (Ref.Equal(localName, _xsiType))
328 {
329 xsiType = xmlAttribute.Value;
330 }
331 else if (Ref.Equal(localName, _xsiNil))
332 {
333 xsiNil = xmlAttribute.Value;
334 }
335 }
336 else if (Ref.Equal(namespaceURI, _nsXmlNs))
337 {
338 _nsManager.AddNamespace((xmlAttribute.Prefix.Length == 0) ? string.Empty : xmlAttribute.LocalName, xmlAttribute.Value);
339 }
340 }
341 _validator.ValidateElement(xmlElement.LocalName, xmlElement.NamespaceURI, _schemaInfo, xsiType, xsiNil, null, null);
344 for (XmlNode xmlNode = xmlElement.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
345 {
347 }
351 {
354 {
356 xmlElement.AppendChild(newChild);
357 }
358 }
360 }
361
363 {
364 XmlAttributeCollection attributes = elementNode.Attributes;
366 for (int i = 0; i < attributes.Count; i++)
367 {
368 xmlAttribute = (XmlAttribute)(_currentNode = attributes[i]);
369 if (!Ref.Equal(xmlAttribute.NamespaceURI, _nsXmlNs))
370 {
373 {
375 }
376 }
377 }
379 {
380 return;
381 }
382 if (_defaultAttributes == null)
383 {
385 }
386 else
387 {
389 }
391 xmlAttribute = null;
392 for (int j = 0; j < _defaultAttributes.Count; j++)
393 {
399 xmlAttribute.AppendChild(_document.CreateTextNode(xmlSchemaAttribute.AttDef.DefaultValueRaw));
400 attributes.Append(xmlAttribute);
402 {
403 xmlUnspecifiedAttribute.SetSpecified(f: false);
404 }
405 }
406 }
407
423
424 private string GetDefaultPrefix(string attributeNS)
425 {
427 string text = null;
430 {
431 string text2 = _nameTable.Add(item.Value);
432 if ((object)text2 == attributeNS)
433 {
434 text = item.Key;
435 if (text.Length != 0)
436 {
437 return text;
438 }
439 }
440 }
441 return text;
442 }
443
444 private object GetNodeValue()
445 {
446 return _currentNode.Value;
447 }
448
450 {
451 _isPartialTreeValid = true;
453 int num = 0;
454 XmlNode parentNode = elementToValidate.ParentNode;
455 do
456 {
457 xmlSchemaInfo = parentNode.SchemaInfo;
458 if (xmlSchemaInfo.SchemaElement != null || xmlSchemaInfo.SchemaType != null)
459 {
460 break;
461 }
463 _nodeSequenceToValidate[num++] = parentNode;
464 parentNode = parentNode.ParentNode;
465 }
466 while (parentNode != null);
467 if (parentNode == null)
468 {
469 num--;
470 _nodeSequenceToValidate[num] = null;
471 return GetTypeFromAncestors(elementToValidate, null, num);
472 }
474 _nodeSequenceToValidate[num++] = parentNode;
476 if (xmlSchemaObject == null)
477 {
478 xmlSchemaObject = xmlSchemaInfo.SchemaType;
479 }
481 }
482
483 private void CheckNodeSequenceCapacity(int currentIndex)
484 {
485 if (_nodeSequenceToValidate == null)
486 {
488 }
489 else if (currentIndex >= _nodeSequenceToValidate.Length - 1)
490 {
494 }
495 }
496
518
520 {
523 int num = ancestorsCount - 1;
525 for (int num2 = num; num2 >= 0; num2--)
526 {
530 if (!flag)
531 {
534 }
536 if (num2 > 0)
537 {
539 }
540 else
541 {
543 }
544 }
548 if (xmlSchemaObject == null)
549 {
551 {
553 {
555 }
556 }
558 {
560 }
561 }
562 return xmlSchemaObject;
563 }
564
566 {
568 if (ancestorType != null)
569 {
570 return complexType.HasWildCard;
571 }
572 return false;
573 }
574
589
591 {
593 XmlAttributeCollection attributes = elementNode.Attributes;
595 string xsiNil = null;
596 string xsiType = null;
597 for (int i = 0; i < attributes.Count; i++)
598 {
599 xmlAttribute = attributes[i];
600 string namespaceURI = xmlAttribute.NamespaceURI;
601 string localName = xmlAttribute.LocalName;
603 {
604 if (Ref.Equal(localName, _xsiType))
605 {
606 xsiType = xmlAttribute.Value;
607 }
608 else if (Ref.Equal(localName, _xsiNil))
609 {
610 xsiNil = xmlAttribute.Value;
611 }
612 }
613 else if (Ref.Equal(namespaceURI, _nsXmlNs))
614 {
615 _nsManager.AddNamespace((xmlAttribute.Prefix.Length == 0) ? string.Empty : xmlAttribute.LocalName, xmlAttribute.Value);
616 }
617 }
618 _validator.ValidateElement(elementNode.LocalName, elementNode.NamespaceURI, newSchemaInfo, xsiType, xsiNil, null, null);
619 if (skipToEnd)
620 {
624 }
625 }
626
628 {
629 XmlNode xmlNode = parentNode.FirstChild;
630 while (xmlNode != null && xmlNode != childToStopAt)
631 {
632 switch (xmlNode.NodeType)
633 {
634 case XmlNodeType.EntityReference:
636 break;
637 case XmlNodeType.Element:
639 break;
640 case XmlNodeType.Text:
641 case XmlNodeType.CDATA:
643 break;
644 case XmlNodeType.Whitespace:
645 case XmlNodeType.SignificantWhitespace:
647 break;
648 default:
650 case XmlNodeType.ProcessingInstruction:
651 case XmlNodeType.Comment:
652 break;
653 }
654 xmlNode = xmlNode.NextSibling;
655 }
656 }
657
672
673 private void TypeFinderCallBack(object sender, ValidationEventArgs arg)
674 {
675 if (arg.Severity == XmlSeverityType.Error)
676 {
677 _isPartialTreeValid = false;
678 }
679 }
680
682 {
683 if (arg.Severity == XmlSeverityType.Error)
684 {
685 _isValid = false;
686 }
688 ex.SetSourceObject(_currentNode);
689 if (_eventHandler != null)
690 {
691 _eventHandler(sender, arg);
692 }
693 else if (arg.Severity == XmlSeverityType.Error)
694 {
695 throw ex;
696 }
697 }
698}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
void Add(TKey key, TValue value)
static string Xml_UnexpectedNodeType
Definition SR.cs:236
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string XmlDocument_NoNodeSchemaInfo
Definition SR.cs:1176
static string Xml_InvalidXmlDocument
Definition SR.cs:216
static string Xdom_NoRootEle
Definition SR.cs:1278
static string XmlDocument_ValidateInvalidNodeType
Definition SR.cs:1172
Definition SR.cs:7
void SetDefaultAttributeSchemaInfo(XmlSchemaAttribute schemaAttribute)
string LookupPrefix(string namespaceName)
XmlSchemaObject GetTypeFromAncestors(XmlElement elementToValidate, XmlSchemaObject ancestorType, int ancestorsCount)
string GetDefaultPrefix(string attributeNS)
bool Validate(XmlNode nodeToValidate)
readonly ValidationEventHandler _eventHandler
void ValidateSingleElement(XmlElement elementNode, bool skipToEnd, XmlSchemaInfo newSchemaInfo)
void ValidateChildrenTillNextAncestor(XmlNode parentNode, XmlNode childToStopAt)
readonly XmlNamespaceManager _nsManager
XmlSchemaAttribute FindSchemaInfo(XmlAttribute attributeToValidate)
void TypeFinderCallBack(object sender, ValidationEventArgs arg)
XmlSchemaComplexType GetComplexType(XmlSchemaObject schemaObject)
void CreateValidator(XmlSchemaObject partialValidationType, XmlSchemaValidationFlags validationFlags)
void ValidateAttributes(XmlElement elementNode)
bool AncestorTypeHasWildcard(XmlSchemaObject ancestorType)
void InternalValidationCallBack(object sender, ValidationEventArgs arg)
XmlSchemaObject FindSchemaInfo(XmlElement elementToValidate)
IDictionary< string, string > GetNamespacesInScope(XmlNamespaceScope scope)
XmlSchemaValidator CreateTypeFinderValidator(XmlSchemaObject partialValidationType)
readonly ValidationEventHandler _internalEventHandler
DocumentSchemaValidator(XmlDocument ownerDocument, XmlSchemaSet schemas, ValidationEventHandler eventHandler)
static bool Equal(string strA, string strB)
Definition Ref.cs:5
XmlSchemaObjectTable GlobalElements
XmlSchemaObjectTable GlobalAttributes
XmlSchemaContentProcessing CurrentProcessContents
void SkipToEndElement(XmlSchemaInfo? schemaInfo)
void ValidateElement(string localName, string namespaceUri, XmlSchemaInfo? schemaInfo)
object? ValidateAttribute(string localName, string namespaceUri, string attributeValue, XmlSchemaInfo? schemaInfo)
object? ValidateEndElement(XmlSchemaInfo? schemaInfo)
void ValidateEndOfAttributes(XmlSchemaInfo? schemaInfo)
void GetUnspecifiedDefaultAttributes(ArrayList defaultAttributes)
void ValidateWhitespace(string elementValue)
XmlAttribute Append(XmlAttribute node)
static Uri ToUri(string s)
virtual XmlText CreateTextNode(string? text)
virtual XmlAttribute CreateDefaultAttribute(string? prefix, string localName, string? namespaceURI)
XmlName AddXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo)
override string BaseURI
XmlName AddAttrXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo)
string Add(char[] array, int offset, int length)
virtual ? string LookupNamespace(string prefix)
virtual ? string LookupPrefix(string uri)
virtual IDictionary< string, string > GetNamespacesInScope(XmlNamespaceScope scope)
virtual void AddNamespace(string prefix, string uri)
virtual ? string Value
Definition XmlNode.cs:62
XmlDocument Document
Definition XmlNode.cs:253
virtual ? XmlNode ParentNode
Definition XmlNode.cs:76
virtual IXmlSchemaInfo SchemaInfo
Definition XmlNode.cs:230
virtual ? XmlNode FirstChild
Definition XmlNode.cs:120
string GetNamespaceOfPrefixStrict(string prefix)
Definition XmlNode.cs:1051
string GetPrefixOfNamespaceStrict(string namespaceURI)
Definition XmlNode.cs:1134
XmlNodeType NodeType
Definition XmlNode.cs:73
IDictionary< string, string > GetNamespacesInScope(XmlNamespaceScope scope)