-
Notifications
You must be signed in to change notification settings - Fork 91
/
Util.cs
205 lines (192 loc) · 8.02 KB
/
Util.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
using S22.Xmpp.Core;
using System;
using System.Xml;
namespace S22.Xmpp {
/// <summary>
/// Contains utility and extension methods.
/// </summary>
internal static class Util {
/// <summary>
/// Creates an exception from the specified Iq stanza.
/// </summary>
/// <param name="errorIq">The Iq stanza to create the exception from. The
/// stanza must be of type IqType.Error.</param>
/// <param name="message">A description of the error. The content of message
/// is intended to be understood by humans.</param>
/// <returns>An exception of type XmppErrorException if an XmppError instance
/// could be created from the specified Iq stanza, or an exception of type
/// XmppException denoting an unrecoverable error.</returns>
/// <exception cref="ArgumentNullException">The errorIq parameter is null.</exception>
/// <exception cref="ArgumentException">The errorIq parameter is not
/// of type IqType.Error.</exception>
internal static Exception ExceptionFromError(Iq errorIq, string message = null) {
errorIq.ThrowIfNull("errorIq");
if (errorIq.Type != IqType.Error) {
throw new ArgumentException("The specified Iq stanza is not of " +
"type 'error'.");
}
return ExceptionFromError(errorIq.Data["error"], message);
}
/// <summary>
/// Creates an exception from the specified XML error element.
/// </summary>
/// <param name="error">An XML XMPP error element.</param>
/// <param name="message">A description of the error. The content of message
/// is intended to be understood by humans.</param>
/// <returns>An exception of type XmppErrorException if an XmppError instance
/// could be created from the specified Iq stanza, or an exception of type
/// XmppException denoting an unrecoverable error.</returns>
internal static Exception ExceptionFromError(XmlElement error,
string message = null) {
try {
return new XmppErrorException(new XmppError(error), message);
} catch {
if (error == null)
return new XmppException("Unspecified error.");
return new XmppException("Invalid XML error-stanza: " +
error.ToXmlString());
}
}
/// <summary>
/// Raises the event. Ensures the event is only raised, if it is not null.
/// </summary>
/// <typeparam name="T">Extends System.EventHandler class</typeparam>
/// <param name="event">Extends System.EventHandler class</param>
/// <param name="sender">The sender of the event</param>
/// <param name="args">The event arguments associated with this event</param>
internal static void Raise<T>(this EventHandler<T> @event, object sender, T args)
where T : EventArgs {
EventHandler<T> handler = @event;
if (handler != null)
handler(sender, args);
}
/// <summary>
/// Throws an ArgumentNullException if the given data item is null.
/// </summary>
/// <param name="data">The item to check for nullity.</param>
/// <param name="name">The name to use when throwing an
/// exception, if necessary.</param>
/// <remarks>Courtesy of Jon Skeet.</remarks>
internal static void ThrowIfNull<T>(this T data, string name)
where T : class {
if (data == null)
throw new ArgumentNullException(name);
}
/// <summary>
/// Throws an ArgumentNullException if the given data item is null.
/// </summary>
/// <param name="data">The item to check for nullity.</param>
/// <remarks>Courtesy of Jon Skeet.</remarks>
internal static void ThrowIfNull<T>(this T data)
where T : class {
if (data == null)
throw new ArgumentNullException();
}
/// <summary>
/// Throws an ArgumentOufOfRangeExcption if the given value is not within
/// the specified range.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="from">The minimum value (including).</param>
/// <param name="to">The maximum value (including).</param>
internal static void ThrowIfOutOfRange(this int value, int from, int to) {
if (value < from || value > to)
throw new ArgumentOutOfRangeException();
}
/// <summary>
/// Throws an ArgumentOufOfRangeExcption if the given value is not within
/// the specified range.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="name">The name to use when throwing an
/// exception, if necessary.</param>
/// <param name="from">The minimum value (including).</param>
/// <param name="to">The maximum value (including).</param>
internal static void ThrowIfOutOfRange(this int value, string name,
int from, int to) {
if (value < from || value > to)
throw new ArgumentOutOfRangeException(name);
}
/// <summary>
/// Throws an ArgumentOufOfRangeExcption if the given value is not within
/// the specified range.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="from">The minimum value (including).</param>
/// <param name="to">The maximum value (including).</param>
internal static void ThrowIfOutOfRange(this long value, long from, long to) {
if (value < from || value > to)
throw new ArgumentOutOfRangeException();
}
/// <summary>
/// Throws an ArgumentOufOfRangeExcption if the given value is not within
/// the specified range.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="name">The name to use when throwing an
/// exception, if necessary.</param>
/// <param name="from">The minimum value (including).</param>
/// <param name="to">The maximum value (including).</param>
internal static void ThrowIfOutOfRange(this long value, string name,
long from, long to) {
if (value < from || value > to)
throw new ArgumentOutOfRangeException(name);
}
/// <summary>
/// Throws an ArgumentNullException if the given string is null and
/// throws an ArgumentException if the given string is empty.
/// </summary>
/// <param name="s">The string to check for nullity and emptiness.</param>
internal static void ThrowIfNullOrEmpty(this string s) {
if (s == null)
throw new ArgumentNullException();
if (s == String.Empty)
throw new ArgumentException();
}
/// <summary>
/// Throws an ArgumentNullException if the given string is null and
/// throws an ArgumentException if the given string is empty.
/// </summary>
/// <param name="s">The string to check for nullity and emptiness.</param>
/// <param name="name">The name to use when throwing an
/// exception, if necessary.</param>
internal static void ThrowIfNullOrEmpty(this string s, string name) {
if (s == null)
throw new ArgumentNullException(name);
if (s == String.Empty)
throw new ArgumentException(name + " must not be empty.");
}
/// <summary>
/// Capitalizes the first character of the string.
/// </summary>
/// <param name="s">The string to capitalize.</param>
/// <returns>A new string with the first character capitalized.</returns>
internal static string Capitalize(this string s) {
return char.ToUpperInvariant(s[0]) + s.Substring(1);
}
/// <summary>
/// Converts the string representation of the name or numeric value of one
/// or more enumerated constants to an equivalent enumerated object.
/// </summary>
/// <typeparam name="T">An enumeration type.</typeparam>
/// <param name="value">A string containing the name or value to
/// convert.</param>
/// <param name="ignoreCase">true to ignore case; false to regard
/// case.</param>
/// <returns>An object of the specified enumeration type whose value is
/// represented by value.</returns>
/// <exception cref="ArgumentNullException">The value parameter is
/// null.</exception>
/// <exception cref="ArgumentException">The specified type is not an
/// enumeration type, or value is either an empty string or only contains
/// white space, or value is a name, but not one of the named
/// constants.</exception>
internal static T ParseEnum<T>(string value, bool ignoreCase = true) where T :
struct, IComparable, IFormattable, IConvertible {
value.ThrowIfNull("value");
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type.");
return (T) Enum.Parse(typeof(T), value, ignoreCase);
}
}
}