| 1 | package fr.sii.ogham.template.freemarker.builder; | |
| 2 | ||
| 3 | import static freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS; | |
| 4 | ||
| 5 | import java.util.ArrayList; | |
| 6 | import java.util.List; | |
| 7 | ||
| 8 | import org.slf4j.Logger; | |
| 9 | import org.slf4j.LoggerFactory; | |
| 10 | ||
| 11 | import fr.sii.ogham.core.builder.Builder; | |
| 12 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 13 | import fr.sii.ogham.core.builder.context.DefaultBuildContext; | |
| 14 | import fr.sii.ogham.core.builder.resolution.ClassPathResolutionBuilder; | |
| 15 | import fr.sii.ogham.core.builder.resolution.FileResolutionBuilder; | |
| 16 | import fr.sii.ogham.core.builder.resolution.ResourceResolutionBuilder; | |
| 17 | import fr.sii.ogham.core.builder.resolution.ResourceResolutionBuilderHelper; | |
| 18 | import fr.sii.ogham.core.builder.resolution.StringResolutionBuilder; | |
| 19 | import fr.sii.ogham.core.builder.template.DetectorBuilder; | |
| 20 | import fr.sii.ogham.core.fluent.AbstractParent; | |
| 21 | import fr.sii.ogham.core.resource.resolver.FirstSupportingResourceResolver; | |
| 22 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
| 23 | import fr.sii.ogham.core.template.detector.FixedEngineDetector; | |
| 24 | import fr.sii.ogham.core.template.detector.OrTemplateDetector; | |
| 25 | import fr.sii.ogham.core.template.detector.SimpleResourceEngineDetector; | |
| 26 | import fr.sii.ogham.core.template.detector.TemplateEngineDetector; | |
| 27 | import fr.sii.ogham.core.template.parser.TemplateParser; | |
| 28 | import fr.sii.ogham.template.freemarker.FreeMarkerFirstSupportingTemplateLoader; | |
| 29 | import fr.sii.ogham.template.freemarker.FreeMarkerParser; | |
| 30 | import fr.sii.ogham.template.freemarker.FreeMarkerTemplateDetector; | |
| 31 | import fr.sii.ogham.template.freemarker.SkipLocaleForStringContentTemplateLookupStrategy; | |
| 32 | import fr.sii.ogham.template.freemarker.TemplateLoaderOptions; | |
| 33 | import fr.sii.ogham.template.freemarker.adapter.ClassPathResolverAdapter; | |
| 34 | import fr.sii.ogham.template.freemarker.adapter.FileResolverAdapter; | |
| 35 | import fr.sii.ogham.template.freemarker.adapter.FirstSupportingResolverAdapter; | |
| 36 | import fr.sii.ogham.template.freemarker.adapter.StringResolverAdapter; | |
| 37 | import fr.sii.ogham.template.freemarker.adapter.TemplateLoaderAdapter; | |
| 38 | import freemarker.cache.TemplateLoader; | |
| 39 | import freemarker.template.Configuration; | |
| 40 | import freemarker.template.TemplateExceptionHandler; | |
| 41 | ||
| 42 | @SuppressWarnings("squid:S00119") | |
| 43 | public abstract class AbstractFreemarkerBuilder<MYSELF extends AbstractFreemarkerBuilder<MYSELF, P>, P> extends AbstractParent<P> | |
| 44 | implements DetectorBuilder<MYSELF>, ResourceResolutionBuilder<MYSELF>, Builder<TemplateParser> { | |
| 45 | private static final Logger LOG = LoggerFactory.getLogger(AbstractFreemarkerBuilder.class); | |
| 46 | ||
| 47 | protected final MYSELF myself; | |
| 48 | protected final BuildContext buildContext; | |
| 49 | private TemplateEngineDetector detector; | |
| 50 | private ResourceResolutionBuilderHelper<MYSELF> resourceResolutionBuilderHelper; | |
| 51 | private Configuration configuration; | |
| 52 | private List<TemplateLoaderAdapter> customAdapters; | |
| 53 | private FreemarkerConfigurationBuilder<MYSELF> configurationBuilder; | |
| 54 | private ClassLoader classLoader; | |
| 55 | ||
| 56 | protected AbstractFreemarkerBuilder(Class<?> selfType) { | |
| 57 | this(selfType, null, new DefaultBuildContext()); | |
| 58 | } | |
| 59 | ||
| 60 | @SuppressWarnings("unchecked") | |
| 61 | protected AbstractFreemarkerBuilder(Class<?> selfType, P parent, BuildContext buildContext) { | |
| 62 | super(parent); | |
| 63 | myself = (MYSELF) selfType.cast(this); | |
| 64 | this.buildContext = buildContext; | |
| 65 | customAdapters = new ArrayList<>(); | |
| 66 | } | |
| 67 | ||
| 68 | @Override | |
| 69 | public MYSELF detector(TemplateEngineDetector detector) { | |
| 70 | this.detector = detector; | |
| 71 |
1
1. detector : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::detector → NO_COVERAGE |
return myself; |
| 72 | } | |
| 73 | ||
| 74 | @Override | |
| 75 | public ClassPathResolutionBuilder<MYSELF> classpath() { | |
| 76 |
1
1. classpath : removed call to fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::initResolutionBuilder → RUN_ERROR |
initResolutionBuilder(); |
| 77 |
1
1. classpath : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::classpath → RUN_ERROR |
return resourceResolutionBuilderHelper.classpath(); |
| 78 | } | |
| 79 | ||
| 80 | @Override | |
| 81 | public FileResolutionBuilder<MYSELF> file() { | |
| 82 |
1
1. file : removed call to fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::initResolutionBuilder → RUN_ERROR |
initResolutionBuilder(); |
| 83 |
1
1. file : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::file → RUN_ERROR |
return resourceResolutionBuilderHelper.file(); |
| 84 | } | |
| 85 | ||
| 86 | @Override | |
| 87 | public StringResolutionBuilder<MYSELF> string() { | |
| 88 |
1
1. string : removed call to fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::initResolutionBuilder → RUN_ERROR |
initResolutionBuilder(); |
| 89 |
1
1. string : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::string → RUN_ERROR |
return resourceResolutionBuilderHelper.string(); |
| 90 | } | |
| 91 | ||
| 92 | @Override | |
| 93 | public MYSELF resolver(ResourceResolver resolver) { | |
| 94 |
1
1. resolver : removed call to fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::initResolutionBuilder → NO_COVERAGE |
initResolutionBuilder(); |
| 95 |
1
1. resolver : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::resolver → NO_COVERAGE |
return resourceResolutionBuilderHelper.resolver(resolver); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Ogham provides a generic resource resolution mechanism | |
| 100 | * ({@link ResourceResolver}). Freemarker uses its own template resolution | |
| 101 | * mechanism ({@link TemplateLoader}). A resolver adapter | |
| 102 | * ({@link TemplateLoaderAdapter}) is the way to transform a | |
| 103 | * {@link ResourceResolver} into a {@link TemplateLoader}. | |
| 104 | * | |
| 105 | * <p> | |
| 106 | * Ogham provides and registers default resolver adapters but you may need | |
| 107 | * to use a custom {@link ResourceResolver}. So you also need to provide the | |
| 108 | * corresponding {@link TemplateLoaderAdapter}. | |
| 109 | * | |
| 110 | * @param adapter | |
| 111 | * the resolver adapter | |
| 112 | * @return this instance for fluent chaining | |
| 113 | */ | |
| 114 | public MYSELF resolverAdapter(TemplateLoaderAdapter adapter) { | |
| 115 | customAdapters.add(adapter); | |
| 116 |
1
1. resolverAdapter : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::resolverAdapter → NO_COVERAGE |
return myself; |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Fluent configurer for Freemarker configuration. | |
| 121 | * | |
| 122 | * @return the fluent builder for Freemarker configuration object | |
| 123 | */ | |
| 124 | public FreemarkerConfigurationBuilder<MYSELF> configuration() { | |
| 125 |
1
1. configuration : negated conditional → RUN_ERROR |
if (configurationBuilder == null) { |
| 126 | configurationBuilder = new FreemarkerConfigurationBuilder<>(myself, buildContext); | |
| 127 | } | |
| 128 |
1
1. configuration : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::configuration → RUN_ERROR |
return configurationBuilder; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Sets a Freemarker configuration. | |
| 133 | * | |
| 134 | * This value preempts any other value defined by calling | |
| 135 | * {@link #configuration()} method. It means that the provided configuration | |
| 136 | * is used as-is and any call to {@link #configuration()} builder methods | |
| 137 | * has no effect on the provided configuration. You have to manually | |
| 138 | * configure it. | |
| 139 | * | |
| 140 | * If this method is called several times, only the last provided | |
| 141 | * configuration is used. | |
| 142 | * | |
| 143 | * @param configuration | |
| 144 | * the Freemarker configuration | |
| 145 | * @return this instance for fluent chaining | |
| 146 | */ | |
| 147 | public MYSELF configuration(Configuration configuration) { | |
| 148 | this.configuration = configuration; | |
| 149 |
1
1. configuration : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::configuration → NO_COVERAGE |
return myself; |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * Merge an existing Freemarker configuration with previously provided | |
| 154 | * configuration. | |
| 155 | * | |
| 156 | * <p> | |
| 157 | * The provided configuration is used and any call to | |
| 158 | * {@link #configuration()} builder methods are applied to the provided | |
| 159 | * configuration. | |
| 160 | * | |
| 161 | * | |
| 162 | * @param configuration | |
| 163 | * The Freemarker configuration to apply | |
| 164 | * @return this instance for fluent chaining | |
| 165 | */ | |
| 166 | public MYSELF mergeConfiguration(Configuration configuration) { | |
| 167 | configuration().base(configuration); | |
| 168 |
1
1. mergeConfiguration : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::mergeConfiguration → RUN_ERROR |
return myself; |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Set the {@link ClassLoader} to use for loading classpath resources. | |
| 173 | * | |
| 174 | * <p> | |
| 175 | * Loading resources from classpath requires a {@link ClassLoader}. Several | |
| 176 | * class loaders may be defined in an application to isolate parts of the | |
| 177 | * application. FreeMarker requires you to provide a {@link ClassLoader} for | |
| 178 | * finding resources in the classpath. This is done for security reasons. | |
| 179 | * | |
| 180 | * <p> | |
| 181 | * By default, Ogham uses the current thread class loader. | |
| 182 | * | |
| 183 | * @param classLoader | |
| 184 | * the class loader to use | |
| 185 | * @return this instance for fluent chaining | |
| 186 | */ | |
| 187 | public MYSELF classLoader(ClassLoader classLoader) { | |
| 188 | this.classLoader = classLoader; | |
| 189 |
1
1. classLoader : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::classLoader → NO_COVERAGE |
return myself; |
| 190 | } | |
| 191 | ||
| 192 | @Override | |
| 193 | public TemplateParser build() { | |
| 194 | LOG.info("Freemarker parser is registered"); | |
| 195 |
1
1. build : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::build → RUN_ERROR |
return buildContext.register(new FreeMarkerParser(buildConfiguration(), buildResolver())); |
| 196 | } | |
| 197 | ||
| 198 | @Override | |
| 199 | public TemplateEngineDetector buildDetector() { | |
| 200 |
2
1. buildDetector : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::buildDetector → RUN_ERROR 2. buildDetector : negated conditional → RUN_ERROR |
return detector == null ? buildDefaultDetector() : detector; |
| 201 | } | |
| 202 | ||
| 203 | private TemplateEngineDetector buildDefaultDetector() { | |
| 204 | FirstSupportingResourceResolver resolver = buildResolver(); | |
| 205 | OrTemplateDetector or = buildContext.register(new OrTemplateDetector()); | |
| 206 |
1
1. buildDefaultDetector : removed call to fr/sii/ogham/core/template/detector/OrTemplateDetector::addDetector → RUN_ERROR |
or.addDetector(buildContext.register(new FreeMarkerTemplateDetector(resolver, ".ftl", ".ftlh"))); |
| 207 |
1
1. buildDefaultDetector : removed call to fr/sii/ogham/core/template/detector/OrTemplateDetector::addDetector → RUN_ERROR |
or.addDetector(buildContext.register(new SimpleResourceEngineDetector(resolver, buildContext.register(new FixedEngineDetector(true))))); |
| 208 |
1
1. buildDefaultDetector : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::buildDefaultDetector → RUN_ERROR |
return or; |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * Builds the resolver used by Freemarker to resolve resources | |
| 213 | * | |
| 214 | * @return the resource resolver | |
| 215 | */ | |
| 216 | public FirstSupportingResourceResolver buildResolver() { | |
| 217 |
1
1. buildResolver : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::buildResolver → RUN_ERROR |
return buildContext.register(new FirstSupportingResourceResolver(buildResolvers())); |
| 218 | } | |
| 219 | ||
| 220 | private Configuration buildConfiguration() { | |
| 221 | Configuration builtConfiguration; | |
| 222 |
1
1. buildConfiguration : negated conditional → RUN_ERROR |
if (this.configuration != null) { |
| 223 | builtConfiguration = this.configuration; | |
| 224 |
1
1. buildConfiguration : negated conditional → RUN_ERROR |
} else if (configurationBuilder != null) { |
| 225 | builtConfiguration = configurationBuilder.build(); | |
| 226 | } else { | |
| 227 | builtConfiguration = new Configuration(DEFAULT_INCOMPATIBLE_IMPROVEMENTS); | |
| 228 |
1
1. buildConfiguration : removed call to freemarker/template/Configuration::setDefaultEncoding → NO_COVERAGE |
builtConfiguration.setDefaultEncoding("UTF-8"); |
| 229 |
1
1. buildConfiguration : removed call to freemarker/template/Configuration::setTemplateExceptionHandler → NO_COVERAGE |
builtConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
| 230 | } | |
| 231 | FirstSupportingResourceResolver builtResolver = buildResolver(); | |
| 232 | FirstSupportingResolverAdapter builtAdapter = buildAdapter(); | |
| 233 |
1
1. buildConfiguration : removed call to freemarker/template/Configuration::setTemplateLoader → RUN_ERROR |
builtConfiguration.setTemplateLoader(buildContext.register(new FreeMarkerFirstSupportingTemplateLoader(builtResolver, builtAdapter))); |
| 234 |
1
1. buildConfiguration : removed call to freemarker/template/Configuration::setTemplateLookupStrategy → RUN_ERROR |
builtConfiguration.setTemplateLookupStrategy(buildContext.register(new SkipLocaleForStringContentTemplateLookupStrategy(builtConfiguration.getTemplateLookupStrategy(), builtResolver, builtAdapter))); |
| 235 |
1
1. buildConfiguration : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::buildConfiguration → RUN_ERROR |
return builtConfiguration; |
| 236 | } | |
| 237 | ||
| 238 | protected List<ResourceResolver> buildResolvers() { | |
| 239 |
1
1. buildResolvers : removed call to fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::initResolutionBuilder → RUN_ERROR |
initResolutionBuilder(); |
| 240 |
1
1. buildResolvers : replaced return value with Collections.emptyList for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::buildResolvers → RUN_ERROR |
return resourceResolutionBuilderHelper.buildResolvers(); |
| 241 | } | |
| 242 | ||
| 243 | protected FirstSupportingResolverAdapter buildAdapter() { | |
| 244 | FirstSupportingResolverAdapter adapter = new FirstSupportingResolverAdapter(); | |
| 245 | for (TemplateLoaderAdapter custom : customAdapters) { | |
| 246 |
1
1. buildAdapter : removed call to fr/sii/ogham/template/freemarker/adapter/FirstSupportingResolverAdapter::addAdapter → NO_COVERAGE |
adapter.addAdapter(custom); |
| 247 | } | |
| 248 |
1
1. buildAdapter : removed call to fr/sii/ogham/template/freemarker/adapter/FirstSupportingResolverAdapter::addAdapter → RUN_ERROR |
adapter.addAdapter(buildContext.register(new ClassPathResolverAdapter(classLoader))); |
| 249 |
1
1. buildAdapter : removed call to fr/sii/ogham/template/freemarker/adapter/FirstSupportingResolverAdapter::addAdapter → RUN_ERROR |
adapter.addAdapter(buildContext.register(new FileResolverAdapter())); |
| 250 |
1
1. buildAdapter : removed call to fr/sii/ogham/template/freemarker/adapter/FirstSupportingResolverAdapter::addAdapter → RUN_ERROR |
adapter.addAdapter(buildContext.register(new StringResolverAdapter())); |
| 251 |
1
1. buildAdapter : removed call to fr/sii/ogham/template/freemarker/adapter/FirstSupportingResolverAdapter::setOptions → RUN_ERROR |
adapter.setOptions(buildContext.register(new TemplateLoaderOptions())); |
| 252 |
1
1. buildAdapter : replaced return value with null for fr/sii/ogham/template/freemarker/builder/AbstractFreemarkerBuilder::buildAdapter → RUN_ERROR |
return adapter; |
| 253 | } | |
| 254 | ||
| 255 | private void initResolutionBuilder() { | |
| 256 |
1
1. initResolutionBuilder : negated conditional → RUN_ERROR |
if (resourceResolutionBuilderHelper == null) { |
| 257 | resourceResolutionBuilderHelper = new ResourceResolutionBuilderHelper<>(myself, buildContext); | |
| 258 | } | |
| 259 | } | |
| 260 | } | |
Mutations | ||
| 71 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 88 |
1.1 |
|
| 89 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 116 |
1.1 |
|
| 125 |
1.1 |
|
| 128 |
1.1 |
|
| 149 |
1.1 |
|
| 168 |
1.1 |
|
| 189 |
1.1 |
|
| 195 |
1.1 |
|
| 200 |
1.1 2.2 |
|
| 206 |
1.1 |
|
| 207 |
1.1 |
|
| 208 |
1.1 |
|
| 217 |
1.1 |
|
| 222 |
1.1 |
|
| 224 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |
|
| 233 |
1.1 |
|
| 234 |
1.1 |
|
| 235 |
1.1 |
|
| 239 |
1.1 |
|
| 240 |
1.1 |
|
| 246 |
1.1 |
|
| 248 |
1.1 |
|
| 249 |
1.1 |
|
| 250 |
1.1 |
|
| 251 |
1.1 |
|
| 252 |
1.1 |
|
| 256 |
1.1 |