Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SequentialOutput.cs
Go to the documentation of this file.
4using System.Text;
5
7
8internal abstract class SequentialOutput : IRecordOutput
9{
10 private static readonly char[] s_TextValueFind = new char[3] { '&', '>', '<' };
11
12 private static readonly string[] s_TextValueReplace = new string[3] { "&amp;", "&gt;", "&lt;" };
13
14 private static readonly char[] s_XmlAttributeValueFind = new char[6] { '&', '>', '<', '"', '\n', '\r' };
15
16 private static readonly string[] s_XmlAttributeValueReplace = new string[6] { "&amp;", "&gt;", "&lt;", "&quot;", "&#xA;", "&#xD;" };
17
18 private readonly Processor _processor;
19
20 protected Encoding encoding;
21
23
24 private bool _firstLine = true;
25
26 private bool _secondRoot;
27
29
30 private bool _isHtmlOutput;
31
32 private bool _isXmlOutput;
33
35
36 private bool _indentOutput;
37
38 private bool _outputDoctype;
39
40 private bool _outputXmlDecl;
41
42 private bool _omitXmlDeclCalled;
43
44 private byte[] _byteBuffer;
45
47
48 [MemberNotNull("_output")]
59
65
66 public void OmitXmlDecl()
67 {
68 _omitXmlDeclCalled = true;
69 _outputXmlDecl = false;
70 }
71
73 {
74 BuilderInfo mainNode = record.MainNode;
76 if (_isHtmlOutput)
77 {
78 if (mainNode.Prefix.Length == 0)
79 {
80 htmlElementProps = mainNode.htmlProps;
81 if (htmlElementProps == null && mainNode.search)
82 {
84 }
86 mainNode.IsEmptyTag = false;
87 }
88 }
89 else if (_isXmlOutput && mainNode.Depth == 0)
90 {
92 {
94 }
95 _secondRoot = true;
96 }
98 {
100 _outputDoctype = false;
101 }
102 if (_cdataElements != null && _cdataElements.Contains(new XmlQualifiedName(mainNode.LocalName, mainNode.NamespaceURI)) && _isXmlOutput)
103 {
105 }
106 Indent(record);
107 Write('<');
108 WriteName(mainNode.Prefix, mainNode.LocalName);
109 WriteAttributes(record.AttributeList, record.AttributeCount, htmlElementProps);
110 if (mainNode.IsEmptyTag)
111 {
112 Write(" />");
113 }
114 else
115 {
116 Write('>');
117 }
118 if (htmlElementProps != null && htmlElementProps.Head)
119 {
120 mainNode.Depth++;
121 Indent(record);
122 mainNode.Depth--;
123 Write("<META http-equiv=\"Content-Type\" content=\"");
125 Write("; charset=");
127 Write("\">");
128 }
129 }
130
132 {
133 BuilderInfo mainNode = record.MainNode;
134 OutputScope currentElementScope = record.Manager.CurrentElementScope;
136 if (currentElementScope.HtmlElementProps != null && currentElementScope.HtmlElementProps.NoEntities)
137 {
138 Write(mainNode.Value);
139 }
140 else if (currentElementScope.ToCData)
141 {
143 }
144 else
145 {
147 }
148 }
149
151 {
152 for (int i = 0; i < node.TextInfoCount; i++)
153 {
154 string text = node.TextInfo[i];
155 if (text == null)
156 {
157 i++;
158 Write(node.TextInfo[i]);
159 }
160 else
161 {
163 }
164 }
165 }
166
167 private void WriteCDataSection(string value)
168 {
169 Write("<![CDATA[");
171 Write("]]>");
172 }
173
175 {
176 Indent(0);
177 Write("<!DOCTYPE ");
178 if (_isXmlOutput)
179 {
180 WriteName(mainNode.Prefix, mainNode.LocalName);
181 }
182 else
183 {
184 WriteName(string.Empty, "html");
185 }
186 Write(' ');
187 if (_output.DoctypePublic != null)
188 {
189 Write("PUBLIC ");
190 Write('"');
192 Write("\" ");
193 }
194 else
195 {
196 Write("SYSTEM ");
197 }
198 if (_output.DoctypeSystem != null)
199 {
200 Write('"');
202 Write('"');
203 }
204 Write('>');
205 }
206
207 private void WriteXmlDeclaration()
208 {
209 _outputXmlDecl = false;
210 Indent(0);
211 Write("<?");
212 WriteName(string.Empty, "xml");
213 Write(" version=\"1.0\"");
214 if (encoding != null)
215 {
216 Write(" encoding=\"");
218 Write('"');
219 }
221 {
222 Write(" standalone=\"");
223 Write(_output.Standalone ? "yes" : "no");
224 Write('"');
225 }
226 Write("?>");
227 }
228
234
236 {
237 Write("<?");
238 WriteName(node.Prefix, node.LocalName);
239 Write(' ');
240 Write(node.Value);
241 if (_isHtmlOutput)
242 {
243 Write('>');
244 }
245 else
246 {
247 Write("?>");
248 }
249 }
250
252 {
253 HtmlElementProps htmlElementProps = record.Manager.CurrentElementScope.HtmlElementProps;
254 if (htmlElementProps == null || !htmlElementProps.Empty)
255 {
256 Indent(record);
257 Write("</");
258 WriteName(record.MainNode.Prefix, record.MainNode.LocalName);
259 Write('>');
260 }
261 }
262
264 {
265 if (_output.Method == XsltOutput.OutputMethod.Unknown)
266 {
267 if (!DecideDefaultOutput(record.MainNode))
268 {
270 }
271 else
272 {
275 }
276 }
277 else
278 {
280 }
281 record.Reset();
282 return Processor.OutputResult.Continue;
283 }
284
285 public void TheEnd()
286 {
288 Close();
289 }
290
292 {
294 switch (node.NodeType)
295 {
296 case XmlNodeType.Element:
297 if (node.NamespaceURI.Length == 0 && string.Equals("html", node.LocalName, StringComparison.OrdinalIgnoreCase))
298 {
300 }
301 break;
302 case XmlNodeType.Text:
303 case XmlNodeType.Whitespace:
304 case XmlNodeType.SignificantWhitespace:
306 {
307 return false;
308 }
310 break;
311 default:
312 return false;
313 }
315 {
317 }
318 return true;
319 }
320
322 {
323 if (_outputCache == null)
324 {
325 _outputCache = new ArrayList();
326 }
327 _outputCache.Add(record.MainNode.Clone());
328 }
329
330 private void OutputCachedRecords()
331 {
332 if (_outputCache != null)
333 {
334 for (int i = 0; i < _outputCache.Count; i++)
335 {
338 }
339 _outputCache = null;
340 }
341 }
342
344 {
345 BuilderInfo mainNode = record.MainNode;
346 if (_outputXmlDecl)
347 {
349 }
350 switch (mainNode.NodeType)
351 {
352 case XmlNodeType.Element:
354 break;
355 case XmlNodeType.Text:
356 case XmlNodeType.Whitespace:
357 case XmlNodeType.SignificantWhitespace:
359 break;
360 case XmlNodeType.EntityReference:
361 Write('&');
362 WriteName(mainNode.Prefix, mainNode.LocalName);
363 Write(';');
364 break;
365 case XmlNodeType.ProcessingInstruction:
367 break;
368 case XmlNodeType.Comment:
369 Indent(record);
370 Write("<!--");
371 Write(mainNode.Value);
372 Write("-->");
373 break;
374 case XmlNodeType.DocumentType:
375 Write(mainNode.Value);
376 break;
377 case XmlNodeType.EndElement:
379 break;
380 case XmlNodeType.Attribute:
381 case XmlNodeType.CDATA:
382 case XmlNodeType.Entity:
383 case XmlNodeType.Document:
384 case XmlNodeType.DocumentFragment:
385 case XmlNodeType.Notation:
386 break;
387 }
388 }
389
391 {
392 if (_outputXmlDecl)
393 {
395 }
396 Indent(0);
397 switch (node.NodeType)
398 {
399 case XmlNodeType.Text:
400 case XmlNodeType.Whitespace:
401 case XmlNodeType.SignificantWhitespace:
403 break;
404 case XmlNodeType.EntityReference:
405 Write('&');
406 WriteName(node.Prefix, node.LocalName);
407 Write(';');
408 break;
409 case XmlNodeType.ProcessingInstruction:
411 break;
412 case XmlNodeType.Comment:
413 Write("<!--");
414 Write(node.Value);
415 Write("-->");
416 break;
417 case XmlNodeType.DocumentType:
418 Write(node.Value);
419 break;
420 case XmlNodeType.Element:
421 case XmlNodeType.Attribute:
422 case XmlNodeType.CDATA:
423 case XmlNodeType.Entity:
424 case XmlNodeType.Document:
425 case XmlNodeType.DocumentFragment:
426 case XmlNodeType.Notation:
427 case XmlNodeType.EndElement:
428 break;
429 }
430 }
431
432 private void WriteName(string prefix, string name)
433 {
434 if (prefix != null && prefix.Length > 0)
435 {
436 Write(prefix);
437 if (name == null || name.Length <= 0)
438 {
439 return;
440 }
441 Write(':');
442 }
443 Write(name);
444 }
445
450
451 private void WriteHtmlAttributeValue(string value)
452 {
453 int length = value.Length;
454 int num = 0;
455 while (num < length)
456 {
457 char c = value[num];
458 num++;
459 switch (c)
460 {
461 case '&':
462 if (num != length && value[num] == '{')
463 {
464 Write(c);
465 }
466 else
467 {
468 Write("&amp;");
469 }
470 break;
471 case '"':
472 Write("&quot;");
473 break;
474 default:
475 Write(c);
476 break;
477 }
478 }
479 }
480
481 private void WriteHtmlUri(string value)
482 {
483 int length = value.Length;
484 int num = 0;
485 while (num < length)
486 {
487 char c = value[num];
488 num++;
489 switch (c)
490 {
491 case '&':
492 if (num != length && value[num] == '{')
493 {
494 Write(c);
495 }
496 else
497 {
498 Write("&amp;");
499 }
500 continue;
501 case '"':
502 Write("&quot;");
503 continue;
504 case '\n':
505 Write("&#xA;");
506 continue;
507 case '\r':
508 Write("&#xD;");
509 continue;
510 }
511 if ('\u007f' < c)
512 {
513 if (_utf8Encoding == null)
514 {
517 }
518 int bytes = _utf8Encoding.GetBytes(value, num - 1, 1, _byteBuffer, 0);
519 for (int i = 0; i < bytes; i++)
520 {
521 Write("%");
522 uint num2 = _byteBuffer[i];
523 Write(num2.ToString("X2", CultureInfo.InvariantCulture));
524 }
525 }
526 else
527 {
528 Write(c);
529 }
530 }
531 }
532
533 private void WriteWithReplace(string value, char[] find, string[] replace)
534 {
535 int length = value.Length;
536 int i;
537 for (i = 0; i < length; i++)
538 {
539 int num = value.IndexOfAny(find, i);
540 if (num == -1)
541 {
542 break;
543 }
544 for (; i < num; i++)
545 {
546 Write(value[i]);
547 }
548 char c = value[i];
549 int num2 = find.Length - 1;
550 while (0 <= num2)
551 {
552 if (find[num2] == c)
553 {
555 break;
556 }
557 num2--;
558 }
559 }
560 if (i == 0)
561 {
562 Write(value);
563 return;
564 }
565 for (; i < length; i++)
566 {
567 Write(value[i]);
568 }
569 }
570
571 private void WriteCData(string value)
572 {
573 Write(value.Replace("]]>", "]]]]><![CDATA[>"));
574 }
575
577 {
578 for (int i = 0; i < count; i++)
579 {
581 string value = builderInfo.Value;
582 bool flag = false;
583 bool flag2 = false;
584 if (htmlElementsProps != null && builderInfo.Prefix.Length == 0)
585 {
587 if (htmlAttributeProps == null && builderInfo.search)
588 {
590 }
591 if (htmlAttributeProps != null)
592 {
595 }
596 }
597 Write(' ');
598 WriteName(builderInfo.Prefix, builderInfo.LocalName);
599 if (!flag || !string.Equals(builderInfo.LocalName, value, StringComparison.OrdinalIgnoreCase))
600 {
601 Write("=\"");
602 if (flag2)
603 {
605 }
606 else if (_isHtmlOutput)
607 {
609 }
610 else
611 {
613 }
614 Write('"');
615 }
616 }
617 }
618
620 {
621 if (!record.Manager.CurrentElementScope.Mixed)
622 {
623 Indent(record.MainNode.Depth);
624 }
625 }
626
627 private void Indent(int depth)
628 {
629 if (_firstLine)
630 {
631 if (_indentOutput)
632 {
633 _firstLine = false;
634 }
635 return;
636 }
637 Write("\r\n");
638 int num = 2 * depth;
639 while (0 < num)
640 {
641 Write(" ");
642 num--;
643 }
644 }
645
646 internal abstract void Write(char outputChar);
647
648 internal abstract void Write(string outputText);
649
650 internal abstract void Close();
651}
virtual int Add(object? value)
virtual bool Contains(object key)
Definition Hashtable.cs:719
static CultureInfo InvariantCulture
static string Xslt_MultipleRoots
Definition SR.cs:2128
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
virtual string WebName
Definition Encoding.cs:386
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
int GetMaxByteCount(int charCount)
static bool IsOnlyWhitespace(string str)
static XsltException Create(string res, params string[] args)
static HtmlAttributeProps GetProps(string name)
static HtmlElementProps GetProps(string name)
bool SetDefaultOutput(XsltOutput.OutputMethod method)
Definition Processor.cs:728
Processor.OutputResult RecordDone(RecordBuilder record)
void WriteProcessingInstruction(BuilderInfo node)
void WriteEndElement(RecordBuilder record)
static readonly string[] s_XmlAttributeValueReplace
void WriteDoctype(BuilderInfo mainNode)
void WriteStartElement(RecordBuilder record)
void OutputRecord(RecordBuilder record)
static readonly string[] s_TextValueReplace
void WriteAttributes(ArrayList list, int count, HtmlElementProps htmlElementsProps)
static readonly char[] s_XmlAttributeValueFind
void WriteName(string prefix, string name)
void WriteProcessingInstruction(RecordBuilder record)
void WriteWithReplace(string value, char[] find, string[] replace)
void WriteTextNode(RecordBuilder record)