/
XmlDeclaration.cs
182 lines (157 loc) · 5.7 KB
/
XmlDeclaration.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
namespace System.Xml
{
// Represents the xml declaration nodes: <?xml version='1.0' ...?>
public class XmlDeclaration : XmlLinkedNode
{
private const string YES = "yes";
private const string NO = "no";
private string _version;
private string _encoding;
private string _standalone;
protected internal XmlDeclaration(string version, string? encoding, string? standalone, XmlDocument doc) : base(doc)
{
if (!IsValidXmlVersion(version))
throw new ArgumentException(SR.Xdom_Version);
if ((standalone != null) && (standalone.Length > 0))
if ((standalone != YES) && (standalone != NO))
throw new ArgumentException(SR.Format(SR.Xdom_standalone, standalone));
this.Encoding = encoding;
this.Standalone = standalone;
this.Version = version;
}
// The version attribute for <?xml version= '1.0' ... ?>
public string Version
{
get { return _version; }
[MemberNotNull(nameof(_version))]
internal set { _version = value; }
}
// Specifies the value of the encoding attribute, as for
// <?xml version= '1.0' encoding= 'UTF-8' ?>
[AllowNull]
public string Encoding
{
get { return _encoding; }
[MemberNotNull(nameof(_encoding))]
set { _encoding = value ?? string.Empty; }
}
// Specifies the value of the standalone attribute.
[AllowNull]
public string Standalone
{
get { return _standalone; }
[MemberNotNull(nameof(_standalone))]
set
{
if (value == null)
_standalone = string.Empty;
else if (value.Length == 0 || value == YES || value == NO)
_standalone = value;
else
throw new ArgumentException(SR.Format(SR.Xdom_standalone, value));
}
}
public override string? Value
{
get { return InnerText; }
set { InnerText = value!; }
}
// Gets or sets the concatenated values of the node and
// all its children.
public override string InnerText
{
get
{
var strb = new ValueStringBuilder(stackalloc char[256]);
strb.Append("version=\"");
strb.Append(Version);
strb.Append('"');
if (Encoding.Length > 0)
{
strb.Append(" encoding=\"");
strb.Append(Encoding);
strb.Append('"');
}
if (Standalone.Length > 0)
{
strb.Append(" standalone=\"");
strb.Append(Standalone);
strb.Append('"');
}
return strb.ToString();
}
set
{
string? tempVersion;
string? tempEncoding;
string? tempStandalone;
string orgEncoding = this.Encoding;
string orgStandalone = this.Standalone;
string? orgVersion = this.Version;
XmlLoader.ParseXmlDeclarationValue(value, out tempVersion, out tempEncoding, out tempStandalone);
try
{
if (tempVersion != null && !IsValidXmlVersion(tempVersion))
throw new ArgumentException(SR.Xdom_Version);
Version = tempVersion!;
if (tempEncoding != null)
Encoding = tempEncoding;
if (tempStandalone != null)
Standalone = tempStandalone;
}
catch
{
Encoding = orgEncoding;
Standalone = orgStandalone;
Version = orgVersion;
throw;
}
}
}
//override methods and properties from XmlNode
// Gets the name of the node.
public override string Name
{
get
{
return "xml";
}
}
// Gets the name of the current node without the namespace prefix.
public override string LocalName
{
get { return Name; }
}
// Gets the type of the current node.
public override XmlNodeType NodeType
{
get { return XmlNodeType.XmlDeclaration; }
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep)
{
Debug.Assert(OwnerDocument != null);
return OwnerDocument.CreateXmlDeclaration(Version, Encoding, Standalone);
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w)
{
w.WriteProcessingInstruction(Name, InnerText);
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w)
{
// Intentionally do nothing since the node doesn't have children.
}
private static bool IsValidXmlVersion(string ver)
{
return ver.Length >= 3 && ver[0] == '1' && ver[1] == '.' && XmlCharType.IsOnlyDigits(ver, 2, ver.Length - 2);
}
}
}