From fd4cf027e4f62d4b90525b24d7540d71844ac0da Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Wed, 19 Jun 2013 15:05:18 +0700 Subject: [PATCH] Changed attributes collection to be rather than to allow arbitrary objects to be attached to a node. See #169. --- .../MvcMusicStore/Code/BlingHelper.cs | 2 +- .../Builder/AspNetSiteMapBuilder.cs | 2 +- .../Builder/ReflectionSiteMapBuilder.cs | 2 +- .../Builder/XmlSiteMapBuilder.cs | 2 +- .../Specialized/AttributeCollection.cs | 68 +++++++++++-------- .../Specialized/IAttributeCollection.cs | 4 +- .../FilteredSiteMapNodeVisibilityProvider.cs | 6 +- .../Web/Html/Models/SiteMapNodeModel.cs | 2 +- 8 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/MvcSiteMapProvider/MvcMusicStore/Code/BlingHelper.cs b/src/MvcSiteMapProvider/MvcMusicStore/Code/BlingHelper.cs index 48bc41e3..0c7abdde 100644 --- a/src/MvcSiteMapProvider/MvcMusicStore/Code/BlingHelper.cs +++ b/src/MvcSiteMapProvider/MvcMusicStore/Code/BlingHelper.cs @@ -22,7 +22,7 @@ public static string Bling(this MvcSiteMapHtmlHelper helper) var node = SiteMaps.Current.CurrentNode; if (node != null && node.Attributes.ContainsKey("bling")) { - return node.Attributes["bling"]; + return node.Attributes["bling"].GetType().Equals(typeof(string)) ? node.Attributes["bling"].ToString() : string.Empty; } return string.Empty; } diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/AspNetSiteMapBuilder.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/AspNetSiteMapBuilder.cs index fb4f5bbf..70105437 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/AspNetSiteMapBuilder.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/AspNetSiteMapBuilder.cs @@ -184,7 +184,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromProviderNode(ISiteMap siteMap, /// /// The node. /// - protected virtual void AcquireAttributesFrom(System.Web.SiteMapNode node, IDictionary attributes) + protected virtual void AcquireAttributesFrom(System.Web.SiteMapNode node, IDictionary attributes) { // Unfortunately, the ASP.NET implementation uses a protected member variable to store // the attributes, so there is no way to loop through them without reflection or some diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/ReflectionSiteMapBuilder.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/ReflectionSiteMapBuilder.cs index 19ebd85c..e51af7ce 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/ReflectionSiteMapBuilder.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/ReflectionSiteMapBuilder.cs @@ -477,7 +477,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa /// /// The attribute. /// - protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary attributes) + protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary attributes) { foreach (var att in attribute.Attributes) { diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/XmlSiteMapBuilder.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/XmlSiteMapBuilder.cs index d06495db..6b22c8c1 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/XmlSiteMapBuilder.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/XmlSiteMapBuilder.cs @@ -214,7 +214,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromXmlElement(ISiteMap siteMap, XE /// /// The node. /// - protected virtual void AcquireAttributesFrom(XElement node, IDictionary attributes) + protected virtual void AcquireAttributesFrom(XElement node, IDictionary attributes) { foreach (XAttribute attribute in node.Attributes()) { diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/AttributeCollection.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/AttributeCollection.cs index 942b9d0c..b51a1301 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/AttributeCollection.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/AttributeCollection.cs @@ -12,7 +12,7 @@ namespace MvcSiteMapProvider.Collections.Specialized /// localization of custom attributes. /// public class AttributeCollection - : RequestCacheableDictionary, IAttributeCollection + : RequestCacheableDictionary, IAttributeCollection { public AttributeCollection( ISiteMap siteMap, @@ -29,22 +29,17 @@ IRequestCache requestCache protected readonly ILocalizationService localizationService; - public override void Add(string key, string value) + public override void Add(string key, object value) { - if (this.IsReadOnly) - { - throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly)); - } - value = localizationService.ExtractExplicitResourceKey(key, value); + ThrowIfReadOnly(); + if (value.GetType().Equals(typeof(string))) + value = localizationService.ExtractExplicitResourceKey(key, value.ToString()); base.Add(key, value); } public override void Clear() { - if (this.IsReadOnly) - { - throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly)); - } + ThrowIfReadOnly(); foreach (var key in this.Keys) { localizationService.RemoveResourceKey(key); @@ -52,41 +47,41 @@ public override void Clear() base.Clear(); } - protected override void Insert(string key, string value, bool add) + protected override void Insert(string key, object value, bool add) { - if (this.IsReadOnly) - { - throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly)); - } - value = localizationService.ExtractExplicitResourceKey(key, value); + ThrowIfReadOnly(); + if (value.GetType().Equals(typeof(string))) + value = localizationService.ExtractExplicitResourceKey(key, value.ToString()); base.Insert(key, value, add); } - public override bool Remove(KeyValuePair item) + public override bool Remove(KeyValuePair item) { - if (this.IsReadOnly) - { - throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly)); - } + ThrowIfReadOnly(); localizationService.RemoveResourceKey(item.Key); return base.Remove(item); } public override bool Remove(string key) { - if (this.IsReadOnly) - { - throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly)); - } + ThrowIfReadOnly(); localizationService.RemoveResourceKey(key); return base.Remove(key); } - public override string this[string key] + public override object this[string key] { get { - return localizationService.GetResourceString(key, base[key], base.siteMap); + var value = base[key]; + if (value.GetType().Equals(typeof(string))) + { + return localizationService.GetResourceString(key, base[key].ToString(), base.siteMap); + } + else + { + return value; + } } set { @@ -94,7 +89,22 @@ public override string this[string key] { throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapNodeReadOnly, key)); } - base[key] = localizationService.ExtractExplicitResourceKey(key, value); + if (value.GetType().Equals(typeof(string))) + { + base[key] = localizationService.ExtractExplicitResourceKey(key, value.ToString()); + } + else + { + base[key] = value; + } + } + } + + protected virtual void ThrowIfReadOnly() + { + if (this.IsReadOnly) + { + throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly)); } } } diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/IAttributeCollection.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/IAttributeCollection.cs index 5eb79506..c92a24b7 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/IAttributeCollection.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Collections/Specialized/IAttributeCollection.cs @@ -7,8 +7,8 @@ namespace MvcSiteMapProvider.Collections.Specialized /// Contract for the specialized collection for dealing with custom site map attributes. /// public interface IAttributeCollection - : IDictionary + : IDictionary { - void CopyTo(IDictionary destination); + void CopyTo(IDictionary destination); } } diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/FilteredSiteMapNodeVisibilityProvider.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/FilteredSiteMapNodeVisibilityProvider.cs index bd884309..7d36f8cd 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/FilteredSiteMapNodeVisibilityProvider.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/FilteredSiteMapNodeVisibilityProvider.cs @@ -26,7 +26,11 @@ public class FilteredSiteMapNodeVisibilityProvider public override bool IsVisible(ISiteMapNode node, IDictionary sourceMetadata) { // Is a visibility attribute specified? - string visibility = node.Attributes["visibility"]; + string visibility = string.Empty; + if (node.Attributes.ContainsKey("visibility")) + { + visibility = node.Attributes["visibility"].GetType().Equals(typeof(string)) ? node.Attributes["visibility"].ToString() : string.Empty; + } if (string.IsNullOrEmpty(visibility)) { return true; diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Html/Models/SiteMapNodeModel.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Html/Models/SiteMapNodeModel.cs index 32144a4b..abe228fb 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Html/Models/SiteMapNodeModel.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Html/Models/SiteMapNodeModel.cs @@ -167,7 +167,7 @@ public SiteMapNodeModel(ISiteMapNode node, IDictionary sourceMet /// Gets or sets the meta attributes. /// /// The meta attributes. - public IDictionary Attributes { get; protected set; } + public IDictionary Attributes { get; protected set; } /// /// Gets or sets the source metadata generated by the HtmlHelper.