Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XdrValidator.cs
Go to the documentation of this file.
3using System.IO;
4using System.Text;
5
6namespace System.Xml.Schema;
7
8internal sealed class XdrValidator : BaseValidator
9{
11
13
15
17
18 private bool _isProcessContents;
19
20 private Hashtable _IDs;
21
23
25
27
29
30 public override bool PreserveWhitespace
31 {
32 get
33 {
34 if (context.ElementDecl == null)
35 {
36 return false;
37 }
39 }
40 }
41
42 internal XdrValidator(BaseValidator validator)
43 : base(validator)
44 {
45 Init();
46 }
47
53
54 [MemberNotNull("_nsManager")]
55 [MemberNotNull("_validationStack")]
56 [MemberNotNull("_name")]
57 [MemberNotNull("_attPresence")]
58 private void Init()
59 {
61 if (_nsManager == null)
62 {
63 _nsManager = new XmlNamespaceManager(base.NameTable);
64 _isProcessContents = true;
65 }
66 _validationStack = new HWStack(10);
69 _attPresence = new Hashtable();
71 schemaInfo = new SchemaInfo();
72 checkDatatype = false;
73 }
74
75 public override void Validate()
76 {
78 {
80 return;
81 }
82 switch (reader.NodeType)
83 {
84 case XmlNodeType.Element:
87 {
88 goto case XmlNodeType.EndElement;
89 }
90 break;
91 case XmlNodeType.Whitespace:
93 break;
94 case XmlNodeType.Text:
95 case XmlNodeType.CDATA:
96 case XmlNodeType.SignificantWhitespace:
98 break;
99 case XmlNodeType.EndElement:
101 break;
102 }
103 }
104
105 private void ValidateElement()
106 {
109 if (base.SchemaNames.IsXDRRoot(elementName.Name, elementName.Namespace) && reader.Depth > 0)
110 {
111 _inlineSchemaParser = new Parser(SchemaType.XDR, base.NameTable, base.SchemaNames, base.EventHandler);
114 }
115 else
116 {
118 }
119 }
120
121 private void ValidateChildElement()
122 {
124 {
125 int errorCode = 0;
127 if (errorCode < 0)
128 {
129 XmlSchemaValidator.ElementValidationError(elementName, context, base.EventHandler, reader, reader.BaseURI, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition, null);
130 }
131 }
132 }
133
134 private void ProcessInlineSchema()
135 {
137 {
138 return;
139 }
142 if (xdrSchema != null && xdrSchema.ErrorCount == 0)
143 {
144 foreach (string key in xdrSchema.TargetNamespaces.Keys)
145 {
147 {
148 schemaInfo.Add(xdrSchema, base.EventHandler);
149 base.SchemaCollection.Add(key, xdrSchema, null, compile: false);
150 break;
151 }
152 }
153 }
154 _inlineSchemaParser = null;
155 }
156
173
174 private void ValidateEndElement()
175 {
177 {
179 }
180 if (context.ElementDecl != null)
181 {
183 {
184 XmlSchemaValidator.CompleteValidationError(context, base.EventHandler, reader, reader.BaseURI, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition, null);
185 }
186 if (checkDatatype)
187 {
188 string value = ((!hasSibling) ? textString : textValue.ToString());
189 CheckValue(value, null);
190 checkDatatype = false;
192 textString = string.Empty;
193 }
194 }
195 Pop();
196 }
197
199 {
200 if (reader.Depth == 0)
201 {
202 LoadSchema(string.Empty);
203 }
205 {
206 do
207 {
209 string localName = reader.LocalName;
210 if (Ref.Equal(namespaceURI, base.SchemaNames.NsXmlNs))
211 {
214 {
215 _nsManager.AddNamespace((reader.Prefix.Length == 0) ? string.Empty : reader.LocalName, reader.Value);
216 }
217 }
218 if (Ref.Equal(namespaceURI, base.SchemaNames.QnDtDt.Namespace) && Ref.Equal(localName, base.SchemaNames.QnDtDt.Name))
219 {
221 }
222 }
223 while (reader.MoveToNextAttribute());
225 }
228 {
230 }
231 return elementDecl;
232 }
233
234 private void ValidateStartElement()
235 {
236 if (context.ElementDecl != null)
237 {
238 if (context.ElementDecl.SchemaType != null)
239 {
241 }
242 else
243 {
245 }
247 {
249 context.IsNill = true;
250 }
252 {
254 }
255 }
257 {
258 return;
259 }
260 do
261 {
262 if ((object)reader.NamespaceURI == base.SchemaNames.NsXmlNs)
263 {
264 continue;
265 }
266 try
267 {
270 if (attributeXdr != null)
271 {
273 {
275 }
276 reader.SchemaTypeObject = ((attributeXdr.SchemaType != null) ? ((object)attributeXdr.SchemaType) : ((object)attributeXdr.Datatype));
277 if (attributeXdr.Datatype != null)
278 {
279 string value = reader.Value;
281 }
282 }
283 }
284 catch (XmlSchemaException ex)
285 {
286 ex.SetSource(reader.BaseURI, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition);
288 }
289 }
290 while (reader.MoveToNextAttribute());
292 }
293
295 {
297 {
298 for (int i = 0; i < context.ElementDecl.DefaultAttDefs.Count; i++)
299 {
301 }
302 }
304 {
305 try
306 {
308 }
309 catch (XmlSchemaException ex)
310 {
311 ex.SetSource(reader.BaseURI, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition);
313 }
314 }
315 if (context.ElementDecl.Datatype != null)
316 {
317 checkDatatype = true;
318 hasSibling = false;
319 textString = string.Empty;
321 }
322 }
323
324 private void LoadSchemaFromLocation(string uri)
325 {
326 if (!XdrBuilder.IsXdrSchema(uri))
327 {
328 return;
329 }
330 string relativeUri = uri.Substring("x-schema:".Length);
331 XmlReader xmlReader = null;
332 SchemaInfo schemaInfo = null;
333 try
334 {
335 Uri uri2 = base.XmlResolver.ResolveUri(base.BaseUri, relativeUri);
336 Stream input = (Stream)base.XmlResolver.GetEntity(uri2, null, null);
337 xmlReader = new XmlTextReader(uri2.ToString(), input, base.NameTable);
338 ((XmlTextReader)xmlReader).XmlResolver = base.XmlResolver;
339 Parser parser = new Parser(SchemaType.XDR, base.NameTable, base.SchemaNames, base.EventHandler);
340 parser.XmlResolver = base.XmlResolver;
341 parser.Parse(xmlReader, uri);
342 while (xmlReader.Read())
343 {
344 }
345 schemaInfo = parser.XdrSchema;
346 }
347 catch (XmlSchemaException ex)
348 {
349 SendValidationEvent(System.SR.Sch_CannotLoadSchema, new string[2] { uri, ex.Message }, XmlSeverityType.Error);
350 }
351 catch (Exception ex2)
352 {
353 SendValidationEvent(System.SR.Sch_CannotLoadSchema, new string[2] { uri, ex2.Message }, XmlSeverityType.Warning);
354 }
355 finally
356 {
357 xmlReader?.Close();
358 }
359 if (schemaInfo != null && schemaInfo.ErrorCount == 0)
360 {
361 base.schemaInfo.Add(schemaInfo, base.EventHandler);
362 base.SchemaCollection.Add(uri, schemaInfo, null, compile: false);
363 }
364 }
365
366 private void LoadSchema(string uri)
367 {
368 if (base.schemaInfo.TargetNamespaces.ContainsKey(uri) || base.XmlResolver == null)
369 {
370 return;
371 }
372 SchemaInfo schemaInfo = null;
373 if (base.SchemaCollection != null)
374 {
375 schemaInfo = base.SchemaCollection.GetSchemaInfo(uri);
376 }
377 if (schemaInfo != null)
378 {
380 {
381 throw new XmlException(System.SR.Xml_MultipleValidaitonTypes, string.Empty, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition);
382 }
383 base.schemaInfo.Add(schemaInfo, base.EventHandler);
384 }
385 else
386 {
388 }
389 }
390
391 private void ProcessTokenizedType(XmlTokenizedType ttype, string name)
392 {
393 switch (ttype)
394 {
395 case XmlTokenizedType.ID:
396 if (FindId(name) != null)
397 {
399 }
400 else
401 {
402 AddID(name, context.LocalName);
403 }
404 break;
405 case XmlTokenizedType.IDREF:
406 {
407 object obj = FindId(name);
408 if (obj == null)
409 {
410 _idRefListHead = new IdRefNode(_idRefListHead, name, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition);
411 }
412 break;
413 }
414 case XmlTokenizedType.ENTITY:
415 BaseValidator.ProcessEntity(schemaInfo, name, this, base.EventHandler, reader.BaseURI, base.PositionInfo.LineNumber, base.PositionInfo.LinePosition);
416 break;
417 case XmlTokenizedType.IDREFS:
418 break;
419 }
420 }
421
422 public override void CompleteValidation()
423 {
424 if (HasSchema)
425 {
427 }
428 else
429 {
431 }
432 }
433
434 private void CheckValue(string value, SchemaAttDef attdef)
435 {
436 try
437 {
439 bool flag = attdef != null;
441 if (xmlSchemaDatatype == null)
442 {
443 return;
444 }
445 if (xmlSchemaDatatype.TokenizedType != 0)
446 {
447 value = value.Trim();
448 }
449 if (value.Length == 0)
450 {
451 return;
452 }
453 object obj = xmlSchemaDatatype.ParseValue(value, base.NameTable, _nsManager);
457 {
459 {
460 string[] array = (string[])obj;
461 for (int i = 0; i < array.Length; i++)
462 {
464 }
465 }
466 else
467 {
468 ProcessTokenizedType(xmlSchemaDatatype.TokenizedType, (string)obj);
469 }
470 }
472 if (schemaDeclBase.MaxLength != uint.MaxValue && value.Length > schemaDeclBase.MaxLength)
473 {
475 }
476 if (schemaDeclBase.MinLength != uint.MaxValue && value.Length < schemaDeclBase.MinLength)
477 {
479 }
480 if (schemaDeclBase.Values != null && !schemaDeclBase.CheckEnumeration(obj))
481 {
482 if (xmlSchemaDatatype.TokenizedType == XmlTokenizedType.NOTATION)
483 {
485 }
486 else
487 {
489 }
490 }
491 if (!schemaDeclBase.CheckValue(obj))
492 {
493 if (flag)
494 {
496 }
497 else
498 {
500 }
501 }
502 }
503 catch (XmlSchemaException)
504 {
505 if (attdef != null)
506 {
508 }
509 else
510 {
512 }
513 }
514 }
515
516 public static void CheckDefaultValue(string value, SchemaAttDef attdef, SchemaInfo sinfo, XmlNamespaceManager nsManager, XmlNameTable NameTable, object sender, ValidationEventHandler eventhandler, string baseUri, int lineNo, int linePos)
517 {
518 try
519 {
520 XmlSchemaDatatype datatype = attdef.Datatype;
521 if (datatype == null)
522 {
523 return;
524 }
525 if (datatype.TokenizedType != 0)
526 {
527 value = value.Trim();
528 }
529 if (value.Length == 0)
530 {
531 return;
532 }
533 object obj = datatype.ParseValue(value, NameTable, nsManager);
534 switch (datatype.TokenizedType)
535 {
536 case XmlTokenizedType.ENTITY:
537 if (datatype.Variety == XmlSchemaDatatypeVariety.List)
538 {
539 string[] array = (string[])obj;
540 for (int i = 0; i < array.Length; i++)
541 {
542 BaseValidator.ProcessEntity(sinfo, array[i], sender, eventhandler, baseUri, lineNo, linePos);
543 }
544 }
545 else
546 {
547 BaseValidator.ProcessEntity(sinfo, (string)obj, sender, eventhandler, baseUri, lineNo, linePos);
548 }
549 break;
550 case XmlTokenizedType.ENUMERATION:
551 if (!attdef.CheckEnumeration(obj))
552 {
553 XmlSchemaException ex = new XmlSchemaException(System.SR.Sch_EnumerationValue, obj.ToString(), baseUri, lineNo, linePos);
554 if (eventhandler == null)
555 {
556 throw ex;
557 }
558 eventhandler(sender, new ValidationEventArgs(ex));
559 }
560 break;
561 }
563 }
564 catch
565 {
566 XmlSchemaException ex2 = new XmlSchemaException(System.SR.Sch_AttributeDefaultDataType, attdef.Name.ToString(), baseUri, lineNo, linePos);
567 if (eventhandler != null)
568 {
570 return;
571 }
572 throw ex2;
573 }
574 }
575
576 internal void AddID(string name, object node)
577 {
578 if (_IDs == null)
579 {
580 _IDs = new Hashtable();
581 }
582 _IDs.Add(name, node);
583 }
584
585 public override object FindId(string name)
586 {
587 if (_IDs != null)
588 {
589 return _IDs[name];
590 }
591 return null;
592 }
593
608
609 private void Pop()
610 {
611 if (_validationStack.Length > 1)
612 {
615 }
616 }
617
618 private void CheckForwardRefs()
619 {
621 while (idRefNode != null)
622 {
623 if (FindId(idRefNode.Id) == null)
624 {
626 }
627 IdRefNode next = idRefNode.Next;
628 idRefNode.Next = null;
629 idRefNode = next;
630 }
631 _idRefListHead = null;
632 }
633
634 private XmlQualifiedName QualifiedName(string name, string ns)
635 {
636 return new XmlQualifiedName(name, XmlSchemaDatatype.XdrCanonizeUri(ns, base.NameTable, base.SchemaNames));
637 }
638}
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static string Xml_MultipleValidaitonTypes
Definition SR.cs:220
static string Sch_AttributeValueDataType
Definition SR.cs:1808
static string Sch_ElementValueDataType
Definition SR.cs:1810
static string Sch_FixedElementValue
Definition SR.cs:494
static string Sch_UndeclaredId
Definition SR.cs:418
static string Sch_MinLengthConstraintFailed
Definition SR.cs:688
static string Sch_FixedAttributeValue
Definition SR.cs:492
static string Sch_AttributeDefaultDataType
Definition SR.cs:498
static string Sch_UndeclaredElement
Definition SR.cs:412
static string Sch_CannotLoadSchema
Definition SR.cs:1078
static string Sch_EnumerationValue
Definition SR.cs:568
static string Sch_DupId
Definition SR.cs:410
static string Sch_MaxLengthConstraintFailed
Definition SR.cs:690
static string Sch_NotationValue
Definition SR.cs:566
static string Xml_NoValidation
Definition SR.cs:222
Definition SR.cs:7
override string ToString()
void AddToTop(object o)
Definition HWStack.cs:90
object Push()
Definition HWStack.cs:52
object Peek()
Definition HWStack.cs:81
static bool Equal(string strA, string strB)
Definition Ref.cs:5
void SendValidationEvent(string code)
XmlValidatingReaderImpl reader
static void ProcessEntity(SchemaInfo sinfo, string name, object sender, ValidationEventHandler eventhandler, string baseUri, int lineNumber, int linePosition)
virtual void InitValidation(ValidationState context)
virtual bool CompleteValidation(ValidationState context)
virtual object ValidateElement(XmlQualifiedName name, ValidationState context, out int errorCode)
SchemaInfo XdrSchema
Definition Parser.cs:56
SchemaType FinishParsing()
Definition Parser.cs:157
void StartParsing(XmlReader reader, string targetNamespace)
Definition Parser.cs:78
void CheckAttributes(Hashtable presence, bool standalone)
IList< IDtdDefaultAttributeInfo > DefaultAttDefs
SchemaElementDecl GetElementDecl(XmlQualifiedName qname)
SchemaAttDef GetAttributeXdr(SchemaElementDecl ed, XmlQualifiedName qname)
void Add(SchemaInfo sinfo, ValidationEventHandler eventhandler)
Dictionary< string, bool > TargetNamespaces
Definition SchemaInfo.cs:95
static bool IsXdrSchema(string uri)
void AddID(string name, object node)
void CheckValue(string value, SchemaAttDef attdef)
static void CheckDefaultValue(string value, SchemaAttDef attdef, SchemaInfo sinfo, XmlNamespaceManager nsManager, XmlNameTable NameTable, object sender, ValidationEventHandler eventhandler, string baseUri, int lineNo, int linePos)
void Push(XmlQualifiedName elementName)
void LoadSchemaFromLocation(string uri)
override object FindId(string name)
XmlQualifiedName QualifiedName(string name, string ns)
void ProcessTokenizedType(XmlTokenizedType ttype, string name)
XmlNamespaceManager _nsManager
XdrValidator(BaseValidator validator)
SchemaElementDecl ThoroughGetElementDecl()
XdrValidator(XmlValidatingReaderImpl reader, XmlSchemaCollection schemaCollection, IValidationEventHandling eventHandling)
static string XdrCanonizeUri(string uri, XmlNameTable nameTable, SchemaNames schemaNames)
object ParseValue(string s, XmlNameTable? nameTable, IXmlNamespaceResolver? nsmgr)
virtual XmlSchemaDatatypeVariety Variety
static XmlSchemaDatatype FromXdrName(string name)
static void ElementValidationError(XmlQualifiedName name, ValidationState context, ValidationEventHandler eventHandler, object sender, string sourceUri, int lineNo, int linePos, XmlSchemaSet schemaSet)
static void CompleteValidationError(ValidationState context, ValidationEventHandler eventHandler, object sender, string sourceUri, int lineNo, int linePos, XmlSchemaSet schemaSet)
static string QNameString(string localName, string ns)
virtual void AddNamespace(string prefix, string uri)
static readonly XmlQualifiedName Empty
void Init(string name, string ns)
bool AddDefaultAttribute(SchemaAttDef attdef)
override XmlNamespaceManager NamespaceManager