Anonymous avatar Anonymous committed 54f4bf1

initial checkin main file

Comments (0)

Files changed (1)

TemplateStrings/Template.cs

+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+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; }   
+
+    }
+
+    public class TemplateValueCollection: Collection<TemplateValue>
+    {}
+
+    public class Template
+    {
+
+        protected virtual TemplateValueCollection TemplateValues
+        {
+            get
+            {
+                TemplateValueCollection templateValues = new TemplateValueCollection();
+
+                templateValues.Add(new TemplateValue { StartDelimiter = "$", IdPattern = "[_a-z][_a-z0-9]*", StartDelimiterEscape="$$" });
+                templateValues.Add(new TemplateValue { StartDelimiter = "${", IdPattern = "[_a-z][_a-z0-9]*", EndDelimiter = "}",StartDelimiterEscape = "$${",EndDelimiterEscape = "}}"});
+
+                return templateValues;
+            }
+        }
+
+        protected virtual string delimiter 
+        {
+            get
+            {
+                return "$";
+            }
+        }
+
+        protected virtual string idpattern 
+        {
+            get
+            {
+                return "[_a-z][_a-z0-9]*";
+            }
+        }
+
+
+        protected virtual string pattern
+        {
+            get
+            {
+                return @"{0}(?:(?<escaped>{0})|(?<named>{1})|{{(?<braced>{1})}}|(?<invalid>))";
+            }
+        }
+
+        public string template
+        {
+            get; 
+            set;
+        }
+
+        public Template(string template)
+        {
+            this.template = template;
+        }
+
+        public string Substitute(IDictionary<string, string> mapping)
+        {
+            return Substitute(mapping, false);
+        }
+
+        public string Substitute(IDictionary<string,string> mapping,bool throwNoException)
+        {
+            var pat = string.Format(pattern, Regex.Escape(delimiter),idpattern);
+
+            Regex regex = new Regex(pat);
+
+
+            regex.Replace(this.template, match =>
+                                             {
+                                                 string val = match.Groups["named"].Value;
+                                                 if(!string.IsNullOrEmpty(val))
+                                                 {
+                                                     try
+                                                     {
+                                                         return mapping[val];
+
+                                                     }
+                                                     catch (Exception)
+                                                     {
+                                                         if (throwNoException)
+                                                         {
+                                                             return match.Value;
+                                                         }
+                                                         else
+                                                         {
+                                                             throw;
+                                                         }
+                                                     }
+                                                     
+
+
+
+                                                 }
+
+                                                 val = match.Groups["braced"].Value;
+                                                 if (!string.IsNullOrEmpty(val))
+                                                 {
+
+
+
+                                                 }
+
+                                                 val = match.Groups["escaped"].Value;
+                                                 if (!string.IsNullOrEmpty(val))
+                                                 {
+                                                     return this.delimiter;
+                                                 }
+
+                                                 val = match.Groups["invalid"].Value;
+                                                 if (!string.IsNullOrEmpty(val))
+                                                 {
+                                                     //test this
+                                                 }
+
+
+                                                 throw new ApplicationException("Unrecognized named group in pattern");
+
+
+
+                                             });
+
+            return "";
+            
+
+        }
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.