[stringtemplate-interest] addAttribute/addAttributes weirdness
Terence Parr
parrt at cs.usfca.edu
Sun Apr 22 17:39:47 PDT 2007
Hi Brian,
I am pretty sure I meant this method as a very raw way to override
everything going on inside ST:
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
As you can see, just immediately sets the attribute list without
checking the formal arguments and so on. I do not iterate through
the list looking to see if you violate things. If you look at the
individual set attribute methods, you will see that it eventually
calls rawSetAttribute and does the check:
if ( formalArguments!=FormalArgument.UNKNOWN &&
getFormalArgument(name)==null )
{
// a normal call to setAttribute with unknown attribute
throw new NoSuchElementException("no such attribute: "+name+
" in template context "+
getEnclosingInstanceStackString());
}
Does that help?
Ter
On Mar 24, 2007, at 8:34 PM, Brian Lewis wrote:
> I'm writing StringTemplate 3.0 group files and noticing some strange
> differences between addAttribute(String, String) and addAttributes
> (Map).
>
> group test;
> page(title, cssFile) ::=
> "<html>$\n$$title$$\n$$if(cssFile)$$cssFile$$endif$$\n$</html>"
> somePage() ::= "$page(title={title of the page})$"
>
> If I getInstanceOf("somePage") and setAttribute("cssFile",
> "style.css"),
> I get NoSuchElementException because cssFile isn't in somePage's
> argument list, even though it *is* in page's argument list.
>
> If I put cssFile in somePage's argument list and take it out of
> page's,
> I get the desired effect, but the usefulness of page is decreased
> because the templates being wrapped are having to care about cssFile,
> which should only be the concern of page.
>
> http://www.antlr.org/wiki/display/ST/Group+Files says that "If you
> reference an attribute that is not formally defined in that template
> or any enclosing template, you also get [NoSuchElementException]." In
> violation (I think) of that, I can put something like $nonesuch$
> anywhere in one of my group file templates like somePage and I don't
> get an exception.
>
> If I put "cssFile" -> "style.css" in a Map and addAttributes() it to
> somePage, I don't get NoSuchElementException, even though cssFile
> isn't
> in somePage's argument list. That seems inconsistent.
>
> If I remove cssFile from page's argument list like so:
> group test;
> page(title) ::=
> "<html>$\n$$title$$\n$$if(cssFile)$$cssFile$$endif$$\n$</html>"
> somePage() ::= "$page(title={title of the page})$"
>
> and addAttributes(), I get the obvious desired output. But why was it
> allowed? cssFile isn't in page's argument list. Why was having cssFile
> in page's argument list suppressing $cssFile$?
>
> We're trying to make a StringTemplateView for Spring. The natural way
> to set the attributes is the setAttributes() Map way. The problem is
> that using setAttributes() seems to disable error checking (?) and
> requires that you counterintuitively remove things from the template
> argument lists.
>
> I hope someone will agree that this stuff is pretty weird. Any help
> would be greatly appreciated. Thanks.
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list