Example #1
0
        protected void ToDOTDefineNodes(object tree, ITreeAdaptor adaptor, StringTemplate treeST)
        {
            if (tree == null)
            {
                return;
            }
            int n = adaptor.GetChildCount(tree);

            if (n == 0)
            {
                // must have already dumped as child from previous
                // invocation; do nothing
                return;
            }

            // define parent node
            StringTemplate parentNodeST = GetNodeST(adaptor, tree);

            treeST.SetAttribute("nodes", parentNodeST);

            // for each child, do a "<unique-name> [label=text]" node def
            for (int i = 0; i < n; i++)
            {
                object         child  = adaptor.GetChild(tree, i);
                StringTemplate nodeST = GetNodeST(adaptor, child);
                treeST.SetAttribute("nodes", nodeST);
                ToDOTDefineNodes(child, adaptor, treeST);
            }
        }
Example #2
0
        protected void ToDOTDefineEdges(object tree, ITreeAdaptor adaptor, StringTemplate treeST)
        {
            if (tree == null)
            {
                return;
            }
            int n = adaptor.GetChildCount(tree);

            if (n == 0)
            {
                // must have already dumped as child from previous
                // invocation; do nothing
                return;
            }

            string parentName = "n" + GetNodeNumber(tree);

            // for each child, do a parent -> child edge using unique node names
            string parentText = adaptor.GetNodeText(tree);

            for (int i = 0; i < n; i++)
            {
                object         child     = adaptor.GetChild(tree, i);
                string         childText = adaptor.GetNodeText(child);
                string         childName = "n" + GetNodeNumber(child);
                StringTemplate edgeST    = _edgeST.GetInstanceOf();
                edgeST.SetAttribute("parent", parentName);
                edgeST.SetAttribute("child", childName);
                edgeST.SetAttribute("parentText", parentText);
                edgeST.SetAttribute("childText", childText);
                treeST.SetAttribute("edges", edgeST);
                ToDOTDefineEdges(child, adaptor, treeST);
            }
        }
Example #3
0
        protected StringTemplate GetNodeST(ITreeAdaptor adaptor, object t)
        {
            string         text       = adaptor.GetNodeText(t);
            StringTemplate nodeST     = _nodeST.GetInstanceOf();
            string         uniqueName = "n" + GetNodeNumber(t);

            nodeST.SetAttribute("name", uniqueName);
            if (text != null)
            {
                text = text.Replace("\"", "\\\\\"");
            }
            nodeST.SetAttribute("text", text);
            return(nodeST);
        }
        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);
        }
Example #5
0
 private static void LoadResources(Controller controller, StringTemplate st)
 {
     if (controller.Resources != null)
     {
         foreach (string key in controller.Resources.Keys)
         {
             st.SetAttribute(key, controller.Resources[key]);
         }
     }
 }
 private string FormatText(string text, ParameterCollection parameterCollection)
 {
     Antlr.StringTemplate.StringTemplate template = new Antlr.StringTemplate.StringTemplate(text);
     IDictionaryEnumerator enumerator = parameterCollection.GetEnumerator();
     while (enumerator.MoveNext())
     {
         string key = enumerator.Key as string;
         object obj2 = enumerator.Value;
         template.SetAttribute(key, obj2);
     }
     return template.ToString();
 }
Example #7
0
 private static void LoadFlash(IRailsEngineContext context, StringTemplate st)
 {
     foreach (DictionaryEntry entry in context.Flash)
     {
         if (entry.Value == null)
         {
             continue;
         }
         //innerContext[entry.Key] = entry.Value;
         st.SetAttribute((string)entry.Key, entry.Value);
     }
 }
Example #8
0
 private static void LoadPropertyBag(Controller controller, StringTemplate st)
 {
     foreach (DictionaryEntry entry in controller.PropertyBag)
     {
         if (entry.Value == null)
         {
             continue;
         }
         //innerContext[entry.Key] = entry.Value;
         st.SetAttribute((string)entry.Key, entry.Value);
     }
 }
Example #9
0
 private static void LoadParams(IRailsEngineContext context, StringTemplate st)
 {
     foreach (string key in context.Params.AllKeys)
     {
         if (key == null)
         {
             continue;                   // Nasty bug?
         }
         object value = context.Params[key];
         if (value == null)
         {
             continue;
         }
         st.SetAttribute(key, value);
         //innerContext[key] = value;
     }
 }
Example #10
0
 public object this[object key]
 {
     get
     {
         if (key is string)
         {
             return(st.GetAttribute((string)key));
         }
         return(null);
     }
     set
     {
         if (key is string)
         {
             st.RemoveAttribute((string)key);
             st.SetAttribute((string)key, value);
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
 }
Example #11
0
        protected void ProcessLayout(IRailsEngineContext context, Controller controller, string contents)
        {
            try
            {
                StringTemplate template = GetLayoutTemplate(controller);
                SetContextAsTemplateAttributes(context, controller, ref template);
                template.SetAttribute(ConfigConstants.CHILD_CONTENT_ATTRIB_KEY, contents);

                context.Response.Output.Write(template.ToString());
            }
            catch (Exception ex)
            {
                if (context.Request.IsLocal)
                {
                    SendErrorDetails(ex, context.Response.Output, "n/a");
                    return;
                }
                else
                {
                    throw new RailsException("Could not obtain layout", ex);
                }
            }
        }
Example #12
0
        internal static void SetContextAsTemplateAttributes(IRailsEngineContext context, Controller controller, ref StringTemplate st)
        {
            st.SetAttribute(ConfigConstants.CONTROLLER_ATTRIB_KEY, controller);
            st.SetAttribute(ConfigConstants.CONTEXT_ATTRIB_KEY, context);
            st.SetAttribute(ConfigConstants.REQUEST_ATTRIB_KEY, context.Request);
            st.SetAttribute(ConfigConstants.RESPONSE_ATTRIB_KEY, context.Response);
            st.SetAttribute(ConfigConstants.SESSION_ATTRIB_KEY, context.Session);

            LoadResources(controller, st);

//			foreach(object helper in controller.Helpers.Values)
//			{
//				st.SetAttribute(helper.GetType().Name, helper);
//			}

            LoadParams(context, st);

            LoadFlash(context, st);

            LoadPropertyBag(controller, st);

            st.SetAttribute(ConfigConstants.SITEROOT_ATTRIB_KEY, context.ApplicationPath);
        }
 public void testLengthOpOfStrippedListWithNullsFrontAndBack()
 {
 StringTemplate e = new StringTemplate(
         "$length(strip(data))$"
     );
 e = e.GetInstanceOf();
 IList data = new ArrayList();
 data.Add(null);
 data.Add(null);
 data.Add(null);
 data.Add("Hi");
 data.Add(null);
 data.Add(null);
 data.Add(null);
 data.Add("mom");
 data.Add(null);
 data.Add(null);
 data.Add(null);
 e.SetAttribute("data", data);
 string expecting = "2"; // nulls are counted
 Assert.AreEqual(expecting, e.ToString());
 }
            public virtual void testNestedIF()
            {
            StringTemplate e = new StringTemplate("$if(title)$" + NL
                + "foo" + NL
                + "$else$" + NL
                + "$if(header)$" + NL
                + "bar" + NL
                + "$else$" + NL
                + "blort" + NL
                + "$endif$" + NL
                + "$endif$");
            e.SetAttribute("title", "sample");
            string expecting = "foo";
            Assert.AreEqual(expecting, e.ToString());

            e = e.GetInstanceOf();
            e.SetAttribute("header", "more");
            expecting = "bar";
            Assert.AreEqual(expecting, e.ToString());

            e = e.GetInstanceOf();
            expecting = "blort";
            Assert.AreEqual(expecting, e.ToString());
            }
Example #15
0
        protected void ToDOTDefineEdges(object tree, ITreeAdaptor adaptor, StringTemplate treeST)
        {
            if ( tree == null )
            {
                return;
            }
            int n = adaptor.GetChildCount(tree);
            if ( n == 0 )
            {
                // must have already dumped as child from previous
                // invocation; do nothing
                return;
            }

            string parentName = "n" + GetNodeNumber(tree);

            // for each child, do a parent -> child edge using unique node names
            string parentText = adaptor.GetNodeText(tree);
            for (int i = 0; i < n; i++)
            {
                object child = adaptor.GetChild(tree, i);
                string childText = adaptor.GetNodeText(child);
                string childName = "n" + GetNodeNumber(child);
                StringTemplate edgeST = _edgeST.GetInstanceOf();
                edgeST.SetAttribute("parent", parentName);
                edgeST.SetAttribute("child", childName);
                edgeST.SetAttribute("parentText", parentText);
                edgeST.SetAttribute("childText", childText);
                treeST.SetAttribute("edges", edgeST);
                ToDOTDefineEdges(child, adaptor, treeST);
            }
        }
 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 virtual void testLiteralStringEscapesOutsideExpressions()
 {
 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(expecting, result);
 }
 public virtual void testIFCondWithParensDollarDelimsTemplate()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     StringTemplate t = new StringTemplate(group, "$if(map.(type))$$type$ $prop$=$map.(type)$;$endif$");
     Hashtable map = new Hashtable();
     map["int"] = "0";
     t.SetAttribute("map", map);
     t.SetAttribute("prop", "x");
     t.SetAttribute("type", "int");
     Assert.AreEqual("int x=0;", t.ToString());
 }
 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 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 void TestAttributeNameCannotHaveDots()
 {
 StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
 StringTemplate t = new StringTemplate(group, "$user.Name$");
 t.SetAttribute("user.Name", "Kunle");
 }
 public void testMapKeys()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = new StringTemplate(group, "<aMap.keys:{k|<k>:<aMap.(k)>}; separator=\", \">");
 HybridDictionary map = new HybridDictionary();
 map.Add("int", "0");
 map.Add("float", "0.0");
 t.SetAttribute("aMap", map);
 Assert.AreEqual("int:0, float:0.0", t.ToString());
 }
 public void testMapValues()
 {
 StringTemplateGroup group = new StringTemplateGroup("test", typeof(AngleBracketTemplateLexer));
 StringTemplate t = new StringTemplate(group, "<aMap.values; separator=\", \">");
 Hashtable map = new Hashtable();
 map["int"] = "0";
 map.Add("float", "0.0");
 t.SetAttribute("aMap", map);
 Assert.AreEqual("0.0, 0", t.ToString());
 }
 public virtual void testAttributeRefButtedUpAgainstEndifAndWhitespace()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate a = new StringTemplate(group, "$if (!firstName)$$email$$endif$");
     a.SetAttribute("email", "*****@*****.**");
     string expecting = "*****@*****.**";
     Assert.AreEqual(expecting, a.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 testLengthOpWithHybridDictionary()
 {
 StringTemplate e = new StringTemplate("$length(names)$");
 e = e.GetInstanceOf();
 IDictionary m = new HybridDictionary();
 m.Add("Tom", "Tom");
 m.Add("Sriram", "Sriram");
 m.Add("Doug", "Doug");
 e.SetAttribute("names", m);
 string expecting = "3";
 Assert.AreEqual(expecting, e.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 void testLengthOpNull()
 {
 StringTemplate e = new StringTemplate(
         "$length(names)$"
     );
 e = e.GetInstanceOf();
 e.SetAttribute("names", null);
 string expecting = "0";
 Assert.AreEqual(expecting, e.ToString());
 }
        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", (object)true);
            Assert.AreEqual(t.ToString(), "x ");

            t = t.GetInstanceOf();
            t.SetAttribute("b", (object)false);
            Assert.AreEqual(t.ToString(), " y");
        }
 public void testLengthOpSingleValue()
 {
 StringTemplate e = new StringTemplate(
         "$length(names)$"
     );
 e = e.GetInstanceOf();
 e.SetAttribute("names", "Ter");
 string expecting = "1";
 Assert.AreEqual(expecting, e.ToString());
 }
 public virtual void testIFCondWithParensTemplate()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".", typeof(AngleBracketTemplateLexer));
     StringTemplate t = new StringTemplate(group, "<if(map.(type))><type> <prop>=<map.(type)>;<endif>");
     Hashtable map = new Hashtable();
     map["int"] = "0";
     t.SetAttribute("map", map);
     t.SetAttribute("prop", "x");
     t.SetAttribute("type", "int");
     Assert.AreEqual("int x=0;", t.ToString());
 }
 public void testLengthOpPrimitive()
 {
 StringTemplate e = new StringTemplate(
         "$length(ints)$"
     );
 e = e.GetInstanceOf();
 e.SetAttribute("ints", new int[] { 1, 2, 3, 4 });
 string expecting = "4";
 Assert.AreEqual(expecting, e.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 testStripOpOfListWithNulls()
 {
 StringTemplate e = new StringTemplate("$strip(data)$");
 e = e.GetInstanceOf();
 IList data = new ArrayList();
 data.Add("Hi");
 data.Add(null);
 data.Add("mom");
 data.Add(null);
 e.SetAttribute("data", data);
 string expecting = "Himom"; // nulls are skipped
 Assert.AreEqual(expecting, e.ToString());
 }
            public virtual void testElseClause()
            {
            StringTemplate e = new StringTemplate("$if(title)$" + NL
                + "foo" + NL
                + "$else$" + NL
                + "bar" + NL
                + "$endif$"
                );
            e.SetAttribute("title", "sample");
            string expecting = "foo";
            Assert.AreEqual(expecting, e.ToString());

            e = e.GetInstanceOf();
            expecting = "bar";
            Assert.AreEqual(expecting, e.ToString());
            }
            public virtual void testStripOpOfListOfListsWithNulls()
            {
            StringTemplate e = new StringTemplate("$strip(data):{list | $strip(list)$}; separator=\",\"$");
            e = e.GetInstanceOf();

            IList dataOne = new ArrayList();
            dataOne.Add("Hi");
            dataOne.Add("mom");

            IList dataTwo = new ArrayList();
            dataTwo.Add("Hi");
            dataTwo.Add(null);
            dataTwo.Add("dad");
            dataTwo.Add(null);

            IList data = new ArrayList();
            data.Add(dataOne);
            data.Add(null);
            data.Add(dataTwo);

            e.SetAttribute("data", data);
            string expecting = "Himom,Hidad"; // nulls are skipped
            Assert.AreEqual(expecting, e.ToString());
            }
            public virtual void testEmbeddedMultiLineIF()
            {
            StringTemplateGroup group = new StringTemplateGroup("test");
            StringTemplate main = new StringTemplate(group, "$sub$");
            StringTemplate sub = new StringTemplate(
                group, ""
                + "begin" + NL
                + "$if(foo)$" + NL
                + "$foo$" + NL
                + "$else$" + NL
                + "blort" + NL
                + "$endif$" + NL
                );
            sub.SetAttribute("foo", "stuff");
            main.SetAttribute("sub", sub);
            string expecting = "begin" + NL
                + "stuff";
            Assert.AreEqual(expecting, main.ToString());

            main = new StringTemplate(group, "$sub$");
            sub = sub.GetInstanceOf();
            main.SetAttribute("sub", sub);
            expecting = "begin" + NL
                + "blort";
            Assert.AreEqual(expecting, main.ToString());
            }
 public void testStripOpOfSingleAlt()
 {
 StringTemplate e = new StringTemplate(
         "$strip(data)$"
     );
 e = e.GetInstanceOf();
 e.SetAttribute("data", "hi");
 string expecting = "hi"; // nulls are skipped
 Assert.AreEqual(expecting, e.ToString());
 }
Example #39
0
        protected void ToDOTDefineNodes(object tree, ITreeAdaptor adaptor, StringTemplate treeST)
        {
            if ( tree == null )
            {
                return;
            }
            int n = adaptor.GetChildCount(tree);
            if ( n == 0 )
            {
                // must have already dumped as child from previous
                // invocation; do nothing
                return;
            }

            // define parent node
            StringTemplate parentNodeST = GetNodeST(adaptor, tree);
            treeST.SetAttribute("nodes", parentNodeST);

            // for each child, do a "<unique-name> [label=text]" node def
            for (int i = 0; i < n; i++)
            {
                object child = adaptor.GetChild(tree, i);
                StringTemplate nodeST = GetNodeST(adaptor, child);
                treeST.SetAttribute("nodes", nodeST);
                ToDOTDefineNodes(child, adaptor, treeST);
            }
        }
 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());
 }