[stringtemplate-interest] Composing Model Adapters
Collin Fagan
collin.fagan at gmail.com
Sun Apr 17 11:55:57 PDT 2011
Hi Everyone,
As I've been working on Strum I have run into the reoccurring question of
'Should this model adapter inherit from ObjectModelAdapter?'. In some cases
it seems right to pass it to OMA when the custom model adapter fails to find
a property. Other times it seems like one might want to try OMA first ..
then go ahead with the 'special case' of the custom model adapter.
Eventually I wanted to just compose a group of model adapters into a
hierarchy. This is where I run to an issue with getProperty(). Since it
throws STNoSuchPropertyException to signal it's failure to find a property I
end up using execptions for flow control, which is a bad idea right?
Example:
public class MultiModelAdapter implements ModelAdaptor {
private List<ModelAdaptor> adaptors = new ArrayList<ModelAdaptor>();
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName) throws
STNoSuchPropertyException {
Object value = null;
boolean found = false;
for(ModelAdaptor adaptor: adaptors){
try{
value = adaptor.getProperty(interp, self, adaptor, property,
propertyName);
found = true;
}catch(STNoSuchPropertyException exception){
continue;
}
}
if(!found){
throw new STNoSuchPropertyException();
}
return value;
}
}
Is there a way around this or is MA just not meant to be composed like this?
Collin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110417/e87c5fe1/attachment-0001.html
More information about the stringtemplate-interest
mailing list