{"id":535,"date":"2016-10-25T08:04:21","date_gmt":"2016-10-25T07:04:21","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=535"},"modified":"2022-12-08T10:03:23","modified_gmt":"2022-12-08T09:03:23","slug":"c-antlr-string-template","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-antlr-string-template\/","title":{"rendered":"C# Antlr String Template + Embedded Resources"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using Application.Business.Repository;\nusing Application.Business.UnitOfWork;\nusing log4net;\nusing Antlr4.StringTemplate;\nusing System.Globalization;\nusing System.Reflection;\nusing System.IO;\n\nnamespace Application.Business.Service\n{\n    public class ResourceTemplates \n    {\n        public const string InvoiceUploaded = \"Application.Business.Templates.Email.InvoiceUploaded.html\";\n        public const string PasswordRequest = \"Application.Business.Templates.Email.PasswordRequest.html\";\n    }\n\n    public class TemplateService : BaseService\n    {\n        private static TemplateService instance;\n        public static TemplateService Instance\n        {\n            get\n            {\n                if (instance == null)\n                {\n                    instance = new TemplateService();\n                }\n                return instance;\n            }\n        }\n\n        public TemplateService() : base(new RepositoryFactory(), new UnitOfWorkFactory(), LogManager.GetLogger(typeof(UserService)))\n        {\n        }\n\n        public string GetEmbeddedResourceAsString(string resourceName) {\n            var assembly = Assembly.GetExecutingAssembly();\n            \/\/ assembly.GetManifestResourceNames(); \/\/ Get all the resources\n            using (Stream stream = assembly.GetManifestResourceStream(resourceName))\n            {\n                if (stream != null)\n                {\n                    using (StreamReader reader = new StreamReader(stream))\n                    {\n                        string result = reader.ReadToEnd();\n                        return result;\n                    }\n                }\n                return string.Empty;\n            }\n        }\n\n        public string ProcessResourceTemplate(string resourceName, string[,] parameters)\n        {\n            string template = GetEmbeddedResourceAsString(resourceName);\n            return ProcessTemplate(template, parameters);\n        }\n\n        public string ProcessTemplate(string stringTemplate, string[,] parameters) \n        {\n            TemplateGroup templateGroup = new TemplateGroup('{', '}');\n            Template template = new Template(templateGroup, stringTemplate);\n\n            for (int i = 0; i &lt; parameters.GetLength(0);i += 1)\n            {\n                template.Add(parameters[i, 0], parameters[i, 1]);\n            }\n            return template.Render(new CultureInfo(\"nl-NL\"), 200);\n        }\n    }\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":7070,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/535\/revisions\/7070"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}