Commits

Anonymous committed 10eba8e

some Refactoring

  • Participants
  • Parent commits 09c8763

Comments (0)

Files changed (5)

File TemplateStrings/Properties/AssemblyInfo.cs

-using System.Reflection;
+using System;
+using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 // [assembly: AssemblyVersion("1.0.*")]
 [assembly: AssemblyVersion("1.0.0.0")]
 [assembly: AssemblyFileVersion("1.0.0.0")]
+
+[assembly:CLSCompliant(true)]

File TemplateStrings/Template.cs

-using System;
-using System.Collections.Generic;
-using System.Text;
-using System.Text.RegularExpressions;
-
-namespace TemplateStrings
-{
-    public class TemplateValue
-    {
-        public string StartDelimiter { get; set; }
-
-        public string StartDelimiterEscape { get; set; }
-
-        public string EndDelimiterEscape { get; set; }
-
-        public string EndDelimiter { get; set; }
-
-        public string IdPattern { get; set; }
-
-        protected internal virtual string GetRegexString()
-        {
-            var builder = new StringBuilder();
-
-            bool hasStartDelimiterEscape = !string.IsNullOrEmpty(StartDelimiterEscape);
-            bool hasStartDelimiter = !string.IsNullOrEmpty(StartDelimiter);
-
-            bool hasIdPattern = !string.IsNullOrEmpty(IdPattern);
-
-            bool hasEndDelimiterEscape = !string.IsNullOrEmpty(EndDelimiterEscape);
-            bool hasEndDelimiter = !string.IsNullOrEmpty(EndDelimiter);
-
-            if (hasStartDelimiterEscape && hasStartDelimiter)
-                builder.Append('(');
-
-            if (hasStartDelimiterEscape)
-            {
-                builder.AppendFormat("(?<escapedstart>{0})", Regex.Escape(StartDelimiterEscape));
-            }
-
-            if (hasStartDelimiter)
-            {
-                if (hasStartDelimiterEscape)
-                    builder.Append('|');
-
-                builder.AppendFormat("(?<start>{0})", Regex.Escape(StartDelimiter));
-            }
-
-            if (hasStartDelimiterEscape && hasStartDelimiter)
-                builder.Append(')');
-
-            if (hasIdPattern)
-                builder.AppendFormat("(?<named>{0})", IdPattern);
-
-            if (hasEndDelimiterEscape && hasEndDelimiter)
-                builder.Append('(');
-
-            if (hasEndDelimiterEscape)
-            {
-                builder.AppendFormat("(?<escapedend>{0})", Regex.Escape(EndDelimiterEscape));
-            }
-
-            if (hasEndDelimiter)
-            {
-                if (hasEndDelimiterEscape)
-                    builder.Append('|');
-
-                builder.AppendFormat("(?<end>{0})", Regex.Escape(EndDelimiter));
-            }
-
-            if (hasEndDelimiterEscape && hasEndDelimiter)
-                builder.Append(')');
-
-            return builder.ToString();
-        }
-    }
-
-
-    public abstract class Template
-    {
-        protected Template(string template)
-        {
-            this.template = template;
-        }
-
-        protected abstract IList<TemplateValue> TemplateValues { get; }
-
-        public string template { get; private set; }
-
-        private void Validate()
-        {
-            foreach (TemplateValue val in TemplateValues)
-            {
-                if (!string.IsNullOrEmpty(val.EndDelimiter) && string.IsNullOrEmpty(val.StartDelimiter))
-                    throw new ArgumentException("When an EndDelimiter is used you must set a StartDelimiter as well.");
-
-                if (!string.IsNullOrEmpty(val.StartDelimiterEscape) && string.IsNullOrEmpty(val.StartDelimiter))
-                    throw new ArgumentException(
-                        "When an StartDelimiterEscape is used you must use set a StartDelimiter as well.");
-
-                if (!string.IsNullOrEmpty(val.EndDelimiterEscape) && string.IsNullOrEmpty(val.EndDelimiter))
-                    throw new ArgumentException(
-                        "When an EndDelimiterEscape is used you must use set a EndDelimiter as well.");
-
-                if (string.IsNullOrEmpty(val.IdPattern))
-                    throw new ArgumentException("You must set an IdPattern");
-
-                //escaped end nur wenn escaped start
-            }
-        }
-
-        public string Substitute(IDictionary<string, object> mapping)
-        {
-            return Substitute(mapping, false);
-        }
-
-        private string GetRegex()
-        {
-            var builder = new StringBuilder(200);
-
-            for (int i = 0; i < TemplateValues.Count; i++)
-            {
-                TemplateValue value = TemplateValues[i];
-
-                builder.AppendFormat("(?<{0}>", i + 1);
-
-                builder.Append(value.GetRegexString());
-
-                builder.Append(")|");
-            }
-
-            builder.Length--;
-
-            return builder.ToString();
-        }
-
-
-//((?<escapedstart>\{\{)  |  (?<start>\{))
-
-//(?<named>[_a-z][_a-z0-9]*)
-
-//((?<escapedend>}})  |  (?<end>}))
-
-        private string GetValueFromDictionary(IDictionary<string, object> mapping, string value)
-        {
-            return mapping[value].ToString();
-        }
-
-        public string Substitute(IDictionary<string, object> mapping, bool throwNoException)
-        {
-            IList<TemplateValue> _TemplateValues = TemplateValues;
-
-            Validate();
-            var regex = new Regex(GetRegex(),
-                                  RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
-
-            return regex.Replace(template, delegate(Match match)
-                                               {
-                                                   TemplateValue templateValue = null;
-
-                                                   for (int i = 0; i < _TemplateValues.Count; i++)
-                                                   {
-                                                       if (match.Groups[(i + 1).ToString()].Success)
-                                                       {
-                                                           templateValue = _TemplateValues[i];
-                                                           break;
-                                                       }
-                                                   }
-
-                                                   if (match.Groups["escapedstart"].Success)
-                                                   {
-                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiterEscape))
-                                                           return templateValue.StartDelimiter + match.Groups["named"] +
-                                                                  templateValue.EndDelimiter;
-                                                       else if (match.Groups["escapedend"].Success)
-                                                           return templateValue.StartDelimiter + match.Groups["named"] +
-                                                                  templateValue.EndDelimiter;
-                                                       else if (match.Groups["end"].Success)
-                                                           return templateValue.StartDelimiter +
-                                                                  GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
-                                                       //maybe test for second match here
-                                                   }
-                                                   
-                                                   if (match.Groups["escapedend"].Success)
-                                                   {
-                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiterEscape))
-                                                           return templateValue.StartDelimiter + match.Groups["named"] +
-                                                                  templateValue.EndDelimiter;
-                                                       else if (match.Groups["escapedstart"].Success)
-                                                           return templateValue.StartDelimiter + match.Groups["named"] +
-                                                                  templateValue.EndDelimiter;
-                                                       else if (match.Groups["start"].Success)
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value) + templateValue.EndDelimiter;
-                                                       //maybe test for second match here
-                                                   }
-
-                                                   if (match.Groups["start"].Success)
-                                                   {
-                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiter))
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
-                                                       else if (match.Groups["escapedend"].Success)
-                                                           return
-                                                               GetValueFromDictionary(mapping,
-                                                                                      match.Groups["named"].Value) +
-                                                               templateValue.EndDelimiter;
-                                                       else if (match.Groups["end"].Success)
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
-                                                   }
-
-                                                   if (match.Groups["end"].Success)
-                                                   {
-                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiter))
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
-                                                       else if (match.Groups["escapedstart"].Success)
-                                                           return templateValue.StartDelimiter + GetValueFromDictionary(mapping,
-                                                                                      match.Groups["named"].Value);
-                                                       else if (match.Groups["start"].Success)
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
-                                                   }
-
-                                                   //what if value not in dictionary but escaped startdelimiter
-
-                                                   throw new ApplicationException("Unrecognized named group in pattern");
-                                               });
-        }
-    }
-
-    public class TemplateStringsPython : Template
-    {
-        public TemplateStringsPython(string template)
-            : base(template)
-        {
-        }
-
-        protected override IList<TemplateValue> TemplateValues
-        {
-            get
-            {
-                return new[]
-                           {
-                               new TemplateValue
-                                   {
-                                       StartDelimiter = "$",
-                                       IdPattern = "[_a-z][_a-z0-9]*",
-                                       StartDelimiterEscape = "$$"
-                                   },
-                               new TemplateValue
-                                   {
-                                       StartDelimiter = "${",
-                                       IdPattern = "[_a-z][_a-z0-9]*",
-                                       StartDelimiterEscape = "$${",
-                                       EndDelimiter = "}"
-                                   }
-                           };
-            }
-        }
-    }
-
-    public class TemplateStringsMvc : Template
-    {
-        public TemplateStringsMvc(string template)
-            : base(template)
-        {
-        }
-
-        protected override IList<TemplateValue> TemplateValues
-        {
-            get
-            {
-                return new[]
-                           {
-                               new TemplateValue
-                                   {
-                                       StartDelimiter = "{",
-                                       StartDelimiterEscape = "{{",
-                                       IdPattern = "[_a-z][_a-z0-9]*",
-                                       EndDelimiter = "}",
-                                       EndDelimiterEscape = "}}"
-                                   }
-                           };
-            }
-        }
-    }
-
-#if NET35
-
-    public static class TemplateStringExtensions
-    {
-        public static string Substitute(this string source,IDictionary<string,object> mapping)
-        {
-            return (new TemplateStringsPython(source)).Substitute(mapping);
-        }
-
-        public static string Substitute(this string source, IDictionary<string, object> mapping, Template strategy)
-        {
-            return strategy.Substitute(mapping);
-        }
-    }
-
-#endif
-
-}

File TemplateStrings/TemplateString.cs

+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace TemplateStrings
+{
+    public class TemplateValue
+    {
+        public string StartDelimiter { get; set; }
+
+        public string StartDelimiterEscape { get; set; }
+
+        public string EndDelimiterEscape { get; set; }
+
+        public string EndDelimiter { get; set; }
+
+        public string IdPattern { get; set; }
+
+        protected internal virtual string GetRegexString()
+        {
+            var builder = new StringBuilder();
+
+            bool hasStartDelimiterEscape = !string.IsNullOrEmpty(StartDelimiterEscape);
+            bool hasStartDelimiter = !string.IsNullOrEmpty(StartDelimiter);
+
+            bool hasIdPattern = !string.IsNullOrEmpty(IdPattern);
+
+            bool hasEndDelimiterEscape = !string.IsNullOrEmpty(EndDelimiterEscape);
+            bool hasEndDelimiter = !string.IsNullOrEmpty(EndDelimiter);
+
+            if (hasStartDelimiterEscape && hasStartDelimiter)
+                builder.Append('(');
+
+            if (hasStartDelimiterEscape)
+            {
+                builder.AppendFormat("(?<escapedstart>{0})", Regex.Escape(StartDelimiterEscape));
+            }
+
+            if (hasStartDelimiter)
+            {
+                if (hasStartDelimiterEscape)
+                    builder.Append('|');
+
+                builder.AppendFormat("(?<start>{0})", Regex.Escape(StartDelimiter));
+            }
+
+            if (hasStartDelimiterEscape && hasStartDelimiter)
+                builder.Append(')');
+
+            if (hasIdPattern)
+                builder.AppendFormat("(?<named>{0})", IdPattern);
+
+            if (hasEndDelimiterEscape && hasEndDelimiter)
+                builder.Append('(');
+
+            if (hasEndDelimiterEscape)
+            {
+                builder.AppendFormat("(?<escapedend>{0})", Regex.Escape(EndDelimiterEscape));
+            }
+
+            if (hasEndDelimiter)
+            {
+                if (hasEndDelimiterEscape)
+                    builder.Append('|');
+
+                builder.AppendFormat("(?<end>{0})", Regex.Escape(EndDelimiter));
+            }
+
+            if (hasEndDelimiterEscape && hasEndDelimiter)
+                builder.Append(')');
+
+            return builder.ToString();
+        }
+    }
+
+
+    public abstract class TemplateString
+    {
+        protected TemplateString(string template)
+        {
+            Template = template;
+        }
+
+        protected abstract IList<TemplateValue> TemplateValues { get; }
+
+        public string Template { get; private set; }
+
+        private void Validate()
+        {
+            foreach (TemplateValue val in TemplateValues)
+            {
+                if (!string.IsNullOrEmpty(val.StartDelimiterEscape) && string.IsNullOrEmpty(val.StartDelimiter))
+                    throw new ArgumentException(
+                        "When an StartDelimiterEscape is used you must use set a StartDelimiter as well.");
+
+                if (!string.IsNullOrEmpty(val.EndDelimiterEscape) && string.IsNullOrEmpty(val.EndDelimiter))
+                    throw new ArgumentException(
+                        "When an EndDelimiterEscape is used you must use set a EndDelimiter as well.");
+
+                if (string.IsNullOrEmpty(val.IdPattern))
+                    throw new ArgumentException("You must set an IdPattern");
+
+                //escaped end nur wenn escaped start
+            }
+        }
+
+        public string Substitute(IDictionary<string, string> mapping)
+        {
+            return Substitute(mapping, false);
+        }
+
+        private string GetRegex()
+        {
+            var builder = new StringBuilder(200);
+
+            for (int i = 0; i < TemplateValues.Count; i++)
+            {
+                TemplateValue value = TemplateValues[i];
+
+                builder.AppendFormat("(?<{0}>", i + 1);
+
+                builder.Append(value.GetRegexString());
+
+                builder.Append(")|");
+            }
+
+            builder.Length--;
+
+            return builder.ToString();
+        }
+
+
+//((?<escapedstart>\{\{)  |  (?<start>\{))
+
+//(?<named>[_a-z][_a-z0-9]*)
+
+//((?<escapedend>}})  |  (?<end>}))
+
+        private string GetValueFromDictionary(IDictionary<string, string> mapping, string value)
+        {
+            return mapping[value].ToString();
+        }
+
+        public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
+        {
+            IList<TemplateValue> _TemplateValues = TemplateValues;
+
+            Validate();
+            var regex = new Regex(GetRegex(),
+                                  RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
+
+            return regex.Replace(Template, delegate(Match match)
+                                               {
+                                                   TemplateValue templateValue = null;
+
+                                                   for (int i = 0; i < _TemplateValues.Count; i++)
+                                                   {
+                                                       if (match.Groups[(i + 1).ToString()].Success)
+                                                       {
+                                                           templateValue = _TemplateValues[i];
+                                                           break;
+                                                       }
+                                                   }
+
+                                                   if (match.Groups["escapedstart"].Success)
+                                                   {
+                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiterEscape))
+                                                           return templateValue.StartDelimiter + match.Groups["named"] +
+                                                                  templateValue.EndDelimiter;
+                                                       else if (match.Groups["escapedend"].Success)
+                                                           return templateValue.StartDelimiter + match.Groups["named"] +
+                                                                  templateValue.EndDelimiter;
+                                                       else if (match.Groups["end"].Success)
+                                                           return templateValue.StartDelimiter +
+                                                                  GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                       //maybe test for second match here
+                                                   }
+
+                                                   if (match.Groups["escapedend"].Success)
+                                                   {
+                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiterEscape))
+                                                           return templateValue.StartDelimiter + match.Groups["named"] +
+                                                                  templateValue.EndDelimiter;
+                                                       else if (match.Groups["escapedstart"].Success)
+                                                           return templateValue.StartDelimiter + match.Groups["named"] +
+                                                                  templateValue.EndDelimiter;
+                                                       else if (match.Groups["start"].Success)
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value) +
+                                                                  templateValue.EndDelimiter;
+                                                       //maybe test for second match here
+                                                   }
+
+                                                   if (match.Groups["start"].Success)
+                                                   {
+                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiter))
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                       else if (match.Groups["escapedend"].Success)
+                                                           return
+                                                               GetValueFromDictionary(mapping,
+                                                                                      match.Groups["named"].Value) +
+                                                               templateValue.EndDelimiter;
+                                                       else if (match.Groups["end"].Success)
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                   }
+
+                                                   if (match.Groups["end"].Success)
+                                                   {
+                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiter))
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                       else if (match.Groups["escapedstart"].Success)
+                                                           return templateValue.StartDelimiter +
+                                                                  GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                       else if (match.Groups["start"].Success)
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                   }
+
+                                                   //what if value not in dictionary but escaped startdelimiter
+
+                                                   throw new ApplicationException("Unrecognized named group in pattern");
+                                               });
+        }
+    }
+
+    public class TemplateStringsPython : TemplateString
+    {
+        public TemplateStringsPython(string template)
+            : base(template)
+        {
+        }
+
+        protected override IList<TemplateValue> TemplateValues
+        {
+            get
+            {
+                return new[]
+                           {
+                               new TemplateValue
+                                   {
+                                       StartDelimiter = "$",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       StartDelimiterEscape = "$$"
+                                   },
+                               new TemplateValue
+                                   {
+                                       StartDelimiter = "${",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       StartDelimiterEscape = "$${",
+                                       EndDelimiter = "}"
+                                   }
+                           };
+            }
+        }
+    }
+
+    public class TemplateStringsMvc : TemplateString
+    {
+        public TemplateStringsMvc(string template)
+            : base(template)
+        {
+        }
+
+        protected override IList<TemplateValue> TemplateValues
+        {
+            get
+            {
+                return new[]
+                           {
+                               new TemplateValue
+                                   {
+                                       StartDelimiter = "{",
+                                       StartDelimiterEscape = "{{",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       EndDelimiter = "}",
+                                       EndDelimiterEscape = "}}"
+                                   }
+                           };
+            }
+        }
+    }
+
+#if NET35
+
+    public static class TemplateStringExtensions
+    {
+        public static string Substitute(this string source, IDictionary<string, string> mapping)
+        {
+            return (new TemplateStringsPython(source)).Substitute(mapping);
+        }
+
+        public static string Substitute(this string source, IDictionary<string, string> mapping, TemplateString strategy)
+        {
+            return strategy.Substitute(mapping);
+        }
+    }
+
+#endif
+}

File TemplateStrings/TemplateStrings.csproj

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Template.cs" />
+    <Compile Include="TemplateString.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>

File TemplateStringsTest/TemplateTest.cs

         [TestMethod]
         public void Substitute_PythonTemplate_ReturnsSubstitutedString()
         {
-            var pythonTemplateTestDictionary = new Dictionary<string, object> { { "ipsum", "test1" } };
+            var pythonTemplateTestDictionary = new Dictionary<string, string> { { "ipsum", "test1" } };
 
             string[,] pythonTemplateTestStrings =
                 {
 
             for (int i = 0; i < pythonTemplateTestStrings.GetLength(0); i++)
             {
-                Template template = new TemplateStringsPython(pythonTemplateTestStrings[i, 0]);
+                TemplateString templateString = new TemplateStringsPython(pythonTemplateTestStrings[i, 0]);
 
-                Assert.AreEqual(pythonTemplateTestStrings[i, 1], template.Substitute(pythonTemplateTestDictionary));
+                Assert.AreEqual(pythonTemplateTestStrings[i, 1], templateString.Substitute(pythonTemplateTestDictionary));
             }
         }
 
         [TestMethod]
         public void Substitute_PythonTemplateReverseFake_ReturnsSubstitutedString()
         {
-            var pythonTemplateTestDictionary = new Dictionary<string, object> { { "ipsum", "test1" } };
+            var pythonTemplateTestDictionary = new Dictionary<string, string> { { "ipsum", "test1" } };
 
             string[,] pythonTemplateTestStrings =
                 {
                     {"Lorem ipsum$ dolor sit amet.", "Lorem test1 dolor sit amet."},
                     {"Lorem {ipsum}$ dolor sit amet.", "Lorem test1 dolor sit amet."},
                     {"Lorem ipsum$$ dolor sit amet.", "Lorem ipsum$ dolor sit amet."},
-                    {"Lorem {ipsum}$$ dolor sit amet.", "Lorem {ipsum}$ dolor sit amet."}
+                    {"Lorem {ipsum}$$ dolor sit amet.", "Lorem {ipsum}$ dolor sit amet."},
 
 					{"Lorem ipsum}$$ dolor sit amet.", "Lorem ipsum}$$ dolor sit amet."},
 					{"Lorem ipsum}$ dolor sit amet.", "Lorem ipsum}$ dolor sit amet."},
 
             for (int i = 0; i < pythonTemplateTestStrings.GetLength(0); i++)
             {
-                Template template = new PythonTemplateReverseFake(pythonTemplateTestStrings[i, 0]);
+                TemplateString templateString = new PythonTemplateReverseFake(pythonTemplateTestStrings[i, 0]);
 
-                Assert.AreEqual(pythonTemplateTestStrings[i, 1], template.Substitute(pythonTemplateTestDictionary));
+                Assert.AreEqual(pythonTemplateTestStrings[i, 1], templateString.Substitute(pythonTemplateTestDictionary));
             }
         }
 
-        class PythonTemplateReverseFake : Template
+        class PythonTemplateReverseFake : TemplateString
         {
             public PythonTemplateReverseFake(string template):base(template)
             {}
         [TestMethod]
         public void Substitute_MvcTemplate_ReturnsSubstitutedString()
         {
-            var mvcTemplateTestDictionary = new Dictionary<string, object> { { "ipsum", "test1" } };
+            var mvcTemplateTestDictionary = new Dictionary<string, string> { { "ipsum", "test1" } };
 
             string[,] mvcTemplateTestStrings =
                 {
 
             for (int i = 0; i < mvcTemplateTestStrings.GetLength(0); i++)
             {
-                Template template = new TemplateStringsMvc(mvcTemplateTestStrings[i, 0]);
+                TemplateString templateString = new TemplateStringsMvc(mvcTemplateTestStrings[i, 0]);
 
-                Assert.AreEqual(mvcTemplateTestStrings[i, 1], template.Substitute(mvcTemplateTestDictionary));
+                Assert.AreEqual(mvcTemplateTestStrings[i, 1], templateString.Substitute(mvcTemplateTestDictionary));
             }
         }
     }