Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using DotVVM.Framework.Configuration;
  6. using DotVVM.Framework.Hosting;
  7. using DotVVM.Framework.Routing;
  8.  
  9. namespace Moon.DotVVM.Routing
  10. {
  11. /// <summary>
  12. /// The DotVVM routing strategy based on <c><-- Route: --></c> comments.
  13. /// </summary>
  14. public class CommentRoutingStrategy : IRoutingStrategy
  15. {
  16. private static readonly Regex routeDefinitionRegex = new Regex(@"<!--\s*Route:\s*(.*),\s*""(.*)""\s*-->", RegexOptions.Compiled);
  17. private readonly string appPath;
  18.  
  19. private readonly DotvvmConfiguration config;
  20. private readonly DirectoryInfo serverDirectory;
  21.  
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="CommentRoutingStrategy" /> class.
  24. /// </summary>
  25. /// <param name="config">The DotVVM configuration.</param>
  26. /// <param name="viewFolderName">The name of the folder containing views.</param>
  27. public CommentRoutingStrategy(DotvvmConfiguration config, string viewFolderName = "Views")
  28. {
  29. this.config = config;
  30.  
  31. appPath = Path.GetFullPath(config.ApplicationPhysicalPath);
  32. serverDirectory = new DirectoryInfo(Path.Combine(appPath, viewFolderName));
  33. }
  34.  
  35. /// <summary>
  36. /// Returns routes for all views discovered in the folder specified.
  37. /// </summary>
  38. public IEnumerable<RouteBase> GetRoutes()
  39. => DiscoverMarkupFiles().Select(BuildRoute).Where(r => r != null);
  40.  
  41. private IEnumerable<FileInfo> DiscoverMarkupFiles()
  42. {
  43. if (serverDirectory.Exists)
  44. {
  45. return serverDirectory.EnumerateFiles("*.dothtml", SearchOption.AllDirectories);
  46. }
  47.  
  48. throw new DotvvmRouteStrategyException("Cannot auto-discover DotVVM routes." +
  49. $" The directory '{serverDirectory.Name}' was not found!");
  50. }
  51.  
  52. private RouteBase BuildRoute(FileInfo markupFile)
  53. {
  54. var match = MatchRouteDefinition(markupFile);
  55.  
  56. if (match == null)
  57. {
  58. return null;
  59. }
  60.  
  61. var appRelativePath = markupFile.FullName
  62. .Substring(appPath.Length)
  63. .Replace('\\', '/')
  64. .TrimStart('/');
  65.  
  66. return new DotvvmRoute(
  67. match.Groups[2].Value,
  68. appRelativePath,
  69. match.Groups[1].Value,
  70. null,
  71. GetPresenter, config);
  72. }
  73.  
  74. private Match MatchRouteDefinition(FileInfo markupFile)
  75. {
  76. using (var input = markupFile.OpenRead())
  77. using (var reader = new StreamReader(input))
  78. {
  79. for (var i = 0; i < 10; i++)
  80. {
  81. var line = reader.ReadLine();
  82. var match = routeDefinitionRegex.Match(line);
  83.  
  84. if (match.Success)
  85. {
  86. return match;
  87. }
  88. }
  89. }
  90.  
  91. return null;
  92. }
  93.  
  94. private DotvvmPresenter GetPresenter()
  95. {
  96. var presenter = config.RouteTable.GetDefaultPresenter();
  97. return (DotvvmPresenter)presenter;
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement