Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XPathDocumentBuilder.cs
Go to the documentation of this file.
1using System;
5using System.Xml;
7
9
10internal sealed class XPathDocumentBuilder : XmlRawWriter
11{
12 private struct NodePageFactory
13 {
14 private XPathNode[] _page;
15
17
18 private int _pageSize;
19
21
23
24 public void Init(int initialPageSize)
25 {
28 _pageInfo = new XPathNodePageInfo(null, 1);
30 }
31
32 public void AllocateSlot(out XPathNode[] page, out int idx)
33 {
34 page = _page;
36 if (++_pageInfo.NodeCount >= _page.Length)
37 {
38 if (_pageSize < 65536)
39 {
40 _pageSize *= 2;
41 }
46 }
47 }
48 }
49
50 private struct TextBlockBuilder
51 {
53
55
56 private string _text;
57
58 private int _lineNum;
59
60 private int _linePos;
61
63
64 public bool HasText => _textType != TextBlockType.None;
65
66 public int LineNumber => _lineNum;
67
68 public int LinePosition => _linePos;
69
70 public void Initialize(IXmlLineInfo lineInfo)
71 {
72 _lineInfo = lineInfo;
74 }
75
77 {
78 if (string.IsNullOrEmpty(text))
79 {
80 return;
81 }
82 if (_textType == TextBlockType.None)
83 {
84 _text = text;
86 if (_lineInfo != null)
87 {
90 }
91 }
92 else
93 {
94 _text += text;
95 if (textType < _textType)
96 {
98 }
99 }
100 }
101
102 public string ReadText()
103 {
104 if (_textType == TextBlockType.None)
105 {
106 return string.Empty;
107 }
109 return _text;
110 }
111 }
112
114
116
118
120
122
124
126
128
129 private bool _atomizeNames;
130
132
133 private int _idxNmsp;
134
136
137 private int _idxParent;
138
140
141 private int _idxSibling;
142
143 private int _lineNumBase;
144
145 private int _linePosBase;
146
148
150
152
154 {
155 _nodePageFact.Init(256);
158 Initialize(doc, lineInfo, baseUri, flags);
159 }
160
161 [MemberNotNull("_doc")]
162 [MemberNotNull("_elemNameIndex")]
163 [MemberNotNull("_infoTable")]
164 [MemberNotNull("_nameTable")]
165 [MemberNotNull("_pageNmsp")]
166 public void Initialize(XPathDocument doc, IXmlLineInfo lineInfo, string baseUri, XPathDocument.LoadFlags flags)
167 {
168 _doc = doc;
169 _nameTable = doc.NameTable;
170 _atomizeNames = (flags & XPathDocument.LoadFlags.AtomizeNames) != 0;
171 _idxParent = (_idxSibling = 0);
173 _textBldr.Initialize(lineInfo);
174 _lineInfo = lineInfo;
175 _lineNumBase = 0;
176 _linePosBase = 0;
178 XPathNode[] page;
179 int idxText = NewNode(out page, XPathNodeType.Text, string.Empty, string.Empty, string.Empty, string.Empty);
181 _idxNmsp = NewNamespaceNode(out _pageNmsp, _nameTable.Add("xml"), _nameTable.Add("http://www.w3.org/XML/1998/namespace"), null, 0);
183 if ((flags & XPathDocument.LoadFlags.Fragment) == 0)
184 {
185 _idxParent = NewNode(out _pageParent, XPathNodeType.Root, string.Empty, string.Empty, string.Empty, baseUri);
187 }
188 else
189 {
191 }
192 }
193
194 public override void WriteDocType(string name, string pubid, string sysid, string subset)
195 {
196 }
197
198 public override void WriteStartElement(string prefix, string localName, string ns)
199 {
200 WriteStartElement(prefix, localName, ns, string.Empty);
201 }
202
203 public void WriteStartElement(string prefix, string localName, string ns, string baseUri)
204 {
205 if (_atomizeNames)
206 {
208 localName = _nameTable.Add(localName);
209 ns = _nameTable.Add(ns);
210 }
211 AddSibling(XPathNodeType.Element, localName, ns, prefix, baseUri);
214 _idxSibling = 0;
215 int num = _pageParent[_idxParent].LocalNameHashCode & 0x3F;
217 if (_elemIdMap != null)
218 {
220 }
221 }
222
223 public override void WriteEndElement()
224 {
226 }
227
228 public override void WriteFullEndElement()
229 {
231 }
232
233 internal override void WriteEndElement(string prefix, string localName, string namespaceName)
234 {
236 }
237
238 internal override void WriteFullEndElement(string prefix, string localName, string namespaceName)
239 {
241 }
242
244 {
245 if (!_pageParent[_idxParent].HasContentChild)
246 {
248 if (textType != TextBlockType.Text)
249 {
250 if ((uint)(textType - 5) > 1u)
251 {
253 goto IL_012d;
254 }
255 }
256 else
257 {
258 if (_lineInfo == null)
259 {
260 goto IL_00aa;
261 }
263 {
265 if (num >= 0 && num <= 255)
266 {
268 goto IL_00aa;
269 }
270 }
271 }
274 }
275 else if (_textBldr.HasText)
276 {
278 }
279 goto IL_012d;
280 IL_00aa:
282 goto IL_012d;
283 IL_012d:
284 if (_pageParent[_idxParent].HasNamespaceDecls)
285 {
288 _pageNmsp = xPathNodeRef.Page;
289 _idxNmsp = xPathNodeRef.Index;
290 }
294 }
295
296 public override void WriteStartAttribute(string prefix, string localName, string namespaceName)
297 {
298 if (_atomizeNames)
299 {
301 localName = _nameTable.Add(localName);
303 }
304 AddSibling(XPathNodeType.Attribute, localName, namespaceName, prefix, string.Empty);
305 }
306
316
317 public override void WriteCData(string text)
318 {
320 }
321
322 public override void WriteComment(string text)
323 {
324 AddSibling(XPathNodeType.Comment, string.Empty, string.Empty, string.Empty, string.Empty);
326 }
327
328 public override void WriteProcessingInstruction(string name, string text)
329 {
331 }
332
333 public void WriteProcessingInstruction(string name, string text, string baseUri)
334 {
335 if (_atomizeNames)
336 {
337 name = _nameTable.Add(name);
338 }
339 AddSibling(XPathNodeType.ProcessingInstruction, name, string.Empty, string.Empty, baseUri);
341 }
342
343 public override void WriteWhitespace(string ws)
344 {
345 WriteString(ws, TextBlockType.Whitespace);
346 }
347
348 public override void WriteString(string text)
349 {
351 }
352
353 public override void WriteChars(char[] buffer, int index, int count)
354 {
355 WriteString(new string(buffer, index, count), TextBlockType.Text);
356 }
357
358 public override void WriteRaw(string data)
359 {
360 WriteString(data, TextBlockType.Text);
361 }
362
363 public override void WriteRaw(char[] buffer, int index, int count)
364 {
365 WriteString(new string(buffer, index, count), TextBlockType.Text);
366 }
367
372
373 public override void WriteEntityRef(string name)
374 {
375 throw new NotImplementedException();
376 }
377
378 public override void WriteCharEntity(char ch)
379 {
381 }
382
383 public override void WriteSurrogateCharEntity(char lowChar, char highChar)
384 {
387 WriteString(new string(value), TextBlockType.Text);
388 }
389
390 public override void Close()
391 {
392 if (_textBldr.HasText)
393 {
395 }
399 {
400 AddSibling(XPathNodeType.Text, string.Empty, string.Empty, string.Empty, string.Empty);
402 }
403 }
404
405 public override void Flush()
406 {
407 }
408
410 {
411 }
412
413 internal override void WriteXmlDeclaration(string xmldecl)
414 {
415 }
416
417 internal override void StartElementContent()
418 {
419 }
420
421 internal override void WriteNamespaceDeclaration(string prefix, string namespaceName)
422 {
423 if (_atomizeNames)
424 {
426 }
429 int num = _idxNmsp;
430 while (num != 0 && (object)pageNode[num].LocalName != prefix)
431 {
432 num = pageNode[num].GetSibling(out pageNode);
433 }
434 XPathNode[] page;
436 if (num != 0)
437 {
439 int num3 = _idxNmsp;
440 XPathNode[] array = page;
441 int num4 = num2;
442 while (num3 != num || pageNode2 != pageNode)
443 {
444 int parent = pageNode2[num3].GetParent(out var pageNode3);
445 parent = NewNamespaceNode(out pageNode3, pageNode2[num3].LocalName, pageNode2[num3].Value, pageNode3, parent);
446 array[num4].SetSibling(_infoTable, pageNode3, parent);
448 num4 = parent;
449 num3 = pageNode2[num3].GetSibling(out pageNode2);
450 }
451 num = pageNode[num].GetSibling(out pageNode);
452 if (num != 0)
453 {
454 array[num4].SetSibling(_infoTable, pageNode, num);
455 }
456 }
457 else if (_idxParent != 0)
458 {
459 page[num2].SetSibling(_infoTable, _pageNmsp, _idxNmsp);
460 }
461 else
462 {
464 }
465 if (_idxParent != 0)
466 {
467 if (!_pageParent[_idxParent].HasNamespaceDecls)
468 {
471 }
472 _pageNmsp = page;
473 _idxNmsp = num2;
474 }
475 }
476
478 {
479 foreach (IDtdAttributeListInfo attributeList in dtdInfo.GetAttributeLists())
480 {
482 if (dtdAttributeInfo != null)
483 {
484 if (_elemIdMap == null)
485 {
486 _elemIdMap = new Hashtable();
487 }
489 }
490 }
491 }
492
498
499 private int NewNamespaceNode(out XPathNode[] page, string prefix, string namespaceUri, XPathNode[] pageElem, int idxElem)
500 {
504 page2[idx].Create(info, XPathNodeType.Namespace, idxElem);
505 page2[idx].SetValue(namespaceUri);
506 page2[idx].SetLineInfoOffsets(lineNumOffset, linePosOffset);
507 page = page2;
508 return idx;
509 }
510
511 private int NewNode(out XPathNode[] page, XPathNodeType xptyp, string localName, string namespaceUri, string prefix, string baseUri)
512 {
516 page2[idx].Create(info, xptyp, _idxParent);
517 page2[idx].SetLineInfoOffsets(lineNumOffset, linePosOffset);
518 page = page2;
519 return idx;
520 }
521
523 {
524 if (_lineInfo == null)
525 {
526 lineNumOffset = 0;
527 linePosOffset = 0;
528 return;
529 }
530 int lineNumber;
531 int linePosition;
532 if (isTextNode)
533 {
534 lineNumber = _textBldr.LineNumber;
535 linePosition = _textBldr.LinePosition;
536 }
537 else
538 {
539 lineNumber = _lineInfo.LineNumber;
540 linePosition = _lineInfo.LinePosition;
541 }
542 lineNumOffset = lineNumber - _lineNumBase;
544 {
545 _lineNumBase = lineNumber;
546 lineNumOffset = 0;
547 }
548 linePosOffset = linePosition - _linePosBase;
550 {
551 _linePosBase = linePosition;
552 linePosOffset = 0;
553 }
554 }
555
556 [MemberNotNull("_pageSibling")]
557 private void AddSibling(XPathNodeType xptyp, string localName, string namespaceUri, string prefix, string baseUri)
558 {
559 if (_textBldr.HasText)
560 {
562 }
563 XPathNode[] page;
564 int idxSibling = NewNode(out page, xptyp, localName, namespaceUri, prefix, baseUri);
565 if (_idxParent != 0)
566 {
568 if (_idxSibling != 0)
569 {
571 }
572 }
575 }
576
577 [MemberNotNull("_pageSibling")]
578 private void CachedTextNode()
579 {
581 string value = _textBldr.ReadText();
582 AddSibling((XPathNodeType)textType, string.Empty, string.Empty, string.Empty, string.Empty);
584 }
585}
int NewNode(out XPathNode[] page, XPathNodeType xptyp, string localName, string namespaceUri, string prefix, string baseUri)
override void WriteXmlDeclaration(string xmldecl)
void Initialize(XPathDocument doc, IXmlLineInfo lineInfo, string baseUri, XPathDocument.LoadFlags flags)
override void WriteStartAttribute(string prefix, string localName, string namespaceName)
override void WriteSurrogateCharEntity(char lowChar, char highChar)
override void WriteNamespaceDeclaration(string prefix, string namespaceName)
XPathDocumentBuilder(XPathDocument doc, IXmlLineInfo lineInfo, string baseUri, XPathDocument.LoadFlags flags)
override void WriteProcessingInstruction(string name, string text)
void ComputeLineInfo(bool isTextNode, out int lineNumOffset, out int linePosOffset)
override void WriteFullEndElement(string prefix, string localName, string namespaceName)
override void WriteStartElement(string prefix, string localName, string ns)
override void WriteChars(char[] buffer, int index, int count)
override void WriteRaw(char[] buffer, int index, int count)
void WriteStartElement(string prefix, string localName, string ns, string baseUri)
override void WriteDocType(string name, string pubid, string sysid, string subset)
XPathNodeRef LinkSimilarElements(XPathNode[] pagePrev, int idxPrev, XPathNode[] pageNext, int idxNext)
void WriteProcessingInstruction(string name, string text, string baseUri)
void WriteString(string text, TextBlockType textType)
override void WriteEndElement(string prefix, string localName, string namespaceName)
override void WriteXmlDeclaration(XmlStandalone standalone)
int NewNamespaceNode(out XPathNode[] page, string prefix, string namespaceUri, XPathNode[] pageElem, int idxElem)
void AddSibling(XPathNodeType xptyp, string localName, string namespaceUri, string prefix, string baseUri)
XPathNodeInfoAtom Create(string localName, string namespaceUri, string prefix, string baseUri, XPathNode[] pageParent, XPathNode[] pageSibling, XPathNode[] pageSimilar, XPathDocument doc, int lineNumBase, int linePosBase)
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
void SetRootNode(XPathNode[] pageRoot, int idxRoot)
int GetRootNode(out XPathNode[] pageRoot)
void AddIdElement(string id, XPathNode[] pageElem, int idxElem)
void AddNamespace(XPathNode[] pageElem, int idxElem, XPathNode[] pageNmsp, int idxNmsp)
void SetCollapsedTextNode(XPathNode[] pageText, int idxText)
void SetXmlNamespaceNode(XPathNode[] pageXmlNmsp, int idxXmlNmsp)
static bool IsText(XPathNodeType type)
string Add(char[] array, int offset, int length)
void WriteTextBlock(string text, TextBlockType textType)
void SetEmptyValue(bool allowShortcutTag)
Definition XPathNode.cs:185
void SetCollapsedValue(string value)
Definition XPathNode.cs:194
void SetValue(string value)
Definition XPathNode.cs:180
void SetSibling(XPathNodeInfoTable infoTable, XPathNode[] pageSibling, int idxSibling)
Definition XPathNode.cs:214
void Create(XPathNodePageInfo pageInfo)
Definition XPathNode.cs:157
int GetParent(out XPathNode[] pageNode)
Definition XPathNode.cs:121
void SetParentProperties(XPathNodeType xptyp)
Definition XPathNode.cs:200
void SetCollapsedLineInfoOffset(int posOffset)
Definition XPathNode.cs:175