~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ServiceModel/XLinqExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
2
//
 
3
// ServiceStack: Useful extensions to simplify parsing xml with XLinq
 
4
//
 
5
// Authors:
 
6
//   Demis Bellot (demis.bellot@gmail.com)
 
7
//
 
8
// Copyright 2012 ServiceStack
 
9
//
 
10
// Licensed under the new BSD license.
 
11
//
 
12
 
 
13
using System;
 
14
using System.Collections.Generic;
 
15
using System.Xml;
 
16
using System.Xml.Linq;
 
17
 
 
18
namespace ServiceStack.ServiceModel
 
19
{
 
20
    public static class XLinqExtensions
 
21
    {
 
22
        public static string GetString(this XElement el, string name)
 
23
        {
 
24
            return el == null ? null : GetElementValueOrDefault(el, name, x => x.Value);
 
25
        }
 
26
 
 
27
        public static string GetStringAttributeOrDefault(this XElement element, string name)
 
28
        {
 
29
            var attr = AnyAttribute(element, name);
 
30
            return attr == null ? null : GetAttributeValueOrDefault(attr, name, x => x.Value);
 
31
        }
 
32
 
 
33
        public static bool GetBool(this XElement el, string name)
 
34
        {
 
35
            AssertElementHasValue(el, name);
 
36
            return (bool)GetElement(el, name);
 
37
        }
 
38
 
 
39
        public static bool GetBoolOrDefault(this XElement el, string name)
 
40
        {
 
41
            return GetElementValueOrDefault(el, name, x => (bool)x);
 
42
        }
 
43
 
 
44
        public static bool? GetNullableBool(this XElement el, string name)
 
45
        {
 
46
            var childEl = GetElement(el, name);
 
47
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (bool?)childEl;
 
48
        }
 
49
 
 
50
        public static int GetInt(this XElement el, string name)
 
51
        {
 
52
            AssertElementHasValue(el, name);
 
53
            return (int)GetElement(el, name);
 
54
        }
 
55
 
 
56
        public static int GetIntOrDefault(this XElement el, string name)
 
57
        {
 
58
            return GetElementValueOrDefault(el, name, x => (int)x);
 
59
        }
 
60
 
 
61
        public static int? GetNullableInt(this XElement el, string name)
 
62
        {
 
63
            var childEl = GetElement(el, name);
 
64
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (int?)childEl;
 
65
        }
 
66
 
 
67
        public static long GetLong(this XElement el, string name)
 
68
        {
 
69
            AssertElementHasValue(el, name);
 
70
            return (long)GetElement(el, name);
 
71
        }
 
72
 
 
73
        public static long GetLongOrDefault(this XElement el, string name)
 
74
        {
 
75
            return GetElementValueOrDefault(el, name, x => (long)x);
 
76
        }
 
77
 
 
78
        public static long? GetNullableLong(this XElement el, string name)
 
79
        {
 
80
            var childEl = GetElement(el, name);
 
81
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (long?)childEl;
 
82
        }
 
83
 
 
84
        public static decimal GetDecimal(this XElement el, string name)
 
85
        {
 
86
            AssertElementHasValue(el, name);
 
87
            return (decimal)GetElement(el, name);
 
88
        }
 
89
 
 
90
        public static decimal GetDecimalOrDefault(this XElement el, string name)
 
91
        {
 
92
            return GetElementValueOrDefault(el, name, x => (decimal)x);
 
93
        }
 
94
 
 
95
        public static decimal? GetNullableDecimal(this XElement el, string name)
 
96
        {
 
97
            var childEl = GetElement(el, name);
 
98
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (decimal?)childEl;
 
99
        }
 
100
 
 
101
        public static DateTime GetDateTime(this XElement el, string name)
 
102
        {
 
103
            AssertElementHasValue(el, name);
 
104
            return (DateTime)GetElement(el, name);
 
105
        }
 
106
 
 
107
        public static DateTime GetDateTimeOrDefault(this XElement el, string name)
 
108
        {
 
109
            return GetElementValueOrDefault(el, name, x => (DateTime)x);
 
110
        }
 
111
 
 
112
        public static DateTime? GetNullableDateTime(this XElement el, string name)
 
113
        {
 
114
            var childEl = GetElement(el, name);
 
115
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (DateTime?)childEl;
 
116
        }
 
117
 
 
118
        public static TimeSpan GetTimeSpan(this XElement el, string name)
 
119
        {
 
120
            AssertElementHasValue(el, name);
 
121
            return (TimeSpan)GetElement(el, name);
 
122
        }
 
123
 
 
124
        public static TimeSpan GetTimeSpanOrDefault(this XElement el, string name)
 
125
        {
 
126
            return GetElementValueOrDefault(el, name, x => (TimeSpan)x);
 
127
        }
 
128
 
 
129
        public static TimeSpan? GetNullableTimeSpan(this XElement el, string name)
 
130
        {
 
131
            var childEl = GetElement(el, name);
 
132
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (TimeSpan?)childEl;
 
133
        }
 
134
 
 
135
        public static Guid GetGuid(this XElement el, string name)
 
136
        {
 
137
            AssertElementHasValue(el, name);
 
138
            return (Guid)GetElement(el, name);
 
139
        }
 
140
 
 
141
        public static Guid GetGuidOrDefault(this XElement el, string name)
 
142
        {
 
143
            return GetElementValueOrDefault(el, name, x => (Guid)x);
 
144
        }
 
145
 
 
146
        public static Guid? GetNullableGuid(this XElement el, string name)
 
147
        {
 
148
            var childEl = GetElement(el, name);
 
149
            return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (Guid?)childEl;
 
150
        }
 
151
 
 
152
        public static T GetElementValueOrDefault<T>(this XElement element, string name, Func<XElement, T> converter)
 
153
        {
 
154
            if (converter == null)
 
155
            {
 
156
                throw new ArgumentNullException("converter");
 
157
            }
 
158
            var el = GetElement(element, name);
 
159
            return el == null || string.IsNullOrEmpty(el.Value) ? default(T) : converter(el);
 
160
        }
 
161
 
 
162
        public static XElement GetElement(this XElement element, string name)
 
163
        {
 
164
            if (element == null)
 
165
            {
 
166
                throw new ArgumentNullException("element");
 
167
            }
 
168
            if (name == null)
 
169
            {
 
170
                throw new ArgumentNullException("name");
 
171
            }
 
172
            return element.AnyElement(name);
 
173
        }
 
174
 
 
175
        public static T GetAttributeValueOrDefault<T>(this XAttribute attr, string name, Func<XAttribute, T> converter)
 
176
        {
 
177
            if (converter == null)
 
178
            {
 
179
                throw new ArgumentNullException("converter");
 
180
            }
 
181
            return attr == null || string.IsNullOrEmpty(attr.Value) ? default(T) : converter(attr);
 
182
        }
 
183
 
 
184
        public static void AssertExactlyOneResult(this XElement queryListItems, string referenceNumber, string formType)
 
185
        {
 
186
            int count = Convert.ToInt32(queryListItems.AnyAttribute("ItemCount").Value);
 
187
            if (count == 0)
 
188
                throw new InvalidOperationException(string.Format("There is no {0} for with a deal reference number {1}", formType, referenceNumber));
 
189
            if (count > 1)
 
190
                throw new InvalidOperationException(
 
191
                    string.Format("There are more than one {0}s with deal reference number {1}", formType, referenceNumber));
 
192
        }
 
193
 
 
194
        public static void AssertElementHasValue(this XElement element, string name)
 
195
        {
 
196
            if (element == null)
 
197
            {
 
198
                throw new ArgumentNullException("element");
 
199
            }
 
200
            if (name == null)
 
201
            {
 
202
                throw new ArgumentNullException("name");
 
203
            }
 
204
            var childEl = element.AnyElement(name);
 
205
            if (childEl == null || string.IsNullOrEmpty(childEl.Value))
 
206
            {
 
207
                throw new ArgumentNullException(name, string.Format("{0} is required", name));
 
208
            }
 
209
        }
 
210
 
 
211
        public static List<string> GetValues(this IEnumerable<XElement> els)
 
212
        {
 
213
            var values = new List<string>();
 
214
            foreach (var el in els)
 
215
            {
 
216
                values.Add(el.Value);
 
217
            }
 
218
            return values;
 
219
        }
 
220
 
 
221
        public static XAttribute AnyAttribute(this XElement element, string name)
 
222
        {
 
223
            if (element == null) return null;
 
224
            foreach (var attribute in element.Attributes())
 
225
            {
 
226
                if (attribute.Name.LocalName == name)
 
227
                {
 
228
                    return attribute;
 
229
                }
 
230
            }
 
231
            return null;
 
232
        }
 
233
 
 
234
        public static IEnumerable<XElement> AllElements(this XElement element, string name)
 
235
        {
 
236
            var els = new List<XElement>();
 
237
            if (element == null) return els;
 
238
            foreach (var node in element.Nodes())
 
239
            {
 
240
                if (node.NodeType != XmlNodeType.Element) continue;
 
241
                var childEl = (XElement)node;
 
242
                if (childEl.Name.LocalName == name)
 
243
                {
 
244
                    els.Add(childEl);
 
245
                }
 
246
            }
 
247
            return els;
 
248
        }
 
249
 
 
250
        public static XElement AnyElement(this XElement element, string name)
 
251
        {
 
252
            if (element == null) return null;
 
253
            foreach (var node in element.Nodes())
 
254
            {
 
255
                if (node.NodeType != XmlNodeType.Element) continue;
 
256
                var childEl = (XElement)node;
 
257
                if (childEl.Name.LocalName == name)
 
258
                {
 
259
                    return childEl;
 
260
                }
 
261
            }
 
262
            return null;
 
263
        }
 
264
 
 
265
        public static XElement AnyElement(this IEnumerable<XElement> elements, string name)
 
266
        {
 
267
            foreach (var element in elements)
 
268
            {
 
269
                if (element.Name.LocalName == name)
 
270
                {
 
271
                    return element;
 
272
                }
 
273
            }
 
274
            return null;
 
275
        }
 
276
 
 
277
        public static IEnumerable<XElement> AllElements(this IEnumerable<XElement> elements, string name)
 
278
        {
 
279
            var els = new List<XElement>();
 
280
            foreach (var element in elements)
 
281
            {
 
282
                els.AddRange(AllElements(element, name));
 
283
            }
 
284
            return els;
 
285
        }
 
286
 
 
287
        public static XElement FirstElement(this XElement element)
 
288
        {
 
289
            if (element.FirstNode.NodeType == XmlNodeType.Element)
 
290
            {
 
291
                return (XElement)element.FirstNode;
 
292
            }
 
293
            return null;
 
294
        }
 
295
    }
 
296
 
 
297
}
 
298
#endif