/
XmlNamespacemanager.cs
374 lines (324 loc) · 11.8 KB
/
XmlNamespacemanager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Xml
{
public class XmlNamespaceManager : IXmlNamespaceResolver, IEnumerable
{
private struct NamespaceDeclaration
{
public string prefix;
public string? uri;
public int scopeId;
public int previousNsIndex;
public void Set(string prefix, string uri, int scopeId, int previousNsIndex)
{
this.prefix = prefix;
this.uri = uri;
this.scopeId = scopeId;
this.previousNsIndex = previousNsIndex;
}
}
// array with namespace declarations
private NamespaceDeclaration[]? _nsdecls;
// index of last declaration
private int _lastDecl;
// name table
private readonly XmlNameTable? _nameTable;
// ID (depth) of the current scope
private int _scopeId;
// hash table for faster lookup when there is lots of namespaces
private Dictionary<string, int>? _hashTable;
private bool _useHashtable;
// atomized prefixes for "xml" and "xmlns"
private readonly string? _xml;
private readonly string? _xmlNs;
// Constants
private const int MinDeclsCountForHashtable = 16;
internal XmlNamespaceManager()
{
}
public XmlNamespaceManager(XmlNameTable nameTable)
{
_nameTable = nameTable;
_xml = nameTable.Add("xml");
_xmlNs = nameTable.Add("xmlns");
_nsdecls = new NamespaceDeclaration[8];
string emptyStr = nameTable.Add(string.Empty);
_nsdecls[0].Set(emptyStr, emptyStr, -1, -1);
_nsdecls[1].Set(_xmlNs, nameTable.Add(XmlReservedNs.NsXmlNs), -1, -1);
_nsdecls[2].Set(_xml, nameTable.Add(XmlReservedNs.NsXml), 0, -1);
_lastDecl = 2;
_scopeId = 1;
}
public virtual XmlNameTable? NameTable
{
get
{
return _nameTable;
}
}
public virtual string DefaultNamespace
{
get
{
string? defaultNs = LookupNamespace(string.Empty);
return defaultNs ?? string.Empty;
}
}
public virtual void PushScope()
{
_scopeId++;
}
public virtual bool PopScope()
{
int decl = _lastDecl;
if (_scopeId == 1)
{
return false;
}
Debug.Assert(_nsdecls != null);
while (_nsdecls[decl].scopeId == _scopeId)
{
if (_useHashtable)
{
Debug.Assert(_hashTable != null);
_hashTable[_nsdecls[decl].prefix] = _nsdecls[decl].previousNsIndex;
}
decl--;
Debug.Assert(decl >= 2);
}
_lastDecl = decl;
_scopeId--;
return true;
}
public virtual void AddNamespace(string prefix, [StringSyntax(StringSyntaxAttribute.Uri)] string uri)
{
ArgumentNullException.ThrowIfNull(prefix);
ArgumentNullException.ThrowIfNull(uri);
Debug.Assert(_nameTable != null);
Debug.Assert(_nsdecls != null);
prefix = _nameTable.Add(prefix);
uri = _nameTable.Add(uri);
if ((Ref.Equal(_xml, prefix) && !uri.Equals(XmlReservedNs.NsXml)))
{
throw new ArgumentException(SR.Xml_XmlPrefix);
}
if (Ref.Equal(_xmlNs, prefix))
{
throw new ArgumentException(SR.Xml_XmlnsPrefix);
}
int declIndex = LookupNamespaceDecl(prefix);
int previousDeclIndex = -1;
if (declIndex != -1)
{
if (_nsdecls[declIndex].scopeId == _scopeId)
{
// redefine if in the same scope
_nsdecls[declIndex].uri = uri;
return;
}
else
{
// otherwise link
previousDeclIndex = declIndex;
}
}
// set new namespace declaration
if (_lastDecl == _nsdecls.Length - 1)
{
NamespaceDeclaration[] newNsdecls = new NamespaceDeclaration[_nsdecls.Length * 2];
Array.Copy(_nsdecls, newNsdecls, _nsdecls.Length);
_nsdecls = newNsdecls;
}
_nsdecls[++_lastDecl].Set(prefix, uri, _scopeId, previousDeclIndex);
// add to hashTable
if (_useHashtable)
{
Debug.Assert(_hashTable != null);
_hashTable[prefix] = _lastDecl;
}
// or create a new hashTable if the threshold has been reached
else if (_lastDecl >= MinDeclsCountForHashtable)
{
// add all to hash table
Debug.Assert(_hashTable == null);
_hashTable = new Dictionary<string, int>(_lastDecl);
for (int i = 0; i <= _lastDecl; i++)
{
_hashTable[_nsdecls[i].prefix] = i;
}
_useHashtable = true;
}
}
public virtual void RemoveNamespace(string prefix, [StringSyntax(StringSyntaxAttribute.Uri)] string uri)
{
ArgumentNullException.ThrowIfNull(prefix);
ArgumentNullException.ThrowIfNull(uri);
Debug.Assert(_nsdecls != null);
int declIndex = LookupNamespaceDecl(prefix);
while (declIndex != -1)
{
if (string.Equals(_nsdecls[declIndex].uri, uri) && _nsdecls[declIndex].scopeId == _scopeId)
{
_nsdecls[declIndex].uri = null;
}
declIndex = _nsdecls[declIndex].previousNsIndex;
}
}
public virtual IEnumerator GetEnumerator()
{
Debug.Assert(_nsdecls != null);
Dictionary<string, string> prefixes = new Dictionary<string, string>(_lastDecl + 1);
for (int thisDecl = 0; thisDecl <= _lastDecl; thisDecl++)
{
if (_nsdecls[thisDecl].uri != null)
{
prefixes[_nsdecls[thisDecl].prefix] = _nsdecls[thisDecl].prefix;
}
}
return prefixes.Keys.GetEnumerator();
}
// This pragma disables a warning that the return type is not CLS-compliant, but generics are part of CLS in Whidbey.
#pragma warning disable 3002
public virtual IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
#pragma warning restore 3002
{
Debug.Assert(_nsdecls != null);
int i = 0;
switch (scope)
{
case XmlNamespaceScope.All:
i = 2;
break;
case XmlNamespaceScope.ExcludeXml:
i = 3;
break;
case XmlNamespaceScope.Local:
i = _lastDecl;
while (_nsdecls[i].scopeId == _scopeId)
{
i--;
Debug.Assert(i >= 2);
}
i++;
break;
}
Dictionary<string, string> dict = new Dictionary<string, string>(_lastDecl - i + 1);
for (; i <= _lastDecl; i++)
{
string? prefix = _nsdecls[i].prefix;
string? uri = _nsdecls[i].uri;
Debug.Assert(prefix != null);
if (uri != null)
{
if (uri.Length > 0 || prefix.Length > 0 || scope == XmlNamespaceScope.Local)
{
dict[prefix] = uri;
}
else
{
// default namespace redeclared to "" -> remove from list for all scopes other than local
dict.Remove(prefix);
}
}
}
return dict;
}
public virtual string? LookupNamespace(string prefix)
{
Debug.Assert(_nsdecls != null);
int declIndex = LookupNamespaceDecl(prefix);
return (declIndex == -1) ? null : _nsdecls[declIndex].uri;
}
private int LookupNamespaceDecl(string prefix)
{
Debug.Assert(_nsdecls != null);
if (_useHashtable)
{
Debug.Assert(_hashTable != null);
int declIndex;
if (_hashTable.TryGetValue(prefix, out declIndex))
{
while (declIndex != -1 && _nsdecls[declIndex].uri == null)
{
declIndex = _nsdecls[declIndex].previousNsIndex;
}
return declIndex;
}
return -1;
}
else
{
// First assume that prefix is atomized
for (int thisDecl = _lastDecl; thisDecl >= 0; thisDecl--)
{
if ((object)_nsdecls[thisDecl].prefix == (object)prefix && _nsdecls[thisDecl].uri != null)
{
return thisDecl;
}
}
// Non-atomized lookup
for (int thisDecl = _lastDecl; thisDecl >= 0; thisDecl--)
{
if (string.Equals(_nsdecls[thisDecl].prefix, prefix) && _nsdecls[thisDecl].uri != null)
{
return thisDecl;
}
}
}
return -1;
}
public virtual string? LookupPrefix([StringSyntax(StringSyntaxAttribute.Uri)] string uri)
{
Debug.Assert(_nsdecls != null);
// Don't assume that prefix is atomized
for (int thisDecl = _lastDecl; thisDecl >= 0; thisDecl--)
{
if (string.Equals(_nsdecls[thisDecl].uri, uri))
{
string prefix = _nsdecls[thisDecl].prefix;
if (string.Equals(LookupNamespace(prefix), uri))
{
return prefix;
}
}
}
return null;
}
public virtual bool HasNamespace(string prefix)
{
Debug.Assert(_nsdecls != null);
// Don't assume that prefix is atomized
for (int thisDecl = _lastDecl; _nsdecls[thisDecl].scopeId == _scopeId; thisDecl--)
{
if (string.Equals(_nsdecls[thisDecl].prefix, prefix) && _nsdecls[thisDecl].uri != null)
{
if (prefix.Length > 0 || _nsdecls[thisDecl].uri!.Length > 0)
{
return true;
}
return false;
}
}
return false;
}
internal bool GetNamespaceDeclaration(int idx, [NotNullWhen(true)] out string? prefix, out string? uri)
{
idx = _lastDecl - idx;
if (idx < 0)
{
prefix = uri = null;
return false;
}
Debug.Assert(_nsdecls != null);
prefix = _nsdecls[idx].prefix;
uri = _nsdecls[idx].uri;
return true;
}
} //XmlNamespaceManager
}