-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathXmlNormalizationState.cs
More file actions
107 lines (94 loc) · 2.62 KB
/
XmlNormalizationState.cs
File metadata and controls
107 lines (94 loc) · 2.62 KB
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
using System.Collections.Generic;
using System.Text;
using Waher.Runtime.Collections;
namespace Waher.Content.Xml
{
/// <summary>
/// Current state of XML normalization process.
/// </summary>
public class XmlNormalizationState
{
private readonly StringBuilder output = new StringBuilder();
private readonly Dictionary<string, string> namespceByPrefix = new Dictionary<string, string>();
private readonly ChunkedList<ChunkedList<KeyValuePair<string, string>>> prefixStack = new ChunkedList<ChunkedList<KeyValuePair<string, string>>>();
private ChunkedList<KeyValuePair<string, string>> newPrefixes = null;
/// <summary>
/// Current state of XML normalization process.
/// </summary>
public XmlNormalizationState()
{
}
/// <summary>
/// Appends a string to the output.
/// </summary>
/// <param name="s">String to append.</param>
public void Append(string s)
{
this.output.Append(s);
}
/// <summary>
/// Appends a character to the output.
/// </summary>
/// <param name="ch">Character to append.</param>
public void Append(char ch)
{
this.output.Append(ch);
}
/// <summary>
/// XML output.
/// </summary>
/// <returns>Normalized XML.</returns>
public override string ToString()
{
return this.output.ToString();
}
/// <summary>
/// Registers prefix for element.
/// </summary>
/// <param name="Prefix">Prefix</param>
/// <param name="Namespace">Namespace</param>
/// <returns>If prefix was new.</returns>
public bool RegisterPrefix(string Prefix, string Namespace)
{
if (this.namespceByPrefix.TryGetValue(Prefix, out string PrevNamespace))
{
if (PrevNamespace == Namespace)
return false;
}
else
PrevNamespace = null;
if (this.newPrefixes is null)
this.newPrefixes = new ChunkedList<KeyValuePair<string, string>>();
this.newPrefixes.Add(new KeyValuePair<string, string>(Prefix, PrevNamespace));
this.namespceByPrefix[Prefix] = Namespace;
return true;
}
/// <summary>
/// Pushes current prefix state to the stack.
/// </summary>
public void PushPrefixes()
{
this.prefixStack.Add(this.newPrefixes);
this.newPrefixes = null;
}
/// <summary>
/// Pops previous prefix state from the stack.
/// </summary>
public void PopPrefixes()
{
if (!this.prefixStack.HasLastItem)
return;
ChunkedList<KeyValuePair<string, string>> ToRemove = this.prefixStack.RemoveLast();
if (!(ToRemove is null))
{
foreach (KeyValuePair<string, string> P in ToRemove)
{
if (P.Value is null)
this.namespceByPrefix.Remove(P.Key);
else
this.namespceByPrefix[P.Key] = P.Value;
}
}
}
}
}