Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlNamespaceManager.cs
Go to the documentation of this file.
4
5namespace System.Xml;
6
8{
9 private struct NamespaceDeclaration
10 {
11 public string prefix;
12
13 public string uri;
14
15 public int scopeId;
16
17 public int previousNsIndex;
18
19 public void Set(string prefix, string uri, int scopeId, int previousNsIndex)
20 {
22 this.uri = uri;
25 }
26 }
27
29
30 private int _lastDecl;
31
32 private readonly XmlNameTable _nameTable;
33
34 private int _scopeId;
35
37
38 private bool _useHashtable;
39
40 private readonly string _xml;
41
42 private readonly string _xmlNs;
43
44 public virtual XmlNameTable? NameTable => _nameTable;
45
46 public virtual string DefaultNamespace
47 {
48 get
49 {
50 string text = LookupNamespace(string.Empty);
51 if (text != null)
52 {
53 return text;
54 }
55 return string.Empty;
56 }
57 }
58
60 {
61 }
62
64 {
66 _xml = nameTable.Add("xml");
67 _xmlNs = nameTable.Add("xmlns");
69 string text = nameTable.Add(string.Empty);
70 _nsdecls[0].Set(text, text, -1, -1);
71 _nsdecls[1].Set(_xmlNs, nameTable.Add("http://www.w3.org/2000/xmlns/"), -1, -1);
72 _nsdecls[2].Set(_xml, nameTable.Add("http://www.w3.org/XML/1998/namespace"), 0, -1);
73 _lastDecl = 2;
74 _scopeId = 1;
75 }
76
77 public virtual void PushScope()
78 {
79 _scopeId++;
80 }
81
82 public virtual bool PopScope()
83 {
84 int num = _lastDecl;
85 if (_scopeId == 1)
86 {
87 return false;
88 }
89 while (_nsdecls[num].scopeId == _scopeId)
90 {
91 if (_useHashtable)
92 {
94 }
95 num--;
96 }
97 _lastDecl = num;
98 _scopeId--;
99 return true;
100 }
101
102 public virtual void AddNamespace(string prefix, string uri)
103 {
104 if (uri == null)
105 {
106 throw new ArgumentNullException("uri");
107 }
108 if (prefix == null)
109 {
110 throw new ArgumentNullException("prefix");
111 }
113 uri = _nameTable.Add(uri);
114 if (Ref.Equal(_xml, prefix) && !uri.Equals("http://www.w3.org/XML/1998/namespace"))
115 {
117 }
118 if (Ref.Equal(_xmlNs, prefix))
119 {
121 }
122 int num = LookupNamespaceDecl(prefix);
123 int previousNsIndex = -1;
124 if (num != -1)
125 {
126 if (_nsdecls[num].scopeId == _scopeId)
127 {
128 _nsdecls[num].uri = uri;
129 return;
130 }
131 previousNsIndex = num;
132 }
133 if (_lastDecl == _nsdecls.Length - 1)
134 {
136 Array.Copy(_nsdecls, array, _nsdecls.Length);
137 _nsdecls = array;
138 }
139 _nsdecls[++_lastDecl].Set(prefix, uri, _scopeId, previousNsIndex);
140 if (_useHashtable)
141 {
143 }
144 else if (_lastDecl >= 16)
145 {
147 for (int i = 0; i <= _lastDecl; i++)
148 {
149 _hashTable[_nsdecls[i].prefix] = i;
150 }
151 _useHashtable = true;
152 }
153 }
154
155 public virtual void RemoveNamespace(string prefix, string uri)
156 {
157 if (uri == null)
158 {
159 throw new ArgumentNullException("uri");
160 }
161 if (prefix == null)
162 {
163 throw new ArgumentNullException("prefix");
164 }
165 for (int num = LookupNamespaceDecl(prefix); num != -1; num = _nsdecls[num].previousNsIndex)
166 {
167 if (string.Equals(_nsdecls[num].uri, uri) && _nsdecls[num].scopeId == _scopeId)
168 {
169 _nsdecls[num].uri = null;
170 }
171 }
172 }
173
174 public virtual IEnumerator GetEnumerator()
175 {
177 for (int i = 0; i <= _lastDecl; i++)
178 {
179 if (_nsdecls[i].uri != null)
180 {
182 }
183 }
184 return dictionary.Keys.GetEnumerator();
185 }
186
188 {
189 int i = 0;
190 switch (scope)
191 {
192 case XmlNamespaceScope.All:
193 i = 2;
194 break;
195 case XmlNamespaceScope.ExcludeXml:
196 i = 3;
197 break;
198 case XmlNamespaceScope.Local:
199 i = _lastDecl;
200 while (_nsdecls[i].scopeId == _scopeId)
201 {
202 i--;
203 }
204 i++;
205 break;
206 }
208 for (; i <= _lastDecl; i++)
209 {
210 string prefix = _nsdecls[i].prefix;
211 string uri = _nsdecls[i].uri;
212 if (uri != null)
213 {
214 if (uri.Length > 0 || prefix.Length > 0 || scope == XmlNamespaceScope.Local)
215 {
216 dictionary[prefix] = uri;
217 }
218 else
219 {
220 dictionary.Remove(prefix);
221 }
222 }
223 }
224 return dictionary;
225 }
226
227 public virtual string? LookupNamespace(string prefix)
228 {
229 int num = LookupNamespaceDecl(prefix);
230 if (num != -1)
231 {
232 return _nsdecls[num].uri;
233 }
234 return null;
235 }
236
237 private int LookupNamespaceDecl(string prefix)
238 {
239 if (_useHashtable)
240 {
242 {
243 while (value != -1 && _nsdecls[value].uri == null)
244 {
246 }
247 return value;
248 }
249 return -1;
250 }
251 for (int num = _lastDecl; num >= 0; num--)
252 {
253 if ((object)_nsdecls[num].prefix == prefix && _nsdecls[num].uri != null)
254 {
255 return num;
256 }
257 }
258 for (int num2 = _lastDecl; num2 >= 0; num2--)
259 {
260 if (string.Equals(_nsdecls[num2].prefix, prefix) && _nsdecls[num2].uri != null)
261 {
262 return num2;
263 }
264 }
265 return -1;
266 }
267
268 public virtual string? LookupPrefix(string uri)
269 {
270 for (int num = _lastDecl; num >= 0; num--)
271 {
272 if (string.Equals(_nsdecls[num].uri, uri))
273 {
274 string prefix = _nsdecls[num].prefix;
275 if (string.Equals(LookupNamespace(prefix), uri))
276 {
277 return prefix;
278 }
279 }
280 }
281 return null;
282 }
283
284 public virtual bool HasNamespace(string prefix)
285 {
286 int num = _lastDecl;
287 while (_nsdecls[num].scopeId == _scopeId)
288 {
289 if (string.Equals(_nsdecls[num].prefix, prefix) && _nsdecls[num].uri != null)
290 {
291 if (prefix.Length > 0 || _nsdecls[num].uri.Length > 0)
292 {
293 return true;
294 }
295 return false;
296 }
297 num--;
298 }
299 return false;
300 }
301
302 internal bool GetNamespaceDeclaration(int idx, [NotNullWhen(true)] out string prefix, out string uri)
303 {
304 idx = _lastDecl - idx;
305 if (idx < 0)
306 {
307 prefix = (uri = null);
308 return false;
309 }
311 uri = _nsdecls[idx].uri;
312 return true;
313 }
314}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
void Add(TKey key, TValue value)
static string Xml_XmlPrefix
Definition SR.cs:304
static string Xml_XmlnsPrefix
Definition SR.cs:306
Definition SR.cs:7
static bool Equal(string strA, string strB)
Definition Ref.cs:5
string Add(char[] array, int offset, int length)
virtual ? string LookupNamespace(string prefix)
virtual ? string LookupPrefix(string uri)
virtual IDictionary< string, string > GetNamespacesInScope(XmlNamespaceScope scope)
XmlNamespaceManager(XmlNameTable nameTable)
virtual bool HasNamespace(string prefix)
bool GetNamespaceDeclaration(int idx, [NotNullWhen(true)] out string prefix, out string uri)
virtual void AddNamespace(string prefix, string uri)
virtual void RemoveNamespace(string prefix, string uri)
Dictionary< string, int > _hashTable
void Set(string prefix, string uri, int scopeId, int previousNsIndex)