| 1 | package fr.sii.ogham.template.freemarker; | |
| 2 | ||
| 3 | import java.io.IOException; | |
| 4 | import java.util.Locale; | |
| 5 | ||
| 6 | import org.slf4j.Logger; | |
| 7 | import org.slf4j.LoggerFactory; | |
| 8 | ||
| 9 | import fr.sii.ogham.core.resource.path.UnresolvedPath; | |
| 10 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
| 11 | import fr.sii.ogham.core.resource.resolver.ResourceResolverRegistry; | |
| 12 | import fr.sii.ogham.template.exception.ResolverAdapterException; | |
| 13 | import fr.sii.ogham.template.freemarker.adapter.StringContentTemplateLoader; | |
| 14 | import fr.sii.ogham.template.freemarker.adapter.TemplateLoaderAdapter; | |
| 15 | import freemarker.cache.TemplateLoader; | |
| 16 | import freemarker.cache.TemplateLookupContext; | |
| 17 | import freemarker.cache.TemplateLookupResult; | |
| 18 | import freemarker.cache.TemplateLookupStrategy; | |
| 19 | ||
| 20 | /** | |
| 21 | * Ogham special {@link TemplateLookupStrategy} that is used in order to skip | |
| 22 | * {@link Locale} resolution for templates that are provided directly by a | |
| 23 | * string and not by a path. | |
| 24 | * | |
| 25 | * @see StringContentTemplateLoader | |
| 26 | * @author Aurélien Baudet | |
| 27 | * | |
| 28 | */ | |
| 29 | public class SkipLocaleForStringContentTemplateLookupStrategy extends TemplateLookupStrategy { | |
| 30 | private static final Logger LOG = LoggerFactory.getLogger(SkipLocaleForStringContentTemplateLookupStrategy.class); | |
| 31 | ||
| 32 | private final TemplateLookupStrategy delegate; | |
| 33 | private final ResourceResolverRegistry resolverRegistry; | |
| 34 | private final TemplateLoaderAdapter adapter; | |
| 35 | ||
| 36 | public SkipLocaleForStringContentTemplateLookupStrategy(TemplateLookupStrategy delegate, ResourceResolverRegistry resolverRegistry, TemplateLoaderAdapter adapter) { | |
| 37 | super(); | |
| 38 | this.delegate = delegate; | |
| 39 | this.resolverRegistry = resolverRegistry; | |
| 40 | this.adapter = adapter; | |
| 41 | } | |
| 42 | ||
| 43 | @Override | |
| 44 | public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException { | |
| 45 | try { | |
| 46 | ResourceResolver matchingResolver = resolverRegistry.getSupportingResolver(new UnresolvedPath(ctx.getTemplateName())); | |
| 47 | // no match, delegate to let delegate decide | |
| 48 |
2
1. lookup : negated conditional → SURVIVED 2. lookup : negated conditional → KILLED |
if (matchingResolver == null) { |
| 49 |
1
1. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → NO_COVERAGE |
return delegate.lookup(ctx); |
| 50 | } | |
| 51 | TemplateLoader matchingAdapter = adapter.adapt(matchingResolver); | |
| 52 | // if it is a template content (directly a string) | |
| 53 | // => skip locale resolution | |
| 54 |
2
1. lookup : negated conditional → SURVIVED 2. lookup : negated conditional → KILLED |
if (matchingAdapter instanceof StringContentTemplateLoader) { |
| 55 |
2
1. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → NO_COVERAGE 2. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → KILLED |
return ctx.lookupWithLocalizedThenAcquisitionStrategy(ctx.getTemplateName(), null); |
| 56 | } | |
| 57 | // standard Freemarker behavior | |
| 58 |
4
1. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → KILLED 2. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → KILLED 3. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → KILLED 4. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → KILLED |
return delegate.lookup(ctx); |
| 59 | } catch (ResolverAdapterException e) { | |
| 60 | LOG.debug("Failed to determine which Freemarker adapter to use for template name {}", ctx.getTemplateName(), e); | |
| 61 |
1
1. lookup : replaced return value with null for fr/sii/ogham/template/freemarker/SkipLocaleForStringContentTemplateLookupStrategy::lookup → NO_COVERAGE |
return delegate.lookup(ctx); |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | } | |
Mutations | ||
| 48 |
1.1 2.2 |
|
| 49 |
1.1 |
|
| 54 |
1.1 2.2 |
|
| 55 |
1.1 2.2 |
|
| 58 |
1.1 2.2 3.3 4.4 |
|
| 61 |
1.1 |