public JTreeStringTemplateModel(StringTemplate st)
 {
     if (st == null)
     {
         throw new System.ArgumentException("root is null");
     }
     root = new StringTemplateWrapper(st);
 }
        public virtual void testEmbeddedCommentsAngleBracketed()
        {
            StringTemplate st = new StringTemplate("Foo <! ignore !>bar" + newline, typeof(AngleBracketTemplateLexer));
            String expecting = "Foo bar" + newline;
            String result = st.ToString();
            Assert.AreEqual(result, expecting);

            st = new StringTemplate("Foo <! ignore" + newline + " and a line break!>" + newline + "bar" + newline, typeof(AngleBracketTemplateLexer));
            expecting = "Foo " + newline + "bar" + newline;
            result = st.ToString();
            Assert.AreEqual(result, expecting);

            st = new StringTemplate("<! start of line $ and <! ick" + newline + "!>boo" + newline, typeof(AngleBracketTemplateLexer));
            expecting = "boo" + newline;
            result = st.ToString();
            Assert.AreEqual(result, expecting);

            st = new StringTemplate("<! start of line !>" + "<! another to ignore !>" + "<! ick" + newline + "!>boo" + newline, typeof(AngleBracketTemplateLexer));
            expecting = "boo" + newline;
            result = st.ToString();
            //System.out.println(result);
            Assert.AreEqual(result, expecting);

            st = new StringTemplate("<! back !><! to back !>" + newline + "<! ick" + newline + "!>boo" + newline, typeof(AngleBracketTemplateLexer));
            expecting = newline + "boo" + newline;
            result = st.ToString();
            Assert.AreEqual(result, expecting);
        }
 /// <summary>To write out the value of a condition expr, invoke the evaluator in eval.g
 /// to walk the condition tree computing the boolean value.  If result
 /// is true, then write subtemplate.
 /// </summary>
 public override int write(StringTemplate self, StringTemplateWriter outWriter)
 {
     if (exprTree == null || self == null || outWriter == null)
     {
         return 0;
     }
     // System.out.println("evaluating conditional tree: "+exprTree.toStringList());
     ActionEvaluator eval = new ActionEvaluator(self, this, outWriter);
     int n = 0;
     try
     {
         // get conditional from tree and compute result
         AST cond = exprTree.getFirstChild();
         bool includeSubtemplate = eval.ifCondition(cond); // eval and write out tree
         // System.out.println("subtemplate "+subtemplate);
         if (includeSubtemplate)
         {
             /* To evaluate the IF chunk, make a new instance whose enclosingInstance
             * points at 'self' so get attribute works.  Otherwise, enclosingInstance
             * points at the template used to make the precompiled code.  We need a
             * new template instance every time we exec this chunk to get the new
             * "enclosing instance" pointer.
             */
             StringTemplate s = subtemplate.getInstanceOf();
             s.setEnclosingInstance(self);
             n = s.write(outWriter);
         }
         else if (elseSubtemplate != null)
         {
             // evaluate ELSE clause if present and IF condition failed
             StringTemplate s = elseSubtemplate.getInstanceOf();
             s.setEnclosingInstance(self);
             n = s.write(outWriter);
         }
     }
     catch (RecognitionException re)
     {
         self.error("can't evaluate tree: " + exprTree.ToStringList(), re);
     }
     return n;
 }
 public virtual void setElseSubtemplate(StringTemplate elseSubtemplate)
 {
     this.elseSubtemplate = elseSubtemplate;
 }
        /// <summary>Walk all the attributes in this template, spitting them out. </summary>
        public virtual void walkAttributes(StringTemplate st)
        {
            if (st == null || st.getAttributes() == null)
            {
                return ;
            }

            ICollection keys = st.getAttributes().Keys;
            saveItemIndex();

            for (IEnumerator iterator = keys.GetEnumerator(); iterator.MoveNext(); )
            {
                String name = (String) iterator.Current;

                if (name.Equals(ASTExpr.REFLECTION_ATTRIBUTES))
                {
                    continue;
                }

                Object value = st.getAttributes()[name];
                incItemIndex();
                indent();
                output.Append("Attribute ");
                output.Append(name);
                output.Append(" values:");
                output.Append(Environment.NewLine);
                indentation++;
                walkAttributeValues(value);
                indentation--;
            }
            restoreItemIndex();
        }
 public virtual void walkStringTemplate(StringTemplate st)
 {
     // make sure the exact ST instance has not been visited
     if (typesVisited.Contains(st))
     {
         return ;
     }
     typesVisited.Add(st, null);
     indent();
     output.Append("Template ");
     output.Append(st.getName());
     output.Append(":");
     output.Append(Environment.NewLine);
     indentation++;
     walkAttributes(st);
     indentation--;
 }
示例#7
0
 public Expr(StringTemplate enclosingTemplate)
 {
     this.enclosingTemplate = enclosingTemplate;
 }
 public virtual void testAlternativeWriter()
 {
     System.Text.StringBuilder buf = new System.Text.StringBuilder();
     StringTemplateWriter w = new AnonymousClassStringTemplateWriter(buf, this);
     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(duh.ToString(), expecting);
 }
 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(t.ToString(), expecting);
 }
 public virtual void testEscapesOutsideExpressions()
 {
     StringTemplate b = new StringTemplate("It\\'s ok...\\$; $a:{\\'hi\\', $it$}$");
     b.setAttribute("a", "Ter");
     String expecting = "It\\'s ok...$; \\'hi\\', Ter";
     String result = b.ToString();
     Assert.AreEqual(result, expecting);
 }
 public virtual void testEscapes()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     String newline = System.Environment.NewLine;
     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");
     //System.out.println("t is '"+t.toString()+"'");
     //System.out.println("u is '"+u.toString()+"'");
     //System.out.println("v is '"+v.toString()+"'");
     String expecting = "dog\"\" && ick";
     Assert.AreEqual(t.ToString(), expecting);
     expecting = "dog\"g && ick";
     Assert.AreEqual(u.ToString(), expecting);
     expecting = "{dog}\" && ick is cool";
     Assert.AreEqual(v.ToString(), expecting);
 }
 public virtual void testEmptyIteratedValueGetsSeparator()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplateErrorListener errors = new ErrorBuffer();
     group.setErrorListener(errors);
     StringTemplate t = new StringTemplate(group, "$names; separator=\",\"$");
     t.setAttribute("names", "Terence");
     t.setAttribute("names", "");
     t.setAttribute("names", "");
     t.setAttribute("names", "Tom");
     t.setAttribute("names", "Frank");
     t.setAttribute("names", "");
     // empty values get separator still
     String expecting = "Terence,,,Tom,Frank,";
     String result = t.ToString();
     Assert.AreEqual(result, expecting);
 }
 public virtual void testEmptyIteratedConditionalWithElseValueGetsSeparator()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplateErrorListener errors = new ErrorBuffer();
     group.setErrorListener(errors);
     StringTemplate t = new StringTemplate(group, "$users:{$if(it.ok)$$it.name$$else$$endif$}; separator=\",\"$");
     t.setAttribute("users.{name,ok}", "Terence", true);
     t.setAttribute("users.{name,ok}", "Tom", false);
     t.setAttribute("users.{name,ok}", "Frank", true);
     t.setAttribute("users.{name,ok}", "Johnny", false);
     // empty conditional values get no separator
     String expecting = "Terence,,Frank,"; // haven't solved the last empty value problem yet
     String result = t.ToString();
     Assert.AreEqual(result, expecting);
 }
        public virtual void testEmbeddedMultiLineIF()
        {
            StringTemplateGroup group = new StringTemplateGroup("test");
            StringTemplate main = new StringTemplate(group, "$sub$");
            StringTemplate sub = new StringTemplate(group, "begin" + newline + "$if(foo)$" + newline + "$foo$" + newline + "$else$" + newline + "blort" + newline + "$endif$" + newline);
            sub.setAttribute("foo", "stuff");
            main.setAttribute("sub", sub);
            String expecting = "begin" + newline + "stuff";
            Assert.AreEqual(main.ToString(), expecting);

            main = new StringTemplate(group, "$sub$");
            sub = sub.getInstanceOf();
            main.setAttribute("sub", sub);
            expecting = "begin" + newline + "blort";
            Assert.AreEqual(main.ToString(), expecting);
        }
 public virtual void testMultiValuedAttributeWithAnonymousTemplateUsingIndexVariableI()
 {
     StringTemplateGroup tgroup = new StringTemplateGroup("dummy", ".");
     StringTemplate t = new StringTemplate(tgroup, " List:" + this.newline + "  " + this.newline + "foo" + this.newline + this.newline + "$names:{<br>$i$. $it$" + this.newline + "}$");
     t.setAttribute("names", "Terence");
     t.setAttribute("names", "Jim");
     t.setAttribute("names", "Sriram");
     String newline = System.Environment.NewLine;
     //System.out.println(t);
     String expecting = " List:" + newline + "  " + newline + "foo" + newline + newline + "<br>1. Terence" + newline + "<br>2. Jim" + newline + "<br>3. Sriram" + newline;
     Assert.AreEqual(t.ToString(), expecting);
 }
        public virtual void testMultiValuedAttributeWithSeparator()
        {
            StringTemplate query;

            // if column can be multi-valued, specify a separator
            StringTemplateGroup group = new StringTemplateGroup("dummy", ".", typeof(AngleBracketTemplateLexer));
            query = new StringTemplate(group, "SELECT <distinct> <column; separator=\", \"> FROM <table>;");
            query.setAttribute("column", "name");
            query.setAttribute("column", "email");
            query.setAttribute("table", "User");
            // uncomment next line to make "DISTINCT" appear in output
            // query.setAttribute("distince", "DISTINCT");
            // System.out.println(query);
            Assert.AreEqual(query.ToString(), "SELECT  name, email FROM User;");
        }
 public virtual void testHashMapPropertyFetch()
 {
     StringTemplate a = new StringTemplate("$stuff.prop$");
     Hashtable map = new Hashtable();
     a.setAttribute("stuff", map);
     map["prop"] = "Terence";
     String results = a.ToString();
     //System.out.println(results);
     String expecting = "Terence";
     Assert.AreEqual(results, expecting);
 }
 public virtual void testNestedAnonymousTemplates()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     String newline = System.Environment.NewLine;
     StringTemplate t = new StringTemplate(group, "$A:{" + newline + "<i>$it:{" + newline + "<b>$it$</b>" + newline + "}$</i>" + newline + "}$");
     t.setAttribute("A", "parrt");
     String expecting = newline + "<i>" + newline + "<b>parrt</b>" + newline + "</i>" + newline;
     Assert.AreEqual(t.ToString(), expecting);
 }
        public virtual void testIFBoolean()
        {
            StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
            StringTemplate t = new StringTemplate(group, "$if(b)$x$endif$ $if(!b)$y$endif$");
            t.setAttribute("b", true);
            Assert.AreEqual(t.ToString(), "x ");

            t = t.getInstanceOf();
            t.setAttribute("b", false);
            Assert.AreEqual(t.ToString(), " y");
        }
示例#21
0
 /// <summary>How to write this node to output; return how many char written </summary>
 public abstract int write(StringTemplate self, StringTemplateWriter outWriter);
 public virtual void testIFTemplate()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".", typeof(AngleBracketTemplateLexer));
     StringTemplate t = new StringTemplate(group, "SELECT <column> FROM PERSON " + "<if(cond)>WHERE ID=<id><endif>;");
     t.setAttribute("column", "name");
     t.setAttribute("cond", "true");
     t.setAttribute("id", "231");
     Assert.AreEqual(t.ToString(), "SELECT name FROM PERSON WHERE ID=231;");
 }
 public AttributeReflectionController(StringTemplate st)
 {
     this.st = st;
 }
 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");
     //System.out.println("ITEM="+item);
     String expecting = "<li><b>Jim</b></li><li><i>Mike</i></li><li><b>Ashar</b></li>";
     Assert.AreEqual(item.ToString(), expecting);
 }
 public ConditionalExpr(StringTemplate enclosingTemplate, AST tree)
     : base(enclosingTemplate, tree, null)
 {
 }
 public virtual void testMissingEndDelimiter()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplateErrorListener errors = new ErrorBuffer();
     group.setErrorListener(errors);
     StringTemplate t = new StringTemplate(group, "stuff $a then more junk etc...");
     String expectingError = "problem parsing template 'anonymous': line 1:31: expecting '$', found '<EOF>'";
     //System.out.println("error: '"+errors+"'");
     //System.out.println("expecting: '"+expectingError+"'");
     Assert.AreEqual(errors.ToString(), expectingError);
 }
 public virtual void setSubtemplate(StringTemplate subtemplate)
 {
     this.subtemplate = subtemplate;
 }
 public virtual void testMultiLevelInheritance()
 {
     // must loop up two levels to find bold()
     StringTemplateGroup rootgroup = new StringTemplateGroup("root");
     StringTemplateGroup level1 = new StringTemplateGroup("level1");
     StringTemplateGroup level2 = new StringTemplateGroup("level2");
     rootgroup.defineTemplate("bold", "<b>$it$</b>");
     level1.setSuperGroup(rootgroup);
     level2.setSuperGroup(level1);
     StringTemplateErrorListener errors = new ErrorBuffer();
     rootgroup.setErrorListener(errors);
     level1.setErrorListener(errors);
     level2.setErrorListener(errors);
     StringTemplate duh = new StringTemplate(level2, "$name:bold()$");
     duh.setAttribute("name", "Terence");
     String expecting = "<b>Terence</b>";
     Assert.AreEqual(duh.ToString(), expecting);
 }
 public virtual void setStringTemplate(StringTemplate st)
 {
     this.st = st;
 }
 public virtual void testMultipleAdditions()
 {
     // 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, "$link(url=\"/member/view?ID=\"+ID+\"&x=y\"+foo, title=\"the title\")$");
     duh.setAttribute("ID", "3321");
     duh.setAttribute("foo", "fubar");
     String expecting = "<a href=\"/member/view?ID=3321&x=yfubar\"><b>the title</b></a>";
     Assert.AreEqual(duh.ToString(), expecting);
 }