JsoupBase64ImageInliner.java

1
package fr.sii.ogham.html.inliner.impl.jsoup;
2
3
import static fr.sii.ogham.html.inliner.ImageInlinerConstants.INLINED_ATTR;
4
import static fr.sii.ogham.html.inliner.ImageInlinerConstants.InlineModes.BASE64;
5
import static fr.sii.ogham.html.inliner.impl.jsoup.ImageInlineUtils.isInlineModeAllowed;
6
7
import java.text.MessageFormat;
8
import java.util.ArrayList;
9
import java.util.List;
10
11
import org.jsoup.Jsoup;
12
import org.jsoup.nodes.Document;
13
import org.jsoup.nodes.Element;
14
import org.jsoup.select.Elements;
15
16
import fr.sii.ogham.core.util.Base64Utils;
17
import fr.sii.ogham.html.inliner.ContentWithImages;
18
import fr.sii.ogham.html.inliner.ImageInliner;
19
import fr.sii.ogham.html.inliner.ImageInlinerConstants;
20
import fr.sii.ogham.html.inliner.ImageInlinerConstants.InlineModes;
21
import fr.sii.ogham.html.inliner.ImageResource;
22
23
/**
24
 * Image inliner that reads the image and converts it into a base64 string. The
25
 * string is then included directly in the HTML content using the src attribute
26
 * of img tag.
27
 * 
28
 * <p>
29
 * The inlining using base64 is only applied if the attribute
30
 * {@link ImageInlinerConstants#INLINE_MODE_ATTR} is set to
31
 * {@link InlineModes#BASE64}.
32
 * </p>
33
 * 
34
 * @author Aurélien Baudet
35
 *
36
 */
37
public class JsoupBase64ImageInliner implements ImageInliner {
38
	private static final String SRC_ATTR = "src";
39
	private static final String IMG_SELECTOR = "img[src=\"{0}\"]";
40
	private static final String BASE64_URI = "data:{0};base64,{1}";
41
42
	@Override
43
	public ContentWithImages inline(String htmlContent, List<ImageResource> images) {
44
		Document doc = Jsoup.parse(htmlContent);
45
		for (ImageResource image : images) {
46
			Elements imgs = getImagesToInline(doc, image);
47
			for (Element img : imgs) {
48
				img.attr(SRC_ATTR, MessageFormat.format(BASE64_URI, image.getMimetype(), Base64Utils.encodeToString(image.getContent())));
49
				img.attr(INLINED_ATTR, true);
50
			}
51
		}
52 3 1. inline : replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::inline → NO_COVERAGE
2. inline : replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::inline → KILLED
3. inline : replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::inline → KILLED
		return new ContentWithImages(doc.outerHtml(), new ArrayList<>(0));
53
	}
54
55
	private static Elements getImagesToInline(Document doc, ImageResource image) {
56
		Elements imgs = doc.select(MessageFormat.format(IMG_SELECTOR, image.getSrcUrl()));
57
		Elements found = new Elements();
58
		for (Element img : imgs) {
59
			// only apply inlining if mode matches
60 3 1. getImagesToInline : negated conditional → NO_COVERAGE
2. getImagesToInline : negated conditional → KILLED
3. getImagesToInline : negated conditional → KILLED
			if (isInlineModeAllowed(img, BASE64)) {
61
				found.add(img);
62
			}
63
		}
64 3 1. getImagesToInline : replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::getImagesToInline → NO_COVERAGE
2. getImagesToInline : replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::getImagesToInline → KILLED
3. getImagesToInline : replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::getImagesToInline → KILLED
		return found;
65
	}
66
}

Mutations

52

1.1
Location : inline
Killed by : oghamall.it.html.translator.JsoupInlineImageTranslatorTest.skipExternalImages(oghamall.it.html.translator.JsoupInlineImageTranslatorTest)
replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::inline → KILLED

2.2
Location : inline
Killed by : none
replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::inline → NO_COVERAGE

3.3
Location : inline
Killed by : oghamcore.ut.html.inliner.impl.JsoupBase64ImageInlinerTest.differentImageFormats(oghamcore.ut.html.inliner.impl.JsoupBase64ImageInlinerTest)
replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::inline → KILLED

60

1.1
Location : getImagesToInline
Killed by : oghamcore.ut.html.inliner.impl.JsoupBase64ImageInlinerTest.differentImageFormats(oghamcore.ut.html.inliner.impl.JsoupBase64ImageInlinerTest)
negated conditional → KILLED

2.2
Location : getImagesToInline
Killed by : oghamall.it.html.translator.JsoupInlineImageTranslatorTest.skipAttach(oghamall.it.html.translator.JsoupInlineImageTranslatorTest)
negated conditional → KILLED

3.3
Location : getImagesToInline
Killed by : none
negated conditional → NO_COVERAGE

64

1.1
Location : getImagesToInline
Killed by : oghamcore.ut.html.inliner.impl.JsoupBase64ImageInlinerTest.differentImageFormats(oghamcore.ut.html.inliner.impl.JsoupBase64ImageInlinerTest)
replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::getImagesToInline → KILLED

2.2
Location : getImagesToInline
Killed by : oghamall.it.html.translator.JsoupInlineImageTranslatorTest.skipExternalImages(oghamall.it.html.translator.JsoupInlineImageTranslatorTest)
replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::getImagesToInline → KILLED

3.3
Location : getImagesToInline
Killed by : none
replaced return value with null for fr/sii/ogham/html/inliner/impl/jsoup/JsoupBase64ImageInliner::getImagesToInline → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM