Skip to content

Commit

Permalink
Merge pull request maartenba#70 from drewfreyling/feature-codecleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
maartenba committed Jul 24, 2012
2 parents cde10cc + 2bc34a4 commit e7fc03c
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Type ResolveControllerType(string areaName, string controllerName)
string controller = controllerName;

// Find controller type
Type controllerType = null;
Type controllerType;
HashSet<string> namespaces = null;
if (areaNamespaces != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public virtual string ResolveUrl(MvcSiteMapNode mvcSiteMapNode, string area, str
key += routeValue.Key + (routeValue.Value ?? string.Empty);
if (_urlkey == key) return _url;

string returnValue = null;
string returnValue;
if (!string.IsNullOrEmpty(mvcSiteMapNode.Route))
{
returnValue = UrlHelper.RouteUrl(mvcSiteMapNode.Route, new RouteValueDictionary(routeValues));
Expand Down
29 changes: 14 additions & 15 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/DefaultSiteMapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ protected virtual XNamespace SiteMapNamespace
/// Builds the sitemap, firstly reads in the XML file, and grabs the outer root element and
/// maps this to become our main out root SiteMap node.
/// </summary>
/// <exception cref="MvcSiteMapException"></exception>
/// <returns>The root SiteMapNode.</returns>
public override SiteMapNode BuildSiteMap()
{
Expand All @@ -528,7 +529,7 @@ public override SiteMapNode BuildSiteMap()
}

// Build sitemap
SiteMapNode rootNode = root;
SiteMapNode rootNode;
lock (synclock)
{
// Return immediately if this method has been called before
Expand All @@ -537,7 +538,7 @@ public override SiteMapNode BuildSiteMap()
return root;
}

XDocument siteMapXml = null;
XDocument siteMapXml;
try
{
// Does the SiteMap XML exist?
Expand All @@ -549,7 +550,7 @@ public override SiteMapNode BuildSiteMap()
// If no namespace is present (or the wrong one is present), replace it
foreach (var e in siteMapXml.Descendants())
{
if (e.Name.Namespace == null || string.IsNullOrEmpty(e.Name.Namespace.NamespaceName) || e.Name.Namespace != this.SiteMapNamespace)
if (string.IsNullOrEmpty(e.Name.Namespace.NamespaceName) || e.Name.Namespace != this.SiteMapNamespace)
{
e.Name = XName.Get(e.Name.LocalName, this.SiteMapNamespace.ToString());
}
Expand Down Expand Up @@ -581,7 +582,7 @@ public override SiteMapNode BuildSiteMap()
isBuildingSiteMap = true;

// List of assemblies
IEnumerable<Assembly> assemblies = null;
IEnumerable<Assembly> assemblies;
if (includeAssembliesForScan.Any())
{
// An include list is given
Expand All @@ -606,7 +607,7 @@ public override SiteMapNode BuildSiteMap()
{
// http://stackoverflow.com/questions/1423733/how-to-tell-if-a-net-assembly-is-dynamic
if (!(assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder)
&& !(assembly.ManifestModule.GetType().Namespace == "System.Reflection.Emit"))
&& assembly.ManifestModule.GetType().Namespace != "System.Reflection.Emit")
{
ProcessNodesInAssembly(assembly);
}
Expand Down Expand Up @@ -716,11 +717,10 @@ protected void SetRootNode(SiteMapNode rootNode)
/// <param name="rootElement">The main root XML element.</param>
protected void ProcessXmlNodes(SiteMapNode rootNode, XElement rootElement)
{
SiteMapNode childNode = rootNode;

// Loop through each element below the current root element.
foreach (XElement node in rootElement.Elements())
{
SiteMapNode childNode;
if (node.Name == this.SiteMapNamespace + NodeName)
{
// If this is a normal mvcSiteMapNode then map the xml element
Expand Down Expand Up @@ -805,7 +805,7 @@ List<IMvcSiteMapNodeAttributeDefinition> assemblyNodes
try
{
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.GetCustomAttributes(typeof(IMvcSiteMapNodeAttribute), true).Count() > 0);
.Where(x => x.GetCustomAttributes(typeof(IMvcSiteMapNodeAttribute), true).Any());

foreach (var method in methods)
{
Expand Down Expand Up @@ -1456,7 +1456,6 @@ protected MvcSiteMapNode GetSiteMapNodeFromXmlElement(XElement node, SiteMapNode

// Add route values to sitemap node
siteMapNode.RouteValues = routeValues;
routeValues = new Dictionary<string, object>();

// Add node's route defaults
var httpContext = new HttpContextWrapper(HttpContext.Current);
Expand Down Expand Up @@ -1561,7 +1560,7 @@ protected SiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(IMvcSiteMapNodeA
foreach (MethodInfo m in ms.OfType<MethodInfo>())
{
var pars = m.GetParameters();
if (pars == null || pars.Length == 0)
if (pars.Length == 0)
{
methodInfo = m;
break;
Expand Down Expand Up @@ -1590,7 +1589,7 @@ protected SiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(IMvcSiteMapNodeA
}

// Determine controller and (index) action
string controller = type.Name.Replace("Controller", "") ?? "Home";
string controller = type.Name.Replace("Controller", "");
string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index";
if (methodInfo != null) // handle custom action name
{
Expand Down Expand Up @@ -1688,13 +1687,13 @@ private static void HandleResourceAttribute(string attributeName, ref string tex
{
if (!string.IsNullOrEmpty(text))
{
string tempStr1 = null;
string tempStr1;
var tempStr2 = text.TrimStart(new[] { ' ' });
if (((tempStr2 != null) && (tempStr2.Length > 10)) && tempStr2.ToLower(CultureInfo.InvariantCulture).StartsWith("$resources:", StringComparison.Ordinal))
if (((tempStr2.Length > 10)) && tempStr2.ToLower(CultureInfo.InvariantCulture).StartsWith("$resources:", StringComparison.Ordinal))
{
tempStr1 = tempStr2.Substring(11);
string tempStr3 = null;
string tempStr4 = null;
string tempStr3;
string tempStr4;
var index = tempStr1.IndexOf(',');
tempStr3 = tempStr1.Substring(0, index);
tempStr4 = tempStr1.Substring(index + 1);
Expand Down
24 changes: 12 additions & 12 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/External/Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,27 +1004,27 @@ Expression ParseStringLiteral()
Expression ParseIntegerLiteral()
{
ValidateToken(TokenId.IntegerLiteral);
string text = token.text;
if (text[0] != '-')
string tokenText = token.text;
if (tokenText[0] != '-')
{
ulong value;
if (!UInt64.TryParse(text, out value))
throw ParseError(Res.InvalidIntegerLiteral, text);
if (!UInt64.TryParse(tokenText, out value))
throw ParseError(Res.InvalidIntegerLiteral, tokenText);
NextToken();
if (value <= (ulong)Int32.MaxValue) return CreateLiteral((int)value, text);
if (value <= (ulong)UInt32.MaxValue) return CreateLiteral((uint)value, text);
if (value <= (ulong)Int64.MaxValue) return CreateLiteral((long)value, text);
return CreateLiteral(value, text);
if (value <= Int32.MaxValue) return CreateLiteral((int)value, tokenText);
if (value <= UInt32.MaxValue) return CreateLiteral((uint)value, tokenText);
if (value <= Int64.MaxValue) return CreateLiteral((long)value, tokenText);
return CreateLiteral(value, tokenText);
}
else
{
long value;
if (!Int64.TryParse(text, out value))
throw ParseError(Res.InvalidIntegerLiteral, text);
if (!Int64.TryParse(tokenText, out value))
throw ParseError(Res.InvalidIntegerLiteral, tokenText);
NextToken();
if (value >= Int32.MinValue && value <= Int32.MaxValue)
return CreateLiteral((int)value, text);
return CreateLiteral(value, text);
return CreateLiteral((int)value, tokenText);
return CreateLiteral(value, tokenText);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace MvcSiteMapProvider.External
/// </summary>
public class HttpRequest2 : HttpRequestWrapper
{
private HttpRequest httpRequest;

/// <summary>
/// Initializes a new instance of the <see cref="HttpRequest2"/> class.
/// </summary>
Expand All @@ -25,7 +23,6 @@ public class HttpRequest2 : HttpRequestWrapper
public HttpRequest2(HttpRequest httpRequest)
: base(httpRequest)
{
this.httpRequest = httpRequest;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public virtual SiteMapNode Clone(string key)
/// <returns>The route data associated with the current node.</returns>
public RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData routeData = null;
RouteData routeData;
if (!string.IsNullOrEmpty(this.Route))
{
routeData = RouteTable.Routes[this.Route].GetRouteData(httpContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class SiteMapHelper
/// <summary>
/// Source metadata
/// </summary>
private static Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "HtmlHelper", typeof(SiteMapHelper).FullName } };
private static readonly Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "HtmlHelper", typeof(SiteMapHelper).FullName } };

/// <summary>
/// Build a sitemap tree, based on the MvcSiteMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class SiteMapTitleHelper
/// <summary>
/// Source metadata
/// </summary>
private static Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "HtmlHelper", typeof(SiteMapTitleHelper).FullName } };
private static readonly Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "HtmlHelper", typeof(SiteMapTitleHelper).FullName } };

/// <summary>
/// Gets the title of SiteMap.CurrentNode
Expand All @@ -45,7 +45,7 @@ public static MvcHtmlString SiteMapTitle(this MvcSiteMapHtmlHelper helper)
/// </returns>
public static MvcHtmlString SiteMapTitle(this MvcSiteMapHtmlHelper helper, string templateName)
{
var model = BuildModel(helper, helper.Provider.CurrentNode ?? helper.Provider.RootNode);
var model = BuildModel(helper.Provider.CurrentNode ?? helper.Provider.RootNode);
return helper
.CreateHtmlHelperForModel(model)
.DisplayFor(m => model, templateName);
Expand All @@ -54,10 +54,9 @@ public static MvcHtmlString SiteMapTitle(this MvcSiteMapHtmlHelper helper, strin
/// <summary>
/// Builds the model.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="startingNode">The starting node.</param>
/// <returns>The model.</returns>
private static SiteMapTitleHelperModel BuildModel(MvcSiteMapHtmlHelper helper, SiteMapNode startingNode)
private static SiteMapTitleHelperModel BuildModel(SiteMapNode startingNode)
{
// Map to model
var mvcNode = startingNode as MvcSiteMapNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class XmlSiteMapResult
/// </summary>
/// <remarks>
/// This number should be 50000 in theory, see http://www.sitemaps.org/protocol.php#sitemapIndex_sitemap.
/// Since citemap files can be maximal 10MB per file and calculating the total sitemap size would degrade performance,
/// Since sitemap files can be maximal 10MB per file and calculating the total sitemap size would degrade performance,
/// an average cap of 35000 has been chosen.
/// </remarks>
private const int MaxNumberOfLinksPerFile = 35000;
Expand Down Expand Up @@ -140,8 +140,6 @@ protected virtual void ExecuteSitemapIndexResult(ControllerContext context, IEnu
/// <param name="page">The page.</param>
protected virtual void ExecuteSitemapResult(ControllerContext context, IEnumerable<SiteMapNode> flattenedHierarchy, long flattenedHierarchyCount, int page)
{
// Count the number of pages
double numPages = Math.Ceiling((double)flattenedHierarchyCount / MaxNumberOfLinksPerFile);

// Output content type
context.HttpContext.Response.ContentType = "text/xml";
Expand Down

0 comments on commit e7fc03c

Please sign in to comment.