예제 #1
0
        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();
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
 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/");
 }
예제 #4
0
 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 == "~/");
 }
예제 #5
0
 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");
 }
예제 #6
0
 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());
 }
예제 #7
0
 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());
 }
예제 #8
0
 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;
     }
 }
예제 #9
0
 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);
     }
 }
예제 #10
0
        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());
        }
예제 #11
0
 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());
 }
예제 #12
0
        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;
        }
예제 #13
0
 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());
 }
예제 #14
0
 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());
 }
예제 #15
0
 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());
 }
예제 #16
0
 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");
 }
예제 #17
0
 public void NormalizingToRootRelativeWorksForCurrentDirectory()
 {
     var pb = new VirtualPathBuilder();
     pb
         .CombineWith("/")
         .CombineWith("/./././.")
         .Normalize()
         .WithRootOperator();
     Assert.AreEqual("~/", pb.ToString());
 }
예제 #18
0
        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);
            }
        }
예제 #19
0
        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());
        }
예제 #20
0
 public void RemoveExtensionWorks()
 {
     var pb = new VirtualPathBuilder();
     pb.RemoveExtension();
     pb.CombineWith("oops.cs");
     Assert.IsTrue(pb.GetExtension() == "cs");
     pb.RemoveExtension();
     Assert.IsNull(pb.GetExtension());
 }
예제 #21
0
 public void PathBuilderWorksWithRootPartAdded()
 {
     var pb = new VirtualPathBuilder();
     pb.CombineWith("/");
     pb.CombineWith("/");
     Assert.AreEqual("/", pb.ToString());
 }
예제 #22
0
 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());
 }
예제 #23
0
        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) == "");
        }
예제 #24
0
        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());
        }
예제 #25
0
 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);
 }
예제 #26
0
        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();
        }
예제 #27
0
 public void RemoveTrailingSlashWorks()
 {
     var pb = new VirtualPathBuilder().CombineWith("/").RemoveTrailingSlash();
     Assert.IsTrue(pb == "");
     pb.Clear().CombineWith("/").RemoveTrailingSlash(true);
     Assert.IsTrue(pb == "/");
 }
예제 #28
0
 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;
 }