Interface to be implemented by all element model processors.
Processors of this kind are executed on the entire elements they match --including their bodies--,
in the form of an
IModel object that contains the complete sequence of events that models such
element and its contents.
Reading and modifying the model
The
IModel object passed as a parameter to the
#process(ITemplateContext,IModel,IElementModelStructureHandler) method is mutable,
so it allows any modifications to be done on it. For example, we might want to modify it so that we
replace every text node from its body with a comment with the same contents:
final IModelFactory modelFactory = context.getModelFactory();
int n = model.size();
while (n-- != 0) {
final ITemplateEvent event = model.get(n);
if (event instanceof IText) {
final IComment comment =
modelFactory.createComment(((IText)event).getText());
model.insert(n, comment);
model.remove(n + 1);
}
}
Note also that the
IModel interface includes an
IModel#accept(IModelVisitor) method,
useful for traversing an entire model looking for specific nodes or relevant data the Visitor pattern.
Using the
structureHandler
Model processors are passed a structure handler object that allows them to instruct the engine to take
any actions that cannot be done by directly acting on the
IModel model object itself.
See the documentation for
IElementModelStructureHandler for more info.
Abstract implementations
Two basic abstract implementations of this interface are offered:
-
AbstractElementModelProcessor, meant for processors that match element events by their element
name (i.e. without looking at any attributes).
-
AbstractAttributeModelProcessor, meant for processors that match element events by one of their
attributes (and optionally also the element name).