/// <summary>
 /// Creates a new instance of the StringTemplateViewEngine
 /// </summary>
 /// <param name="viewPath">The physical path to the root views directory</param>
 public StringTemplateViewEngine(string viewPath)
 {
    Group = new TemplateGroupDirectory(viewPath, '$', '$')
               {
                  Listener = new Log4NetTemplateErrorListener()
               };
 }
        public virtual void LoadPrecRuleTemplates()
        {
            string templateGroupFile = Path.Combine("Tool", "Templates", "LeftRecursiveRules.stg");
            recRuleTemplates = new TemplateGroupFile(
                Path.Combine(
                    Path.GetDirectoryName(typeof(AntlrTool).GetTypeInfo().Assembly.Location),
                    templateGroupFile),
                Encoding.UTF8);
            if (!recRuleTemplates.IsDefined("recRule"))
            {
                tool.errMgr.ToolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules");
            }

            // use codegen to get correct language templates; that's it though
            CodeGenerator gen = new CodeGenerator(tool, null, language);
            TemplateGroup templates = gen.GetTemplates();
            if (templates == null)
            {
                // this class will still operate using Java templates
                templates = new CodeGenerator(tool, null, "Java").GetTemplates();
                Debug.Assert(templates != null);
            }

            codegenTemplates = templates;
        }
Example #3
0
 public LexerATNFactory(LexerGrammar g)
     : base(g)
 {
     // use codegen to get correct language templates for lexer commands
     string language = g.GetOptionString("language");
     CodeGenerator gen = new CodeGenerator(g.tool, null, language);
     AbstractTarget target = gen.GetTarget();
     codegenTemplates = target != null ? target.GetTemplates() : null;
 }
Example #4
0
        public virtual TemplateGroup GetTemplates()
        {
            if (templates == null)
            {
                templates = LoadTemplates();
            }

            return templates;
        }
Example #5
0
 public static string ToListString(this IList list)
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("listTemplate", "[<list:{x|<x>}; separator=\", \">]", new string[] { "list" });
     group.RegisterRenderer(typeof(IList), new CollectionRenderer());
     Template st = group.GetInstanceOf("listTemplate");
     st.Add("list", list);
     return st.Render();
 }
 public void TestMissingDictionaryValue2()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<if(m.foo)>[<m.foo>]<endif>", new string[] { "m" });
     Template t = group.GetInstanceOf("test");
     t.Add("m", new Dictionary<string, string>());
     string expecting = "";
     string result = t.Render();
     Assert.AreEqual(expecting, result);
 }
 public void TestEmptyListGetsNoOutput()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test",
         "begin\n" +
         "<users:{u | name: <u>}; separator=\", \">\n" +
         "end\n", new string[] { "users" });
     Template t = group.GetInstanceOf("test");
     t.Add("users", new List<string>());
     string expecting = "begin" + newline + "end";
     string result = t.Render();
     Assert.AreEqual(expecting, result);
 }
Example #8
0
 public void TestAttrSeparator()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <name; separator=sep>!", new string[] { "name", "sep" });
     Template st = group.GetInstanceOf("test");
     st.Add("sep", ", ");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected = "hi Ter, Tom, Sumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #9
0
 public void TestIllegalOption()
 {
     ErrorBuffer errors = new ErrorBuffer();
     TemplateGroup group = new TemplateGroup();
     group.Listener = errors;
     group.DefineTemplate("test", "<name; bad=\"ugly\">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     string expected = "Ter";
     string result = st.Render();
     Assert.AreEqual(expected, result);
     expected = "[test 1:7: no such option: bad]";
     Assert.AreEqual(expected, errors.Errors.ToListString());
 }
Example #10
0
 public void TestDoubleListApplyWithNullValueAndNullOption()
 {
     // first apply sends [Template, null, Template] to second apply, which puts [] around
     // the value.  This verifies that null not blank comes out of first apply
     // since we don't get [null].
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<name:{n | <n>}:{n | [<n>]}; null=\"n/a\">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", null);
     st.Add("name", "Sumana");
     string expected = "[Ter]n/a[Sumana]";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
 public void TestIndirectMap()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("a", "[<x>]", new string[] { "x" });
     group.DefineTemplate("test", "hi <names:(templateName)()>!", new string[] { "names", "templateName" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("templateName", "a");
     string expected =
         "hi [Ter][Tom][Sumana]!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #12
0
 public void TestParallelMap()
 {
     TemplateGroup group = new TemplateGroup('$', '$');
     group.DefineTemplate("test", "hi $names,phones:{n,p | $n$:$p$;}$", new string[] { "names", "phones" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("phones", "x5001");
     st.Add("phones", "x5002");
     st.Add("phones", "x5003");
     string expected =
         "hi Ter:x5001;Tom:x5002;Sumana:x5003;";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #13
0
 public void TestEmptyExpr2()
 {
     string template = "hi <> ";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:3: doesn't look like an expression" + newline;
     Assert.AreEqual(expected, result);
 }
Example #14
0
        public static string ReplacePropertyTokensInGroups(IEnumerable collection, string groupTemplate)
        {
            StringBuilder resultBuilder = new StringBuilder();

            foreach (var listItem in collection)
            {
                //We need new template for every collection item so that sub collection items like registrants are cleaned up
                TemplateGroup eventGroup = new TemplateGroup(TextTemplateUtils.DefaultTokenStartDelimiter, TextTemplateUtils.DefaultTokenEndDelimiter);
                eventGroup.RegisterRenderer(typeof(string), new StringRenderer());
                Template eventTemplate = new Template(eventGroup, groupTemplate);

                AddPropertiesToTemplate(listItem, eventTemplate);
                resultBuilder.Append(eventTemplate.Render(CultureInfo.InvariantCulture));
            }

            return resultBuilder.ToString();
        }
        protected TemplateGroup GetTemplateGroup(List<string> templateNames)
        {
            if (tg == null)
            {
                // combile the header and all .st files and load everything into a TemplateGroup
                tg = new TemplateGroup();
                foreach (var templateName in templateNames)
                {
                    tg.ImportTemplates(GetTemplateGroupFromResource(templateName));
                }
                foreach (var type in attributeRenderers.Keys)
                {
                    var renderer = attributeRenderers[type];
                    tg.RegisterRenderer(type, renderer);
                }
            }

            return tg;
        }
Example #16
0
        public void TestEvalSTIteratingSubtemplateInSTFromAnotherGroup()
        {
            ErrorBuffer errors = new ErrorBuffer();
            TemplateGroup innerGroup = new TemplateGroup();
            innerGroup.Listener = errors;
            innerGroup.DefineTemplate("test", "<m:samegroup()>", new string[] { "m" });
            innerGroup.DefineTemplate("samegroup", "hi ", new string[] { "x" });
            Template st = innerGroup.GetInstanceOf("test");
            st.Add("m", new int[] { 1, 2, 3 });

            TemplateGroup outerGroup = new TemplateGroup();
            outerGroup.DefineTemplate("errorMessage", "<x>", new string[] { "x" });
            Template outerST = outerGroup.GetInstanceOf("errorMessage");
            outerST.Add("x", st);

            string expected = "hi hi hi ";
            string result = outerST.Render();

            Assert.AreEqual(errors.Errors.Count, 0); // ignores no such prop errors

            Assert.AreEqual(expected, result);
        }
Example #17
0
        public void TestEvalSTFromAnotherGroup()
        {
            ErrorBuffer errors = new ErrorBuffer();
            TemplateGroup innerGroup = new TemplateGroup();
            innerGroup.Listener = errors;
            innerGroup.DefineTemplate("bob", "inner");
            Template st = innerGroup.GetInstanceOf("bob");

            TemplateGroup outerGroup = new TemplateGroup();
            outerGroup.Listener = errors;
            outerGroup.DefineTemplate("errorMessage", "<x>", new string[] { "x" });
            outerGroup.DefineTemplate("bob", "outer"); // should not be visible to test() in innerGroup
            Template outerST = outerGroup.GetInstanceOf("errorMessage");
            outerST.Add("x", st);

            string expected = "inner";
            string result = outerST.Render();

            Assert.AreEqual(errors.Errors.Count, 0); // ignores no such prop errors

            Assert.AreEqual(expected, result);
        }
 public void TestIndirectTemplateIncludeViaTemplate()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("foo", "bar");
     group.DefineTemplate("tname", "foo");
     string template = "<(tname())()>";
     group.DefineTemplate("test", template, new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     string expected = "bar";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #19
0
 public void TestSeparatorWithNullFirstValueAndNullOption()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <name; null=\"n/a\", separator=\", \">!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", null);
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected = "hi n/a, Tom, Sumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
 public void TestIndirectTemplateIncludeWithArgs()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("foo", "<x><y>", new string[] { "x", "y" });
     string template = "<(name)({1},{2})>";
     group.DefineTemplate("test", template, new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "foo");
     string expected = "12";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #21
0
 public void TestParallelMapThenMap()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("bold", "[<x>]", new string[] { "x" });
     group.DefineTemplate("test", "hi <names,phones:{n,p | <n>:<p>;}:bold()>", new string[] { "names", "phones" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("phones", "x5001");
     st.Add("phones", "x5002");
     string expected =
         "hi [Ter:x5001;][Tom:x5002;][Sumana:;]";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #22
0
 public void TestPropConvertsToString()
 {
     ErrorBufferAllErrors errors = new ErrorBufferAllErrors();
     TemplateGroup group = new TemplateGroup();
     group.Listener = errors;
     string template = "<u.(name)>";
     Template st = new Template(group, template);
     st.Add("u", new User(1, "parrt"));
     st.Add("name", 100);
     string expected = "";
     string result = st.Render();
     Assert.AreEqual(expected, result);
     TemplateRuntimeMessage msg = (TemplateRuntimeMessage)errors.Errors[0];
     TemplateNoSuchPropertyException e = (TemplateNoSuchPropertyException)msg.Cause;
     Assert.AreEqual("Antlr4.Test.StringTemplate.BaseTest+User.100", e.PropertyName);
 }
Example #23
0
 public void TestParallelMapWith3Versus2Elements()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <names,phones:{n,p | <n>:<p>;}>", new string[] { "names", "phones" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("phones", "x5001");
     st.Add("phones", "x5002");
     string expected =
         "hi Ter:x5001;Tom:x5002;Sumana:;";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #24
0
 public void TestWeirdChar2()
 {
     string template = "\n<\\\n";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:2: invalid escaped char: '<EOF>'" + newline +
                       "test 1:2: expecting '>', found '<EOF>'" + newline;
     Assert.AreEqual(expected, result);
 }
Example #25
0
 public void TestRoundRobinMap()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("a", "[<x>]", new string[] { "x" });
     group.DefineTemplate("b", "(<x>)", new string[] { "x" });
     group.DefineTemplate("test", "hi <name:a(),b()>!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected =
         "hi [Ter](Tom)[Sumana]!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #26
0
 public void TestWeirdChar()
 {
     string template = "   <*>";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:4: invalid character '*'" + newline +
                       "test 1:0: this doesn't look like a template: \"   <*>\"" + newline;
     Assert.AreEqual(expected, result);
 }
Example #27
0
 public void TestUnterminatedExpr()
 {
     string template = "hi <t()$";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:7: invalid character '$'" + newline +
         "test 1:7: invalid character '<EOF>'" + newline +
         "test 1:7: premature EOF" + newline;
     Assert.AreEqual(expected, result);
 }
Example #28
0
 public void TestNullValueAndNullOption()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<name; null=\"n/a\">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", null);
     string expected = "n/a";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #29
0
 public void TestOptionDoesntApplyToNestedTemplate()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("foo", "<zippo>");
     group.DefineTemplate("test", "<foo(); null=\"n/a\">", new string[] { "zippo" });
     Template st = group.GetInstanceOf("test");
     st.Add("zippo", null);
     string expected = "";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #30
0
 public void TestSeparatorWithSpaces()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <name; separator= \", \">!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     Console.WriteLine(st.impl.Ast.ToStringTree());
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected = "hi Ter, Tom, Sumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }