mscorlib(4.0.0.0) API with additions
XmlSchemaSet.cs
1 using System.Collections;
3 using System.Threading;
4 
5 namespace System.Xml.Schema
6 {
8  public class XmlSchemaSet
9  {
10  private XmlNameTable nameTable;
11 
12  private SchemaNames schemaNames;
13 
14  private SortedList schemas;
15 
16  private ValidationEventHandler internalEventHandler;
17 
18  private ValidationEventHandler eventHandler;
19 
20  private bool isCompiled;
21 
22  private Hashtable schemaLocations;
23 
24  private Hashtable chameleonSchemas;
25 
26  private Hashtable targetNamespaces;
27 
28  private bool compileAll;
29 
30  private SchemaInfo cachedCompiledInfo;
31 
32  private XmlReaderSettings readerSettings;
33 
34  private XmlSchema schemaForSchema;
35 
36  private XmlSchemaCompilationSettings compilationSettings;
37 
38  internal XmlSchemaObjectTable elements;
39 
40  internal XmlSchemaObjectTable attributes;
41 
42  internal XmlSchemaObjectTable schemaTypes;
43 
44  internal XmlSchemaObjectTable substitutionGroups;
45 
46  private XmlSchemaObjectTable typeExtensions;
47 
48  private object internalSyncObject;
49 
50  internal object InternalSyncObject
51  {
52  get
53  {
54  if (internalSyncObject == null)
55  {
56  object value = new object();
57  Interlocked.CompareExchange<object>(ref internalSyncObject, value, (object)null);
58  }
59  return internalSyncObject;
60  }
61  }
62 
65  public XmlNameTable NameTable => nameTable;
66 
70  public bool IsCompiled => isCompiled;
71 
75  {
76  set
77  {
78  readerSettings.XmlResolver = value;
79  }
80  }
81 
85  {
86  get
87  {
88  return compilationSettings;
89  }
90  set
91  {
92  compilationSettings = value;
93  }
94  }
95 
98  public int Count => schemas.Count;
99 
103  {
104  get
105  {
106  if (elements == null)
107  {
108  elements = new XmlSchemaObjectTable();
109  }
110  return elements;
111  }
112  }
113 
117  {
118  get
119  {
120  if (attributes == null)
121  {
122  attributes = new XmlSchemaObjectTable();
123  }
124  return attributes;
125  }
126  }
127 
131  {
132  get
133  {
134  if (schemaTypes == null)
135  {
136  schemaTypes = new XmlSchemaObjectTable();
137  }
138  return schemaTypes;
139  }
140  }
141 
142  internal XmlSchemaObjectTable SubstitutionGroups
143  {
144  get
145  {
146  if (substitutionGroups == null)
147  {
148  substitutionGroups = new XmlSchemaObjectTable();
149  }
150  return substitutionGroups;
151  }
152  }
153 
154  internal Hashtable SchemaLocations => schemaLocations;
155 
156  internal XmlSchemaObjectTable TypeExtensions
157  {
158  get
159  {
160  if (typeExtensions == null)
161  {
162  typeExtensions = new XmlSchemaObjectTable();
163  }
164  return typeExtensions;
165  }
166  }
167 
168  internal SchemaInfo CompiledInfo => cachedCompiledInfo;
169 
170  internal XmlReaderSettings ReaderSettings => readerSettings;
171 
172  internal SortedList SortedSchemas => schemas;
173 
174  internal bool CompileAll => compileAll;
175 
178  {
179  add
180  {
181  eventHandler = (ValidationEventHandler)Delegate.Remove(eventHandler, internalEventHandler);
182  eventHandler = (ValidationEventHandler)Delegate.Combine(eventHandler, value);
183  if (eventHandler == null)
184  {
185  eventHandler = internalEventHandler;
186  }
187  }
188  remove
189  {
190  eventHandler = (ValidationEventHandler)Delegate.Remove(eventHandler, value);
191  if (eventHandler == null)
192  {
193  eventHandler = internalEventHandler;
194  }
195  }
196  }
197 
199  public XmlSchemaSet()
200  : this(new NameTable())
201  {
202  }
203 
207  public XmlSchemaSet(XmlNameTable nameTable)
208  {
209  if (nameTable == null)
210  {
211  throw new ArgumentNullException("nameTable");
212  }
213  this.nameTable = nameTable;
214  schemas = new SortedList();
215  schemaLocations = new Hashtable();
216  chameleonSchemas = new Hashtable();
217  targetNamespaces = new Hashtable();
218  internalEventHandler = InternalValidationCallback;
219  eventHandler = internalEventHandler;
220  readerSettings = new XmlReaderSettings();
221  if (readerSettings.GetXmlResolver() == null)
222  {
223  readerSettings.XmlResolver = new XmlUrlResolver();
224  readerSettings.IsXmlResolverSet = false;
225  }
226  readerSettings.NameTable = nameTable;
227  readerSettings.DtdProcessing = DtdProcessing.Prohibit;
228  compilationSettings = new XmlSchemaCompilationSettings();
229  cachedCompiledInfo = new SchemaInfo();
230  compileAll = true;
231  }
232 
239  public XmlSchema Add(string targetNamespace, string schemaUri)
240  {
241  if (schemaUri == null || schemaUri.Length == 0)
242  {
243  throw new ArgumentNullException("schemaUri");
244  }
245  if (targetNamespace != null)
246  {
247  targetNamespace = XmlComplianceUtil.CDataNormalize(targetNamespace);
248  }
249  XmlSchema schema = null;
250  lock (InternalSyncObject)
251  {
252  XmlResolver xmlResolver = readerSettings.GetXmlResolver();
253  if (xmlResolver == null)
254  {
255  xmlResolver = new XmlUrlResolver();
256  }
257  Uri schemaUri2 = xmlResolver.ResolveUri(null, schemaUri);
258  if (!IsSchemaLoaded(schemaUri2, targetNamespace, out schema))
259  {
260  XmlReader xmlReader = XmlReader.Create(schemaUri, readerSettings);
261  try
262  {
263  schema = Add(targetNamespace, ParseSchema(targetNamespace, xmlReader));
264  while (xmlReader.Read())
265  {
266  }
267  return schema;
268  }
269  finally
270  {
271  xmlReader.Close();
272  }
273  }
274  return schema;
275  }
276  }
277 
284  public XmlSchema Add(string targetNamespace, XmlReader schemaDocument)
285  {
286  if (schemaDocument == null)
287  {
288  throw new ArgumentNullException("schemaDocument");
289  }
290  if (targetNamespace != null)
291  {
292  targetNamespace = XmlComplianceUtil.CDataNormalize(targetNamespace);
293  }
294  lock (InternalSyncObject)
295  {
296  XmlSchema schema = null;
297  Uri schemaUri = new Uri(schemaDocument.BaseURI, UriKind.RelativeOrAbsolute);
298  if (IsSchemaLoaded(schemaUri, targetNamespace, out schema))
299  {
300  return schema;
301  }
302  DtdProcessing dtdProcessing = readerSettings.DtdProcessing;
303  SetDtdProcessing(schemaDocument);
304  schema = Add(targetNamespace, ParseSchema(targetNamespace, schemaDocument));
305  readerSettings.DtdProcessing = dtdProcessing;
306  return schema;
307  }
308  }
309 
314  public void Add(XmlSchemaSet schemas)
315  {
316  if (schemas == null)
317  {
318  throw new ArgumentNullException("schemas");
319  }
320  if (this != schemas)
321  {
322  bool lockTaken = false;
323  bool lockTaken2 = false;
324  try
325  {
326  while (true)
327  {
328  Monitor.TryEnter(InternalSyncObject, ref lockTaken);
329  if (lockTaken)
330  {
331  Monitor.TryEnter(schemas.InternalSyncObject, ref lockTaken2);
332  if (lockTaken2)
333  {
334  break;
335  }
336  Monitor.Exit(InternalSyncObject);
337  lockTaken = false;
338  Thread.Yield();
339  }
340  }
341  if (schemas.IsCompiled)
342  {
343  CopyFromCompiledSet(schemas);
344  }
345  else
346  {
347  bool flag = false;
348  string text = null;
349  foreach (XmlSchema value in schemas.SortedSchemas.Values)
350  {
351  text = value.TargetNamespace;
352  if (text == null)
353  {
354  text = string.Empty;
355  }
356  if (!this.schemas.ContainsKey(value.SchemaId) && FindSchemaByNSAndUrl(value.BaseUri, text, null) == null)
357  {
358  XmlSchema xmlSchema2 = Add(value.TargetNamespace, value);
359  if (xmlSchema2 == null)
360  {
361  flag = true;
362  break;
363  }
364  }
365  }
366  if (flag)
367  {
368  foreach (XmlSchema value2 in schemas.SortedSchemas.Values)
369  {
370  this.schemas.Remove(value2.SchemaId);
371  schemaLocations.Remove(value2.BaseUri);
372  }
373  }
374  }
375  }
376  finally
377  {
378  if (lockTaken)
379  {
380  Monitor.Exit(InternalSyncObject);
381  }
382  if (lockTaken2)
383  {
384  Monitor.Exit(schemas.InternalSyncObject);
385  }
386  }
387  }
388  }
389 
395  public XmlSchema Add(XmlSchema schema)
396  {
397  if (schema == null)
398  {
399  throw new ArgumentNullException("schema");
400  }
401  lock (InternalSyncObject)
402  {
403  if (schemas.ContainsKey(schema.SchemaId))
404  {
405  return schema;
406  }
407  return Add(schema.TargetNamespace, schema);
408  }
409  }
410 
416  public XmlSchema Remove(XmlSchema schema)
417  {
418  return Remove(schema, forceCompile: true);
419  }
420 
426  public bool RemoveRecursive(XmlSchema schemaToRemove)
427  {
428  if (schemaToRemove == null)
429  {
430  throw new ArgumentNullException("schemaToRemove");
431  }
432  if (!schemas.ContainsKey(schemaToRemove.SchemaId))
433  {
434  return false;
435  }
436  lock (InternalSyncObject)
437  {
438  if (schemas.ContainsKey(schemaToRemove.SchemaId))
439  {
440  Hashtable hashtable = new Hashtable();
441  hashtable.Add(GetTargetNamespace(schemaToRemove), schemaToRemove);
442  for (int i = 0; i < schemaToRemove.ImportedNamespaces.Count; i++)
443  {
444  string text = (string)schemaToRemove.ImportedNamespaces[i];
445  if (hashtable[text] == null)
446  {
447  hashtable.Add(text, text);
448  }
449  }
450  ArrayList arrayList = new ArrayList();
451  XmlSchema xmlSchema;
452  for (int j = 0; j < schemas.Count; j++)
453  {
454  xmlSchema = (XmlSchema)schemas.GetByIndex(j);
455  if (xmlSchema != schemaToRemove && !schemaToRemove.ImportedSchemas.Contains(xmlSchema))
456  {
457  arrayList.Add(xmlSchema);
458  }
459  }
460  xmlSchema = null;
461  for (int k = 0; k < arrayList.Count; k++)
462  {
463  xmlSchema = (XmlSchema)arrayList[k];
464  if (xmlSchema.ImportedNamespaces.Count > 0)
465  {
466  foreach (string key in hashtable.Keys)
467  {
468  if (xmlSchema.ImportedNamespaces.Contains(key))
469  {
470  SendValidationEvent(new XmlSchemaException("Sch_SchemaNotRemoved", string.Empty), XmlSeverityType.Warning);
471  return false;
472  }
473  }
474  }
475  }
476  Remove(schemaToRemove, forceCompile: true);
477  for (int l = 0; l < schemaToRemove.ImportedSchemas.Count; l++)
478  {
479  XmlSchema schema = (XmlSchema)schemaToRemove.ImportedSchemas[l];
480  Remove(schema, forceCompile: true);
481  }
482  return true;
483  }
484  }
485  return false;
486  }
487 
492  public bool Contains(string targetNamespace)
493  {
494  if (targetNamespace == null)
495  {
496  targetNamespace = string.Empty;
497  }
498  return targetNamespaces[targetNamespace] != null;
499  }
500 
506  public bool Contains(XmlSchema schema)
507  {
508  if (schema == null)
509  {
510  throw new ArgumentNullException("schema");
511  }
512  return schemas.ContainsValue(schema);
513  }
514 
517  public void Compile()
518  {
519  if (!isCompiled)
520  {
521  if (schemas.Count == 0)
522  {
523  ClearTables();
524  cachedCompiledInfo = new SchemaInfo();
525  isCompiled = true;
526  compileAll = false;
527  }
528  else
529  {
530  lock (InternalSyncObject)
531  {
532  if (!isCompiled)
533  {
534  Compiler compiler = new Compiler(nameTable, eventHandler, schemaForSchema, compilationSettings);
535  SchemaInfo schemaInfo = new SchemaInfo();
536  int i = 0;
537  if (!compileAll)
538  {
539  compiler.ImportAllCompiledSchemas(this);
540  }
541  try
542  {
543  XmlSchema buildInSchema = Preprocessor.GetBuildInSchema();
544  for (i = 0; i < schemas.Count; i++)
545  {
546  XmlSchema xmlSchema = (XmlSchema)schemas.GetByIndex(i);
547  Monitor.Enter(xmlSchema);
548  if (!xmlSchema.IsPreprocessed)
549  {
550  SendValidationEvent(new XmlSchemaException("Sch_SchemaNotPreprocessed", string.Empty), XmlSeverityType.Error);
551  isCompiled = false;
552  return;
553  }
554  if (xmlSchema.IsCompiledBySet)
555  {
556  if (!compileAll)
557  {
558  continue;
559  }
560  if (xmlSchema == buildInSchema)
561  {
562  compiler.Prepare(xmlSchema, cleanup: false);
563  continue;
564  }
565  }
566  compiler.Prepare(xmlSchema, cleanup: true);
567  }
568  isCompiled = compiler.Execute(this, schemaInfo);
569  if (isCompiled)
570  {
571  if (!compileAll)
572  {
573  schemaInfo.Add(cachedCompiledInfo, eventHandler);
574  }
575  compileAll = false;
576  cachedCompiledInfo = schemaInfo;
577  }
578  }
579  finally
580  {
581  if (i == schemas.Count)
582  {
583  i--;
584  }
585  for (int num = i; num >= 0; num--)
586  {
587  XmlSchema xmlSchema2 = (XmlSchema)schemas.GetByIndex(num);
588  if (xmlSchema2 == Preprocessor.GetBuildInSchema())
589  {
590  Monitor.Exit(xmlSchema2);
591  }
592  else
593  {
594  xmlSchema2.IsCompiledBySet = isCompiled;
595  Monitor.Exit(xmlSchema2);
596  }
597  }
598  }
599  }
600  }
601  }
602  }
603  }
604 
612  {
613  if (schema == null)
614  {
615  throw new ArgumentNullException("schema");
616  }
617  if (!schemas.ContainsKey(schema.SchemaId))
618  {
619  throw new ArgumentException(Res.GetString("Sch_SchemaDoesNotExist"), "schema");
620  }
621  XmlSchema result = schema;
622  lock (InternalSyncObject)
623  {
624  RemoveSchemaFromGlobalTables(schema);
625  RemoveSchemaFromCaches(schema);
626  if (schema.BaseUri != null)
627  {
628  schemaLocations.Remove(schema.BaseUri);
629  }
630  string targetNamespace = GetTargetNamespace(schema);
631  if (Schemas(targetNamespace).Count == 0)
632  {
633  targetNamespaces.Remove(targetNamespace);
634  }
635  isCompiled = false;
636  compileAll = true;
637  if (schema.ErrorCount != 0)
638  {
639  return result;
640  }
641  if (PreprocessSchema(ref schema, schema.TargetNamespace))
642  {
643  if (targetNamespaces[targetNamespace] == null)
644  {
645  targetNamespaces.Add(targetNamespace, targetNamespace);
646  }
647  if (schemaForSchema == null && targetNamespace == "http://www.w3.org/2001/XMLSchema" && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null)
648  {
649  schemaForSchema = schema;
650  }
651  for (int i = 0; i < schema.ImportedSchemas.Count; i++)
652  {
653  XmlSchema xmlSchema = (XmlSchema)schema.ImportedSchemas[i];
654  if (!schemas.ContainsKey(xmlSchema.SchemaId))
655  {
656  schemas.Add(xmlSchema.SchemaId, xmlSchema);
657  }
658  targetNamespace = GetTargetNamespace(xmlSchema);
659  if (targetNamespaces[targetNamespace] == null)
660  {
661  targetNamespaces.Add(targetNamespace, targetNamespace);
662  }
663  if (schemaForSchema == null && targetNamespace == "http://www.w3.org/2001/XMLSchema" && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null)
664  {
665  schemaForSchema = schema;
666  }
667  }
668  return schema;
669  }
670  return result;
671  }
672  }
673 
677  public void CopyTo(XmlSchema[] schemas, int index)
678  {
679  if (schemas == null)
680  {
681  throw new ArgumentNullException("schemas");
682  }
683  if (index < 0 || index > schemas.Length - 1)
684  {
685  throw new ArgumentOutOfRangeException("index");
686  }
687  this.schemas.Values.CopyTo(schemas, index);
688  }
689 
693  {
694  return schemas.Values;
695  }
696 
700  public ICollection Schemas(string targetNamespace)
701  {
702  ArrayList arrayList = new ArrayList();
703  if (targetNamespace == null)
704  {
705  targetNamespace = string.Empty;
706  }
707  for (int i = 0; i < schemas.Count; i++)
708  {
709  XmlSchema xmlSchema = (XmlSchema)schemas.GetByIndex(i);
710  if (GetTargetNamespace(xmlSchema) == targetNamespace)
711  {
712  arrayList.Add(xmlSchema);
713  }
714  }
715  return arrayList;
716  }
717 
718  private XmlSchema Add(string targetNamespace, XmlSchema schema)
719  {
720  if (schema == null || schema.ErrorCount != 0)
721  {
722  return null;
723  }
724  if (PreprocessSchema(ref schema, targetNamespace))
725  {
726  AddSchemaToSet(schema);
727  isCompiled = false;
728  return schema;
729  }
730  return null;
731  }
732 
733  internal void Add(string targetNamespace, XmlReader reader, Hashtable validatedNamespaces)
734  {
735  if (reader == null)
736  {
737  throw new ArgumentNullException("reader");
738  }
739  if (targetNamespace == null)
740  {
741  targetNamespace = string.Empty;
742  }
743  if (validatedNamespaces[targetNamespace] != null)
744  {
745  if (FindSchemaByNSAndUrl(new Uri(reader.BaseURI, UriKind.RelativeOrAbsolute), targetNamespace, null) == null)
746  {
747  throw new XmlSchemaException("Sch_ComponentAlreadySeenForNS", targetNamespace);
748  }
749  }
750  else
751  {
752  if (IsSchemaLoaded(new Uri(reader.BaseURI, UriKind.RelativeOrAbsolute), targetNamespace, out XmlSchema schema))
753  {
754  return;
755  }
756  schema = ParseSchema(targetNamespace, reader);
757  DictionaryEntry[] array = new DictionaryEntry[schemaLocations.Count];
758  schemaLocations.CopyTo(array, 0);
759  Add(targetNamespace, schema);
760  if (schema.ImportedSchemas.Count <= 0)
761  {
762  return;
763  }
764  int num = 0;
765  string text;
766  while (true)
767  {
768  if (num < schema.ImportedSchemas.Count)
769  {
770  XmlSchema xmlSchema = (XmlSchema)schema.ImportedSchemas[num];
771  text = xmlSchema.TargetNamespace;
772  if (text == null)
773  {
774  text = string.Empty;
775  }
776  if (validatedNamespaces[text] != null && FindSchemaByNSAndUrl(xmlSchema.BaseUri, text, array) == null)
777  {
778  break;
779  }
780  num++;
781  continue;
782  }
783  return;
784  }
785  RemoveRecursive(schema);
786  throw new XmlSchemaException("Sch_ComponentAlreadySeenForNS", text);
787  }
788  }
789 
790  internal XmlSchema FindSchemaByNSAndUrl(Uri schemaUri, string ns, DictionaryEntry[] locationsTable)
791  {
792  if (schemaUri == null || schemaUri.OriginalString.Length == 0)
793  {
794  return null;
795  }
796  XmlSchema xmlSchema = null;
797  if (locationsTable == null)
798  {
799  xmlSchema = (XmlSchema)schemaLocations[schemaUri];
800  }
801  else
802  {
803  for (int i = 0; i < locationsTable.Length; i++)
804  {
805  if (schemaUri.Equals(locationsTable[i].Key))
806  {
807  xmlSchema = (XmlSchema)locationsTable[i].Value;
808  break;
809  }
810  }
811  }
812  if (xmlSchema != null)
813  {
814  string a = (xmlSchema.TargetNamespace == null) ? string.Empty : xmlSchema.TargetNamespace;
815  if (a == ns)
816  {
817  return xmlSchema;
818  }
819  if (a == string.Empty)
820  {
821  ChameleonKey key = new ChameleonKey(ns, xmlSchema);
822  xmlSchema = (XmlSchema)chameleonSchemas[key];
823  }
824  else
825  {
826  xmlSchema = null;
827  }
828  }
829  return xmlSchema;
830  }
831 
832  private void SetDtdProcessing(XmlReader reader)
833  {
834  if (reader.Settings != null)
835  {
836  readerSettings.DtdProcessing = reader.Settings.DtdProcessing;
837  return;
838  }
839  XmlTextReader xmlTextReader = reader as XmlTextReader;
840  if (xmlTextReader != null)
841  {
842  readerSettings.DtdProcessing = xmlTextReader.DtdProcessing;
843  }
844  }
845 
846  private void AddSchemaToSet(XmlSchema schema)
847  {
848  schemas.Add(schema.SchemaId, schema);
849  string targetNamespace = GetTargetNamespace(schema);
850  if (targetNamespaces[targetNamespace] == null)
851  {
852  targetNamespaces.Add(targetNamespace, targetNamespace);
853  }
854  if (schemaForSchema == null && targetNamespace == "http://www.w3.org/2001/XMLSchema" && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null)
855  {
856  schemaForSchema = schema;
857  }
858  for (int i = 0; i < schema.ImportedSchemas.Count; i++)
859  {
860  XmlSchema xmlSchema = (XmlSchema)schema.ImportedSchemas[i];
861  if (!schemas.ContainsKey(xmlSchema.SchemaId))
862  {
863  schemas.Add(xmlSchema.SchemaId, xmlSchema);
864  }
865  targetNamespace = GetTargetNamespace(xmlSchema);
866  if (targetNamespaces[targetNamespace] == null)
867  {
868  targetNamespaces.Add(targetNamespace, targetNamespace);
869  }
870  if (schemaForSchema == null && targetNamespace == "http://www.w3.org/2001/XMLSchema" && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null)
871  {
872  schemaForSchema = schema;
873  }
874  }
875  }
876 
877  private void ProcessNewSubstitutionGroups(XmlSchemaObjectTable substitutionGroupsTable, bool resolve)
878  {
879  foreach (XmlSchemaSubstitutionGroup value in substitutionGroupsTable.Values)
880  {
881  if (resolve)
882  {
883  ResolveSubstitutionGroup(value, substitutionGroupsTable);
884  }
885  XmlQualifiedName examplar = value.Examplar;
886  XmlSchemaSubstitutionGroup xmlSchemaSubstitutionGroup2 = (XmlSchemaSubstitutionGroup)substitutionGroups[examplar];
887  if (xmlSchemaSubstitutionGroup2 != null)
888  {
889  for (int i = 0; i < value.Members.Count; i++)
890  {
891  if (!xmlSchemaSubstitutionGroup2.Members.Contains(value.Members[i]))
892  {
893  xmlSchemaSubstitutionGroup2.Members.Add(value.Members[i]);
894  }
895  }
896  }
897  else
898  {
899  AddToTable(substitutionGroups, examplar, value);
900  }
901  }
902  }
903 
904  private void ResolveSubstitutionGroup(XmlSchemaSubstitutionGroup substitutionGroup, XmlSchemaObjectTable substTable)
905  {
906  List<XmlSchemaElement> list = null;
907  XmlSchemaElement xmlSchemaElement = (XmlSchemaElement)elements[substitutionGroup.Examplar];
908  if (substitutionGroup.Members.Contains(xmlSchemaElement))
909  {
910  return;
911  }
912  for (int i = 0; i < substitutionGroup.Members.Count; i++)
913  {
914  XmlSchemaElement xmlSchemaElement2 = (XmlSchemaElement)substitutionGroup.Members[i];
915  XmlSchemaSubstitutionGroup xmlSchemaSubstitutionGroup = (XmlSchemaSubstitutionGroup)substTable[xmlSchemaElement2.QualifiedName];
916  if (xmlSchemaSubstitutionGroup == null)
917  {
918  continue;
919  }
920  ResolveSubstitutionGroup(xmlSchemaSubstitutionGroup, substTable);
921  for (int j = 0; j < xmlSchemaSubstitutionGroup.Members.Count; j++)
922  {
923  XmlSchemaElement xmlSchemaElement3 = (XmlSchemaElement)xmlSchemaSubstitutionGroup.Members[j];
924  if (xmlSchemaElement3 != xmlSchemaElement2)
925  {
926  if (list == null)
927  {
928  list = new List<XmlSchemaElement>();
929  }
930  list.Add(xmlSchemaElement3);
931  }
932  }
933  }
934  if (list != null)
935  {
936  for (int k = 0; k < list.Count; k++)
937  {
938  substitutionGroup.Members.Add(list[k]);
939  }
940  }
941  substitutionGroup.Members.Add(xmlSchemaElement);
942  }
943 
944  internal XmlSchema Remove(XmlSchema schema, bool forceCompile)
945  {
946  if (schema == null)
947  {
948  throw new ArgumentNullException("schema");
949  }
950  lock (InternalSyncObject)
951  {
952  if (schemas.ContainsKey(schema.SchemaId))
953  {
954  if (forceCompile)
955  {
956  RemoveSchemaFromGlobalTables(schema);
957  RemoveSchemaFromCaches(schema);
958  }
959  schemas.Remove(schema.SchemaId);
960  if (schema.BaseUri != null)
961  {
962  schemaLocations.Remove(schema.BaseUri);
963  }
964  string targetNamespace = GetTargetNamespace(schema);
965  if (Schemas(targetNamespace).Count == 0)
966  {
967  targetNamespaces.Remove(targetNamespace);
968  }
969  if (forceCompile)
970  {
971  isCompiled = false;
972  compileAll = true;
973  }
974  return schema;
975  }
976  }
977  return null;
978  }
979 
980  private void ClearTables()
981  {
982  GlobalElements.Clear();
983  GlobalAttributes.Clear();
984  GlobalTypes.Clear();
985  SubstitutionGroups.Clear();
986  TypeExtensions.Clear();
987  }
988 
989  internal bool PreprocessSchema(ref XmlSchema schema, string targetNamespace)
990  {
991  Preprocessor preprocessor = new Preprocessor(nameTable, GetSchemaNames(nameTable), eventHandler, compilationSettings);
992  preprocessor.XmlResolver = readerSettings.GetXmlResolver_CheckConfig();
993  preprocessor.ReaderSettings = readerSettings;
994  preprocessor.SchemaLocations = schemaLocations;
995  preprocessor.ChameleonSchemas = chameleonSchemas;
996  bool result = preprocessor.Execute(schema, targetNamespace, loadExternals: true);
997  schema = preprocessor.RootSchema;
998  return result;
999  }
1000 
1001  internal XmlSchema ParseSchema(string targetNamespace, XmlReader reader)
1002  {
1003  XmlNameTable nt = reader.NameTable;
1004  SchemaNames schemaNames = GetSchemaNames(nt);
1005  Parser parser = new Parser(SchemaType.XSD, nt, schemaNames, eventHandler);
1006  parser.XmlResolver = readerSettings.GetXmlResolver_CheckConfig();
1007  try
1008  {
1009  SchemaType schemaType = parser.Parse(reader, targetNamespace);
1010  }
1011  catch (XmlSchemaException e)
1012  {
1013  SendValidationEvent(e, XmlSeverityType.Error);
1014  return null;
1015  }
1016  return parser.XmlSchema;
1017  }
1018 
1019  internal void CopyFromCompiledSet(XmlSchemaSet otherSet)
1020  {
1021  SortedList sortedSchemas = otherSet.SortedSchemas;
1022  bool flag = (schemas.Count == 0) ? true : false;
1023  ArrayList arrayList = new ArrayList();
1024  SchemaInfo schemaInfo = new SchemaInfo();
1025  for (int i = 0; i < sortedSchemas.Count; i++)
1026  {
1027  XmlSchema xmlSchema = (XmlSchema)sortedSchemas.GetByIndex(i);
1028  Uri baseUri = xmlSchema.BaseUri;
1029  if (schemas.ContainsKey(xmlSchema.SchemaId) || (baseUri != null && baseUri.OriginalString.Length != 0 && schemaLocations[baseUri] != null))
1030  {
1031  arrayList.Add(xmlSchema);
1032  continue;
1033  }
1034  schemas.Add(xmlSchema.SchemaId, xmlSchema);
1035  if (baseUri != null && baseUri.OriginalString.Length != 0)
1036  {
1037  schemaLocations.Add(baseUri, xmlSchema);
1038  }
1039  string targetNamespace = GetTargetNamespace(xmlSchema);
1040  if (targetNamespaces[targetNamespace] == null)
1041  {
1042  targetNamespaces.Add(targetNamespace, targetNamespace);
1043  }
1044  }
1045  VerifyTables();
1046  foreach (XmlSchemaElement value in otherSet.GlobalElements.Values)
1047  {
1048  if (AddToTable(elements, value.QualifiedName, value))
1049  {
1050  continue;
1051  }
1052  goto IL_026e;
1053  }
1054  foreach (XmlSchemaAttribute value2 in otherSet.GlobalAttributes.Values)
1055  {
1056  if (AddToTable(attributes, value2.QualifiedName, value2))
1057  {
1058  continue;
1059  }
1060  goto IL_026e;
1061  }
1062  foreach (XmlSchemaType value3 in otherSet.GlobalTypes.Values)
1063  {
1064  if (AddToTable(schemaTypes, value3.QualifiedName, value3))
1065  {
1066  continue;
1067  }
1068  goto IL_026e;
1069  }
1070  ProcessNewSubstitutionGroups(otherSet.SubstitutionGroups, resolve: false);
1071  schemaInfo.Add(cachedCompiledInfo, eventHandler);
1072  schemaInfo.Add(otherSet.CompiledInfo, eventHandler);
1073  cachedCompiledInfo = schemaInfo;
1074  if (flag)
1075  {
1076  isCompiled = true;
1077  compileAll = false;
1078  }
1079  return;
1080  IL_026e:
1081  foreach (XmlSchema value4 in sortedSchemas.Values)
1082  {
1083  if (!arrayList.Contains(value4))
1084  {
1085  Remove(value4, forceCompile: false);
1086  }
1087  }
1088  foreach (XmlSchemaElement value5 in otherSet.GlobalElements.Values)
1089  {
1090  if (!arrayList.Contains((XmlSchema)value5.Parent))
1091  {
1092  elements.Remove(value5.QualifiedName);
1093  }
1094  }
1095  foreach (XmlSchemaAttribute value6 in otherSet.GlobalAttributes.Values)
1096  {
1097  if (!arrayList.Contains((XmlSchema)value6.Parent))
1098  {
1099  attributes.Remove(value6.QualifiedName);
1100  }
1101  }
1102  foreach (XmlSchemaType value7 in otherSet.GlobalTypes.Values)
1103  {
1104  if (!arrayList.Contains((XmlSchema)value7.Parent))
1105  {
1106  schemaTypes.Remove(value7.QualifiedName);
1107  }
1108  }
1109  }
1110 
1111  internal XmlResolver GetResolver()
1112  {
1113  return readerSettings.GetXmlResolver_CheckConfig();
1114  }
1115 
1116  internal ValidationEventHandler GetEventHandler()
1117  {
1118  return eventHandler;
1119  }
1120 
1121  internal SchemaNames GetSchemaNames(XmlNameTable nt)
1122  {
1123  if (nameTable != nt)
1124  {
1125  return new SchemaNames(nt);
1126  }
1127  if (schemaNames == null)
1128  {
1129  schemaNames = new SchemaNames(nameTable);
1130  }
1131  return schemaNames;
1132  }
1133 
1134  internal bool IsSchemaLoaded(Uri schemaUri, string targetNamespace, out XmlSchema schema)
1135  {
1136  schema = null;
1137  if (targetNamespace == null)
1138  {
1139  targetNamespace = string.Empty;
1140  }
1141  if (GetSchemaByUri(schemaUri, out schema))
1142  {
1143  if (!schemas.ContainsKey(schema.SchemaId) || (targetNamespace.Length != 0 && !(targetNamespace == schema.TargetNamespace)))
1144  {
1145  if (schema.TargetNamespace == null)
1146  {
1147  XmlSchema xmlSchema = FindSchemaByNSAndUrl(schemaUri, targetNamespace, null);
1148  if (xmlSchema != null && schemas.ContainsKey(xmlSchema.SchemaId))
1149  {
1150  schema = xmlSchema;
1151  }
1152  else
1153  {
1154  schema = Add(targetNamespace, schema);
1155  }
1156  }
1157  else if (targetNamespace.Length != 0 && targetNamespace != schema.TargetNamespace)
1158  {
1159  SendValidationEvent(new XmlSchemaException("Sch_MismatchTargetNamespaceEx", new string[2]
1160  {
1161  targetNamespace,
1162  schema.TargetNamespace
1163  }), XmlSeverityType.Error);
1164  schema = null;
1165  }
1166  else
1167  {
1168  AddSchemaToSet(schema);
1169  }
1170  }
1171  return true;
1172  }
1173  return false;
1174  }
1175 
1176  internal bool GetSchemaByUri(Uri schemaUri, out XmlSchema schema)
1177  {
1178  schema = null;
1179  if (schemaUri == null || schemaUri.OriginalString.Length == 0)
1180  {
1181  return false;
1182  }
1183  schema = (XmlSchema)schemaLocations[schemaUri];
1184  if (schema != null)
1185  {
1186  return true;
1187  }
1188  return false;
1189  }
1190 
1191  internal string GetTargetNamespace(XmlSchema schema)
1192  {
1193  if (schema.TargetNamespace != null)
1194  {
1195  return schema.TargetNamespace;
1196  }
1197  return string.Empty;
1198  }
1199 
1200  private void RemoveSchemaFromCaches(XmlSchema schema)
1201  {
1202  List<XmlSchema> list = new List<XmlSchema>();
1203  schema.GetExternalSchemasList(list, schema);
1204  for (int i = 0; i < list.Count; i++)
1205  {
1206  if (list[i].BaseUri != null && list[i].BaseUri.OriginalString.Length != 0)
1207  {
1208  schemaLocations.Remove(list[i].BaseUri);
1209  }
1210  ICollection keys = chameleonSchemas.Keys;
1211  ArrayList arrayList = new ArrayList();
1212  foreach (ChameleonKey item in keys)
1213  {
1214  if (item.chameleonLocation.Equals(list[i].BaseUri) && (item.originalSchema == null || item.originalSchema == list[i]))
1215  {
1216  arrayList.Add(item);
1217  }
1218  }
1219  for (int j = 0; j < arrayList.Count; j++)
1220  {
1221  chameleonSchemas.Remove(arrayList[j]);
1222  }
1223  }
1224  }
1225 
1226  private void RemoveSchemaFromGlobalTables(XmlSchema schema)
1227  {
1228  if (schemas.Count != 0)
1229  {
1230  VerifyTables();
1231  foreach (XmlSchemaElement value in schema.Elements.Values)
1232  {
1233  XmlSchemaElement xmlSchemaElement2 = (XmlSchemaElement)elements[value.QualifiedName];
1234  if (xmlSchemaElement2 == value)
1235  {
1236  elements.Remove(value.QualifiedName);
1237  }
1238  }
1239  foreach (XmlSchemaAttribute value2 in schema.Attributes.Values)
1240  {
1241  XmlSchemaAttribute xmlSchemaAttribute2 = (XmlSchemaAttribute)attributes[value2.QualifiedName];
1242  if (xmlSchemaAttribute2 == value2)
1243  {
1244  attributes.Remove(value2.QualifiedName);
1245  }
1246  }
1247  foreach (XmlSchemaType value3 in schema.SchemaTypes.Values)
1248  {
1249  XmlSchemaType xmlSchemaType2 = (XmlSchemaType)schemaTypes[value3.QualifiedName];
1250  if (xmlSchemaType2 == value3)
1251  {
1252  schemaTypes.Remove(value3.QualifiedName);
1253  }
1254  }
1255  }
1256  }
1257 
1258  private bool AddToTable(XmlSchemaObjectTable table, XmlQualifiedName qname, XmlSchemaObject item)
1259  {
1260  if (qname.Name.Length == 0)
1261  {
1262  return true;
1263  }
1264  XmlSchemaObject xmlSchemaObject = table[qname];
1265  if (xmlSchemaObject != null)
1266  {
1267  if (xmlSchemaObject == item || xmlSchemaObject.SourceUri == item.SourceUri)
1268  {
1269  return true;
1270  }
1271  string res = string.Empty;
1272  if (item is XmlSchemaComplexType)
1273  {
1274  res = "Sch_DupComplexType";
1275  }
1276  else if (item is XmlSchemaSimpleType)
1277  {
1278  res = "Sch_DupSimpleType";
1279  }
1280  else if (item is XmlSchemaElement)
1281  {
1282  res = "Sch_DupGlobalElement";
1283  }
1284  else if (item is XmlSchemaAttribute)
1285  {
1286  if (qname.Namespace == "http://www.w3.org/XML/1998/namespace")
1287  {
1288  XmlSchema buildInSchema = Preprocessor.GetBuildInSchema();
1289  XmlSchemaObject xmlSchemaObject2 = buildInSchema.Attributes[qname];
1290  if (xmlSchemaObject == xmlSchemaObject2)
1291  {
1292  table.Insert(qname, item);
1293  return true;
1294  }
1295  if (item == xmlSchemaObject2)
1296  {
1297  return true;
1298  }
1299  }
1300  res = "Sch_DupGlobalAttribute";
1301  }
1302  SendValidationEvent(new XmlSchemaException(res, qname.ToString()), XmlSeverityType.Error);
1303  return false;
1304  }
1305  table.Add(qname, item);
1306  return true;
1307  }
1308 
1309  private void VerifyTables()
1310  {
1311  if (elements == null)
1312  {
1313  elements = new XmlSchemaObjectTable();
1314  }
1315  if (attributes == null)
1316  {
1317  attributes = new XmlSchemaObjectTable();
1318  }
1319  if (schemaTypes == null)
1320  {
1321  schemaTypes = new XmlSchemaObjectTable();
1322  }
1323  if (substitutionGroups == null)
1324  {
1325  substitutionGroups = new XmlSchemaObjectTable();
1326  }
1327  }
1328 
1329  private void InternalValidationCallback(object sender, ValidationEventArgs e)
1330  {
1331  if (e.Severity == XmlSeverityType.Error)
1332  {
1333  throw e.Exception;
1334  }
1335  }
1336 
1337  private void SendValidationEvent(XmlSchemaException e, XmlSeverityType severity)
1338  {
1339  if (eventHandler != null)
1340  {
1341  eventHandler(this, new ValidationEventArgs(e, severity));
1342  return;
1343  }
1344  throw e;
1345  }
1346  }
1347 }
UriKind
Defines the kinds of T:System.Uris for the M:System.Uri.IsWellFormedUriString(System....
Definition: UriKind.cs:5
abstract string BaseURI
When overridden in a derived class, gets the base URI of the current node.
Definition: XmlReader.cs:215
XmlSchemaSet()
Initializes a new instance of the T:System.Xml.Schema.XmlSchemaSet class.
virtual ICollection Values
Gets the values in a T:System.Collections.SortedList object.
Definition: SortedList.cs:642
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
virtual int Count
Gets the number of elements contained in a T:System.Collections.SortedList object.
Definition: SortedList.cs:634
bool RemoveRecursive(XmlSchema schemaToRemove)
Removes the specified XML Schema definition language (XSD) schema and all the schemas it imports from...
string TargetNamespace
Gets or sets the Uniform Resource Identifier (URI) of the schema target namespace.
Definition: XmlSchema.cs:151
static bool TryEnter(object obj)
Attempts to acquire an exclusive lock on the specified object.
Definition: Monitor.cs:63
XmlSeverityType
Represents the severity of the validation event.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Provides a mechanism that synchronizes access to objects.
Definition: Monitor.cs:13
void Add(XmlSchemaSet schemas)
Adds all the XML Schema definition language (XSD) schemas in the given T:System.Xml....
Resolves external XML resources named by a Uniform Resource Identifier (URI).
Definition: XmlResolver.cs:8
int Count
Gets the number of logical XML Schema definition language (XSD) schemas in the T:System....
Definition: XmlSchemaSet.cs:98
XmlSchema Add(string targetNamespace, XmlReader schemaDocument)
Adds the XML Schema definition language (XSD) schema contained in the T:System.Xml....
bool Contains(XmlSchema schema)
Indicates whether the specified XML Schema definition language (XSD) T:System.Xml....
Specifies a set of features to support on the T:System.Xml.XmlReader object created by the Overload:S...
ICollection Schemas()
Returns a collection of all the XML Schema definition language (XSD) schemas in the T:System....
bool Contains(string targetNamespace)
Indicates whether an XML Schema definition language (XSD) schema with the specified target namespace ...
XmlSchemaObjectTable GlobalElements
Gets all the global elements in all the XML Schema definition language (XSD) schemas in the T:System....
static Delegate Remove(Delegate source, Delegate value)
Removes the last occurrence of the invocation list of a delegate from the invocation list of another ...
Definition: Delegate.cs:287
static Delegate Combine(Delegate a, Delegate b)
Concatenates the invocation lists of two delegates.
Definition: Delegate.cs:202
DtdProcessing DtdProcessing
Gets or sets a value that determines the processing of DTDs.
Provides schema compilation options for the T:System.Xml.Schema.XmlSchemaSet class This class cannot ...
virtual void Add(object key, object value)
Adds an element with the specified key and value to a T:System.Collections.SortedList object.
Definition: SortedList.cs:806
virtual void Remove(object key)
Removes the element with the specified key from a T:System.Collections.SortedList object.
Definition: SortedList.cs:1071
Represents a reader that provides fast, noncached, forward-only access to XML data....
Definition: XmlReader.cs:15
void Compile()
Compiles the XML Schema definition language (XSD) schemas added to the T:System.Xml....
static void Enter(object obj)
Acquires an exclusive lock on the specified object.
virtual object GetByIndex(int index)
Gets the value at the specified index of a T:System.Collections.SortedList object.
Definition: SortedList.cs:940
Implements a single-threaded T:System.Xml.XmlNameTable.
Definition: NameTable.cs:5
virtual void CopyTo(Array array, int arrayIndex)
Copies the T:System.Collections.Hashtable elements to a one-dimensional T:System.Array instance at th...
Definition: Hashtable.cs:1084
virtual bool Contains(object item)
Determines whether an element is in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2486
XmlSchema Reprocess(XmlSchema schema)
Reprocesses an XML Schema definition language (XSD) schema that already exists in the T:System....
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
XmlSchema Remove(XmlSchema schema)
Removes the specified XML Schema definition language (XSD) schema from the T:System....
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
virtual ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
Definition: Hashtable.cs:617
Resolves external XML resources named by a Uniform Resource Identifier (URI).
XmlNameTable NameTable
Gets or sets the T:System.Xml.XmlNameTable used for atomized string comparisons.
virtual bool ContainsKey(object key)
Determines whether a T:System.Collections.SortedList object contains a specific key.
Definition: SortedList.cs:862
Provides the collections for contained elements in the T:System.Xml.Schema.XmlSchema class (for examp...
Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and ...
Definition: SortedList.cs:14
static void Exit(object obj)
Releases an exclusive lock on the specified object.
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
virtual Uri ResolveUri(Uri baseUri, string relativeUri)
When overridden in a derived class, resolves the absolute URI from the base and relative URIs.
Definition: XmlResolver.cs:38
Table of atomized string objects.
Definition: XmlNameTable.cs:5
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
XmlSchemaObjectTable GlobalAttributes
Gets all the global attributes in all the XML Schema definition language (XSD) schemas in the T:Syste...
The exception that is thrown when one of the arguments provided to a method is not valid.
abstract bool Read()
When overridden in a derived class, reads the next node from the stream.
XmlSchema Add(XmlSchema schema)
Adds the given T:System.Xml.Schema.XmlSchema to the T:System.Xml.Schema.XmlSchemaSet.
bool IsCompiled
Gets a value that indicates whether the XML Schema definition language (XSD) schemas in the T:System....
Definition: XmlSchemaSet.cs:70
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
XmlSchemaSet(XmlNameTable nameTable)
Initializes a new instance of the T:System.Xml.Schema.XmlSchemaSet class with the specified T:System....
XmlSchemaObjectTable SchemaTypes
Gets the post-schema-compilation value of all schema types in the schema.
Definition: XmlSchema.cs:297
XmlResolver XmlResolver
Sets the T:System.Xml.XmlResolver used to resolve namespaces or locations referenced in include and i...
Definition: XmlSchemaSet.cs:75
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
An in-memory representation of an XML Schema, as specified in the World Wide Web Consortium (W3C) XML...
Definition: XmlSchema.cs:13
void CopyTo(XmlSchema[] schemas, int index)
Copies all the T:System.Xml.Schema.XmlSchema objects from the T:System.Xml.Schema....
XmlResolver XmlResolver
Sets the T:System.Xml.XmlResolver used to access external documents.
ValidationEventHandler ValidationEventHandler
Specifies an event handler for receiving information about XML Schema definition language (XSD) schem...
ICollection Schemas(string targetNamespace)
Returns a collection of all the XML Schema definition language (XSD) schemas in the T:System....
DtdProcessing
Specifies the options for processing DTDs. The T:System.Xml.DtdProcessing enumeration is used by the ...
Definition: DtdProcessing.cs:5
Returns detailed information about the schema exception.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
Definition: Uri.cs:19
XmlSchemaCompilationSettings CompilationSettings
Gets or sets the T:System.Xml.Schema.XmlSchemaCompilationSettings for the T:System....
Definition: XmlSchemaSet.cs:85
Defines a dictionary key/value pair that can be set or retrieved.
XmlSchemaObjectTable GlobalTypes
Gets all of the global simple and complex types in all the XML Schema definition language (XSD) schem...
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
static XmlReader Create(string inputUri)
Creates a new T:System.Xml.XmlReader instance with specified URI.
Definition: XmlReader.cs:2199
virtual bool ContainsValue(object value)
Determines whether a T:System.Collections.SortedList object contains a specific value.
Definition: SortedList.cs:871
virtual void Close()
When overridden in a derived class, changes the P:System.Xml.XmlReader.ReadState to F:System....
Definition: XmlReader.cs:1152
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658
static bool Yield()
Causes the calling thread to yield execution to another thread that is ready to run on the current pr...
Definition: Thread.cs:797
XmlSchema Add(string targetNamespace, string schemaUri)
Adds the XML Schema definition language (XSD) schema at the URL specified to the T:System....
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
Contains a cache of XML Schema definition language (XSD) schemas.
Definition: XmlSchemaSet.cs:8
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18