public ContentManager(RazorConfig config) { if (config == null) { throw new ArgumentNullException("config"); } config = config.AsReadonly(); _contentProvider = new CompositeContentProvider(); ContentProvider.ContentModified += OnContentModified; config.ContentProviders.ToList().ForEach(ctor => AddContentProvider(ctor())); _pathBuilder = new VirtualPathBuilder(config.RootOperator.Path); _autoIncludeName = config.Templates.AutoIncludeName.EmptyAsNull(); _defaultExtension = config.Templates.DefaultExtension.EmptyAsNull(); if (_autoIncludeName != null && !_autoIncludeName.Contains(".") && _defaultExtension != null) { _autoIncludeName = _autoIncludeName + "." + _defaultExtension.TrimStart('.'); } var shared = config.Templates.SharedLocation.EmptyAsNull(); var searchPathList = new List <string>(); if (shared != null) { searchPathList.Add(GetPathBuilderWithRootOperator(shared).RemoveTrailingSlash()); } _virtualSearchPathList = searchPathList.AsReadOnly(); }
private void GetClassName(string virtualTemplatePath, out string @namespace, out string className) { virtualTemplatePath = new VirtualPathBuilder(_razorContext.Config.RootOperator.Path) .CombineWith(virtualTemplatePath) .Normalize() .WithRootOperator() .RemoveExtension() .ToString() .RemoveRoot(); var sb = new StringBuilder(); foreach (var ch in virtualTemplatePath) { if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_') { sb.Append(ch); } else if (ch == '/') { sb.Append('.'); } } className = sb.ToString(); var index = className.LastIndexOf("."); if (index == -1) { @namespace = _razorEngine.Host.DefaultNamespace; } else { @namespace = className.Substring(0, index); className = className.Substring(index + 1); } }
public void AppendTrailingSlashWorks() { var pb = new VirtualPathBuilder().CombineWith("/").AppendTrailingSlash(); Assert.IsTrue(pb == "/"); pb.Clear().CombineWith("/foo").AppendTrailingSlash(); Assert.IsTrue(pb.HasTrailingSlash()); Assert.IsTrue(pb == "/foo/"); }
public void CombineWithWorks() { var pb = new VirtualPathBuilder("/foo").CombineWith("oops/../doo/.").Normalize(); Assert.IsTrue(pb == "/foo/doo"); pb = new VirtualPathBuilder("/foo").CombineWith("oops", "..", "doo", ".", ".").Normalize(); Assert.IsTrue(pb == "/foo/doo"); pb = new VirtualPathBuilder("/foo").CombineWith("oops", "..", "doo", ".", ".","~/").Normalize(); Assert.IsTrue(pb == "/foo"); Assert.IsTrue(pb == "~/"); }
public void AddOrReplaceExtensionWorks() { var pb = new VirtualPathBuilder().CombineWith("~/t.cs"); Assert.IsTrue(pb.GetExtension() == "cs"); pb.AddOrReplaceExtension("vb"); Assert.IsTrue(pb.GetExtension() == "vb"); pb.Clear().CombineWith("foo"); Assert.IsFalse(pb.HasExtension()); pb.AddOrReplaceExtension("vb"); Assert.IsTrue(pb.GetExtension() == "vb"); }
public void ClearWorks() { var pb = new VirtualPathBuilder("/Foo"); Assert.AreEqual(string.Empty, pb.ToString()); Assert.AreEqual("/Foo/", pb.ApplicationRoot); pb.CombineWith("Doo"); Assert.AreEqual("Doo", pb.ToString()); Assert.AreEqual("/Foo/Doo", pb.Normalize().ToString()); pb.Clear(); Assert.AreEqual(string.Empty, pb.ToString()); pb.CombineWith("."); Assert.AreEqual("", pb.Normalize().ToString()); }
public void GetExtensionWorks() { var pb = new VirtualPathBuilder().CombineWith("."); Assert.IsTrue(pb == "."); Assert.IsFalse(pb.HasExtension()); Assert.IsNull(pb.GetExtension()); pb.Clear().CombineWith("~/oops.cshtml"); Assert.IsTrue(pb.HasExtension()); pb.Normalize(); Assert.IsTrue(pb.HasExtension()); Assert.IsTrue(pb.GetExtension() == "cshtml"); Assert.IsTrue(pb.HasExtension()); Assert.IsTrue(pb.GetExtension(true) == "cshtml"); Assert.IsFalse(pb.HasExtension()); }
public static bool operator ==(VirtualPathBuilder pb, string s) { try{ if (Equals(s, null) || Equals(pb, null)) return Equals(s, null) && Equals(pb, null); if (string.Compare(pb._builder.ToString(), s, StringComparison.OrdinalIgnoreCase) == 0) return true; var first = pb.Clone().Normalize().ToString(); var second = new VirtualPathBuilder(pb.ApplicationRoot).CombineWith(s).Normalize().ToString(); return string.Compare(first, second, StringComparison.OrdinalIgnoreCase) == 0; } catch{ // on any normalize error return false return false; } }
public static bool operator ==(VirtualPathBuilder pb, string s) { try{ if (Equals(s, null) || Equals(pb, null)) { return(Equals(s, null) && Equals(pb, null)); } if (string.Compare(pb._builder.ToString(), s, StringComparison.OrdinalIgnoreCase) == 0) { return(true); } var first = pb.Clone().Normalize().ToString(); var second = new VirtualPathBuilder(pb.ApplicationRoot).CombineWith(s).Normalize().ToString(); return(string.Compare(first, second, StringComparison.OrdinalIgnoreCase) == 0); } catch { // on any normalize error return false return(false); } }
public void IsAbsolutePathWorks() { var pb = new VirtualPathBuilder().CombineWith("~/"); Assert.IsFalse(pb.IsAbsolutePath()); pb.Normalize(); Assert.IsTrue(pb.IsAbsolutePath()); pb = new VirtualPathBuilder().CombineWith("~"); Assert.IsFalse(pb.IsAbsolutePath()); pb.Normalize(); Assert.IsTrue(pb.IsAbsolutePath()); pb = new VirtualPathBuilder().CombineWith("~/Foo"); Assert.IsFalse(pb.IsAbsolutePath()); pb.Normalize(); Assert.IsTrue(pb.IsAbsolutePath()); pb = new VirtualPathBuilder("/Foo"); Assert.IsFalse(pb.IsAbsolutePath()); Assert.IsTrue(pb.CombineWith("/Foo/").IsAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("/Foo").IsAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("/Foo/Doo").IsAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("/Foo/Doo/Boo").IsAbsolutePath()); Assert.IsTrue(pb.CombineWith("../..") == "/Foo"); Assert.IsTrue(pb.WithRootOperator().HasRootOperator()); pb = new VirtualPathBuilder("/Foo"); pb.CombineWith("/Doo/Foo"); Assert.IsTrue(pb.IsAbsolutePath()); Assert.IsFalse(pb.IsValidAbsolutePath()); }
public void HasTrailingSlashWorks() { var pb = new VirtualPathBuilder().CombineWith("/").Normalize(); Assert.IsTrue(pb.HasTrailingSlash()); Assert.IsFalse(pb.HasTrailingSlash(true)); pb.Clear().CombineWith("/foo/"); Assert.IsTrue(pb.HasTrailingSlash()); pb.Clear().CombineWith("/foo"); Assert.IsFalse(pb.HasTrailingSlash()); }
private static VirtualPathBuilder NormalizeWorks(string appName, string part, params string[] normalizeResults) { var pb = new VirtualPathBuilder(appName); pb.CombineWith(part); Assert.IsTrue(pb == part); if (normalizeResults.Length > 0) { foreach (var normalizeResult in normalizeResults) { Assert.IsTrue(pb == normalizeResult); } pb = pb.Normalize(); } else { var caught = false; try { pb.Normalize(); } catch (InvalidOperationException ex) { Debug.WriteLine("Expected error: {0}".FormatWith(ex.Message)); caught = true; } Assert.IsTrue(caught); return null; } return pb; }
public void IsRelativePathWorks() { var pb = new VirtualPathBuilder(); Assert.IsFalse(pb.IsRelativePath()); pb.CombineWith("."); Assert.IsTrue(pb.IsRelativePath()); pb.Clear().CombineWith("Foo"); Assert.IsTrue(pb.IsRelativePath()); pb.Normalize(); Assert.IsFalse(pb.IsRelativePath()); Assert.IsTrue(pb.IsAbsolutePath()); }
private static void AssertIsValidRoot(VirtualPathBuilder pb) { Assert.IsTrue(pb.IsAbsolutePath()); Assert.IsTrue(pb.IsApplicationRoot()); Assert.IsFalse(pb.IsRelativePath()); Assert.IsFalse(pb.HasRootOperator()); Assert.IsFalse(pb.HasExtension()); Assert.IsFalse(pb.HasTrailingSlash(ignoreRoot:true)); Assert.IsTrue(pb.IsValidAbsolutePath()); }
private static void MonkeyTest(string appName) { var pb = new VirtualPathBuilder(appName); Assert.AreEqual("", pb.ToString()); Assert.AreEqual((appName+"/").Replace("//","/"), pb.ApplicationRoot); Assert.AreEqual(pb.Length, 0); Assert.IsTrue(pb == ""); Assert.IsFalse(pb.IsAbsolutePath()); Assert.IsFalse(pb.IsApplicationRoot()); Assert.IsFalse(pb.IsRelativePath()); Assert.IsFalse(pb.HasRootOperator()); Assert.IsFalse(pb.HasExtension()); Assert.IsFalse(pb.HasTrailingSlash()); Assert.IsFalse(pb.IsValidAbsolutePath()); }
public void WithRootOperatorWorks() { var pb = new VirtualPathBuilder("/Foo"); var caught = false; try{ pb.WithRootOperator(); } catch(InvalidOperationException){ caught = true; } Assert.IsTrue(caught); Assert.IsTrue(pb.CombineWith("/Foo").WithRootOperator() == "~/"); Assert.IsTrue(pb.Clear().CombineWith("Doo").WithRootOperator() == "~/Doo"); }
public void NormalizingToRootRelativeWorksForCurrentDirectory() { var pb = new VirtualPathBuilder(); pb .CombineWith("/") .CombineWith("/./././.") .Normalize() .WithRootOperator(); Assert.AreEqual("~/", pb.ToString()); }
private void GetClassName(string virtualTemplatePath, out string @namespace, out string className) { virtualTemplatePath = new VirtualPathBuilder(_razorContext.Config.RootOperator.Path) .CombineWith(virtualTemplatePath) .Normalize() .WithRootOperator() .RemoveExtension() .ToString() .RemoveRoot(); var sb = new StringBuilder(); bool firstTokenChar = true; foreach(var ch in virtualTemplatePath) { if (ch >= '0' && ch <= '9'){ if (firstTokenChar) sb.Append('_'); sb.Append(ch); firstTokenChar = false; } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'){ sb.Append(firstTokenChar ? char.ToUpper(ch) : ch); firstTokenChar = false; } else if (ch == '/'){ sb.Append('.'); firstTokenChar = true; } } className = sb.ToString(); var index = className.LastIndexOf(".", StringComparison.Ordinal); if (index == -1) { @namespace = _razorEngine.Host.DefaultNamespace; } else { @namespace = className.Substring(0, index); className = className.Substring(index + 1); } }
public void HasRootOperatorWorks() { var pb = new VirtualPathBuilder().CombineWith("~/"); Assert.IsTrue(pb.HasRootOperator()); Assert.IsFalse(pb.Normalize().HasRootOperator()); Assert.IsTrue(pb == "/"); Assert.IsTrue(pb.WithRootOperator().HasRootOperator()); pb = new VirtualPathBuilder().CombineWith("~"); Assert.IsTrue(pb.HasRootOperator()); Assert.IsFalse(pb.Normalize().HasRootOperator()); Assert.IsTrue(pb == "/"); Assert.IsTrue(pb.WithRootOperator().HasRootOperator()); pb = new VirtualPathBuilder().CombineWith("~/Foo"); Assert.IsTrue(pb.HasRootOperator()); Assert.IsFalse(pb.Normalize().HasRootOperator()); Assert.IsTrue(pb == "/Foo"); Assert.IsTrue(pb.WithRootOperator().HasRootOperator()); }
public void RemoveExtensionWorks() { var pb = new VirtualPathBuilder(); pb.RemoveExtension(); pb.CombineWith("oops.cs"); Assert.IsTrue(pb.GetExtension() == "cs"); pb.RemoveExtension(); Assert.IsNull(pb.GetExtension()); }
public void PathBuilderWorksWithRootPartAdded() { var pb = new VirtualPathBuilder(); pb.CombineWith("/"); pb.CombineWith("/"); Assert.AreEqual("/", pb.ToString()); }
public void PathBuilderCanBeCreatedFromTwoParts() { var pb = new VirtualPathBuilder(); pb.CombineWith("~/part1", "part2").Normalize(); Assert.AreEqual("/part1/part2",pb.ToString()); pb.WithRootOperator(); Assert.AreEqual("~/part1/part2", pb.ToString()); }
public void GetFirstPartWorks() { var pb = new VirtualPathBuilder("/foo").CombineWith("~/app/path/"); Assert.IsTrue(pb.GetFirstPart() == "~"); Assert.IsTrue(pb.GetFirstPart(true) == "~"); Assert.IsTrue(pb.GetFirstPart() == "/app"); Assert.IsTrue(pb.GetFirstPart(true) == "/app"); Assert.IsTrue(pb.GetFirstPart() == "/path"); Assert.IsTrue(pb.GetFirstPart(true) == "/path"); Assert.IsTrue(pb.GetFirstPart(true) == "/"); pb = new VirtualPathBuilder("/foo").CombineWith("~/app/path/").Normalize(); Assert.IsTrue(pb.GetFirstPart() == "/foo"); Assert.IsTrue(pb.GetFirstPart(true) == "/foo"); Assert.IsTrue(pb.GetFirstPart() == "/app"); Assert.IsTrue(pb.GetFirstPart(true) == "/app"); Assert.IsTrue(pb.GetFirstPart() == "/path"); Assert.IsTrue(pb.GetFirstPart(true) == "/path"); Assert.IsTrue(pb.GetFirstPart(true) == ""); }
public void IsValidAbsolutePathWorks() { var pb = new VirtualPathBuilder(); Assert.IsFalse(pb.IsApplicationRoot()); Assert.IsFalse(pb.Clear().CombineWith(".").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("/").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("~").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("~/").Normalize().IsValidAbsolutePath()); pb = new VirtualPathBuilder("/Foo"); Assert.IsFalse(pb.IsApplicationRoot()); Assert.IsFalse(pb.Clear().CombineWith(".").Normalize().IsValidAbsolutePath()); Assert.IsFalse(pb.Clear().CombineWith("/").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("/Foo").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("/Foo/").Normalize().IsValidAbsolutePath()); Assert.IsFalse(pb.Clear().CombineWith("/Foo2/").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("~").Normalize().IsValidAbsolutePath()); Assert.IsTrue(pb.Clear().CombineWith("~/Anything").Normalize().IsValidAbsolutePath()); }
public void ResolveRootOperatorWorks() { var pb = new VirtualPathBuilder(); Assert.IsTrue(pb.CombineWith("Foo").ResolveRootOperator().ToString() == "/Foo"); Assert.IsTrue(pb.Clear().CombineWith("~/").ResolveRootOperator().ToString() == "/"); Assert.IsTrue(pb.Clear().CombineWith("~").ResolveRootOperator().ToString() == "/"); pb = new VirtualPathBuilder("/Foo"); Assert.IsTrue(pb.CombineWith("~/Doo").ResolveRootOperator().ToString() == "/Foo/Doo"); var caught = false; try{ pb.Clear().CombineWith("/Oops").ResolveRootOperator(); } catch(InvalidOperationException){ caught = true; } Assert.IsTrue(caught); }
public void NormalizeWorks() { var pb = new VirtualPathBuilder().Normalize(); Assert.IsTrue(pb == ""); Assert.IsTrue(pb.ToString() == ""); pb.Clear().CombineWith(".").Normalize(); Assert.IsTrue(pb == ""); Assert.IsTrue(pb.ToString() == ""); var caught = false; try{ pb.Clear().CombineWith("..").Normalize(); } catch(InvalidOperationException){ caught = true; } Assert.IsTrue(caught); CombineWithWorks(); }
public void RemoveTrailingSlashWorks() { var pb = new VirtualPathBuilder().CombineWith("/").RemoveTrailingSlash(); Assert.IsTrue(pb == ""); pb.Clear().CombineWith("/").RemoveTrailingSlash(true); Assert.IsTrue(pb == "/"); }
protected string NormalizeVirtualPath(string virtualPath) { if (virtualPath.NullOrEmpty()) throw new ArgumentNullException("virtualPath"); virtualPath = new VirtualPathBuilder(Context.Config.RootOperator.Path) .CombineWith(virtualPath) .WithRootOperator() .AddOrKeepExtension(Context.Config.Templates.DefaultExtension); return virtualPath; }