Interface IElementModelProcessor

  • All Superinterfaces:
    IElementProcessor, IProcessor
    All Known Implementing Classes:
    AbstractAttributeModelProcessor, AbstractElementModelProcessor

    public interface IElementModelProcessor
    extends IElementProcessor

    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:

    Since:
    3.0.0
    Author:
    Daniel Fernández
    See Also:
    AbstractElementModelProcessor, AbstractAttributeModelProcessor, IElementModelStructureHandler
    • Method Detail

      • process

        void process​(ITemplateContext context,
                     IModel model,
                     IElementModelStructureHandler structureHandler)

        Execute the processor.

        The IModel object represents the section of template (a fragment) on which the processor is executing, and can be directly modified. Instructions to be given to the template engine such as local variable creation, inlining etc. should be done via the IElementModelStructureHandler handler.

        Parameters:
        context - the execution context.
        model - the model this processor is executing on.
        structureHandler - the handler that will centralise modifications and commands to the engine.