| 1 | package fr.sii.ogham.email.builder; | |
| 2 | ||
| 3 | import java.util.List; | |
| 4 | ||
| 5 | import org.slf4j.Logger; | |
| 6 | import org.slf4j.LoggerFactory; | |
| 7 | ||
| 8 | import fr.sii.ogham.core.builder.Builder; | |
| 9 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 10 | import fr.sii.ogham.core.builder.env.EnvironmentBuilder; | |
| 11 | import fr.sii.ogham.core.builder.resolution.ClassPathResolutionBuilder; | |
| 12 | import fr.sii.ogham.core.builder.resolution.FileResolutionBuilder; | |
| 13 | import fr.sii.ogham.core.builder.resolution.ResourceResolutionBuilder; | |
| 14 | import fr.sii.ogham.core.builder.resolution.ResourceResolutionBuilderHelper; | |
| 15 | import fr.sii.ogham.core.builder.resolution.StringResolutionBuilder; | |
| 16 | import fr.sii.ogham.core.fluent.AbstractParent; | |
| 17 | import fr.sii.ogham.core.resource.path.LookupAwareRelativePathResolver; | |
| 18 | import fr.sii.ogham.core.resource.path.RelativePathResolver; | |
| 19 | import fr.sii.ogham.core.resource.resolver.FirstSupportingResourceResolver; | |
| 20 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
| 21 | import fr.sii.ogham.core.translator.content.ContentTranslator; | |
| 22 | import fr.sii.ogham.html.inliner.CssInliner; | |
| 23 | import fr.sii.ogham.html.inliner.impl.jsoup.JsoupCssInliner; | |
| 24 | import fr.sii.ogham.html.translator.InlineCssTranslator; | |
| 25 | ||
| 26 | /** | |
| 27 | * Configures how CSS are applied on HTML emails. | |
| 28 | * | |
| 29 | * Inlining CSS means that CSS styles are loaded and applied on the matching | |
| 30 | * HTML nodes using the {@code style} HTML attribute. | |
| 31 | * | |
| 32 | * @author Aurélien Baudet | |
| 33 | * | |
| 34 | */ | |
| 35 | public class CssInliningBuilder extends AbstractParent<CssHandlingBuilder> implements ResourceResolutionBuilder<CssInliningBuilder>, Builder<ContentTranslator> { | |
| 36 | private static final Logger LOG = LoggerFactory.getLogger(CssInliningBuilder.class); | |
| 37 | ||
| 38 | private final BuildContext buildContext; | |
| 39 | private ResourceResolutionBuilderHelper<CssInliningBuilder> resourceResolutionBuilderHelper; | |
| 40 | private boolean useJsoup; | |
| 41 | ||
| 42 | /** | |
| 43 | * Initializes the builder with a parent builder. The parent builder is used | |
| 44 | * when calling {@link #and()} method. The {@link EnvironmentBuilder} is | |
| 45 | * used to evaluate properties when {@link #build()} method is called. | |
| 46 | * | |
| 47 | * @param parent | |
| 48 | * the parent builder | |
| 49 | * @param buildContext | |
| 50 | * for registering instances and property evaluation | |
| 51 | */ | |
| 52 | public CssInliningBuilder(CssHandlingBuilder parent, BuildContext buildContext) { | |
| 53 | super(parent); | |
| 54 | this.buildContext = buildContext; | |
| 55 | resourceResolutionBuilderHelper = new ResourceResolutionBuilderHelper<>(this, buildContext); | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Enable CSS inlining: CSS styles are loaded and applied on the matching | |
| 60 | * HTML nodes using the {@code style} HTML attribute. | |
| 61 | * | |
| 62 | * The implementation uses <a href="https://jsoup.org/">jsoup</a> to parse | |
| 63 | * HTML content. | |
| 64 | * | |
| 65 | * @return this instance for fluent chaining | |
| 66 | */ | |
| 67 | public CssInliningBuilder jsoup() { | |
| 68 | useJsoup = true; | |
| 69 |
1
1. jsoup : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::jsoup → RUN_ERROR |
return this; |
| 70 | } | |
| 71 | ||
| 72 | @Override | |
| 73 | public ClassPathResolutionBuilder<CssInliningBuilder> classpath() { | |
| 74 |
1
1. classpath : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::classpath → RUN_ERROR |
return resourceResolutionBuilderHelper.classpath(); |
| 75 | } | |
| 76 | ||
| 77 | @Override | |
| 78 | public FileResolutionBuilder<CssInliningBuilder> file() { | |
| 79 |
1
1. file : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::file → RUN_ERROR |
return resourceResolutionBuilderHelper.file(); |
| 80 | } | |
| 81 | ||
| 82 | @Override | |
| 83 | public StringResolutionBuilder<CssInliningBuilder> string() { | |
| 84 |
1
1. string : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::string → RUN_ERROR |
return resourceResolutionBuilderHelper.string(); |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public CssInliningBuilder resolver(ResourceResolver resolver) { | |
| 89 |
1
1. resolver : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::resolver → NO_COVERAGE |
return resourceResolutionBuilderHelper.resolver(resolver); |
| 90 | } | |
| 91 | ||
| 92 | @Override | |
| 93 | public ContentTranslator build() { | |
| 94 | CssInliner cssInliner = buildInliner(); | |
| 95 |
1
1. build : negated conditional → RUN_ERROR |
if (cssInliner == null) { |
| 96 | LOG.info("CSS won't be applied on HTML content of your emails because no inliner is configured"); | |
| 97 | return null; | |
| 98 | } | |
| 99 |
1
1. build : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::build → RUN_ERROR |
return buildContext.register(new InlineCssTranslator(cssInliner, buildResolver(), buildRelativePathProvider())); |
| 100 | } | |
| 101 | ||
| 102 | private CssInliner buildInliner() { | |
| 103 |
1
1. buildInliner : negated conditional → RUN_ERROR |
if (useJsoup) { |
| 104 |
1
1. buildInliner : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::buildInliner → RUN_ERROR |
return buildContext.register(new JsoupCssInliner()); |
| 105 | } | |
| 106 | return null; | |
| 107 | } | |
| 108 | ||
| 109 | private ResourceResolver buildResolver() { | |
| 110 | List<ResourceResolver> resolvers = resourceResolutionBuilderHelper.buildResolvers(); | |
| 111 |
1
1. buildResolver : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::buildResolver → RUN_ERROR |
return buildContext.register(new FirstSupportingResourceResolver(resolvers)); |
| 112 | } | |
| 113 | ||
| 114 | private RelativePathResolver buildRelativePathProvider() { | |
| 115 |
1
1. buildRelativePathProvider : replaced return value with null for fr/sii/ogham/email/builder/CssInliningBuilder::buildRelativePathProvider → RUN_ERROR |
return buildContext.register(new LookupAwareRelativePathResolver(resourceResolutionBuilderHelper.getAllLookups())); |
| 116 | } | |
| 117 | } | |
Mutations | ||
| 69 |
1.1 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 84 |
1.1 |
|
| 89 |
1.1 |
|
| 95 |
1.1 |
|
| 99 |
1.1 |
|
| 103 |
1.1 |
|
| 104 |
1.1 |
|
| 111 |
1.1 |
|
| 115 |
1.1 |