Skip to content

Commit

Permalink
Fixes #113, allow attributes to be added from MvcSiteMapNodeAttribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightOwl888 committed Apr 6, 2013
1 parent fe40bbf commit efab442
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Reflection;
using System.Web.Mvc;
using MvcSiteMapProvider.Caching;
using MvcSiteMapProvider.Xml;
using MvcSiteMapProvider.Collections.Specialized;

namespace MvcSiteMapProvider.Builder
{
Expand All @@ -18,6 +20,7 @@ public class ReflectionSiteMapBuilder
public ReflectionSiteMapBuilder(
IEnumerable<String> includeAssemblies,
IEnumerable<String> excludeAssemblies,
ISiteMapXmlReservedAttributeNameProvider reservedAttributeNameProvider,
INodeKeyGenerator nodeKeyGenerator,
IDynamicNodeBuilder dynamicNodeBuilder,
ISiteMapNodeFactory siteMapNodeFactory,
Expand All @@ -28,6 +31,8 @@ ISiteMapCacheKeyGenerator siteMapCacheKeyGenerator
throw new ArgumentNullException("includeAssemblies");
if (excludeAssemblies == null)
throw new ArgumentNullException("excludeAssemblies");
if (reservedAttributeNameProvider == null)
throw new ArgumentNullException("reservedAttributeNameProvider");
if (nodeKeyGenerator == null)
throw new ArgumentNullException("nodeKeyGenerator");
if (dynamicNodeBuilder == null)
Expand All @@ -39,13 +44,15 @@ ISiteMapCacheKeyGenerator siteMapCacheKeyGenerator

this.includeAssemblies = includeAssemblies;
this.excludeAssemblies = excludeAssemblies;
this.reservedAttributeNameProvider = reservedAttributeNameProvider;
this.nodeKeyGenerator = nodeKeyGenerator;
this.dynamicNodeBuilder = dynamicNodeBuilder;
this.siteMapNodeFactory = siteMapNodeFactory;
this.siteMapCacheKey = siteMapCacheKeyGenerator.GenerateKey();
}
protected readonly IEnumerable<string> includeAssemblies;
protected readonly IEnumerable<string> excludeAssemblies;
protected readonly ISiteMapXmlReservedAttributeNameProvider reservedAttributeNameProvider;
protected readonly INodeKeyGenerator nodeKeyGenerator;
protected readonly IDynamicNodeBuilder dynamicNodeBuilder;
protected readonly ISiteMapNodeFactory siteMapNodeFactory;
Expand Down Expand Up @@ -406,28 +413,34 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa
// Assign defaults
siteMapNode.Title = title;
siteMapNode.Description = description;
AcquireAttributesFrom(attribute, siteMapNode.Attributes);
AcquireRolesFrom(attribute, siteMapNode.Roles);
siteMapNode.Clickable = attribute.Clickable;
siteMapNode.VisibilityProvider = attribute.VisibilityProvider;
siteMapNode.UrlResolver = attribute.UrlResolver;
siteMapNode.DynamicNodeProvider = attribute.DynamicNodeProvider;
siteMapNode.ImageUrl = attribute.ImageUrl;
siteMapNode.TargetFrame = attribute.TargetFrame;
siteMapNode.HttpMethod = httpMethod;
siteMapNode.LastModifiedDate = attribute.LastModifiedDate;
siteMapNode.ChangeFrequency = attribute.ChangeFrequency;
siteMapNode.UpdatePriority = attribute.UpdatePriority;
if (!string.IsNullOrEmpty(attribute.Url)) siteMapNode.Url = attribute.Url;
siteMapNode.CacheResolvedUrl = attribute.CacheResolvedUrl;
siteMapNode.CanonicalUrl = attribute.CanonicalUrl;
siteMapNode.CanonicalKey = attribute.CanonicalKey;
AcquireMetaRobotsValuesFrom(attribute, siteMapNode.MetaRobotsValues);
siteMapNode.LastModifiedDate = attribute.LastModifiedDate;
siteMapNode.ChangeFrequency = attribute.ChangeFrequency;
siteMapNode.UpdatePriority = attribute.UpdatePriority;

// Handle route details
siteMapNode.Route = attribute.Route;
siteMapNode.RouteValues.Add("area", area);
siteMapNode.RouteValues.Add("controller", controller);
siteMapNode.RouteValues.Add("action", action);
AcquireRouteValuesFrom(attribute, siteMapNode.RouteValues);
AcquirePreservedRouteParametersFrom(attribute, siteMapNode.PreservedRouteParameters);
siteMapNode.UrlResolver = attribute.UrlResolver;

// Specified area, controller and action properties will override any
// provided in the attributes collection.
if (!string.IsNullOrEmpty(area)) siteMapNode.RouteValues.Add("area", area);
if (!string.IsNullOrEmpty(controller)) siteMapNode.RouteValues.Add("controller", controller);
if (!string.IsNullOrEmpty(action)) siteMapNode.RouteValues.Add("action", action);

// Handle MVC details

Expand All @@ -440,11 +453,48 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa
return siteMapNode;
}

/// <summary>
/// Acquires the attributes from a given <see cref="T:IMvcSiteMapNodeAttribute"/>
/// </summary>
/// <param name="attribute">The attribute.</param>
/// <returns></returns>
protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary<string, string> attributes)
{
foreach (var att in attribute.Attributes)
{
var attributeName = att.Key.ToString();
var attributeValue = att.Value;

if (reservedAttributeNameProvider.IsRegularAttribute(attributeName))
{
attributes[attributeName] = attributeValue;
}
}
}

/// <summary>
/// Acquires the route values from a given XElement.
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
protected virtual void AcquireRouteValuesFrom(IMvcSiteMapNodeAttribute attribute, IRouteValueCollection routeValues)
{
foreach (var att in attribute.Attributes)
{
var attributeName = att.Key.ToString();
var attributeValue = att.Value;

if (reservedAttributeNameProvider.IsRouteAttribute(attributeName))
{
routeValues[attributeName] = attributeValue;
}
}
}

/// <summary>
/// Acquires the roles list from a given <see cref="T:IMvcSiteMapNodeAttribute"/>
/// </summary>
/// <param name="node">The node.</param>
/// <param name="attribute">The attribute.</param>
/// <param name="roles">The roles IList to populate.</param>
protected virtual void AcquireRolesFrom(IMvcSiteMapNodeAttribute attribute, IList<string> roles)
{
Expand All @@ -460,7 +510,7 @@ protected virtual void AcquireRolesFrom(IMvcSiteMapNodeAttribute attribute, ILis
/// <summary>
/// Acquires the meta robots values list from a given <see cref="T:IMvcSiteMapNodeAttribute"/>
/// </summary>
/// <param name="node">The node.</param>
/// <param name="attribute">The attribute.</param>
/// <param name="metaRobotsValues">The meta robots values IList to populate.</param>
protected virtual void AcquireMetaRobotsValuesFrom(IMvcSiteMapNodeAttribute attribute, IList<string> metaRobotsValues)
{
Expand All @@ -476,7 +526,7 @@ protected virtual void AcquireMetaRobotsValuesFrom(IMvcSiteMapNodeAttribute attr
/// <summary>
/// Acquires the preserved route parameters list from a given <see cref="T:IMvcSiteMapNodeAttribute"/>
/// </summary>
/// <param name="node">The node.</param>
/// <param name="attribute">The attribute.</param>
/// <param name="preservedRouteParameters">The preserved route parameters IList to populate.</param>
protected virtual void AcquirePreservedRouteParametersFrom(IMvcSiteMapNodeAttribute attribute, IList<string> preservedRouteParameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private ISiteMapBuilder ResolveSiteMapBuilder(ConfigurationSettings settings)

if (settings.ScanAssembliesForSiteMapNodes)
{
var reflectionSiteMapBuilder = this.ResolveReflectionSiteMapBuilder(settings.IncludeAssembliesForScan, settings.ExcludeAssembliesForScan);
var reflectionSiteMapBuilder = this.ResolveReflectionSiteMapBuilder(settings.IncludeAssembliesForScan, settings.ExcludeAssembliesForScan, settings.AttributesToIgnore);
return new CompositeSiteMapBuilder(xmlSiteMapBuilder, reflectionSiteMapBuilder, visitingSiteMapBuilder);
}
else
Expand All @@ -105,11 +105,12 @@ private ISiteMapBuilder ResolveXmlSiteMapBuilder(string siteMapFile, IEnumerable
this.siteMapXmlNameProvider);
}

private ISiteMapBuilder ResolveReflectionSiteMapBuilder(IEnumerable<string> includeAssemblies, IEnumerable<string> excludeAssemblies)
private ISiteMapBuilder ResolveReflectionSiteMapBuilder(IEnumerable<string> includeAssemblies, IEnumerable<string> excludeAssemblies, IEnumerable<string> attributesToIgnore)
{
return new ReflectionSiteMapBuilder(
includeAssemblies,
excludeAssemblies,
new SiteMapXmlReservedAttributeNameProvider(attributesToIgnore),
this.nodeKeyGenerator,
this.dynamicNodeBuilder,
this.siteMapNodeFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace MvcSiteMapProvider
{
Expand Down Expand Up @@ -143,6 +144,12 @@ public interface IMvcSiteMapNodeAttribute
/// </value>
string PreservedRouteParameters { get; set; }

/// <summary>
/// Gets or sets the attributes (optional).
/// </summary>
/// <value>The attributes.</value>
IDictionary<string, string> Attributes { get; set; }

/// <summary>
/// Gets or sets the name of the cache key this node is associated with
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace MvcSiteMapProvider
{
Expand All @@ -14,6 +15,7 @@ public class MvcSiteMapNodeAttribute : Attribute, IMvcSiteMapNodeAttribute
public MvcSiteMapNodeAttribute()
{
Clickable = true;
Attributes = new Dictionary<string, string>();
}

/// <summary>
Expand Down Expand Up @@ -152,6 +154,12 @@ public MvcSiteMapNodeAttribute()
/// </value>
public string PreservedRouteParameters { get; set; }

/// <summary>
/// Gets or sets the attributes (optional).
/// </summary>
/// <value>The attributes.</value>
public IDictionary<string, string> Attributes { get; set; }

/// <summary>
/// Gets or sets the name of the cache key this node is associated with
/// </summary>
Expand Down

0 comments on commit efab442

Please sign in to comment.