/
XDocumentType.cs
189 lines (171 loc) · 6.04 KB
/
XDocumentType.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
183
184
185
186
187
188
189
// 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.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
namespace System.Xml.Linq
{
/// <summary>
/// Represents an XML Document Type Definition (DTD).
/// </summary>
public class XDocumentType : XNode
{
private string _name;
private string? _publicId;
private string? _systemId;
private string? _internalSubset;
/// <summary>
/// Initializes an empty instance of the <see cref="XDocumentType"/> class.
/// </summary>
public XDocumentType(string name, string? publicId, string? systemId, string? internalSubset)
{
_name = XmlConvert.VerifyName(name);
_publicId = publicId;
_systemId = systemId;
_internalSubset = internalSubset;
}
/// <summary>
/// Initializes an instance of the XDocumentType class
/// from another XDocumentType object.
/// </summary>
/// <param name="other"><see cref="XDocumentType"/> object to copy from.</param>
public XDocumentType(XDocumentType other)
{
ArgumentNullException.ThrowIfNull(other);
_name = other._name;
_publicId = other._publicId;
_systemId = other._systemId;
_internalSubset = other._internalSubset;
}
internal XDocumentType(XmlReader r)
{
_name = r.Name;
_publicId = r.GetAttribute("PUBLIC");
_systemId = r.GetAttribute("SYSTEM");
_internalSubset = r.Value;
r.Read();
}
/// <summary>
/// Gets or sets the internal subset for this Document Type Definition (DTD).
/// </summary>
public string? InternalSubset
{
get
{
return _internalSubset;
}
set
{
bool notify = NotifyChanging(this, XObjectChangeEventArgs.Value);
_internalSubset = value;
if (notify) NotifyChanged(this, XObjectChangeEventArgs.Value);
}
}
/// <summary>
/// Gets or sets the name for this Document Type Definition (DTD).
/// </summary>
public string Name
{
get
{
return _name;
}
set
{
value = XmlConvert.VerifyName(value);
bool notify = NotifyChanging(this, XObjectChangeEventArgs.Name);
_name = value;
if (notify) NotifyChanged(this, XObjectChangeEventArgs.Name);
}
}
/// <summary>
/// Gets the node type for this node.
/// </summary>
/// <remarks>
/// This property will always return XmlNodeType.DocumentType.
/// </remarks>
public override XmlNodeType NodeType
{
get
{
return XmlNodeType.DocumentType;
}
}
/// <summary>
/// Gets or sets the public identifier for this Document Type Definition (DTD).
/// </summary>
public string? PublicId
{
get
{
return _publicId;
}
set
{
bool notify = NotifyChanging(this, XObjectChangeEventArgs.Value);
_publicId = value;
if (notify) NotifyChanged(this, XObjectChangeEventArgs.Value);
}
}
/// <summary>
/// Gets or sets the system identifier for this Document Type Definition (DTD).
/// </summary>
public string? SystemId
{
get
{
return _systemId;
}
set
{
bool notify = NotifyChanging(this, XObjectChangeEventArgs.Value);
_systemId = value;
if (notify) NotifyChanged(this, XObjectChangeEventArgs.Value);
}
}
/// <summary>
/// Write this <see cref="XDocumentType"/> to the passed in <see cref="XmlWriter"/>.
/// </summary>
/// <param name="writer">
/// The <see cref="XmlWriter"/> to write this <see cref="XDocumentType"/> to.
/// </param>
public override void WriteTo(XmlWriter writer)
{
ArgumentNullException.ThrowIfNull(writer);
writer.WriteDocType(_name, _publicId, _systemId, _internalSubset);
}
/// <summary>
/// Write this <see cref="XDocumentType"/> to the passed in <see cref="XmlWriter"/>.
/// </summary>
/// <param name="writer">
/// The <see cref="XmlWriter"/> to write this <see cref="XDocumentType"/> to.
/// </param>
/// <param name="cancellationToken">
/// A cancellation token.
/// </param>
public override Task WriteToAsync(XmlWriter writer, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(writer);
if (cancellationToken.IsCancellationRequested)
return Task.FromCanceled(cancellationToken);
return writer.WriteDocTypeAsync(_name, _publicId, _systemId, _internalSubset);
}
internal override XNode CloneNode()
{
return new XDocumentType(this);
}
internal override bool DeepEquals(XNode node)
{
XDocumentType? other = node as XDocumentType;
return other != null && _name == other._name && _publicId == other._publicId &&
_systemId == other.SystemId && _internalSubset == other._internalSubset;
}
internal override int GetDeepHashCode()
{
return _name.GetHashCode() ^
(_publicId != null ? _publicId.GetHashCode() : 0) ^
(_systemId != null ? _systemId.GetHashCode() : 0) ^
(_internalSubset != null ? _internalSubset.GetHashCode() : 0);
}
}
}