mscorlib(4.0.0.0) API with additions
XmlSchemaCollection.cs
1 using System.Collections;
2 using System.Threading;
4 
5 namespace System.Xml.Schema
6 {
8  [Obsolete("Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation. http://go.microsoft.com/fwlink/?linkid=14202")]
10  {
11  private Hashtable collection;
12 
13  private XmlNameTable nameTable;
14 
15  private SchemaNames schemaNames;
16 
17  private ReaderWriterLock wLock;
18 
19  private int timeout = -1;
20 
21  private bool isThreadSafe = true;
22 
23  private ValidationEventHandler validationEventHandler;
24 
25  private XmlResolver xmlResolver;
26 
29  public int Count => collection.Count;
30 
33  public XmlNameTable NameTable => nameTable;
34 
35  internal XmlResolver XmlResolver
36  {
37  set
38  {
39  xmlResolver = value;
40  }
41  }
42 
46  public XmlSchema this[string ns]
47  {
48  get
49  {
50  return ((XmlSchemaCollectionNode)collection[(ns != null) ? ns : string.Empty])?.Schema;
51  }
52  }
53 
57  {
58  get
59  {
60  return true;
61  }
62  }
63 
66  object ICollection.SyncRoot
67  {
68  get
69  {
70  return this;
71  }
72  }
73 
76  int ICollection.Count
77  {
78  get
79  {
80  return collection.Count;
81  }
82  }
83 
84  internal ValidationEventHandler EventHandler
85  {
86  get
87  {
88  return validationEventHandler;
89  }
90  set
91  {
92  validationEventHandler = value;
93  }
94  }
95 
98  {
99  add
100  {
101  validationEventHandler = (ValidationEventHandler)Delegate.Combine(validationEventHandler, value);
102  }
103  remove
104  {
105  validationEventHandler = (ValidationEventHandler)Delegate.Remove(validationEventHandler, value);
106  }
107  }
108 
111  : this(new NameTable())
112  {
113  }
114 
118  {
119  if (nametable == null)
120  {
121  throw new ArgumentNullException("nametable");
122  }
123  nameTable = nametable;
124  collection = Hashtable.Synchronized(new Hashtable());
125  xmlResolver = XmlReaderSection.CreateDefaultResolver();
126  isThreadSafe = true;
127  if (isThreadSafe)
128  {
129  wLock = new ReaderWriterLock();
130  }
131  }
132 
138  public XmlSchema Add(string ns, string uri)
139  {
140  if (uri == null || uri.Length == 0)
141  {
142  throw new ArgumentNullException("uri");
143  }
144  XmlTextReader xmlTextReader = new XmlTextReader(uri, nameTable);
145  xmlTextReader.XmlResolver = xmlResolver;
146  XmlSchema xmlSchema = null;
147  try
148  {
149  xmlSchema = Add(ns, xmlTextReader, xmlResolver);
150  while (xmlTextReader.Read())
151  {
152  }
153  return xmlSchema;
154  }
155  finally
156  {
157  xmlTextReader.Close();
158  }
159  }
160 
167  public XmlSchema Add(string ns, XmlReader reader)
168  {
169  return Add(ns, reader, xmlResolver);
170  }
171 
179  public XmlSchema Add(string ns, XmlReader reader, XmlResolver resolver)
180  {
181  if (reader == null)
182  {
183  throw new ArgumentNullException("reader");
184  }
185  XmlNameTable nt = reader.NameTable;
186  SchemaInfo schemaInfo = new SchemaInfo();
187  Parser parser = new Parser(SchemaType.None, nt, GetSchemaNames(nt), validationEventHandler);
188  parser.XmlResolver = resolver;
189  SchemaType schemaType;
190  try
191  {
192  schemaType = parser.Parse(reader, ns);
193  }
194  catch (XmlSchemaException e)
195  {
196  SendValidationEvent(e);
197  return null;
198  }
199  if (schemaType == SchemaType.XSD)
200  {
201  schemaInfo.SchemaType = SchemaType.XSD;
202  return Add(ns, schemaInfo, parser.XmlSchema, compile: true, resolver);
203  }
204  SchemaInfo xdrSchema = parser.XdrSchema;
205  return Add(ns, parser.XdrSchema, null, compile: true, resolver);
206  }
207 
211  public XmlSchema Add(XmlSchema schema)
212  {
213  return Add(schema, xmlResolver);
214  }
215 
221  public XmlSchema Add(XmlSchema schema, XmlResolver resolver)
222  {
223  if (schema == null)
224  {
225  throw new ArgumentNullException("schema");
226  }
227  SchemaInfo schemaInfo = new SchemaInfo();
228  schemaInfo.SchemaType = SchemaType.XSD;
229  return Add(schema.TargetNamespace, schemaInfo, schema, compile: true, resolver);
230  }
231 
234  public void Add(XmlSchemaCollection schema)
235  {
236  if (schema == null)
237  {
238  throw new ArgumentNullException("schema");
239  }
240  if (this != schema)
241  {
242  IDictionaryEnumerator enumerator = schema.collection.GetEnumerator();
243  while (enumerator.MoveNext())
244  {
245  XmlSchemaCollectionNode xmlSchemaCollectionNode = (XmlSchemaCollectionNode)enumerator.Value;
246  Add(xmlSchemaCollectionNode.NamespaceURI, xmlSchemaCollectionNode);
247  }
248  }
249  }
250 
255  public bool Contains(XmlSchema schema)
256  {
257  if (schema == null)
258  {
259  throw new ArgumentNullException("schema");
260  }
261  return this[schema.TargetNamespace] != null;
262  }
263 
268  public bool Contains(string ns)
269  {
270  return collection[(ns != null) ? ns : string.Empty] != null;
271  }
272 
276  {
277  return new XmlSchemaCollectionEnumerator(collection);
278  }
279 
283  {
284  return new XmlSchemaCollectionEnumerator(collection);
285  }
286 
290  void ICollection.CopyTo(Array array, int index)
291  {
292  if (array == null)
293  {
294  throw new ArgumentNullException("array");
295  }
296  if (index < 0)
297  {
298  throw new ArgumentOutOfRangeException("index");
299  }
300  XmlSchemaCollectionEnumerator enumerator = GetEnumerator();
301  while (true)
302  {
303  if (enumerator.MoveNext())
304  {
305  if (index == array.Length && array.IsFixedSize)
306  {
307  break;
308  }
309  array.SetValue(enumerator.Current, index++);
310  continue;
311  }
312  return;
313  }
314  throw new ArgumentOutOfRangeException("index");
315  }
316 
320  public void CopyTo(XmlSchema[] array, int index)
321  {
322  if (array == null)
323  {
324  throw new ArgumentNullException("array");
325  }
326  if (index < 0)
327  {
328  throw new ArgumentOutOfRangeException("index");
329  }
330  XmlSchemaCollectionEnumerator enumerator = GetEnumerator();
331  while (true)
332  {
333  if (!enumerator.MoveNext())
334  {
335  return;
336  }
337  XmlSchema current = enumerator.Current;
338  if (current != null)
339  {
340  if (index == array.Length)
341  {
342  break;
343  }
344  array[index++] = enumerator.Current;
345  }
346  }
347  throw new ArgumentOutOfRangeException("index");
348  }
349 
350  internal SchemaInfo GetSchemaInfo(string ns)
351  {
352  return ((XmlSchemaCollectionNode)collection[(ns != null) ? ns : string.Empty])?.SchemaInfo;
353  }
354 
355  internal SchemaNames GetSchemaNames(XmlNameTable nt)
356  {
357  if (nameTable != nt)
358  {
359  return new SchemaNames(nt);
360  }
361  if (schemaNames == null)
362  {
363  schemaNames = new SchemaNames(nameTable);
364  }
365  return schemaNames;
366  }
367 
368  internal XmlSchema Add(string ns, SchemaInfo schemaInfo, XmlSchema schema, bool compile)
369  {
370  return Add(ns, schemaInfo, schema, compile, xmlResolver);
371  }
372 
373  private XmlSchema Add(string ns, SchemaInfo schemaInfo, XmlSchema schema, bool compile, XmlResolver resolver)
374  {
375  int num = 0;
376  if (schema != null)
377  {
378  if (schema.ErrorCount == 0 && compile)
379  {
380  if (!schema.CompileSchema(this, resolver, schemaInfo, ns, validationEventHandler, nameTable, CompileContentModel: true))
381  {
382  num = 1;
383  }
384  ns = ((schema.TargetNamespace == null) ? string.Empty : schema.TargetNamespace);
385  }
386  num += schema.ErrorCount;
387  }
388  else
389  {
390  num += schemaInfo.ErrorCount;
391  ns = NameTable.Add(ns);
392  }
393  if (num == 0)
394  {
395  XmlSchemaCollectionNode xmlSchemaCollectionNode = new XmlSchemaCollectionNode();
396  xmlSchemaCollectionNode.NamespaceURI = ns;
397  xmlSchemaCollectionNode.SchemaInfo = schemaInfo;
398  xmlSchemaCollectionNode.Schema = schema;
399  Add(ns, xmlSchemaCollectionNode);
400  return schema;
401  }
402  return null;
403  }
404 
405  private void Add(string ns, XmlSchemaCollectionNode node)
406  {
407  if (isThreadSafe)
408  {
409  wLock.AcquireWriterLock(timeout);
410  }
411  try
412  {
413  if (collection[ns] != null)
414  {
415  collection.Remove(ns);
416  }
417  collection.Add(ns, node);
418  }
419  finally
420  {
421  if (isThreadSafe)
422  {
423  wLock.ReleaseWriterLock();
424  }
425  }
426  }
427 
428  private void SendValidationEvent(XmlSchemaException e)
429  {
430  if (validationEventHandler != null)
431  {
432  validationEventHandler(this, new ValidationEventArgs(e));
433  return;
434  }
435  throw e;
436  }
437  }
438 }
XmlResolver XmlResolver
Sets the T:System.Xml.XmlResolver used for resolving DTD references.
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
int Count
Gets the number of namespaces defined in this collection.
void ReleaseWriterLock()
Decrements the lock count on the writer lock.
bool MoveNext()
Advances the enumerator to the next element of the collection.
string TargetNamespace
Gets or sets the Uniform Resource Identifier (URI) of the schema target namespace.
Definition: XmlSchema.cs:151
XmlNameTable NameTable
Gets the default XmlNameTable used by the XmlSchemaCollection when loading new schemas.
XmlSchema Add(string ns, XmlReader reader)
Adds the schema contained in the T:System.Xml.XmlReader to the schema collection.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
ValidationEventHandler ValidationEventHandler
Sets an event handler for receiving information about the XDR and XML schema validation errors.
Resolves external XML resources named by a Uniform Resource Identifier (URI).
Definition: XmlResolver.cs:8
XmlSchema Add(XmlSchema schema)
Adds the T:System.Xml.Schema.XmlSchema to the collection.
XmlSchemaCollectionEnumerator GetEnumerator()
Provides support for the "for each" style iteration over the collection of schemas.
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
Defines a lock that supports single writers and multiple readers.
Represents a reader that provides fast, non-cached, forward-only access to XML data....
Supports a simple iteration over a collection. This class cannot be inherited.
Represents a reader that provides fast, noncached, forward-only access to XML data....
Definition: XmlReader.cs:15
XmlSchema Add(XmlSchema schema, XmlResolver resolver)
Adds the T:System.Xml.Schema.XmlSchema to the collection. The specified T:System.Xml....
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Implements a single-threaded T:System.Xml.XmlNameTable.
Definition: NameTable.cs:5
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
XmlSchemaCollection(XmlNameTable nametable)
Initializes a new instance of the XmlSchemaCollection class with the specified T:System....
XmlSchemaCollection()
Initializes a new instance of the XmlSchemaCollection class.
XmlSchema Add(string ns, XmlReader reader, XmlResolver resolver)
Adds the schema contained in the T:System.Xml.XmlReader to the schema collection. The specified T:Sys...
void AcquireWriterLock(int millisecondsTimeout)
Acquires the writer lock, using an T:System.Int32 value for the time-out.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.ICollection is synchronized (threa...
Definition: ICollection.cs:33
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
bool Contains(XmlSchema schema)
Gets a value indicating whether the targetNamespace of the specified T:System.Xml....
static Hashtable Synchronized(Hashtable table)
Returns a synchronized (thread-safe) wrapper for the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1397
override void Close()
Changes the P:System.Xml.XmlReader.ReadState to Closed.
Table of atomized string objects.
Definition: XmlNameTable.cs:5
bool Contains(string ns)
Gets a value indicating whether a schema with the specified namespace is in the collection.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
abstract string Add(char[] array, int offset, int length)
When overridden in a derived class, atomizes the specified string and adds it to the XmlNameTable.
An in-memory representation of an XML Schema, as specified in the World Wide Web Consortium (W3C) XML...
Definition: XmlSchema.cs:13
object Value
Gets the value of the current dictionary entry.
override bool Read()
Reads the next node from the stream.
Enumerates the elements of a nongeneric dictionary.
XmlSchema Add(string ns, string uri)
Adds the schema located by the given URL into the schema collection.
void CopyTo(XmlSchema[] array, int index)
Copies all the XmlSchema objects from this collection into the given array starting at the given inde...
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
Returns detailed information about the schema exception.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
void Add(XmlSchemaCollection schema)
Adds all the namespaces defined in the given collection (including their associated schemas) to this ...
Represents an XML reader section.
abstract XmlNameTable NameTable
When overridden in a derived class, gets the T:System.Xml.XmlNameTable associated with this implement...
Definition: XmlReader.cs:373
Contains a cache of XML Schema definition language (XSD) and XML-Data Reduced (XDR) schemas....