All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.sii.ogham.template.thymeleaf.buider.ThymeleafEngineConfigBuilder Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package fr.sii.ogham.template.thymeleaf.buider;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.cache.ICacheManager;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.messageresolver.IMessageResolver;
import org.thymeleaf.templatemode.ITemplateModeHandler;
import org.thymeleaf.templatemode.StandardTemplateModeHandlers;
import org.thymeleaf.templateresolver.ITemplateResolver;

import fr.sii.ogham.core.builder.AbstractParent;
import fr.sii.ogham.core.builder.Builder;

/**
 * Fluent builder to configure Thymeleaf engine.
 * 
 * @author Aurélien Baudet
 *
 * @param 

* the type of the parent builder (when calling {@link #and()} * method) */ public class ThymeleafEngineConfigBuilder

extends AbstractParent

implements Builder { private Set dialects; private Map dialectsByPrefix; private Set templateResolvers; private ICacheManager cacheManager; private Set messageResolvers; private Set defaultMessageResolvers; private Set templateModeHandlers; private Set defaultTemplateModeHandlers; /** * Initializes the builder with a parent builder. The parent builder is used * when calling {@link #and()} method. * * @param parent * the parent builder */ public ThymeleafEngineConfigBuilder(P parent) { super(parent); } /** *

* Sets a new unique dialect for this template engine. *

*

* This operation is equivalent to removing all the currently configured * dialects and then adding this one. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param dialect * the new unique {@link IDialect} to be used. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setDialect(final IDialect dialect) { dialects().clear(); dialects().add(dialect); return this; } /** *

* Adds a new dialect for this template engine, using the specified prefix. *

*

* This dialect will be added to the set of currently configured ones. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param prefix * the prefix that will be used for this dialect * @param dialect * the new {@link IDialect} to be added to the existing ones. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

addDialect(final String prefix, final IDialect dialect) { this.dialectsByPrefix().put(prefix, dialect); return this; } /** *

* Adds a new dialect for this template engine, using the dialect's * specified default dialect. *

*

* This dialect will be added to the set of currently configured ones. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param dialect * the new {@link IDialect} to be added to the existing ones. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

addDialect(final IDialect dialect) { dialects().add(dialect); return this; } /** *

* Sets a new set of dialects for this template engine, referenced by the * prefixes they will be using. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param dialects * the new map of {@link IDialect} objects to be used, referenced * by their prefixes. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setDialectsByPrefix(final Map dialects) { dialectsByPrefix().clear(); dialectsByPrefix().putAll(dialects); return this; } /** *

* Sets a new set of dialects for this template engine, all of them using * their default prefixes. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param dialects * the new set of {@link IDialect} objects to be used. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setDialects(final Set dialects) { this.dialects().clear(); this.dialects().addAll(dialects); return this; } /** *

* Sets an additional set of dialects for this template engine, all of them * using their default prefixes. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param additionalDialects * the new set of {@link IDialect} objects to be used. * * @since 2.0.9 * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setAdditionalDialects(final Set additionalDialects) { dialects().addAll(additionalDialects); return this; } /** *

* Removes all the currently configured dialects. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @return this for fluent use */ public ThymeleafEngineConfigBuilder

clearDialects() { dialects().clear(); return this; } /** *

* Sets the entire set of template resolvers. *

* * @param templateResolvers * the new template resolvers. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setTemplateResolvers(final Set templateResolvers) { this.templateResolvers().clear(); this.templateResolvers().addAll(templateResolvers); return this; } /** *

* Adds a new template resolver to the current set. *

* * @param templateResolver * the new template resolver. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

addTemplateResolver(final ITemplateResolver templateResolver) { templateResolvers().add(templateResolver); return this; } /** *

* Sets a single template resolver for this template engine. *

*

* Calling this method is equivalent to calling * {@link #setTemplateResolvers(Set)} passing a Set with only one template * resolver. *

* * @param templateResolver * the template resolver to be set. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setTemplateResolver(final ITemplateResolver templateResolver) { templateResolvers().clear(); templateResolvers().add(templateResolver); return this; } /** *

* Sets the Cache Manager to be used. If set to null, no caches will be used * throughout the engine. *

*

* By default, an instance of * {@link org.thymeleaf.cache.StandardCacheManager} is set. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param cacheManager * the cache manager to be set. * @return this for fluent use * */ public ThymeleafEngineConfigBuilder

setCacheManager(final ICacheManager cacheManager) { this.cacheManager = cacheManager; return this; } /** *

* Sets the message resolvers to be used by this template engine. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param messageResolvers * the Set of template resolvers. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setMessageResolvers(final Set messageResolvers) { this.messageResolvers().clear(); this.messageResolvers().addAll(messageResolvers); return this; } /** *

* Adds a message resolver to the set of message resolvers to be used by the * template engine. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param messageResolver * the new message resolver to be added. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

addMessageResolver(final IMessageResolver messageResolver) { messageResolvers().add(messageResolver); return this; } /** *

* Sets a single message resolver for this template engine. *

*

* Calling this method is equivalent to calling * {@link #setMessageResolvers(Set)} passing a Set with only one message * resolver. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param messageResolver * the message resolver to be set. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setMessageResolver(final IMessageResolver messageResolver) { messageResolvers().clear(); messageResolvers().add(messageResolver); return this; } /** *

* Sets the default message resolvers. These are used when no message * resolvers are set via the {@link #setMessageResolver(IMessageResolver)}, * {@link #setMessageResolvers(Set)} or * {@link #addMessageResolver(IMessageResolver)} methods. *

*

* This method is useful for creating subclasses of TemplateEngine * that establish default configurations for message resolvers. *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param defaultMessageResolvers * the default message resolvers. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setDefaultMessageResolvers(final Set defaultMessageResolvers) { this.defaultMessageResolvers().clear(); this.defaultMessageResolvers().addAll(defaultMessageResolvers); return this; } /** *

* Sets the Template Mode Handlers to be used by this template engine. Every * available template mode must have its corresponding handler. *

*

* By default, template mode handlers set are * {@link StandardTemplateModeHandlers#ALL_TEMPLATE_MODE_HANDLERS} *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param templateModeHandlers * the Set of Template Mode Handlers. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setTemplateModeHandlers(final Set templateModeHandlers) { this.templateModeHandlers().clear(); this.templateModeHandlers().addAll(templateModeHandlers); return this; } /** *

* Adds a Template Mode Handler to the set of Template Mode Handlers to be * used by the template engine. Every available template mode must have its * corresponding handler. *

*

* By default, template mode handlers set are * {@link StandardTemplateModeHandlers#ALL_TEMPLATE_MODE_HANDLERS} *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param templateModeHandler * the new Template Mode Handler to be added. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

addTemplateModeHandler(final ITemplateModeHandler templateModeHandler) { templateModeHandlers().add(templateModeHandler); return this; } /** *

* Sets the default Template Mode Handlers. These are used when no Template * Mode Handlers are set via the {@link #setTemplateModeHandlers(Set)} or * {@link #addTemplateModeHandler(ITemplateModeHandler)} methods. *

*

* This method is useful for creating subclasses of TemplateEngine * that establish default configurations for Template Mode Handlers. *

*

* By default, template mode handlers set are * {@link StandardTemplateModeHandlers#ALL_TEMPLATE_MODE_HANDLERS} *

*

* This operation can only be executed before processing templates for the * first time. Once a template is processed, the template engine is * considered to be initialized, and from then on any attempt to * change its configuration will result in an exception. *

* * @param defaultTemplateModeHandlers * the default Template Mode Handlers. * @return this for fluent use */ public ThymeleafEngineConfigBuilder

setDefaultTemplateModeHandlers(final Set defaultTemplateModeHandlers) { defaultTemplateModeHandlers().clear(); defaultTemplateModeHandlers().addAll(defaultTemplateModeHandlers); return this; } @Override public TemplateEngine build() { TemplateEngine engine = new TemplateEngine(); configureDialects(engine); configureMessageResolvers(engine); configureTemplateResolvers(engine); configureTemplateModeHandlers(engine); configureCacheManager(engine); return engine; } private void configureCacheManager(TemplateEngine engine) { if (cacheManager != null) { engine.setCacheManager(cacheManager); } } private void configureTemplateModeHandlers(TemplateEngine engine) { if (defaultTemplateModeHandlers != null) { engine.setDefaultTemplateModeHandlers(defaultTemplateModeHandlers); } if (templateModeHandlers != null) { engine.setTemplateModeHandlers(templateModeHandlers); } } private void configureTemplateResolvers(TemplateEngine engine) { if (templateResolvers != null) { engine.setTemplateResolvers(templateResolvers); } } private void configureMessageResolvers(TemplateEngine engine) { if (defaultMessageResolvers != null) { engine.setDefaultMessageResolvers(defaultMessageResolvers); } if (messageResolvers != null) { engine.setMessageResolvers(messageResolvers); } } private void configureDialects(TemplateEngine engine) { if (dialects != null) { engine.setDialects(dialects); } if (dialectsByPrefix != null) { engine.setDialectsByPrefix(dialectsByPrefix); } } private Set dialects() { if (dialects == null) { dialects = new HashSet<>(); } return dialects; } private Map dialectsByPrefix() { if (dialectsByPrefix == null) { dialectsByPrefix = new HashMap<>(); } return dialectsByPrefix; } private Set messageResolvers() { if (messageResolvers == null) { messageResolvers = new HashSet<>(); } return messageResolvers; } private Set templateResolvers() { if (templateResolvers == null) { templateResolvers = new HashSet<>(); } return templateResolvers; } private Set defaultMessageResolvers() { if (defaultMessageResolvers == null) { defaultMessageResolvers = new HashSet<>(); } return defaultMessageResolvers; } private Set templateModeHandlers() { if (templateModeHandlers == null) { templateModeHandlers = new HashSet<>(); } return templateModeHandlers; } private Set defaultTemplateModeHandlers() { if (defaultTemplateModeHandlers == null) { defaultTemplateModeHandlers = new HashSet<>(); } return defaultTemplateModeHandlers; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy