forked from smiley22/S22.Xmpp
-
Notifications
You must be signed in to change notification settings - Fork 51
/
XmppError.cs
239 lines (223 loc) · 8.08 KB
/
XmppError.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Sharp.Xmpp
{
/// <summary>
/// Represents an XMPP error.
/// </summary>
public class XmppError
{
/// <summary>
/// The type of the error.
/// </summary>
private ErrorType type;
/// <summary>
/// The error condition of the error stanza.
/// </summary>
private ErrorCondition condition;
/// <summary>
/// The type of the error.
/// </summary>
public ErrorType Type
{
get
{
return type;
}
set
{
type = value;
Data.SetAttribute("type", value.ToString().ToLowerInvariant());
}
}
/// <summary>
/// The XMPP error condition value of this error.
/// </summary>
public ErrorCondition Condition
{
get
{
return condition;
}
set
{
SetCondition(value);
}
}
/// <summary>
/// A textual description of the error in more detail. This may be null.
/// </summary>
public string Text
{
set
{
var text = Data["text"];
if (text != null)
{
if (value == null)
Data.RemoveChild(text);
else
text.InnerText = value;
}
else
{
if (value != null)
{
Data.Child(Xml.Element("text",
"urn:ietf:params:xml:ns:xmpp-stanzas").Text(value));
}
}
}
get
{
var text = Data["text"];
if (text != null)
return String.IsNullOrEmpty(text.InnerText) ? null : text.InnerText;
return null;
}
}
/// <summary>
/// Provides access to the XML of the underlying error stanza.
/// </summary>
public XmlElement Data
{
get;
private set;
}
/// <summary>
/// Returns a textual representation of this Error instance.
/// </summary>
/// <returns>A textual representation of this Error instance.</returns>
public override string ToString()
{
return Data.ToXmlString();
}
/// <summary>
/// Initializes a new instance of the Error class.
/// </summary>
/// <param name="type">The type of the error.</param>
/// <param name="condition">The condition of the error.</param>
/// <param name="data">The content of the error element.</param>
public XmppError(ErrorType type, ErrorCondition condition, params XmlElement[] data) :
this(type, condition, null, data)
{
}
/// <summary>
/// Initializes a new instance of the Error class.
/// </summary>
/// <param name="type">The type of the error.</param>
/// <param name="condition">The condition of the error.</param>
/// <param name="text">A more-detailed textual description of the error.</param>
/// <param name="data">The content of the error element.</param>
internal XmppError(ErrorType type, ErrorCondition condition, string text = null,
params XmlElement[] data)
{
Data = Xml.Element("error");
Type = type;
Condition = condition;
Text = text;
if (data != null)
{
foreach (var e in data)
{
if (e != null)
Data.Child(e);
}
}
}
/// <summary>
/// Initializes a new Error instance from the specified XML element.
/// </summary>
/// <param name="error">The 'error' XML element to initialize this
/// instance with.</param>
/// <exception cref="ArgumentNullException">The error parameter is null.</exception>
/// <exception cref="ArgumentException">The error parameter contains
/// invalid XML data.</exception>
internal XmppError(XmlElement error)
{
error.ThrowIfNull("error");
// Verify mandatory error type attribute.
ErrorType type = (ErrorType)Enum.Parse(typeof(ErrorType),
error.GetAttribute("type"), true);
// Look for mandatory error condition element.
ErrorCondition? condition = null;
foreach (var v in Enum.GetValues(typeof(ErrorCondition)))
{
string s = ErrorConditionToTagName((ErrorCondition)v);
if (error[s] != null)
condition = (ErrorCondition)v;
}
if (!condition.HasValue)
throw new ArgumentException("The error XML element does not contain a " +
"valid XMPP error condition element.");
Data = error;
Type = type;
Condition = condition.Value;
}
/// <summary>
/// Sets the error condition of the XMPP error to the specified value.
/// </summary>
/// <param name="condition">A value from the ErrorCondition enumeration.</param>
private void SetCondition(ErrorCondition condition)
{
ISet<XmlElement> set = new HashSet<XmlElement>();
// Remove old condition, if any.
foreach (var v in Enum.GetValues(typeof(ErrorCondition)))
{
string s = ErrorConditionToTagName((ErrorCondition)v);
if (Data[s] != null)
set.Add(Data[s]);
}
foreach (var e in set)
Data.RemoveChild(e);
// Add new condition element.
string tag = ErrorConditionToTagName(condition);
Data.Child(Xml.Element(tag,
"urn:ietf:params:xml:ns:xmpp-stanzas"));
this.condition = condition;
}
/// <summary>
/// Returns the XMPP element name of the specified error condition.
/// </summary>
/// <param name="condition">A value from the ErrorCondition enumeration
/// to convert into an element name.</param>
/// <returns>The XML element name of the specified error condition.</returns>
private string ErrorConditionToTagName(ErrorCondition condition)
{
StringBuilder b = new StringBuilder();
string s = condition.ToString();
for (int i = 0; i < s.Length; i++)
{
if (char.IsUpper(s, i) && i > 0)
b.Append('-');
b.Append(char.ToLower(s[i]));
}
return b.ToString();
}
/// <summary>
/// Returns the corresponding value from the ErrorCondition enumeration for
/// the specified XML element name.
/// </summary>
/// <param name="tagName">An XML element name of a defined XMPP error
/// condition.</param>
/// <returns>The value from the ErrorCondition enumeration corresponding to
/// the specified element name.</returns>
/// <exception cref="ArgumentNullException">The tagName parameter is null.</exception>
/// <exception cref="ArgumentException">The specified element name
/// is not a valid XMPP error condition.</exception>
private ErrorCondition TagNameToErrorCondition(string tagName)
{
tagName.ThrowIfNull("tagName");
var values = Enum.GetValues(typeof(ErrorCondition));
foreach (var v in values)
{
if (ErrorConditionToTagName((ErrorCondition)v) == tagName)
return (ErrorCondition)v;
}
throw new ArgumentException("The specified tag name is not a valid " +
"XMPP error condition.");
}
}
}