Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RecordBuilder.cs
Go to the documentation of this file.
2using System.Text;
4
6
7internal sealed class RecordBuilder
8{
9 private int _outputState;
10
12
13 private readonly IRecordOutput _output;
14
15 private readonly XmlNameTable _nameTable;
16
17 private readonly OutKeywords _atoms;
18
20
21 private readonly BuilderInfo _mainNode = new BuilderInfo();
22
23 private readonly ArrayList _attributeList = new ArrayList();
24
25 private int _attributeCount;
26
27 private readonly ArrayList _namespaceList = new ArrayList();
28
29 private int _namespaceCount;
30
31 private readonly BuilderInfo _dummy = new BuilderInfo();
32
34
35 private bool _popScope;
36
37 private int _recordState;
38
39 private int _recordDepth;
40
41 internal int OutputState
42 {
43 get
44 {
45 return _outputState;
46 }
47 set
48 {
50 }
51 }
52
54 {
55 get
56 {
57 return _next;
58 }
59 set
60 {
61 _next = value;
62 }
63 }
64
66
68
70
72
74
82
87
88 private bool CanOutput(int state)
89 {
90 if (_recordState == 0 || (state & 0x2000) == 0)
91 {
92 return true;
93 }
94 _recordState = 2;
97 return _output.RecordDone(this) == Processor.OutputResult.Continue;
98 }
99
100 internal Processor.OutputResult BeginEvent(int state, XPathNodeType nodeType, string prefix, string name, string nspace, bool empty, object htmlProps, bool search)
101 {
102 if (!CanOutput(state))
103 {
104 return Processor.OutputResult.Overflow;
105 }
109 prefix = ((prefix != null) ? _nameTable.Add(prefix) : _atoms.Empty);
110 name = ((name != null) ? _nameTable.Add(name) : _atoms.Empty);
111 nspace = ((nspace != null) ? _nameTable.Add(nspace) : _atoms.Empty);
112 switch (nodeType)
113 {
114 case XPathNodeType.Element:
116 _mainNode.search = search;
118 break;
119 case XPathNodeType.Attribute:
120 BeginAttribute(prefix, name, nspace, htmlProps, search);
121 break;
122 case XPathNodeType.Namespace:
123 BeginNamespace(name, nspace);
124 break;
125 case XPathNodeType.ProcessingInstruction:
127 {
128 return Processor.OutputResult.Error;
129 }
130 break;
131 case XPathNodeType.Comment:
132 BeginComment();
133 break;
134 }
135 return CheckRecordBegin(state);
136 }
137
139 {
140 if (!CanOutput(state))
141 {
142 return Processor.OutputResult.Overflow;
143 }
147 if (((uint)state & 0x2000u) != 0)
148 {
151 }
153 return CheckRecordBegin(state);
154 }
155
157 {
158 if (!CanOutput(state))
159 {
160 return Processor.OutputResult.Overflow;
161 }
164 _popScope = (state & 0x10000) != 0;
165 if (((uint)state & 0x1000u) != 0 && _mainNode.IsEmptyTag)
166 {
167 return Processor.OutputResult.Continue;
168 }
170 if (((uint)state & 0x2000u) != 0 && nodeType == XPathNodeType.Element)
171 {
172 EndElement();
173 }
174 return CheckRecordEnd(state);
175 }
176
177 internal void Reset()
178 {
179 if (_recordState == 2)
180 {
181 _recordState = 0;
182 }
183 }
184
185 internal void TheEnd()
186 {
187 if (_recordState == 1)
188 {
189 _recordState = 2;
191 _output.RecordDone(this);
192 }
193 _output.TheEnd();
194 }
195
196 private int FindAttribute(string name, string nspace, ref string prefix)
197 {
198 for (int i = 0; i < _attributeCount; i++)
199 {
201 if (Ref.Equal(builderInfo.LocalName, name))
202 {
203 if (Ref.Equal(builderInfo.NamespaceURI, nspace))
204 {
205 return i;
206 }
207 if (Ref.Equal(builderInfo.Prefix, prefix))
208 {
209 prefix = string.Empty;
210 }
211 }
212 }
213 return -1;
214 }
215
226
236
237 private int NewAttribute()
238 {
240 {
242 }
243 return _attributeCount++;
244 }
245
246 private void BeginAttribute(string prefix, string name, string nspace, object htmlAttrProps, bool search)
247 {
248 int num = FindAttribute(name, nspace, ref prefix);
249 if (num == -1)
250 {
251 num = NewAttribute();
252 }
254 builderInfo.Initialize(prefix, name, nspace);
258 builderInfo.search = search;
260 }
261
262 private void BeginNamespace(string name, string nspace)
263 {
264 bool thisScope = false;
265 if (Ref.Equal(name, _atoms.Empty))
266 {
268 {
270 }
271 }
272 else
273 {
275 if (text != null)
276 {
277 if (!Ref.Equal(nspace, text) && !thisScope)
278 {
280 }
281 }
282 else
283 {
285 }
286 }
289 }
290
291 private bool BeginProcessingInstruction(string prefix, string name, string nspace)
292 {
293 _currentInfo.NodeType = XmlNodeType.ProcessingInstruction;
298 return true;
299 }
300
301 private void BeginComment()
302 {
305 }
306
307 private void AdjustDepth(int state)
308 {
309 switch (state & 0x300)
310 {
311 case 256:
312 _recordDepth++;
313 break;
314 case 512:
315 _recordDepth--;
316 break;
317 }
318 }
319
320 private void ResetRecord(int state)
321 {
322 if (((uint)state & 0x2000u) != 0)
323 {
324 _attributeCount = 0;
325 _namespaceCount = 0;
332 }
333 }
334
335 private void PopElementScope()
336 {
337 if (_popScope)
338 {
340 _popScope = false;
341 }
342 }
343
345 {
346 if (((uint)state & 0x4000u) != 0)
347 {
348 _recordState = 2;
351 return _output.RecordDone(this);
352 }
353 _recordState = 1;
354 return Processor.OutputResult.Continue;
355 }
356
358 {
359 if (((uint)state & 0x4000u) != 0)
360 {
361 _recordState = 2;
364 return _output.RecordDone(this);
365 }
366 return Processor.OutputResult.Continue;
367 }
368
369 private void SetEmptyFlag(int state)
370 {
371 if (((uint)state & 0x400u) != 0)
372 {
373 _mainNode.IsEmptyTag = false;
374 }
375 }
376
377 private void AnalyzeSpaceLang()
378 {
379 for (int i = 0; i < _attributeCount; i++)
380 {
382 if (Ref.Equal(builderInfo.Prefix, _atoms.Xml))
383 {
385 if (Ref.Equal(builderInfo.LocalName, _atoms.Lang))
386 {
388 }
389 else if (Ref.Equal(builderInfo.LocalName, _atoms.Space))
390 {
392 }
393 }
394 }
395 }
396
436
437 private void FixupAttributes(int attributeCount)
438 {
439 for (int i = 0; i < attributeCount; i++)
440 {
442 if (Ref.Equal(builderInfo.NamespaceURI, _atoms.Empty))
443 {
445 continue;
446 }
447 if (Ref.Equal(builderInfo.Prefix, _atoms.Empty))
448 {
450 continue;
451 }
452 bool thisScope = false;
454 if (text != null)
455 {
456 if (!Ref.Equal(builderInfo.NamespaceURI, text))
457 {
458 if (thisScope)
459 {
461 }
462 else
463 {
464 DeclareNamespace(builderInfo.NamespaceURI, builderInfo.Prefix);
465 }
466 }
467 }
468 else
469 {
470 DeclareNamespace(builderInfo.NamespaceURI, builderInfo.Prefix);
471 }
472 }
473 }
474
475 private void AppendNamespaces()
476 {
477 for (int num = _namespaceCount - 1; num >= 0; num--)
478 {
480 builderInfo.Initialize((BuilderInfo)_namespaceList[num]);
481 }
482 }
483
484 private void AnalyzeComment()
485 {
487 string value = _mainNode.Value;
488 bool flag = false;
489 int i = 0;
490 int num = 0;
491 for (; i < value.Length; i++)
492 {
493 char c = value[i];
494 if (c == '-')
495 {
496 if (flag)
497 {
498 if (stringBuilder == null)
499 {
500 stringBuilder = new StringBuilder(value, num, i, 2 * value.Length);
501 }
502 else
503 {
504 stringBuilder.Append(value, num, i - num);
505 }
506 stringBuilder.Append(" -");
507 num = i + 1;
508 }
509 flag = true;
510 }
511 else
512 {
513 flag = false;
514 }
515 }
516 if (stringBuilder != null)
517 {
518 if (num < value.Length)
519 {
520 stringBuilder.Append(value, num, value.Length - num);
521 }
522 if (flag)
523 {
524 stringBuilder.Append(' ');
525 }
526 _mainNode.Value = stringBuilder.ToString();
527 }
528 else if (flag)
529 {
531 }
532 }
533
535 {
537 string value = _mainNode.Value;
538 bool flag = false;
539 int i = 0;
540 int num = 0;
541 for (; i < value.Length; i++)
542 {
543 switch (value[i])
544 {
545 case '?':
546 flag = true;
547 break;
548 case '>':
549 if (flag)
550 {
551 if (stringBuilder == null)
552 {
553 stringBuilder = new StringBuilder(value, num, i, 2 * value.Length);
554 }
555 else
556 {
557 stringBuilder.Append(value, num, i - num);
558 }
559 stringBuilder.Append(" >");
560 num = i + 1;
561 }
562 flag = false;
563 break;
564 default:
565 flag = false;
566 break;
567 }
568 }
569 if (stringBuilder != null)
570 {
571 if (num < value.Length)
572 {
573 stringBuilder.Append(value, num, value.Length - num);
574 }
575 _mainNode.Value = stringBuilder.ToString();
576 }
577 }
578
579 private void FinalizeRecord()
580 {
581 switch (_mainNode.NodeType)
582 {
583 case XmlNodeType.Element:
584 {
585 int attributeCount = _attributeCount;
586 FixupElement();
587 FixupAttributes(attributeCount);
590 break;
591 }
592 case XmlNodeType.Comment:
594 break;
595 case XmlNodeType.ProcessingInstruction:
597 break;
598 }
599 }
600
601 private int NewNamespace()
602 {
604 {
606 }
607 return _namespaceCount++;
608 }
609
627
628 private string DeclareNewNamespace(string nspace)
629 {
630 string text = _scopeManager.GeneratePrefix("xp_{0}");
632 return text;
633 }
634
635 internal string GetPrefixForNamespace(string nspace)
636 {
637 string prefix = null;
639 {
640 return prefix;
641 }
643 }
644
645 private static XmlSpace TranslateXmlSpace(string space)
646 {
647 if (space == "default")
648 {
649 return XmlSpace.Default;
650 }
651 if (space == "preserve")
652 {
653 return XmlSpace.Preserve;
654 }
655 return XmlSpace.None;
656 }
657}
virtual int Add(object? value)
static bool Equal(string strA, string strB)
Definition Ref.cs:5
string Add(char[] array, int offset, int length)
void ValueAppend(string s, bool disableEscaping)
void Initialize(string prefix, string name, string nspace)
bool FindPrefix(string nspace, out string prefix)
void PushScope(string name, string nspace, string prefix)
void PushNamespace(string prefix, string nspace)
readonly OutputScopeManager _scopeManager
void FixupAttributes(int attributeCount)
RecordBuilder(IRecordOutput output, XmlNameTable nameTable)
int FindAttribute(string name, string nspace, ref string prefix)
string GetPrefixForNamespace(string nspace)
Processor.OutputResult BeginEvent(int state, XPathNodeType nodeType, string prefix, string name, string nspace, bool empty, object htmlProps, bool search)
static XmlSpace TranslateXmlSpace(string space)
string DeclareNewNamespace(string nspace)
void BeginAttribute(string prefix, string name, string nspace, object htmlAttrProps, bool search)
void ValueAppend(string s, bool disableOutputEscaping)
Processor.OutputResult EndEvent(int state, XPathNodeType nodeType)
Processor.OutputResult CheckRecordEnd(int state)
void DeclareNamespace(string nspace, string prefix)
bool BeginProcessingInstruction(string prefix, string name, string nspace)
Processor.OutputResult CheckRecordBegin(int state)
void BeginNamespace(string name, string nspace)
void BeginElement(string prefix, string name, string nspace, bool empty)
Processor.OutputResult TextEvent(int state, string text, bool disableOutputEscaping)
Processor.OutputResult RecordDone(RecordBuilder record)