Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlSchemas.cs
Go to the documentation of this file.
5using System.IO;
7
9
11{
13
15
17
18 private bool _shareTypes;
19
21
23
24 private bool _isCompiled;
25
26 private static volatile XmlSchema s_xsd;
27
28 private static volatile XmlSchema s_xml;
29
30 public XmlSchema this[int index]
31 {
32 get
33 {
34 return (XmlSchema)base.List[index];
35 }
36 set
37 {
38 base.List[index] = value;
39 }
40 }
41
42 public XmlSchema? this[string? ns]
43 {
44 get
45 {
47 if (list.Count == 0)
48 {
49 return null;
50 }
51 if (list.Count == 1)
52 {
53 return (XmlSchema)list[0];
54 }
56 }
57 }
58
59 internal SchemaObjectCache Cache
60 {
61 get
62 {
63 if (_cache == null)
64 {
66 }
67 return _cache;
68 }
69 }
70
72 {
73 get
74 {
75 if (_mergedSchemas == null)
76 {
78 }
79 return _mergedSchemas;
80 }
81 }
82
84 {
85 get
86 {
87 if (_references == null)
88 {
89 _references = new Hashtable();
90 }
91 return _references;
92 }
93 }
94
96 {
97 get
98 {
99 if (_schemaSet == null)
100 {
101 _schemaSet = new XmlSchemaSet();
104 }
105 return _schemaSet;
106 }
107 }
108
109 public bool IsCompiled => _isCompiled;
110
111 internal static XmlSchema XsdSchema
112 {
113 get
114 {
115 if (s_xsd == null)
116 {
117 s_xsd = CreateFakeXsdSchema("http://www.w3.org/2001/XMLSchema", "schema");
118 }
119 return s_xsd;
120 }
121 }
122
123 internal static XmlSchema XmlSchema
124 {
125 get
126 {
127 if (s_xml == null)
128 {
129 s_xml = System.Xml.Schema.XmlSchema.Read(new StringReader("<?xml version='1.0' encoding='UTF-8' ?>\r\n<xs:schema targetNamespace='http://www.w3.org/XML/1998/namespace' xmlns:xs='http://www.w3.org/2001/XMLSchema' xml:lang='en'>\r\n <xs:attribute name='lang' type='xs:language'/>\r\n <xs:attribute name='space'>\r\n <xs:simpleType>\r\n <xs:restriction base='xs:NCName'>\r\n <xs:enumeration value='default'/>\r\n <xs:enumeration value='preserve'/>\r\n </xs:restriction>\r\n </xs:simpleType>\r\n </xs:attribute>\r\n <xs:attribute name='base' type='xs:anyURI'/>\r\n <xs:attribute name='id' type='xs:ID' />\r\n <xs:attributeGroup name='specialAttrs'>\r\n <xs:attribute ref='xml:base'/>\r\n <xs:attribute ref='xml:lang'/>\r\n <xs:attribute ref='xml:space'/>\r\n </xs:attributeGroup>\r\n</xs:schema>"), null);
130 }
131 return s_xml;
132 }
133 }
134
135 public IList GetSchemas(string? ns)
136 {
137 return (IList)SchemaSet.Schemas(ns);
138 }
139
140 internal int Add(XmlSchema schema, bool delay)
141 {
142 if (delay)
143 {
144 if (delayedSchemas[schema] == null)
145 {
147 }
148 return -1;
149 }
150 return Add(schema);
151 }
152
153 public int Add(XmlSchema schema)
154 {
155 if (base.List.Contains(schema))
156 {
157 return base.List.IndexOf(schema);
158 }
159 return base.List.Add(schema);
160 }
161
162 public int Add(XmlSchema schema, Uri? baseUri)
163 {
164 if (base.List.Contains(schema))
165 {
166 return base.List.IndexOf(schema);
167 }
168 if (baseUri != null)
169 {
170 schema.BaseUri = baseUri;
171 }
172 return base.List.Add(schema);
173 }
174
175 public void Add(XmlSchemas schemas)
176 {
177 foreach (XmlSchema schema in schemas)
178 {
179 Add(schema);
180 }
181 }
182
184 {
186 }
187
188 public void Insert(int index, XmlSchema schema)
189 {
190 base.List.Insert(index, schema);
191 }
192
194 {
195 return base.List.IndexOf(schema);
196 }
197
199 {
200 return base.List.Contains(schema);
201 }
202
203 public bool Contains(string? targetNamespace)
204 {
206 }
207
209 {
210 base.List.Remove(schema);
211 }
212
213 public void CopyTo(XmlSchema[] array, int index)
214 {
215 base.List.CopyTo(array, index);
216 }
217
218 protected override void OnInsert(int index, object? value)
219 {
221 }
222
223 protected override void OnRemove(int index, object? value)
224 {
226 }
227
228 protected override void OnClear()
229 {
230 _schemaSet = null;
231 }
232
233 protected override void OnSet(int index, object? oldValue, object? newValue)
234 {
237 }
238
240 {
241 if (_isCompiled)
242 {
244 }
246 {
248 return;
249 }
252 }
253
255 {
257 string targetNamespace = schema.TargetNamespace;
258 foreach (XmlSchemaExternal include in schema.Includes)
259 {
261 {
263 }
264 }
265 foreach (XmlSchemaObject item in arrayList)
266 {
267 schema.Includes.Remove(item);
268 }
269 }
270
272 {
274 }
275
276 public object? Find(XmlQualifiedName name, Type type)
277 {
278 return Find(name, type, checkCache: true);
279 }
280
281 internal object Find(XmlQualifiedName name, Type type, bool checkCache)
282 {
283 if (!IsCompiled)
284 {
285 foreach (XmlSchema item in base.List)
286 {
288 }
289 }
290 IList list = (IList)SchemaSet.Schemas(name.Namespace);
291 if (list == null)
292 {
293 return null;
294 }
295 foreach (XmlSchema item2 in list)
296 {
299 if (typeof(XmlSchemaType).IsAssignableFrom(type))
300 {
301 xmlSchemaObject = item2.SchemaTypes[name];
302 if (xmlSchemaObject == null || !type.IsAssignableFrom(xmlSchemaObject.GetType()))
303 {
304 continue;
305 }
306 }
307 else if (type == typeof(XmlSchemaGroup))
308 {
309 xmlSchemaObject = item2.Groups[name];
310 }
312 {
313 xmlSchemaObject = item2.AttributeGroups[name];
314 }
315 else if (type == typeof(XmlSchemaElement))
316 {
317 xmlSchemaObject = item2.Elements[name];
318 }
319 else if (type == typeof(XmlSchemaAttribute))
320 {
321 xmlSchemaObject = item2.Attributes[name];
322 }
323 else if (type == typeof(XmlSchemaNotation))
324 {
325 xmlSchemaObject = item2.Notations[name];
326 }
328 {
329 xmlSchemaObject = Cache.AddItem(xmlSchemaObject, name, this);
330 }
331 if (xmlSchemaObject != null)
332 {
333 return xmlSchemaObject;
334 }
335 }
336 return null;
337 }
338
343
344 internal static void Preprocess(XmlSchema schema)
345 {
346 if (!schema.IsPreprocessed)
347 {
348 try
349 {
353 preprocessor.Execute(schema, schema.TargetNamespace, loadExternals: false);
354 }
355 catch (XmlSchemaException ex)
356 {
357 throw CreateValidationException(ex, ex.Message);
358 }
359 }
360 }
361
362 public static bool IsDataSet(XmlSchema schema)
363 {
364 foreach (XmlSchemaObject item in schema.Items)
365 {
366 if (!(item is XmlSchemaElement))
367 {
368 continue;
369 }
371 if (xmlSchemaElement.UnhandledAttributes == null)
372 {
373 continue;
374 }
375 XmlAttribute[] unhandledAttributes = xmlSchemaElement.UnhandledAttributes;
377 {
378 if (xmlAttribute.LocalName == "IsDataSet" && xmlAttribute.NamespaceURI == "urn:schemas-microsoft-com:xml-msdata" && (xmlAttribute.Value == "True" || xmlAttribute.Value == "true" || xmlAttribute.Value == "1"))
379 {
380 return true;
381 }
382 }
383 }
384 return false;
385 }
386
387 [RequiresUnreferencedCode("calls Merge")]
388 private void Merge(XmlSchema schema)
389 {
390 if (MergedSchemas[schema] == null)
391 {
392 IList list = (IList)SchemaSet.Schemas(schema.TargetNamespace);
393 if (list != null && list.Count > 0)
394 {
396 Merge(list, schema);
397 }
398 else
399 {
400 Add(schema);
402 }
403 }
404 }
405
406 private void AddImport(IList schemas, string ns)
407 {
408 foreach (XmlSchema schema in schemas)
409 {
410 bool flag = true;
411 foreach (XmlSchemaExternal include in schema.Includes)
412 {
414 {
415 flag = false;
416 break;
417 }
418 }
419 if (flag)
420 {
423 schema.Includes.Add(xmlSchemaImport);
424 }
425 }
426 }
427
428 [RequiresUnreferencedCode("Calls MergeFailedMessage")]
430 {
431 foreach (XmlSchema original in originals)
432 {
433 if (schema == original)
434 {
435 return;
436 }
437 }
438 foreach (XmlSchemaExternal include in schema.Includes)
439 {
441 {
443 if (include.Schema != null)
444 {
445 Merge(include.Schema);
446 }
447 else
448 {
450 }
451 }
452 else if (include.Schema == null)
453 {
454 if (include.SchemaLocation != null)
455 {
457 }
458 }
459 else
460 {
462 Merge(originals, include.Schema);
463 }
464 }
465 bool[] array = new bool[schema.Items.Count];
466 int num = 0;
467 for (int i = 0; i < schema.Items.Count; i++)
468 {
471 if (xmlSchemaObject2 != null)
472 {
473 if (!Cache.Match(xmlSchemaObject2, xmlSchemaObject, _shareTypes))
474 {
476 }
477 array[i] = true;
478 num++;
479 }
480 }
481 if (num == schema.Items.Count)
482 {
483 return;
484 }
486 for (int j = 0; j < schema.Items.Count; j++)
487 {
488 if (!array[j])
489 {
490 xmlSchema2.Items.Add(schema.Items[j]);
491 }
492 }
495 }
496
497 private static string ItemName(XmlSchemaObject o)
498 {
500 {
501 return ((XmlSchemaNotation)o).Name;
502 }
503 if (o is XmlSchemaGroup)
504 {
505 return ((XmlSchemaGroup)o).Name;
506 }
508 {
509 return ((XmlSchemaElement)o).Name;
510 }
511 if (o is XmlSchemaType)
512 {
513 return ((XmlSchemaType)o).Name;
514 }
516 {
517 return ((XmlSchemaAttributeGroup)o).Name;
518 }
520 {
521 return ((XmlSchemaAttribute)o).Name;
522 }
523 return null;
524 }
525
527 {
528 while (item.Parent != null)
529 {
530 if (item.Parent is XmlSchemaType)
531 {
533 if (xmlSchemaType.Name != null && xmlSchemaType.Name.Length != 0)
534 {
535 return xmlSchemaType.QualifiedName;
536 }
537 }
538 item = item.Parent;
539 }
540 return XmlQualifiedName.Empty;
541 }
542
543 [return: NotNullIfNotNull("o")]
544 private static string GetSchemaItem(XmlSchemaObject o, string ns, string details)
545 {
546 if (o == null)
547 {
548 return null;
549 }
550 while (o.Parent != null && !(o.Parent is XmlSchema))
551 {
552 o = o.Parent;
553 }
554 if (ns == null || ns.Length == 0)
555 {
557 while (xmlSchemaObject.Parent != null)
558 {
560 }
562 {
563 ns = ((XmlSchema)xmlSchemaObject).TargetNamespace;
564 }
565 }
566 string text = null;
568 {
569 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, "notation", ((XmlSchemaNotation)o).Name, details);
570 }
571 if (o is XmlSchemaGroup)
572 {
573 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, "group", ((XmlSchemaGroup)o).Name, details);
574 }
576 {
578 if (xmlSchemaElement.Name == null || xmlSchemaElement.Name.Length == 0)
579 {
581 return System.SR.Format(System.SR.XmlSchemaElementReference, xmlSchemaElement.RefName.ToString(), parentName.Name, parentName.Namespace);
582 }
583 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, "element", xmlSchemaElement.Name, details);
584 }
585 if (o is XmlSchemaType)
586 {
587 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, (o.GetType() == typeof(XmlSchemaSimpleType)) ? "simpleType" : "complexType", ((XmlSchemaType)o).Name, null);
588 }
590 {
591 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, "attributeGroup", ((XmlSchemaAttributeGroup)o).Name, details);
592 }
594 {
596 if (xmlSchemaAttribute.Name == null || xmlSchemaAttribute.Name.Length == 0)
597 {
600 }
601 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, "attribute", xmlSchemaAttribute.Name, details);
602 }
604 {
606 return System.SR.Format(System.SR.XmlSchemaContentDef, parentName3.Name, parentName3.Namespace, null);
607 }
609 {
610 string p = ((o is XmlSchemaImport) ? "import" : ((o is XmlSchemaInclude) ? "include" : ((o is XmlSchemaRedefine) ? "redefine" : o.GetType().Name)));
611 return System.SR.Format(System.SR.XmlSchemaItem, ns, p, details);
612 }
613 if (o is XmlSchema)
614 {
615 return System.SR.Format(System.SR.XmlSchema, ns, details);
616 }
617 return System.SR.Format(System.SR.XmlSchemaNamedItem, ns, o.GetType().Name, null, details);
618 }
619
620 [RequiresUnreferencedCode("Creates XmlSerializer")]
634
635 [RequiresUnreferencedCode("calls Dump")]
636 private static string MergeFailedMessage(XmlSchemaObject src, XmlSchemaObject dest, string ns)
637 {
639 text = text + "\r\n" + Dump(src);
640 return text + "\r\n" + Dump(dest);
641 }
642
644 {
645 string text = ItemName(o);
646 if (text == null)
647 {
648 return null;
649 }
650 Type type = o.GetType();
651 foreach (XmlSchema original in originals)
652 {
653 foreach (XmlSchemaObject item in original.Items)
654 {
655 if (item.GetType() == type && text == ItemName(item))
656 {
657 return item;
658 }
659 }
660 }
661 return null;
662 }
663
664 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
665 public void Compile(ValidationEventHandler? handler, bool fullCompile)
666 {
667 if (_isCompiled)
668 {
669 return;
670 }
672 {
673 Merge(value);
674 }
676 if (fullCompile)
677 {
678 _schemaSet = new XmlSchemaSet();
681 foreach (XmlSchema value2 in References.Values)
682 {
684 }
685 int num = _schemaSet.Count;
686 foreach (XmlSchema item in base.List)
687 {
688 if (!SchemaSet.Contains(item))
689 {
691 num++;
692 }
693 }
694 if (!SchemaSet.Contains("http://www.w3.org/2001/XMLSchema"))
695 {
698 num++;
699 }
700 if (!SchemaSet.Contains("http://www.w3.org/XML/1998/namespace"))
701 {
704 num++;
705 }
709 return;
710 }
711 try
712 {
718 foreach (XmlSchema item2 in SchemaSet.Schemas())
719 {
720 preprocessor.Execute(item2, item2.TargetNamespace, loadExternals: true);
721 }
722 }
723 catch (XmlSchemaException ex)
724 {
725 throw CreateValidationException(ex, ex.Message);
726 }
727 }
728
730 {
731 XmlSchemaObject xmlSchemaObject = exception.SourceSchemaObject;
732 if (exception.LineNumber == 0 && exception.LinePosition == 0)
733 {
735 }
736 string text = null;
737 if (xmlSchemaObject != null)
738 {
739 while (xmlSchemaObject.Parent != null)
740 {
742 }
744 {
745 text = ((XmlSchema)xmlSchemaObject).TargetNamespace;
746 }
747 }
749 }
750
751 internal static void IgnoreCompileErrors(object sender, ValidationEventArgs args)
752 {
753 }
754
755 private static XmlSchema CreateFakeXsdSchema(string ns, string name)
756 {
762 xmlSchemaElement.SchemaType = schemaType;
764 return xmlSchema;
765 }
766
767 [RequiresUnreferencedCode("calls GenerateSchemaGraph")]
769 {
771 _cache = cache;
772 if (shareTypes)
773 {
774 cache.GenerateSchemaGraph(this);
775 }
776 }
777
779 {
781 while (xmlSchemaObject.Parent != null)
782 {
784 }
786 }
787}
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
virtual bool Contains(object key)
Definition Hashtable.cs:719
virtual ICollection Values
Definition Hashtable.cs:534
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static CultureInfo InvariantCulture
static string XmlSchemaNamedItem
Definition SR.cs:1784
static string XmlSchemaContentDef
Definition SR.cs:1786
static string XmlSchemaItem
Definition SR.cs:1782
static string XmlSchemaCompiled
Definition SR.cs:1534
static string XmlSerializableMergeItem
Definition SR.cs:1766
static string XmlSchemaElementReference
Definition SR.cs:1778
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string XmlSchemaDuplicateNamespace
Definition SR.cs:1532
static string XmlSchemaIncludeLocation
Definition SR.cs:1748
static string XmlSchemaAttributeReference
Definition SR.cs:1780
static string XmlSchemaSyntaxErrorDetails
Definition SR.cs:1776
static string XmlSchema
Definition SR.cs:1788
Definition SR.cs:7
XmlSchema Reprocess(XmlSchema schema)
XmlSchema? Remove(XmlSchema schema)
bool Contains(string? targetNamespace)
XmlSchema? Add(string? targetNamespace, string schemaUri)
static ? XmlSchema Read(TextReader reader, ValidationEventHandler? validationEventHandler)
Definition XmlSchema.cs:432
void Remove(XmlSchema schema)
int IndexOf(XmlSchema schema)
bool Contains(string? targetNamespace)
void Merge(IList originals, XmlSchema schema)
void SetCache(SchemaObjectCache cache, bool shareTypes)
override void OnSet(int index, object? oldValue, object? newValue)
bool Contains(XmlSchema schema)
bool IsReference(XmlSchemaObject type)
static void Preprocess(XmlSchema schema)
void AddReference(XmlSchema schema)
void Compile(ValidationEventHandler? handler, bool fullCompile)
static volatile XmlSchema s_xsd
Definition XmlSchemas.cs:26
static string MergeFailedMessage(XmlSchemaObject src, XmlSchemaObject dest, string ns)
override void OnInsert(int index, object? value)
int Add(XmlSchema schema, Uri? baseUri)
int Add(XmlSchema schema, bool delay)
void AddImport(IList schemas, string ns)
static string ItemName(XmlSchemaObject o)
override void OnRemove(int index, object? value)
void AddName(XmlSchema schema)
void Merge(XmlSchema schema)
void Insert(int index, XmlSchema schema)
static volatile XmlSchema s_xml
Definition XmlSchemas.cs:28
static void IgnoreCompileErrors(object sender, ValidationEventArgs args)
void Add(XmlSchemas schemas)
void CopyTo(XmlSchema[] array, int index)
void RemoveName(XmlSchema schema)
static XmlSchema CreateFakeXsdSchema(string ns, string name)
static Exception CreateValidationException(XmlSchemaException exception, string message)
static XmlQualifiedName GetParentName(XmlSchemaObject item)
object? Find(XmlQualifiedName name, Type type)
static string GetSchemaItem(XmlSchemaObject o, string ns, string details)
void Prepare(XmlSchema schema)
object Find(XmlQualifiedName name, Type type, bool checkCache)
static bool IsDataSet(XmlSchema schema)
static string Dump(XmlSchemaObject o)
XmlSchemaObject Find(XmlSchemaObject o, IList originals)
static readonly XmlQualifiedName Empty
static XmlWriter Create(string outputFileName)
Definition XmlWriter.cs:468