[stringtemplate-interest] Attribute not found exception? Revisited
John Snyders
jjsnyders at rcn.com
Fri Nov 10 09:05:33 PST 2006
Response in this color
-John
So far I have overloaded a Map class which records which keys are
requested.
I pass this in as the attributes list. At the end of a template gen I
know what was
requested and what was not.
[John Snyders] Nice solution but how does it handle conditional logic
in the template?
Conditional logic in the template does cause a problem, in that if a
certain path is followed then it will not know all the parameters... Hmmm,
but do we really want to know all the parameters, or only those that we need
to complete the given concrete instance....
Also this would force a particular data model. Specifically all access
would need to go throught the map or something that looked like a map. How
could you tell that a scalar attribute ($foo$) was used?
Err. But this is situation current in ST as far as I know. ST refers to a
map internally which has name->object. One can either set this map up via
setAttribute(java.lang.String name, java.lang.Object value) and friends or
via setAttributes(java.util.Map attributes). I am using setAttributes.
Perhaps I am missing your point, but the scalar attribute is handled in the
normal way.
[John Snyders] I had not used setAttributes only setAttribute. I see what
you mean now.
Q: Can I do this with out actually rendering, (via toString())... i.e.
is there another way?
[John Snyders] In theory the parsed template should know about all the
attribute references. An alternate tree walk could report on all possible
attribute references. What it wouldn't know is if the references was to a
map key, property, field etc.
Hmmm, I it wonder... could be possible to use this approach to find the
conditional blocks... hmmm. i.e. As I have it now I do not know if the
attribute was being queried for a condition or for a value. As far as I
understand the conditional blocks are controlled by an attribute (either
null/set or true/false). The attributes in a conditional block are not
neccessarily required for a given rendering of the template. The query for
any/all attributes is marked using the extended map. Once you know the
attribute is part of a conditional block you can toggle its state and rerun
the process above to give a new set of attributes... Then you land up with a
bunch of attributes needed for a given set of "states".
references to maps can be resolved by querying the template group for a
map with the given name. For example:
System.out.println("-----\nmissed keys");
Set<String> keys = attributes.getMissedKeys();
for (String key: keys) {
// Check if the key was a map reference:
Map result = templateGroup.getMap(key);
if (result == null) {
System.out.println("key: "+key);
}
}
Properties are something I have not though about yet... will have to think
about it for a while:
$user.name$
$user.gender$
Currently I know that I need an attribute user. But I am not checking if
the properties exists.
Perhaps a dynamic wrapper/proxy class, which my map will insert when the
value is requested... I will have to think some more on this.
One needs a way to handle $user.name.lastname$ and
$user.bla.bla........fred$
[John Snyders] Having ST tell you directly from the AST would avoid this.
Have you tried using the information here
http://www.antlr.org/wiki/display/ST/Debugging
* Sometimes its useful to know (say for testing) that a "required"
string is missing. What is
required and what is not is quite specific to the template. Here is
how I do it for the HTML
I am generating:
[John Snyders] This is a seperate problem right? This is the case I
think is more interesting and important.
It is separate, but at the same time related.
The model/controler defines what attributes will be available under
what conditions. This contract should be unit testable. Using a template to
do this as you describe is a nice way to unit test the contract without
having to instrument the model/controler. Your example has instrumented the
actual template.
Correct
I think it would be better to somehow substitute a special unit test
template. It should check more than just what is required but what is
expected for a given request. Has anyone created extensions to JUnit to do
this?
This is where that relationship above comes in... There is only a marginal
difference between required and expected in my eyes (unless you can provide
me with solid counter example). By querying the template for what attributes
it uses you can find out what is "expected". Expected by this definition is
different in that the value may be unset. Or have I miss understood you?
[John Snyders] I think we are saying the same thing
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061110/005d7fc5/attachment-0001.html
More information about the stringtemplate-interest
mailing list