Search in sources :

Example 1 with IPreProcessor

use of org.thymeleaf.preprocessor.IPreProcessor in project thymeleaf-tests by thymeleaf.

the class Dialect01 method getPreProcessors.

@Override
public Set<IPreProcessor> getPreProcessors() {
    final Set<IPreProcessor> preProcessors = new HashSet<IPreProcessor>();
    preProcessors.add(new PreProcessor(TemplateMode.HTML, Dialect01PreProcessor.class, 1000));
    preProcessors.add(new PreProcessor(TemplateMode.XML, Dialect01PreProcessor.class, 1000));
    preProcessors.add(new PreProcessor(TemplateMode.TEXT, Dialect01PreProcessor.class, 1000));
    preProcessors.add(new PreProcessor(TemplateMode.JAVASCRIPT, Dialect01PreProcessor.class, 1000));
    preProcessors.add(new PreProcessor(TemplateMode.CSS, Dialect01PreProcessor.class, 1000));
    return preProcessors;
}
Also used : IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) PreProcessor(org.thymeleaf.preprocessor.PreProcessor) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) HashSet(java.util.HashSet)

Example 2 with IPreProcessor

use of org.thymeleaf.preprocessor.IPreProcessor in project thymeleaf by thymeleaf.

the class TemplateManager method createTemplateProcessingHandlerChain.

private static ITemplateHandler createTemplateProcessingHandlerChain(final IEngineContext context, final boolean setPreProcessors, final boolean setPostProcessors, final ITemplateHandler handler, final Writer writer) {
    final IEngineConfiguration configuration = context.getConfiguration();
    /*
         * Declare the pair of pointers that will allow us to build the chain of template handlers
         */
    ITemplateHandler firstHandler = null;
    ITemplateHandler lastHandler = null;
    /*
         * First type of handlers to be added: pre-processors (if any)
         */
    if (setPreProcessors) {
        final Set<IPreProcessor> preProcessors = configuration.getPreProcessors(context.getTemplateMode());
        if (preProcessors != null && preProcessors.size() > 0) {
            for (final IPreProcessor preProcessor : preProcessors) {
                final Class<? extends ITemplateHandler> preProcessorClass = preProcessor.getHandlerClass();
                final ITemplateHandler preProcessorHandler;
                try {
                    preProcessorHandler = preProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of pre-processor " + preProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                preProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = preProcessorHandler;
                    lastHandler = preProcessorHandler;
                } else {
                    lastHandler.setNext(preProcessorHandler);
                    lastHandler = preProcessorHandler;
                }
            }
        }
    }
    /*
         * Initialize and add to the chain te Processor Handler itself, the central piece of the chain
         */
    handler.setContext(context);
    if (firstHandler == null) {
        firstHandler = handler;
        lastHandler = handler;
    } else {
        lastHandler.setNext(handler);
        lastHandler = handler;
    }
    /*
         * After the Processor Handler, we now must add the post-processors (if any)
         */
    if (setPostProcessors) {
        final Set<IPostProcessor> postProcessors = configuration.getPostProcessors(context.getTemplateMode());
        if (postProcessors != null && postProcessors.size() > 0) {
            for (final IPostProcessor postProcessor : postProcessors) {
                final Class<? extends ITemplateHandler> postProcessorClass = postProcessor.getHandlerClass();
                final ITemplateHandler postProcessorHandler;
                try {
                    postProcessorHandler = postProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of post-processor " + postProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                postProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = postProcessorHandler;
                    lastHandler = postProcessorHandler;
                } else {
                    lastHandler.setNext(postProcessorHandler);
                    lastHandler = postProcessorHandler;
                }
            }
        }
    }
    /*
         * Last step: the OUTPUT HANDLER
         */
    if (writer != null) {
        final OutputTemplateHandler outputHandler = new OutputTemplateHandler(writer);
        outputHandler.setContext(context);
        if (firstHandler == null) {
            firstHandler = outputHandler;
        } else {
            lastHandler.setNext(outputHandler);
        }
    }
    return firstHandler;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Example 3 with IPreProcessor

use of org.thymeleaf.preprocessor.IPreProcessor in project thymeleaf by thymeleaf.

the class ConfigurationPrinterHelper method printPreProcessorsForTemplateMode.

private static void printPreProcessorsForTemplateMode(final ConfigLogBuilder logBuilder, final Set<IPreProcessor> preProcessors, final TemplateMode templateMode) {
    if (preProcessors == null || preProcessors.isEmpty()) {
        return;
    }
    final List<IPreProcessor> preProcessorsForTemplateMode = new ArrayList<IPreProcessor>();
    for (final IPreProcessor preProcessor : preProcessors) {
        if (!templateMode.equals(preProcessor.getTemplateMode())) {
            continue;
        }
        preProcessorsForTemplateMode.add(preProcessor);
    }
    if (preProcessorsForTemplateMode.isEmpty()) {
        // Nothing to show, there are no artifacts for this template mode
        return;
    }
    Collections.sort(preProcessorsForTemplateMode, ProcessorComparators.PRE_PROCESSOR_COMPARATOR);
    logBuilder.line("[THYMELEAF]     * Pre-Processors for Template Mode: {} by [precedence]", templateMode);
    for (final IPreProcessor preProcessor : preProcessorsForTemplateMode) {
        logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(preProcessor.getPrecedence()), preProcessor.getClass().getName() });
    }
}
Also used : ArrayList(java.util.ArrayList) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor)

Example 4 with IPreProcessor

use of org.thymeleaf.preprocessor.IPreProcessor in project sling by apache.

the class TemplateManager method createTemplateProcessingHandlerChain.

private static ITemplateHandler createTemplateProcessingHandlerChain(final IEngineContext context, final boolean setPreProcessors, final boolean setPostProcessors, final ITemplateHandler handler, final Writer writer) {
    final IEngineConfiguration configuration = context.getConfiguration();
    /*
         * Declare the pair of pointers that will allow us to build the chain of template handlers
         */
    ITemplateHandler firstHandler = null;
    ITemplateHandler lastHandler = null;
    /*
         * First type of handlers to be added: pre-processors (if any)
         */
    if (setPreProcessors) {
        final Set<IPreProcessor> preProcessors = configuration.getPreProcessors(context.getTemplateMode());
        if (preProcessors != null && preProcessors.size() > 0) {
            for (final IPreProcessor preProcessor : preProcessors) {
                final Class<? extends ITemplateHandler> preProcessorClass = preProcessor.getHandlerClass();
                final ITemplateHandler preProcessorHandler;
                try {
                    preProcessorHandler = preProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of pre-processor " + preProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                preProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = preProcessorHandler;
                    lastHandler = preProcessorHandler;
                } else {
                    lastHandler.setNext(preProcessorHandler);
                    lastHandler = preProcessorHandler;
                }
            }
        }
    }
    /*
         * Initialize and add to the chain te Processor Handler itself, the central piece of the chain
         */
    handler.setContext(context);
    if (firstHandler == null) {
        firstHandler = handler;
        lastHandler = handler;
    } else {
        lastHandler.setNext(handler);
        lastHandler = handler;
    }
    /*
         * After the Processor Handler, we now must add the post-processors (if any)
         */
    if (setPostProcessors) {
        final Set<IPostProcessor> postProcessors = configuration.getPostProcessors(context.getTemplateMode());
        if (postProcessors != null && postProcessors.size() > 0) {
            for (final IPostProcessor postProcessor : postProcessors) {
                final Class<? extends ITemplateHandler> postProcessorClass = postProcessor.getHandlerClass();
                final ITemplateHandler postProcessorHandler;
                try {
                    postProcessorHandler = postProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of post-processor " + postProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                postProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = postProcessorHandler;
                    lastHandler = postProcessorHandler;
                } else {
                    lastHandler.setNext(postProcessorHandler);
                    lastHandler = postProcessorHandler;
                }
            }
        }
    }
    /*
         * Last step: the OUTPUT HANDLER
         */
    if (writer != null) {
        final OutputTemplateHandler outputHandler = new OutputTemplateHandler(writer);
        outputHandler.setContext(context);
        if (firstHandler == null) {
            firstHandler = outputHandler;
        } else {
            lastHandler.setNext(outputHandler);
        }
    }
    return firstHandler;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Example 5 with IPreProcessor

use of org.thymeleaf.preprocessor.IPreProcessor in project thymeleaf by thymeleaf.

the class ConfigurationPrinterHelper method printDebugConfiguration.

private static void printDebugConfiguration(final ConfigLogBuilder logBuilder, final IDialect idialect, final String dialectPrefix) {
    if (idialect instanceof IProcessorDialect) {
        final IProcessorDialect dialect = (IProcessorDialect) idialect;
        final Set<IProcessor> processors = dialect.getProcessors(dialectPrefix);
        printProcessorsForTemplateMode(logBuilder, processors, TemplateMode.HTML);
        printProcessorsForTemplateMode(logBuilder, processors, TemplateMode.XML);
        printProcessorsForTemplateMode(logBuilder, processors, TemplateMode.TEXT);
        printProcessorsForTemplateMode(logBuilder, processors, TemplateMode.JAVASCRIPT);
        printProcessorsForTemplateMode(logBuilder, processors, TemplateMode.CSS);
        printProcessorsForTemplateMode(logBuilder, processors, TemplateMode.RAW);
    }
    if (idialect instanceof IPreProcessorDialect) {
        final IPreProcessorDialect dialect = (IPreProcessorDialect) idialect;
        final Set<IPreProcessor> preProcessors = dialect.getPreProcessors();
        printPreProcessorsForTemplateMode(logBuilder, preProcessors, TemplateMode.HTML);
        printPreProcessorsForTemplateMode(logBuilder, preProcessors, TemplateMode.XML);
        printPreProcessorsForTemplateMode(logBuilder, preProcessors, TemplateMode.TEXT);
        printPreProcessorsForTemplateMode(logBuilder, preProcessors, TemplateMode.JAVASCRIPT);
        printPreProcessorsForTemplateMode(logBuilder, preProcessors, TemplateMode.CSS);
        printPreProcessorsForTemplateMode(logBuilder, preProcessors, TemplateMode.RAW);
    }
    if (idialect instanceof IPostProcessorDialect) {
        final IPostProcessorDialect dialect = (IPostProcessorDialect) idialect;
        final Set<IPostProcessor> postProcessors = dialect.getPostProcessors();
        printPostProcessorsForTemplateMode(logBuilder, postProcessors, TemplateMode.HTML);
        printPostProcessorsForTemplateMode(logBuilder, postProcessors, TemplateMode.XML);
        printPostProcessorsForTemplateMode(logBuilder, postProcessors, TemplateMode.TEXT);
        printPostProcessorsForTemplateMode(logBuilder, postProcessors, TemplateMode.JAVASCRIPT);
        printPostProcessorsForTemplateMode(logBuilder, postProcessors, TemplateMode.CSS);
        printPostProcessorsForTemplateMode(logBuilder, postProcessors, TemplateMode.RAW);
    }
    if (idialect instanceof IExpressionObjectDialect) {
        final IExpressionObjectDialect dialect = (IExpressionObjectDialect) idialect;
        final IExpressionObjectFactory expressionObjectFactory = dialect.getExpressionObjectFactory();
        if (expressionObjectFactory != null) {
            final Set<String> expressionObjectNames = expressionObjectFactory.getAllExpressionObjectNames();
            if (expressionObjectNames != null && !expressionObjectNames.isEmpty()) {
                logBuilder.line("[THYMELEAF]     * Expression Objects:");
                for (final String expressionObjectName : expressionObjectNames) {
                    logBuilder.line("[THYMELEAF]         * #{}", new Object[] { expressionObjectName });
                }
            }
        }
    }
    if (idialect instanceof IExecutionAttributeDialect) {
        final IExecutionAttributeDialect dialect = (IExecutionAttributeDialect) idialect;
        final Map<String, Object> executionAttributes = dialect.getExecutionAttributes();
        if (executionAttributes != null && !executionAttributes.isEmpty()) {
            logBuilder.line("[THYMELEAF]     * Execution Attributes:");
            for (final Map.Entry<String, Object> executionAttributesEntry : executionAttributes.entrySet()) {
                final String attrName = executionAttributesEntry.getKey();
                final String attrValue = (executionAttributesEntry.getValue() == null ? null : executionAttributesEntry.getValue().toString());
                logBuilder.line("[THYMELEAF]         * \"{}\": {}", new Object[] { attrName, attrValue });
            }
        }
    }
}
Also used : IExpressionObjectFactory(org.thymeleaf.expression.IExpressionObjectFactory) IExecutionAttributeDialect(org.thymeleaf.dialect.IExecutionAttributeDialect) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) IProcessor(org.thymeleaf.processor.IProcessor) IProcessorDialect(org.thymeleaf.dialect.IProcessorDialect) IPostProcessorDialect(org.thymeleaf.dialect.IPostProcessorDialect) IPreProcessorDialect(org.thymeleaf.dialect.IPreProcessorDialect) IExpressionObjectDialect(org.thymeleaf.dialect.IExpressionObjectDialect) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) Map(java.util.Map)

Aggregations

IPreProcessor (org.thymeleaf.preprocessor.IPreProcessor)6 IPostProcessor (org.thymeleaf.postprocessor.IPostProcessor)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)2 IExecutionAttributeDialect (org.thymeleaf.dialect.IExecutionAttributeDialect)2 IExpressionObjectDialect (org.thymeleaf.dialect.IExpressionObjectDialect)2 IPostProcessorDialect (org.thymeleaf.dialect.IPostProcessorDialect)2 IPreProcessorDialect (org.thymeleaf.dialect.IPreProcessorDialect)2 IProcessorDialect (org.thymeleaf.dialect.IProcessorDialect)2 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)2 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)2 IExpressionObjectFactory (org.thymeleaf.expression.IExpressionObjectFactory)2 IProcessor (org.thymeleaf.processor.IProcessor)2 EnumMap (java.util.EnumMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1