/// </summary> private System.ComponentModel.Container components = null; #region Constructors private StringTemplateTreeView() { // // Required for Windows Form Designer support
public void RenderBody(TextWriter writer) { object body = st.ArgumentContext[ConfigConstants.COMPONENT_BODY_KEY]; if (body == null) { throw new RailsException("This component does not have a body content to be rendered"); } if (body is StringTemplate) { StringTemplate instanceST = ((StringTemplate)body).GetInstanceOf(); instanceST.EnclosingInstance = ((StringTemplate)body).EnclosingInstance; body = instanceST; if (st.Attributes != null) { foreach (DictionaryEntry entry in st.Attributes) { ((StringTemplate)body).SetAttribute(entry.Key.ToString(), entry.Value); } } } writer.Write(body.ToString()); }
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); } }
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); } }
/// <summary> /// Just print out the string; no reference to self because this /// is a literal--not sensitive to attribute values. /// /// These strings never wrap because they are not part of a /// <![CDATA[<...> expression. <"foo"; wrap="\n"> ]]> should wrap though /// if necessary. /// </summary> public override int Write(StringTemplate self, IStringTemplateWriter output) { if (str != null) { int n = output.Write(str); return n; } return 0; }
public override StringTemplate LookupTemplate(StringTemplate enclosingInstance, string name) { if (name.StartsWith("super.")) { if (superGroup != null) { int dot = name.IndexOf('.'); name = name.Substring(dot + 1, (name.Length) - (dot + 1)); StringTemplate superScopeST = superGroup.LookupTemplate(enclosingInstance, name); return(superScopeST); } throw new StringTemplateException(Name + " has no super group; invalid template: " + name); } StringTemplate st = (StringTemplate)templates[name]; if (st != null) { if (st.NativeGroup.TemplateHasChanged(name)) { templates.Remove(name); st = null; } } if (st == null) { if (!templatesDefinedInGroupFile) { st = LoadTemplate(name); if (st != null) { templates[name] = st; } } if ((st == null) && (superGroup != null)) { st = superGroup.GetInstanceOf(enclosingInstance, name); if (st != null) { st.Group = this; if (!(st is ViewComponentStringTemplate)) { templates[name] = st; } } } if (st == null) { templates[name] = NOT_FOUND_ST; } } else if (st == NOT_FOUND_ST) { return(null); } return(st); }
/// <summary> /// Retrieves the ST layout template for the specified controller. /// </summary> protected StringTemplate GetLayoutTemplate(Controller controller) { string layoutName = controller.LayoutName; string templateName = string.Format("{0}/{1}", ConfigConstants.LAYOUTS_DIR, layoutName.Replace('\\', '/')); StringTemplateGroup templateManager = GetTemplateManager(controller); StringTemplate st = templateManager.GetInstanceOf(templateName); return(st); }
public override StringTemplate LookupTemplate(StringTemplate enclosingInstance, string name) { if (name.StartsWith("super.")) { if (superGroup != null) { int dot = name.IndexOf('.'); name = name.Substring(dot + 1, (name.Length) - (dot + 1)); StringTemplate superScopeST = superGroup.LookupTemplate(enclosingInstance,name); return superScopeST; } throw new StringTemplateException(Name + " has no super group; invalid template: " + name); } StringTemplate st = (StringTemplate) templates[name]; if (st != null) { if (st.NativeGroup.TemplateHasChanged(name)) { templates.Remove(name); st = null; } } if (st == null) { if (!templatesDefinedInGroupFile) { st = LoadTemplate(name); if (st != null) { templates[name] = st; } } if ((st == null) && (superGroup != null)) { st = superGroup.GetInstanceOf(enclosingInstance, name); if (st != null) { st.Group = this; if (!(st is ViewComponentStringTemplate)) { templates[name] = st; } } } if (st == null) { templates[name] = NOT_FOUND_ST; } } else if (st == NOT_FOUND_ST) { return null; } return st; }
/// <summary> /// Make the 'to' template look exactly like the 'from' template /// except for the attributes. /// </summary> override protected void dup(StringTemplate from, StringTemplate to) { base.dup(from, to); ViewComponentStringTemplate fromST = (ViewComponentStringTemplate)from; ViewComponentStringTemplate toST = (ViewComponentStringTemplate)to; toST.viewComponent = fromST.viewComponent; toST.viewComponentName = fromST.viewComponentName; toST.viewComponentContext = fromST.viewComponentContext; }
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(); }
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); } }
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); } }
/// <summary> /// Retrieves the ST template for specified controller and template name. /// </summary> protected StringTemplate GetViewTemplate(Controller controller, string viewName) { StringTemplate st = null; StringTemplateGroup templateManager = GetTemplateManager(controller); if (viewName.IndexOfAny(ConfigConstants.PATH_SEPARATOR_CHARS) == -1) { // try locations in order bool controllerHasArea = ((controller.AreaName != null) && (controller.AreaName.Trim().Length > 0)); string templateName; // <area>/<controller>/<view> if (controllerHasArea) { templateName = string.Format("{0}/{1}/{2}", controller.AreaName, controller.Name, viewName); st = templateManager.GetInstanceOf(templateName); } if (st == null) { // <area>/shared/<view> if (controllerHasArea) { templateName = string.Format("{0}/{1}/{2}", controller.AreaName, ConfigConstants.SHARED_DIR, viewName); st = templateManager.GetInstanceOf(templateName); } if (st == null) { // <controller>/<view> templateName = string.Format("{0}/{1}", controller.Name, viewName); st = templateManager.GetInstanceOf(templateName); if (st == null) { // shared/<view> templateName = string.Format("{0}/{1}", ConfigConstants.SHARED_DIR, viewName); st = templateManager.GetInstanceOf(templateName); } } } } if (st == null) { st = templateManager.GetInstanceOf(viewName.Replace('\\', '/')); } if (st == null) { throw new Castle.MonoRail.Framework.RailsException("Could not find the view: " + viewName); } return(st); }
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 override void Process(IRailsEngineContext context, Controller controller, string viewName) { TextWriter writer; StringBuilder sb = null; bool hasLayout = (controller.LayoutName != null); AdjustContentType(context); if (hasLayout) { //Because we are rendering within a layout we need to cache the output sb = new StringBuilder(); writer = new StringWriter(sb); } else { writer = context.Response.Output; } try { StringTemplate template = GetViewTemplate(controller, viewName); SetContextAsTemplateAttributes(context, controller, ref template); writer.Write(template.ToString()); } catch (Exception ex) { if (hasLayout) { // Restore original writer writer = context.Response.Output; } if (context.Request.IsLocal) { SendErrorDetails(ex, writer, viewName); return; } else { throw new RailsException("Could not obtain view", ex); } } if (hasLayout) { ProcessLayout(context, controller, sb.ToString()); } }
/// <summary> /// To write out the value of a condition expr, we invoke the evaluator /// in eval.g to walk the condition tree computing the boolean value. /// If result is true, then we write subtemplate. /// </summary> public override int Write(StringTemplate self, IStringTemplateWriter output) { if (exprTree == null || self == null || output == null) { return 0; } // System.out.println("evaluating conditional tree: "+exprTree.toStringList()); ActionEvaluator eval = new ActionEvaluator(self, this, output); ActionParser.initializeASTFactory(eval.getASTFactory()); 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.EnclosingInstance = self; // make sure we evaluate in context of enclosing template's // group so polymorphism works. :) s.Group = self.Group; s.NativeGroup = self.NativeGroup; n = s.Write(output); } else if (elseSubtemplate != null) { // evaluate ELSE clause if present and IF condition failed StringTemplate s = elseSubtemplate.GetInstanceOf(); s.EnclosingInstance = self; s.Group = self.Group; s.NativeGroup = self.NativeGroup; n = s.Write(output); } } catch (RecognitionException re) { self.Error("can't evaluate tree: " + exprTree.ToStringList(), re); } return n; }
public override StringTemplate GetInstanceOf(StringTemplate enclosingInstance, string name) { StringTemplate st = LookupTemplate(enclosingInstance, name); if (st != null) { StringTemplate instanceST = st.GetInstanceOf(); if (st is ViewComponentStringTemplate) { ((ViewComponentStringTemplate)st).ViewComponentName = null; ((ViewComponentStringTemplate)instanceST).CreateViewComponent(); } return(instanceST); } return(null); }
public StringTemplate ToDOT(object tree, ITreeAdaptor adaptor, StringTemplate _treeST, StringTemplate _edgeST) { StringTemplate treeST = _treeST.GetInstanceOf(); nodeNumber = 0; ToDOTDefineNodes(tree, adaptor, treeST); nodeNumber = 0; ToDOTDefineEdges(tree, adaptor, treeST); /* * if ( adaptor.GetChildCount(tree)==0 ) { * // single node, don't do edge. * treeST.SetAttribute("nodes", adaptor.GetNodeText(tree)); * } */ return(treeST); }
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; } }
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); }
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); } } }
override public int Write(IStringTemplateWriter output) { SetPredefinedAttributes(); SetDefaultArgumentValues(); //IRailsEngineContext context = (IRailsEngineContext) GetAttribute(ConfigConstants.CONTEXT_ATTRIB_KEY); IRailsEngineContext context = MonoRailHttpHandler.CurrentContext; StringWriter writer = new StringWriter(); StringTemplateViewContextAdapter viewComponentContext = new StringTemplateViewContextAdapter(viewComponentName, this); viewComponentContext.TextWriter = writer; viewComponent.Init(context, viewComponentContext); viewComponent.Render(); if (viewComponentContext.ViewToRender != null) { StringTemplate viewST = group.GetEmbeddedInstanceOf(this, viewComponentContext.ViewToRender); writer.Write(viewST.ToString()); } if (viewComponentName.Equals("CaptureFor")) { string keyToSet = (string)GetAttribute("id"); object valToSet = GetAttribute(keyToSet); OutermostEnclosingInstance.RemoveAttribute(keyToSet); OutermostEnclosingInstance.SetAttribute(keyToSet, valToSet); } output.Write(writer.ToString()); // if (LintMode) // { // CheckForTrouble(); // } return(0); }
public override StringTemplate LookupTemplate(StringTemplate enclosingInstance, string name) { if (name.StartsWith("super.")) { if (superGroup != null) { int dot = name.IndexOf('.'); name = name.Substring(dot + 1, (name.Length) - (dot + 1)); StringTemplate superScopeST = superGroup.LookupTemplate(enclosingInstance,name); return superScopeST; } throw new StringTemplateException(Name + " has no super group; invalid template: " + name); } StringTemplate st = (StringTemplate) templates[name]; bool foundCachedWrappingTemplate = (st is ViewComponentStringTemplate); if ((st == null) || foundCachedWrappingTemplate) { if (enclosingInstance != null) { string prefix; int index = name.IndexOf('/'); if (index == -1) prefix = name; else prefix = name.Substring(0, index); if ("blockcomponent".Equals(prefix)) { if (st == null) st = new ViewComponentStringTemplate(name, this); ((ViewComponentStringTemplate)st).ViewComponentName = name.Substring(index+1); } else if ("component".Equals(prefix)) { if (st == null) st = new ViewComponentStringTemplate(name, this); ((ViewComponentStringTemplate)st).ViewComponentName = name.Substring(index+1); } else if ("capturefor".Equals(prefix)) { if (st == null) st = new ViewComponentStringTemplate(name, this); ((ViewComponentStringTemplate)st).ViewComponentName = "CaptureFor"; } if ((!foundCachedWrappingTemplate) && (st != null)) { templates[name] = st; } } if (st == null && superGroup != null) { st = superGroup.GetInstanceOf(enclosingInstance, name); if (st != null) { st.Group = this; } } if (st == null) { templates[name] = NOT_FOUND_ST; } } else if (st == NOT_FOUND_ST) { return null; } return st; }
public override StringTemplate LookupTemplate(StringTemplate enclosingInstance, string name) { if (name.StartsWith("super.")) { if (superGroup != null) { int dot = name.IndexOf('.'); name = name.Substring(dot + 1, (name.Length) - (dot + 1)); StringTemplate superScopeST = superGroup.LookupTemplate(enclosingInstance, name); return(superScopeST); } throw new StringTemplateException(Name + " has no super group; invalid template: " + name); } StringTemplate st = (StringTemplate)templates[name]; bool foundCachedWrappingTemplate = (st is ViewComponentStringTemplate); if ((st == null) || foundCachedWrappingTemplate) { if (enclosingInstance != null) { string prefix; int index = name.IndexOf('/'); if (index == -1) { prefix = name; } else { prefix = name.Substring(0, index); } if ("blockcomponent".Equals(prefix)) { if (st == null) { st = new ViewComponentStringTemplate(name, this); } ((ViewComponentStringTemplate)st).ViewComponentName = name.Substring(index + 1); } else if ("component".Equals(prefix)) { if (st == null) { st = new ViewComponentStringTemplate(name, this); } ((ViewComponentStringTemplate)st).ViewComponentName = name.Substring(index + 1); } else if ("capturefor".Equals(prefix)) { if (st == null) { st = new ViewComponentStringTemplate(name, this); } ((ViewComponentStringTemplate)st).ViewComponentName = "CaptureFor"; } if ((!foundCachedWrappingTemplate) && (st != null)) { templates[name] = st; } } if (st == null && superGroup != null) { st = superGroup.GetInstanceOf(enclosingInstance, name); if (st != null) { st.Group = this; } } if (st == null) { templates[name] = NOT_FOUND_ST; } } else if (st == NOT_FOUND_ST) { return(null); } return(st); }
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 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 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 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 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"); }
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); } }
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 StringTemplateViewContextAdapter(string componentName, StringTemplate st) { this.componentName = componentName; this.st = st; this.contextVarsAdapter = new StAttributes2IDictionaryAdapter(st); }
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 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 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 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 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 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 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 void TestAttributeNameCannotHaveDots() { StringTemplateGroup group = new StringTemplateGroup("dummy", "."); StringTemplate t = new StringTemplate(group, "$user.Name$"); t.SetAttribute("user.Name", "Kunle"); }
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()); }
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 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 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="" "")+"" ""$</ul>" ); t.SetAttribute("name", "Ter"); t.SetAttribute("name", "Tom"); t.SetAttribute("name", "Mel"); //System.out.println(t); string expecting = "<ul>Ter</li> <li> Tom</li> <li> Mel</ul>"; Assert.AreEqual(expecting, t.ToString()); }
public override StringTemplate GetInstanceOf(StringTemplate enclosingInstance, string name) { StringTemplate st = LookupTemplate(enclosingInstance, name); if (st != null) { StringTemplate instanceST = st.GetInstanceOf(); if (st is ViewComponentStringTemplate) { ((ViewComponentStringTemplate)st).ViewComponentName = null; ((ViewComponentStringTemplate)instanceST).CreateViewComponent(); } return instanceST; } return null; }
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 StringTemplate ToDOT(object tree, ITreeAdaptor adaptor, StringTemplate _treeST, StringTemplate _edgeST) { StringTemplate treeST = _treeST.GetInstanceOf(); nodeNumber = 0; ToDOTDefineNodes(tree, adaptor, treeST); nodeNumber = 0; ToDOTDefineEdges(tree, adaptor, treeST); /* if ( adaptor.GetChildCount(tree)==0 ) { // single node, don't do edge. treeST.SetAttribute("nodes", adaptor.GetNodeText(tree)); } */ return treeST; }
public StAttributes2IDictionaryAdapter(StringTemplate st) { this.st = st; }
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 void testStripOpOfNull() { StringTemplate e = new StringTemplate( "$strip(data)$" ); e = e.GetInstanceOf(); string expecting = ""; // nulls are skipped Assert.AreEqual(expecting, e.ToString()); }