mscorlib(4.0.0.0) API with additions
XmlNamespaceManager.cs
1 using System.Collections;
3 
4 namespace System.Xml
5 {
7  [global::__DynamicallyInvokable]
9  {
10  private struct NamespaceDeclaration
11  {
12  public string prefix;
13 
14  public string uri;
15 
16  public int scopeId;
17 
18  public int previousNsIndex;
19 
20  public void Set(string prefix, string uri, int scopeId, int previousNsIndex)
21  {
22  this.prefix = prefix;
23  this.uri = uri;
24  this.scopeId = scopeId;
25  this.previousNsIndex = previousNsIndex;
26  }
27  }
28 
29  private static volatile IXmlNamespaceResolver s_EmptyResolver;
30 
31  private NamespaceDeclaration[] nsdecls;
32 
33  private int lastDecl;
34 
35  private XmlNameTable nameTable;
36 
37  private int scopeId;
38 
39  private Dictionary<string, int> hashTable;
40 
41  private bool useHashtable;
42 
43  private string xml;
44 
45  private string xmlNs;
46 
47  private const int MinDeclsCountForHashtable = 16;
48 
49  internal static IXmlNamespaceResolver EmptyResolver
50  {
51  get
52  {
53  if (s_EmptyResolver == null)
54  {
55  s_EmptyResolver = new XmlNamespaceManager(new NameTable());
56  }
57  return s_EmptyResolver;
58  }
59  }
60 
63  [global::__DynamicallyInvokable]
64  public virtual XmlNameTable NameTable
65  {
66  [global::__DynamicallyInvokable]
67  get
68  {
69  return nameTable;
70  }
71  }
72 
75  [global::__DynamicallyInvokable]
76  public virtual string DefaultNamespace
77  {
78  [global::__DynamicallyInvokable]
79  get
80  {
81  string text = LookupNamespace(string.Empty);
82  if (text != null)
83  {
84  return text;
85  }
86  return string.Empty;
87  }
88  }
89 
90  internal XmlNamespaceManager()
91  {
92  }
93 
98  [global::__DynamicallyInvokable]
100  {
101  this.nameTable = nameTable;
102  xml = nameTable.Add("xml");
103  xmlNs = nameTable.Add("xmlns");
104  nsdecls = new NamespaceDeclaration[8];
105  string text = nameTable.Add(string.Empty);
106  nsdecls[0].Set(text, text, -1, -1);
107  nsdecls[1].Set(xmlNs, nameTable.Add("http://www.w3.org/2000/xmlns/"), -1, -1);
108  nsdecls[2].Set(xml, nameTable.Add("http://www.w3.org/XML/1998/namespace"), 0, -1);
109  lastDecl = 2;
110  scopeId = 1;
111  }
112 
114  [global::__DynamicallyInvokable]
115  public virtual void PushScope()
116  {
117  scopeId++;
118  }
119 
123  [global::__DynamicallyInvokable]
124  public virtual bool PopScope()
125  {
126  int num = lastDecl;
127  if (scopeId == 1)
128  {
129  return false;
130  }
131  while (nsdecls[num].scopeId == scopeId)
132  {
133  if (useHashtable)
134  {
135  hashTable[nsdecls[num].prefix] = nsdecls[num].previousNsIndex;
136  }
137  num--;
138  }
139  lastDecl = num;
140  scopeId--;
141  return true;
142  }
143 
150  [global::__DynamicallyInvokable]
151  public virtual void AddNamespace(string prefix, string uri)
152  {
153  if (uri == null)
154  {
155  throw new ArgumentNullException("uri");
156  }
157  if (prefix == null)
158  {
159  throw new ArgumentNullException("prefix");
160  }
161  prefix = nameTable.Add(prefix);
162  uri = nameTable.Add(uri);
163  if (Ref.Equal(xml, prefix) && !uri.Equals("http://www.w3.org/XML/1998/namespace"))
164  {
165  throw new ArgumentException(Res.GetString("Xml_XmlPrefix"));
166  }
167  if (Ref.Equal(xmlNs, prefix))
168  {
169  throw new ArgumentException(Res.GetString("Xml_XmlnsPrefix"));
170  }
171  int num = LookupNamespaceDecl(prefix);
172  int previousNsIndex = -1;
173  if (num != -1)
174  {
175  if (nsdecls[num].scopeId == scopeId)
176  {
177  nsdecls[num].uri = uri;
178  return;
179  }
180  previousNsIndex = num;
181  }
182  if (lastDecl == nsdecls.Length - 1)
183  {
184  NamespaceDeclaration[] destinationArray = new NamespaceDeclaration[nsdecls.Length * 2];
185  Array.Copy(nsdecls, 0, destinationArray, 0, nsdecls.Length);
186  nsdecls = destinationArray;
187  }
188  nsdecls[++lastDecl].Set(prefix, uri, scopeId, previousNsIndex);
189  if (useHashtable)
190  {
191  hashTable[prefix] = lastDecl;
192  }
193  else if (lastDecl >= 16)
194  {
195  hashTable = new Dictionary<string, int>(lastDecl);
196  for (int i = 0; i <= lastDecl; i++)
197  {
198  hashTable[nsdecls[i].prefix] = i;
199  }
200  useHashtable = true;
201  }
202  }
203 
208  [global::__DynamicallyInvokable]
209  public virtual void RemoveNamespace(string prefix, string uri)
210  {
211  if (uri == null)
212  {
213  throw new ArgumentNullException("uri");
214  }
215  if (prefix == null)
216  {
217  throw new ArgumentNullException("prefix");
218  }
219  for (int num = LookupNamespaceDecl(prefix); num != -1; num = nsdecls[num].previousNsIndex)
220  {
221  if (string.Equals(nsdecls[num].uri, uri) && nsdecls[num].scopeId == scopeId)
222  {
223  nsdecls[num].uri = null;
224  }
225  }
226  }
227 
230  [global::__DynamicallyInvokable]
231  public virtual IEnumerator GetEnumerator()
232  {
233  Dictionary<string, string> dictionary = new Dictionary<string, string>(lastDecl + 1);
234  for (int i = 0; i <= lastDecl; i++)
235  {
236  if (nsdecls[i].uri != null)
237  {
238  dictionary[nsdecls[i].prefix] = nsdecls[i].prefix;
239  }
240  }
241  return dictionary.Keys.GetEnumerator();
242  }
243 
247  [global::__DynamicallyInvokable]
249  {
250  int i = 0;
251  switch (scope)
252  {
253  case XmlNamespaceScope.All:
254  i = 2;
255  break;
256  case XmlNamespaceScope.ExcludeXml:
257  i = 3;
258  break;
259  case XmlNamespaceScope.Local:
260  i = lastDecl;
261  while (nsdecls[i].scopeId == scopeId)
262  {
263  i--;
264  }
265  i++;
266  break;
267  }
268  Dictionary<string, string> dictionary = new Dictionary<string, string>(lastDecl - i + 1);
269  for (; i <= lastDecl; i++)
270  {
271  string prefix = nsdecls[i].prefix;
272  string uri = nsdecls[i].uri;
273  if (uri != null)
274  {
275  if (uri.Length > 0 || prefix.Length > 0 || scope == XmlNamespaceScope.Local)
276  {
277  dictionary[prefix] = uri;
278  }
279  else
280  {
281  dictionary.Remove(prefix);
282  }
283  }
284  }
285  return dictionary;
286  }
287 
291  [global::__DynamicallyInvokable]
292  public virtual string LookupNamespace(string prefix)
293  {
294  int num = LookupNamespaceDecl(prefix);
295  if (num != -1)
296  {
297  return nsdecls[num].uri;
298  }
299  return null;
300  }
301 
302  private int LookupNamespaceDecl(string prefix)
303  {
304  if (useHashtable)
305  {
306  if (hashTable.TryGetValue(prefix, out int value))
307  {
308  while (value != -1 && nsdecls[value].uri == null)
309  {
310  value = nsdecls[value].previousNsIndex;
311  }
312  return value;
313  }
314  return -1;
315  }
316  for (int num = lastDecl; num >= 0; num--)
317  {
318  if ((object)nsdecls[num].prefix == prefix && nsdecls[num].uri != null)
319  {
320  return num;
321  }
322  }
323  for (int num2 = lastDecl; num2 >= 0; num2--)
324  {
325  if (string.Equals(nsdecls[num2].prefix, prefix) && nsdecls[num2].uri != null)
326  {
327  return num2;
328  }
329  }
330  return -1;
331  }
332 
336  [global::__DynamicallyInvokable]
337  public virtual string LookupPrefix(string uri)
338  {
339  for (int num = lastDecl; num >= 0; num--)
340  {
341  if (string.Equals(nsdecls[num].uri, uri))
342  {
343  string prefix = nsdecls[num].prefix;
344  if (string.Equals(LookupNamespace(prefix), uri))
345  {
346  return prefix;
347  }
348  }
349  }
350  return null;
351  }
352 
357  [global::__DynamicallyInvokable]
358  public virtual bool HasNamespace(string prefix)
359  {
360  int num = lastDecl;
361  while (nsdecls[num].scopeId == scopeId)
362  {
363  if (string.Equals(nsdecls[num].prefix, prefix) && nsdecls[num].uri != null)
364  {
365  if (prefix.Length > 0 || nsdecls[num].uri.Length > 0)
366  {
367  return true;
368  }
369  return false;
370  }
371  num--;
372  }
373  return false;
374  }
375 
376  internal bool GetNamespaceDeclaration(int idx, out string prefix, out string uri)
377  {
378  idx = lastDecl - idx;
379  if (idx < 0)
380  {
381  prefix = (uri = null);
382  return false;
383  }
384  prefix = nsdecls[idx].prefix;
385  uri = nsdecls[idx].uri;
386  return true;
387  }
388  }
389 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Definition: __Canon.cs:3
virtual bool PopScope()
Pops a namespace scope off the stack.
virtual void AddNamespace(string prefix, string uri)
Adds the given namespace to the collection.
XmlNamespaceScope
Defines the namespace scope.
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
XmlNamespaceManager(XmlNameTable nameTable)
Initializes a new instance of the T:System.Xml.XmlNamespaceManager class with the specified T:System....
Resolves, adds, and removes namespaces to a collection and provides scope management for these namesp...
virtual string LookupPrefix(string uri)
Finds the prefix declared for the given namespace URI.
virtual XmlNameTable NameTable
Gets the T:System.Xml.XmlNameTable associated with this object.
virtual string DefaultNamespace
Gets the namespace URI for the default namespace.
virtual IEnumerator GetEnumerator()
Returns an enumerator to use to iterate through the namespaces in the T:System.Xml....
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Table of atomized string objects.
Definition: XmlNameTable.cs:5
virtual void RemoveNamespace(string prefix, string uri)
Removes the given namespace for the given prefix.
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
virtual void PushScope()
Pushes a namespace scope onto the stack.
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.
virtual IDictionary< string, string > GetNamespacesInScope(XmlNamespaceScope scope)
Gets a collection of namespace names keyed by prefix which can be used to enumerate the namespaces cu...
virtual bool HasNamespace(string prefix)
Gets a value indicating whether the supplied prefix has a namespace defined for the current pushed sc...
Provides read-only access to a set of prefix and namespace mappings.
virtual string LookupNamespace(string prefix)
Gets the namespace URI for the specified prefix.
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