From 09367376b5b1d511a165ecf75553debf21bd663a Mon Sep 17 00:00:00 2001 From: markrendle Date: Tue, 16 Feb 2010 16:14:36 +0000 Subject: [PATCH] Changed XElementExtensions to use (prefix,name) arguments --- .../Linq/EnumerableExtensionTests.cs | 2 +- NExtLib.Tests/NExtLib.Tests.csproj | 7 +- .../Properties/Resources.Designer.cs | 17 +++ NExtLib.Tests/Properties/Resources.resx | 6 +- .../Resources/XmlWithPrefixedNamespace.txt | 9 ++ .../Xml/Linq/XElementExtensionsTests.cs | 24 +++- .../NExtLib.Unit.csproj | 2 +- .../Properties/AssemblyInfo.cs | 0 .../ShouldExtensions.cs | 2 +- NExtLib.sln | 2 +- NExtLib/Async/AsyncException.cs | 19 +++ NExtLib/Async/Future.cs | 22 ++++ NExtLib/Async/Future`1.cs | 66 ++++++++++ NExtLib/Func.cs | 16 +++ .../EnumerableOfKeyValuePairExtensions.cs | 22 ++++ NExtLib/NExtLib.csproj | 23 ++++ NExtLib/Properties/Resources.Designer.cs | 72 ++++++++++ NExtLib/Properties/Resources.resx | 123 ++++++++++++++++++ NExtLib/StringExtensions.cs | 15 +++ NExtLib/Xml/Linq/XAttributeExtensions.cs | 21 +++ NExtLib/Xml/Linq/XElementExtensions.cs | 18 ++- .../Syndication/SyndicationItemExtensions.cs | 34 +++++ 22 files changed, 502 insertions(+), 20 deletions(-) create mode 100644 NExtLib.Tests/Resources/XmlWithPrefixedNamespace.txt rename NExtLib.UnitTest/NExtLib.UnitTest.csproj => NExtLib.Unit/NExtLib.Unit.csproj (97%) rename {NExtLib.UnitTest => NExtLib.Unit}/Properties/AssemblyInfo.cs (100%) rename {NExtLib.UnitTest => NExtLib.Unit}/ShouldExtensions.cs (91%) create mode 100644 NExtLib/Async/AsyncException.cs create mode 100644 NExtLib/Async/Future.cs create mode 100644 NExtLib/Async/Future`1.cs create mode 100644 NExtLib/Func.cs create mode 100644 NExtLib/Linq/EnumerableOfKeyValuePairExtensions.cs create mode 100644 NExtLib/Properties/Resources.Designer.cs create mode 100644 NExtLib/Properties/Resources.resx create mode 100644 NExtLib/StringExtensions.cs create mode 100644 NExtLib/Xml/Linq/XAttributeExtensions.cs create mode 100644 NExtLib/Xml/Syndication/SyndicationItemExtensions.cs diff --git a/NExtLib.Tests/Linq/EnumerableExtensionTests.cs b/NExtLib.Tests/Linq/EnumerableExtensionTests.cs index 44a3c59..c239bdd 100644 --- a/NExtLib.Tests/Linq/EnumerableExtensionTests.cs +++ b/NExtLib.Tests/Linq/EnumerableExtensionTests.cs @@ -4,7 +4,7 @@ using System.Text; using NUnit.Framework; using NExtLib.Linq; -using NExtLib.UnitTest; +using NExtLib.Unit; namespace NExtLib.Tests.Linq { diff --git a/NExtLib.Tests/NExtLib.Tests.csproj b/NExtLib.Tests/NExtLib.Tests.csproj index 223220a..0b06231 100644 --- a/NExtLib.Tests/NExtLib.Tests.csproj +++ b/NExtLib.Tests/NExtLib.Tests.csproj @@ -59,9 +59,9 @@ - + {072F8BE4-81C1-4276-A9A5-1AEC87A84265} - NExtLib.UnitTest + NExtLib.Unit {4A191FAD-DC1D-46FC-A941-7DEB2C8C4C15} @@ -71,6 +71,9 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + An error occurred during execution. + + \ No newline at end of file diff --git a/NExtLib/StringExtensions.cs b/NExtLib/StringExtensions.cs new file mode 100644 index 0000000..af748f0 --- /dev/null +++ b/NExtLib/StringExtensions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NExtLib +{ + public static class StringExtensions + { + public static string EnsureStartsWith(this string source, string value) + { + return (source == null || source.StartsWith(value)) ? source : value + source; + } + } +} diff --git a/NExtLib/Xml/Linq/XAttributeExtensions.cs b/NExtLib/Xml/Linq/XAttributeExtensions.cs new file mode 100644 index 0000000..7b6af89 --- /dev/null +++ b/NExtLib/Xml/Linq/XAttributeExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace System.Xml.Linq +{ + public static class XAttributeExtensions + { + /// + /// Returns the string value of the Attribute or null if the attribute is null. + /// Null-safe, eliminates need for null-checking. + /// + /// The attribute. + /// + public static string ValueOrDefault(this XAttribute attribute) + { + return attribute != null ? attribute.Value : null; + } + } +} diff --git a/NExtLib/Xml/Linq/XElementExtensions.cs b/NExtLib/Xml/Linq/XElementExtensions.cs index 45a4e4e..bb30dfa 100644 --- a/NExtLib/Xml/Linq/XElementExtensions.cs +++ b/NExtLib/Xml/Linq/XElementExtensions.cs @@ -7,16 +7,22 @@ namespace System.Xml.Linq { public static class XElementExtensions { - public static XElement XElement(this XElement element, string name) + public static XElement Element(this XElement element, string prefix, string name) { - var xname = element.GetDefaultNamespace() + name; - return element.Element(xname); + var ns = string.IsNullOrEmpty(prefix) ? element.GetDefaultNamespace() : element.GetNamespaceOfPrefix(prefix); + return element.Element(ns + name); } - public static IEnumerable XElements(this XElement element, string name) + public static IEnumerable Elements(this XElement element, string prefix, string name) { - var xname = element.GetDefaultNamespace() + name; - return element.Elements(xname); + var ns = string.IsNullOrEmpty(prefix) ? element.GetDefaultNamespace() : element.GetNamespaceOfPrefix(prefix); + return element.Elements(ns + name); + } + + public static XAttribute Attribute(this XElement element, string prefix, string name) + { + var ns = string.IsNullOrEmpty(prefix) ? element.GetDefaultNamespace() : element.GetNamespaceOfPrefix(prefix); + return element.Attribute(ns + name); } } } diff --git a/NExtLib/Xml/Syndication/SyndicationItemExtensions.cs b/NExtLib/Xml/Syndication/SyndicationItemExtensions.cs new file mode 100644 index 0000000..53f9c0d --- /dev/null +++ b/NExtLib/Xml/Syndication/SyndicationItemExtensions.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.Linq; +using System.ServiceModel.Syndication; + +namespace NExtLib.Xml.Syndication +{ + public static class SyndicationItemExtensions + { + public static XElement ContentAsXElement(this SyndicationItem item) + { + var content = item.Content as XmlSyndicationContent; + + if (content == null) return null; + + return XElement.ReadFrom(content.GetReaderAtContent()) as XElement; + } + + public static IEnumerable ContentsAsXElements(this SyndicationFeed feed) + { + XElement content; + + foreach (var item in feed.Items) + { + if ((content = item.ContentAsXElement()) != null) + { + yield return content; + } + } + } + } +}