mscorlib(4.0.0.0) API with additions
XmlSchemaObjectTable.cs
1 using System.Collections;
3 
4 namespace System.Xml.Schema
5 {
7  public class XmlSchemaObjectTable
8  {
9  internal enum EnumeratorType
10  {
11  Keys,
12  Values,
14  }
15 
16  internal struct XmlSchemaObjectEntry
17  {
18  internal XmlQualifiedName qname;
19 
20  internal XmlSchemaObject xso;
21 
22  public XmlSchemaObjectEntry(XmlQualifiedName name, XmlSchemaObject value)
23  {
24  qname = name;
25  xso = value;
26  }
27 
28  public XmlSchemaObject IsMatch(string localName, string ns)
29  {
30  if (localName == qname.Name && ns == qname.Namespace)
31  {
32  return xso;
33  }
34  return null;
35  }
36 
37  public void Reset()
38  {
39  qname = null;
40  xso = null;
41  }
42  }
43 
44  internal class NamesCollection : ICollection, IEnumerable
45  {
46  private List<XmlSchemaObjectEntry> entries;
47 
48  private int size;
49 
50  public int Count => size;
51 
52  public object SyncRoot => ((ICollection)entries).SyncRoot;
53 
54  public bool IsSynchronized => ((ICollection)entries).IsSynchronized;
55 
56  internal NamesCollection(List<XmlSchemaObjectEntry> entries, int size)
57  {
58  this.entries = entries;
59  this.size = size;
60  }
61 
62  public void CopyTo(Array array, int arrayIndex)
63  {
64  if (array == null)
65  {
66  throw new ArgumentNullException("array");
67  }
68  if (arrayIndex < 0)
69  {
70  throw new ArgumentOutOfRangeException("arrayIndex");
71  }
72  for (int i = 0; i < size; i++)
73  {
74  array.SetValue(entries[i].qname, arrayIndex++);
75  }
76  }
77 
78  public IEnumerator GetEnumerator()
79  {
80  return new XSOEnumerator(entries, size, EnumeratorType.Keys);
81  }
82  }
83 
84  internal class ValuesCollection : ICollection, IEnumerable
85  {
86  private List<XmlSchemaObjectEntry> entries;
87 
88  private int size;
89 
90  public int Count => size;
91 
92  public object SyncRoot => ((ICollection)entries).SyncRoot;
93 
94  public bool IsSynchronized => ((ICollection)entries).IsSynchronized;
95 
96  internal ValuesCollection(List<XmlSchemaObjectEntry> entries, int size)
97  {
98  this.entries = entries;
99  this.size = size;
100  }
101 
102  public void CopyTo(Array array, int arrayIndex)
103  {
104  if (array == null)
105  {
106  throw new ArgumentNullException("array");
107  }
108  if (arrayIndex < 0)
109  {
110  throw new ArgumentOutOfRangeException("arrayIndex");
111  }
112  for (int i = 0; i < size; i++)
113  {
114  array.SetValue(entries[i].xso, arrayIndex++);
115  }
116  }
117 
118  public IEnumerator GetEnumerator()
119  {
120  return new XSOEnumerator(entries, size, EnumeratorType.Values);
121  }
122  }
123 
124  internal class XSOEnumerator : IEnumerator
125  {
126  private List<XmlSchemaObjectEntry> entries;
127 
128  private EnumeratorType enumType;
129 
130  protected int currentIndex;
131 
132  protected int size;
133 
134  protected XmlQualifiedName currentKey;
135 
136  protected XmlSchemaObject currentValue;
137 
138  public object Current
139  {
140  get
141  {
142  if (currentIndex == -1)
143  {
144  throw new InvalidOperationException(Res.GetString("Sch_EnumNotStarted", string.Empty));
145  }
146  if (currentIndex >= size)
147  {
148  throw new InvalidOperationException(Res.GetString("Sch_EnumFinished", string.Empty));
149  }
150  switch (enumType)
151  {
152  case EnumeratorType.Keys:
153  return currentKey;
154  case EnumeratorType.Values:
155  return currentValue;
156  case EnumeratorType.DictionaryEntry:
157  return new DictionaryEntry(currentKey, currentValue);
158  default:
159  return null;
160  }
161  }
162  }
163 
164  internal XSOEnumerator(List<XmlSchemaObjectEntry> entries, int size, EnumeratorType enumType)
165  {
166  this.entries = entries;
167  this.size = size;
168  this.enumType = enumType;
169  currentIndex = -1;
170  }
171 
172  public bool MoveNext()
173  {
174  if (currentIndex >= size - 1)
175  {
176  currentValue = null;
177  currentKey = null;
178  return false;
179  }
180  currentIndex++;
181  currentValue = entries[currentIndex].xso;
182  currentKey = entries[currentIndex].qname;
183  return true;
184  }
185 
186  public void Reset()
187  {
188  currentIndex = -1;
189  currentValue = null;
190  currentKey = null;
191  }
192  }
193 
194  internal class XSODictionaryEnumerator : XSOEnumerator, IDictionaryEnumerator, IEnumerator
195  {
196  public DictionaryEntry Entry
197  {
198  get
199  {
200  if (currentIndex == -1)
201  {
202  throw new InvalidOperationException(Res.GetString("Sch_EnumNotStarted", string.Empty));
203  }
204  if (currentIndex >= size)
205  {
206  throw new InvalidOperationException(Res.GetString("Sch_EnumFinished", string.Empty));
207  }
208  return new DictionaryEntry(currentKey, currentValue);
209  }
210  }
211 
212  public object Key
213  {
214  get
215  {
216  if (currentIndex == -1)
217  {
218  throw new InvalidOperationException(Res.GetString("Sch_EnumNotStarted", string.Empty));
219  }
220  if (currentIndex >= size)
221  {
222  throw new InvalidOperationException(Res.GetString("Sch_EnumFinished", string.Empty));
223  }
224  return currentKey;
225  }
226  }
227 
228  public object Value
229  {
230  get
231  {
232  if (currentIndex == -1)
233  {
234  throw new InvalidOperationException(Res.GetString("Sch_EnumNotStarted", string.Empty));
235  }
236  if (currentIndex >= size)
237  {
238  throw new InvalidOperationException(Res.GetString("Sch_EnumFinished", string.Empty));
239  }
240  return currentValue;
241  }
242  }
243 
244  internal XSODictionaryEnumerator(List<XmlSchemaObjectEntry> entries, int size, EnumeratorType enumType)
245  : base(entries, size, enumType)
246  {
247  }
248  }
249 
251 
253 
256  public int Count => table.Count;
257 
261  public XmlSchemaObject this[XmlQualifiedName name]
262  {
263  get
264  {
265  if (table.TryGetValue(name, out XmlSchemaObject value))
266  {
267  return value;
268  }
269  return null;
270  }
271  }
272 
275  public ICollection Names => new NamesCollection(entries, table.Count);
276 
279  public ICollection Values => new ValuesCollection(entries, table.Count);
280 
281  internal XmlSchemaObjectTable()
282  {
283  }
284 
285  internal void Add(XmlQualifiedName name, XmlSchemaObject value)
286  {
287  table.Add(name, value);
288  entries.Add(new XmlSchemaObjectEntry(name, value));
289  }
290 
291  internal void Insert(XmlQualifiedName name, XmlSchemaObject value)
292  {
293  XmlSchemaObject value2 = null;
294  if (table.TryGetValue(name, out value2))
295  {
296  table[name] = value;
297  int index = FindIndexByValue(value2);
298  entries[index] = new XmlSchemaObjectEntry(name, value);
299  }
300  else
301  {
302  Add(name, value);
303  }
304  }
305 
306  internal void Replace(XmlQualifiedName name, XmlSchemaObject value)
307  {
308  if (table.TryGetValue(name, out XmlSchemaObject value2))
309  {
310  table[name] = value;
311  int index = FindIndexByValue(value2);
312  entries[index] = new XmlSchemaObjectEntry(name, value);
313  }
314  }
315 
316  internal void Clear()
317  {
318  table.Clear();
319  entries.Clear();
320  }
321 
322  internal void Remove(XmlQualifiedName name)
323  {
324  if (table.TryGetValue(name, out XmlSchemaObject value))
325  {
326  table.Remove(name);
327  int index = FindIndexByValue(value);
328  entries.RemoveAt(index);
329  }
330  }
331 
332  private int FindIndexByValue(XmlSchemaObject xso)
333  {
334  for (int i = 0; i < entries.Count; i++)
335  {
336  if (entries[i].xso == xso)
337  {
338  return i;
339  }
340  }
341  return -1;
342  }
343 
348  public bool Contains(XmlQualifiedName name)
349  {
350  return table.ContainsKey(name);
351  }
352 
356  {
357  return new XSODictionaryEnumerator(entries, table.Count, EnumeratorType.DictionaryEntry);
358  }
359  }
360 }
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 elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.Generic.List`1.
Definition: List.cs:1375
Definition: __Canon.cs:3
Represents the root class for the Xml schema object model hierarchy and serves as a base class for cl...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
string Namespace
Gets a string representation of the namespace of the T:System.Xml.XmlQualifiedName.
bool ContainsKey(TKey key)
Determines whether the T:System.Collections.Generic.Dictionary`2 contains the specified key.
Definition: Dictionary.cs:1303
int Count
Gets the number of items contained in the T:System.Xml.Schema.XmlSchemaObjectTable.
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
string Name
Gets a string representation of the qualified name of the T:System.Xml.XmlQualifiedName.
ICollection Names
Returns a collection of all the named elements in the T:System.Xml.Schema.XmlSchemaObjectTable.
int Count
Gets the number of key/value pairs contained in the T:System.Collections.Generic.Dictionary`2.
Definition: Dictionary.cs:890
Provides the collections for contained elements in the T:System.Xml.Schema.XmlSchema class (for examp...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents an XML qualified name.
Represents a collection of keys and values.To browse the .NET Framework source code for this type,...
Definition: Dictionary.cs:17
bool Contains(XmlQualifiedName name)
Determines if the qualified name specified exists in the collection.
void Clear()
Removes all elements from the T:System.Collections.Generic.List`1.
Definition: List.cs:614
Enumerates the elements of a nongeneric dictionary.
The exception that is thrown when a method call is invalid for the object's current state.
void Add(TKey key, TValue value)
Adds the specified key and value to the dictionary.
Definition: Dictionary.cs:1244
IDictionaryEnumerator GetEnumerator()
Returns an enumerator that can iterate through the T:System.Xml.Schema.XmlSchemaObjectTable.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
ICollection Values
Returns a collection of all the values for all the elements in the T:System.Xml.Schema....
Defines a dictionary key/value pair that can be set or retrieved.
void Clear()
Removes all keys and values from the T:System.Collections.Generic.Dictionary`2.
Definition: Dictionary.cs:1280
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Definition: Dictionary.cs:1624
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9