[stringtemplate-interest] Applying templates with more than one parameter to multi-valued attributes
Varghese
vcottagiri at zafinlabs.com
Sat Oct 28 11:50:20 PDT 2006
Hi,
I seem to have trouble with applying templates to multi-valued
attributes. These templates have more than one input parameter.
The problem seems to be when applying templates to multi-valued
attributes, if the enclosed template has more than one parameter, then
the enclosed template cannot resolve the properties of the the formal
argument(the iterated value)
What am i missing?
The idea is a template formats an object and then calls on other
templates to format collections in that object, and so on. The other
templates need to access the enclosing template's attributes. Allowing
enclosing attributes to pass through will solve this problem, but what
if the called template needs the iterator index or a literal parameter
as formal parameters eg:
<a.bList:bFormatter(aDisplay={<a.display>}, bIndex={<i>}), arg={ABC}>
in which case the template definition would be
bFormatter(b, aDisplay, bIndex, arg) ::= <<...>>
Thanks
Vergis
The output is
aFormatter a.display:'A1'
bFormatter b.display:'' a.display:'A1'
bFormatter b.display:'' a.display:'A1'
bFormatter b.display:'' a.display:'A1'
The expected output should be something like
aFormatter a.display:'A1'
bFormatter b.display:'A1 B1' a.display:'A1'
cFormatter c.display:'A1 B1 C1' b.display:'A1 B1' a.display:'A1'
cFormatter c.display:'A1 B1 C2' b.display:'A1 B1' a.display:'A1'
cFormatter c.display:'A1 B1 C3' b.display:'A1 B1' a.display:'A1'
bFormatter b.display:'A1 B2' a.display:'A1'
cFormatter c.display:'A1 B2 C1' b.display:'A1 B2' a.display:'A1'
cFormatter c.display:'A1 B2 C2' b.display:'A1 B2' a.display:'A1'
cFormatter c.display:'A1 B2 C3' b.display:'A1 B2' a.display:'A1'
bFormatter b.display:'A1 B3' a.display:'A1'
cFormatter c.display:'A1 B3 C1' b.display:'A1 B3' a.display:'A1'
cFormatter c.display:'A1 B3 C2' b.display:'A1 B3' a.display:'A1'
cFormatter c.display:'A1 B3 C3' b.display:'A1 B3' a.display:'A1'
TEMPLATE DEFINITION : scopedListingTest.st
group scopedListingTestGroup;
aFormatter(a) ::= <<
aFormatter a.display:'<a.display>'
<a.bList:bFormatter(aDisplay={<a.display>})>
>>
bFormatter(b, aDisplay) ::= <<
bFormatter b.display:'<b.display>' a.display:'<aDisplay>'
<b.cList:cFormatter(bDisplay={<b.display>}, aDisplay={<aDisplay>})>
>>
cFormatter(c, bDisplay, aDisplay) ::= <<
cFormatter c.display:'<c.display>' b.display:'<b.display>'
a.display:'<aDisplay>'
>>
// -----------------------Enclosing attributes pass
through--------------------
aFormatterDefault(a) ::= <<
aFormatterDefault a.display:'<a.display>'
<a.bList:bFormatterDefault()><\n>
>>
bFormatterDefault(b) ::= <<
bFormatterDefault b.display:'<b.display>' a.display:'<a.display>'
<b.cList:cFormatterDefault()>
>>
cFormatterDefault(c) ::= <<
cFormatterDefault c.display:'<c.display>' b.display:'<b.display>'
a.display:'<a.display>'<\n>
>>
TEST CODE : ScopedListingTest.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
public class ScopedListingTest{
public static void main(String[] args) {
InputStream resourceStream =
ScopedListingTest.class.getResourceAsStream("/scopedListingTest.st");
BufferedReader br = new BufferedReader(new
InputStreamReader(resourceStream));
StringTemplateGroup group = new
StringTemplateGroup(br,AngleBracketTemplateLexer.class);
A a = new A(1);
StringTemplate template = group.getInstanceOf("aFormatter");
template.setAttribute("a", a);
System.out.println(template);
System.out.println();
StringTemplate templateDefault =
group.getInstanceOf("aFormatterDefault");
templateDefault.setAttribute("a", a);
System.out.println(templateDefault);
System.out.println();
}
}
class A{
private int display;
private List bList = new ArrayList();
public A(int i) {
this.display = i;
bList.add(new B(1, getDisplay()));
bList.add(new B(2, getDisplay()));
bList.add(new B(3, getDisplay()));
}
public String getDisplay() {
return "A"+display;
}
public List getBList() {
return bList;
}
}
class B{
private int display;
private List cList = new ArrayList();
private String parentDisplay;
public B(int display, String parentDisplay) {
this.parentDisplay = parentDisplay;
this.display = display;
cList.add(new C(1, getDisplay()));
cList.add(new C(2, getDisplay()));
cList.add(new C(3, getDisplay()));
}
public String getDisplay() {
return parentDisplay+" B"+display;
}
public List getCList() {
return cList;
}
}
class C{
private int display;
private String parentDisplay;
public C(int i, String parentDisplay) {
this.display = i;
this.parentDisplay = parentDisplay;
}
public String getDisplay() {
return parentDisplay+" C"+display;
}
}
OUTPUT:
aFormatter a.display:'A1'
bFormatter b.display:'' a.display:'A1'
bFormatter b.display:'' a.display:'A1'
bFormatter b.display:'' a.display:'A1'
aFormatterDefault A1
bFormatterDefault b.display:'A1 B1' a.display:'A1'
cFormatterDefault c.display:'A1 B1 C1' b.display:'A1 B1'
a.display:'A1'
cFormatterDefault c.display:'A1 B1 C2' b.display:'A1 B1'
a.display:'A1'
cFormatterDefault c.display:'A1 B1 C3' b.display:'A1 B1'
a.display:'A1'
bFormatterDefault b.display:'A1 B2' a.display:'A1'
cFormatterDefault c.display:'A1 B2 C1' b.display:'A1 B2'
a.display:'A1'
cFormatterDefault c.display:'A1 B2 C2' b.display:'A1 B2'
a.display:'A1'
cFormatterDefault c.display:'A1 B2 C3' b.display:'A1 B2'
a.display:'A1'
bFormatterDefault b.display:'A1 B3' a.display:'A1'
cFormatterDefault c.display:'A1 B3 C1' b.display:'A1 B3'
a.display:'A1'
cFormatterDefault c.display:'A1 B3 C2' b.display:'A1 B3'
a.display:'A1'
cFormatterDefault c.display:'A1 B3 C3' b.display:'A1 B3'
a.display:'A1'
More information about the stringtemplate-interest
mailing list