public static void Main(string[] args)
        {
            StringTemplateGroup group = new StringTemplateGroup("dummy");
            StringTemplate bold = group.DefineTemplate("bold", "<b>$attr$</b>");
            StringTemplate banner = group.DefineTemplate("banner", "the banner");
            StringTemplate st = new StringTemplate(group,
                "<html>\n" +
                "$banner(a=b)$" +
                "<p><b>$name$:$email$</b>" +
                "$if(member)$<i>$fontTag$member</font></i>$endif$");
            st.SetAttribute("name", "Terence");
            st.SetAttribute("name", "Tom");
            st.SetAttribute("email", "*****@*****.**");
            st.SetAttribute("templateAttr", bold);

            StringTemplateTreeView frame =
                new StringTemplateTreeView("StringTemplateTreeView Example", st);
            Application.Run(frame);
        }
 public void testListOfIntArrays()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = group.DefineTemplate("t", "<data:array()>");
 group.DefineTemplate("array", "[<it:element(); separator=\",\">]");
 group.DefineTemplate("element", "<it>");
 IList data = new ArrayList();
 data.Add(new int[] { 1, 2, 3 });
 data.Add(new int[] { 10, 20, 30 });
 t.SetAttribute("data", data);
 string expecting = "[1,2,3][10,20,30]";
 Assert.AreEqual(expecting, t.ToString());
 }
 public virtual void testAlternativeWriter()
 {
 StringBuilder buf = new StringBuilder();
 IStringTemplateWriter w = new AlternativeWriter(buf);
 StringTemplateGroup group = new StringTemplateGroup("test");
 group.DefineTemplate("bold", "<b>$x$</b>");
 StringTemplate name = new StringTemplate(group, "$name:bold(x=name)$");
 name.SetAttribute("name", "Terence");
 name.Write(w);
 Assert.AreEqual(buf.ToString(), "<b>Terence</b>");
 }
 public virtual void testExprInParens()
 {
     // specify a template to apply to an attribute
     // Use a template group so we can specify the start/stop chars
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     StringTemplate bold = group.DefineTemplate("bold", "<b>$it$</b>");
     StringTemplate duh = new StringTemplate(group, @"$(""blort: ""+(list)):bold()$");
     duh.SetAttribute("list", "a");
     duh.SetAttribute("list", "b");
     duh.SetAttribute("list", "c");
     // System.out.println(duh);
     string expecting = "<b>blort: abc</b>";
     Assert.AreEqual(expecting, duh.ToString());
 }
 public virtual void testComplicatedSeparatorExpr()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate bold = group.DefineTemplate("bulletSeparator", "</li>$foo$<li>");
     // make separator a complicated expression with args passed to included template
     StringTemplate t = new StringTemplate(
         group, @"<ul>$name; separator=bulletSeparator(foo="" "")+""&nbsp;""$</ul>"
         );
     t.SetAttribute("name", "Ter");
     t.SetAttribute("name", "Tom");
     t.SetAttribute("name", "Mel");
     //System.out.println(t);
     string expecting = "<ul>Ter</li> <li>&nbsp;Tom</li> <li>&nbsp;Mel</ul>";
     Assert.AreEqual(expecting, t.ToString());
 }
 public virtual void testChangingAttrValueTemplateApplicationToVector()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate bold = group.DefineTemplate("bold", "<b>$x$</b>");
     StringTemplate t = new StringTemplate(group, "$names:bold(x=it)$");
     t.SetAttribute("names", "Terence");
     t.SetAttribute("names", "Tom");
     string expecting = "<b>Terence</b><b>Tom</b>";
     Assert.AreEqual(expecting, t.ToString());
 }
 public virtual void testChangingAttrValueRepeatedTemplateApplicationToVector()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     StringTemplate bold = group.DefineTemplate("bold", "<b>$item$</b>");
     StringTemplate italics = group.DefineTemplate("italics", "<i>$it$</i>");
     StringTemplate members = new StringTemplate(group, "$members:bold(item=it):italics(it=it)$");
     members.SetAttribute("members", "Jim");
     members.SetAttribute("members", "Mike");
     members.SetAttribute("members", "Ashar");
     string expecting = "<i><b>Jim</b></i><i><b>Mike</b></i><i><b>Ashar</b></i>";
     Assert.AreEqual(expecting, members.ToString());
 }
 public void testNullSingleValueInListWithTemplateApply()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = group.DefineTemplate("t", "<data:array(); null=\"-1\", separator=\", \">");
 group.DefineTemplate("array", "<it>");
 IList data = new ArrayList();
 data.Add(null);
 t.SetAttribute("data", data);
 string expecting = "-1";
 Assert.AreEqual(expecting, t.ToString());
 }
 public virtual void testApplyRepeatedAnonymousTemplateWithForeignTemplateRefToMultiValuedAttribute()
 {
     // specify a template to apply to an attribute
     // Use a template group so we can specify the start/stop chars
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     group.DefineTemplate("link", @"<a href=""$url$""><b>$title$</b></a>");
     StringTemplate duh = new StringTemplate(
         group, @"start|$p:{$link(url=""/member/view?ID=""+it.ID, title=it.firstName)$ $if(it.canEdit)$canEdit$endif$}:"
         + "{$it$<br>\n}$|end"
         );
     duh.SetAttribute("p", new Connector());
     duh.SetAttribute("p", new Connector2());
     string expecting = @"start|<a href=""/member/view?ID=1""><b>Terence</b></a> <br>" + NL
         + @"<a href=""/member/view?ID=2""><b>Tom</b></a> canEdit<br>" + NL + "|end";
     Assert.AreEqual(expecting, duh.ToString());
 }
 public virtual void testEmptyExprAsFirstLineGetsNoOutput()
 {
 StringTemplateGroup group =
     new StringTemplateGroup("test");
 IStringTemplateErrorListener errors = new ErrorBuffer();
 group.ErrorListener = errors;
 group.DefineTemplate("bold", "<b>$it$</b>");
 StringTemplate t = new StringTemplate(group,
     "$users$\n" +
     "end\n");
 string expecting = "end\n";
 string result = t.ToString();
 Assert.AreEqual(expecting, result);
 }
 public virtual void testAlternatingTemplateApplication()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     StringTemplate listItem = group.DefineTemplate("listItem", "<li>$it$</li>");
     StringTemplate bold = group.DefineTemplate("bold", "<b>$it$</b>");
     StringTemplate italics = group.DefineTemplate("italics", "<i>$it$</i>");
     StringTemplate item = new StringTemplate(group, "$item:bold(),italics():listItem()$");
     item.SetAttribute("item", "Jim");
     item.SetAttribute("item", "Mike");
     item.SetAttribute("item", "Ashar");
     string expecting = "<li><b>Jim</b></li><li><i>Mike</i></li><li><b>Ashar</b></li>";
     Assert.AreEqual(expecting, item.ToString());
 }
 public virtual void testTemplatePolymorphism()
 {
 StringTemplateGroup group = new StringTemplateGroup("super");
 StringTemplateGroup subGroup = new StringTemplateGroup("sub");
 subGroup.SuperGroup = group;
 // bold is defined in both super and sub
 // if you create an instance of page via the subgroup,
 // then bold() should evaluate to the subgroup not the super
 // even though page is defined in the super.  Just like polymorphism.
 group.DefineTemplate("bold", "<b>$it$</b>");
 group.DefineTemplate("page", "$name:bold()$");
 subGroup.DefineTemplate("bold", "<strong>$it$</strong>");
 StringTemplate st = subGroup.GetInstanceOf("page");
 st.SetAttribute("name", "Ter");
 string expecting = "<strong>Ter</strong>";
 Assert.AreEqual(expecting, st.ToString());
 }
 public virtual void testLazyEvalOfSuperInApplySuperTemplateRef()
 {
 StringTemplateGroup group = new StringTemplateGroup("base");
 StringTemplateGroup subGroup = new StringTemplateGroup("sub");
 subGroup.SuperGroup = group;
 group.DefineTemplate("bold", "<b>$it$</b>");
 subGroup.DefineTemplate("bold", "<strong>$it$</strong>");
 // this is the same as testApplySuperTemplateRef() test
 // 'cept notice that here the supergroup defines page
 // As long as you create the instance via the subgroup,
 // "super." will evaluate lazily (i.e., not statically
 // during template compilation) to the templates
 // getGroup().superGroup value.  If I create instance
 // of page in group not subGroup, however, I will get
 // an error as superGroup is null for group "group".
 group.DefineTemplate("page", "$name:super.bold()$");
 StringTemplate st = subGroup.GetInstanceOf("page");
 st.SetAttribute("name", "Ter");
 string error = null;
 try
 {
     st.ToString();
 }
 catch (StringTemplateException iae)
 {
     error = iae.Message;
 }
 string expectingError = "base has no super group; invalid template: super.bold";
 Assert.AreEqual(expectingError, error);
 }
 public virtual void testApplySuperTemplateRef()
 {
 StringTemplateGroup group = new StringTemplateGroup("super");
 StringTemplateGroup subGroup = new StringTemplateGroup("sub");
 subGroup.SuperGroup = group;
 group.DefineTemplate("bold", "<b>$it$</b>");
 subGroup.DefineTemplate("bold", "<strong>$it$</strong>");
 subGroup.DefineTemplate("page", "$name:super.bold()$");
 StringTemplate st = subGroup.GetInstanceOf("page");
 st.SetAttribute("name", "Ter");
 string expecting = "<b>Ter</b>";
 Assert.AreEqual(expecting, st.ToString());
 }
 public virtual void testSuperTemplateRef()
 {
 // you can refer to a template defined in a super group via super.t()
 StringTemplateGroup group = new StringTemplateGroup("super");
 StringTemplateGroup subGroup = new StringTemplateGroup("sub");
 subGroup.SuperGroup = group;
 group.DefineTemplate("page", "$font()$:text");
 group.DefineTemplate("font", "Helvetica");
 subGroup.DefineTemplate("font", "$super.font()$ and Times");
 StringTemplate st = subGroup.GetInstanceOf("page");
 string expecting = "Helvetica and Times:text";
 Assert.AreEqual(expecting, st.ToString());
 }
 public void testNullOptionSingleNullValue()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = group.DefineTemplate("t", "<data; null=\"0\">");
 string expecting = "0";
 Assert.AreEqual(expecting, t.ToString());
 }
 public void testNullValueInListNoNullOption()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = group.DefineTemplate("t", "<data; separator=\", \">");
 IList data = new ArrayList();
 data.Add(null);
 data.Add(1);
 data.Add(null);
 data.Add(3);
 data.Add(4);
 data.Add(null);
 t.SetAttribute("data", data);
 string expecting = "1, 3, 4";
 Assert.AreEqual(expecting, t.ToString());
 }
        public virtual void testApplyTemplateNameExpression()
        {
            StringTemplateGroup group = new StringTemplateGroup("test");
            StringTemplate bold = group.DefineTemplate("foobar", "foo$attr$bar");
            StringTemplate t = new StringTemplate(group, @"$data:(name+""bar"")()$");
            t.SetAttribute("data", "Ter");
            t.SetAttribute("data", "Tom");
            t.SetAttribute("name", "foo");

            string expecting = "fooTerbarfooTombar";
            Assert.AreEqual(expecting, t.ToString());
        }
 public void testNullSingleValueWithTemplateApply()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = group.DefineTemplate("t", "<data:array(); null=\"-1\", separator=\", \">");
 group.DefineTemplate("array", "<it>");
 string expecting = "-1";
 Assert.AreEqual(expecting, t.ToString());
 }
 public void testArgumentContext()
 {
 // t is referenced within foo and so will be evaluated in that
 // context.  it can therefore see name.
 StringTemplateGroup group = new StringTemplateGroup("test");
 StringTemplate main = group.DefineTemplate("main", "$foo(t={Hi, $name$}, name=\"parrt\")$");
 StringTemplate foo = group.DefineTemplate("foo", "$t$");
 string expecting = "Hi, parrt";
 Assert.AreEqual(expecting, main.ToString());
 }
 public void TestTemplateNameCannotHaveDots2()
 {
 StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
 StringTemplate t = group.DefineTemplate("user.name", "$person.email$");
 }
 public void testApplyTemplateNameTemplateEval()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate foobar = group.DefineTemplate("foobar", "foo$it$bar");
     StringTemplate a = group.DefineTemplate("a", "$it$bar");
     StringTemplate t = new StringTemplate(group, "$data:(\"foo\":a())()$");
     t.SetAttribute("data", "Ter");
     t.SetAttribute("data", "Tom");
     string expecting = "fooTerbarfooTombar";
     Assert.AreEqual(expecting, t.ToString());
 }
        public virtual void testCollectionAttributes()
        {
            StringTemplateGroup group = new StringTemplateGroup("test");
            StringTemplate bold = group.DefineTemplate("bold", "<b>$it$</b>");
            StringTemplate t = new StringTemplate(
                group, "$data$, $data:bold()$, " + "$list:bold():bold()$, $array$, $a2$, $a3$, $a4$"
                );
            ArrayList v = new ArrayList(10);
            v.Add("1");
            v.Add("2");
            v.Add("3");
            IList list = new ArrayList();
            list.Add("a");
            list.Add("b");
            list.Add("c");
            t.SetAttribute("data", v);
            t.SetAttribute("list", list);
            t.SetAttribute("array", ((object)new string[] { "x", "y" }));
            t.SetAttribute("a2", ((object)new int[] { 10, 20 }));
            t.SetAttribute("a3", ((object)new float[] { 1.2f, 1.3f }));
            t.SetAttribute("a4", ((object)new double[] { 8.7, 9.2 }));

            string expecting = "123, <b>1</b><b>2</b><b>3</b>, "
                + "<b><b>a</b></b><b><b>b</b></b><b><b>c</b></b>, xy, 1020, 1.21.3, 8.79.2";
            Assert.AreEqual(expecting, t.ToString());
        }
 public virtual void testApplyTemplateToSingleValuedAttributeWithDefaultAttribute()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate bold = group.DefineTemplate("bold", "<b>$it$</b>");
     StringTemplate name = new StringTemplate(group, "$name:bold()$");
     name.SetAttribute("name", "Terence");
     Assert.AreEqual(name.ToString(), "<b>Terence</b>");
 }
 public virtual void testExpressionAsRHSOfAssignment()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate hostname = group.DefineTemplate("hostname", "$machine$.jguru.com");
     StringTemplate bold = group.DefineTemplate("bold", "<b>$x$</b>");
     StringTemplate t = new StringTemplate(group, @"$bold(x=hostname(machine=""www""))$");
     string expecting = "<b>www.jguru.com</b>";
     Assert.AreEqual(expecting, t.ToString());
 }
 public void testEscapeEscape()
 {
 StringTemplateGroup group = new StringTemplateGroup("test");
 StringTemplate t = group.DefineTemplate("t", "\\\\$v$");
 t.SetAttribute("v", "Joe");
 string expecting = "\\Joe";
 Assert.AreEqual(expecting, t.ToString());
 }
 public virtual void testLiteralStringEscapes()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     group.DefineTemplate("foo", "$x$ && $it$");
     StringTemplate t = new StringTemplate(group, @"$A:foo(x=""dog\""\"""")$");
     StringTemplate u = new StringTemplate(group, @"$A:foo(x=""dog\""g"")$");
     StringTemplate v = new StringTemplate(group, @"$A:{$it:foo(x=""\{dog\}\"""")$ is cool}$");
     t.SetAttribute("A", "ick");
     u.SetAttribute("A", "ick");
     v.SetAttribute("A", "ick");
     string expecting = @"dog"""" && ick";
     Assert.AreEqual(expecting, t.ToString());
     expecting = @"dog""g && ick";
     Assert.AreEqual(expecting, u.ToString());
     expecting = @"{dog}"" && ick is cool";
     Assert.AreEqual(expecting, v.ToString());
     }
 public void testEscapeEscapeNestedAngle()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = group.DefineTemplate("t", "<v:{a|\\\\<a>}>");
 t.SetAttribute("v", "Joe");
 string expecting = "\\Joe";
 Assert.AreEqual(expecting, t.ToString());
 }