PdfReplacedElementFactory.java

1
package org.europa.together.application.internal;
2
3
import java.io.FileInputStream;
4
import java.io.InputStream;
5
import org.w3c.dom.Element;
6
import org.xhtmlrenderer.extend.FSImage;
7
import org.xhtmlrenderer.extend.ReplacedElement;
8
import org.xhtmlrenderer.extend.ReplacedElementFactory;
9
import org.xhtmlrenderer.extend.UserAgentCallback;
10
import org.xhtmlrenderer.layout.LayoutContext;
11
import org.xhtmlrenderer.pdf.ITextFSImage;
12
import org.xhtmlrenderer.pdf.ITextImageElement;
13
import org.xhtmlrenderer.render.BlockBox;
14
import org.xhtmlrenderer.simple.extend.FormSubmissionListener;
15
import java.io.File;
16
import org.apache.commons.io.IOUtils;
17
import org.europa.together.application.LogbackLogger;
18
import org.europa.together.business.Logger;
19
import org.europa.together.domain.LogLevel;
20
import org.openpdf.text.Image;
21
22
/**
23
 * Helper class to replace elements fürm HTML content to include them in PDF.
24
 */
25
public class PdfReplacedElementFactory implements ReplacedElementFactory {
26
27
    private static final Logger LOGGER = new LogbackLogger(PdfReplacedElementFactory.class);
28
29
    private int height;
30
    private int widht;
31
32
    @Override
33
    public ReplacedElement createReplacedElement(final LayoutContext c, final BlockBox box,
34
            final UserAgentCallback uac, final int cssWidth, final int cssHeight) {
35
        Element e = box.getElement();
36
        String nodeName = e.getNodeName();
37 1 1. createReplacedElement : negated conditional → KILLED
        if (nodeName.equals("img")) {
38
            String imagePath = e.getAttribute("src");
39
            InputStream input = null;
40
            try {
41
                LOGGER.log("imagePath: " + imagePath, LogLevel.DEBUG);
42
                LOGGER.log("File exist: " + new File(imagePath).exists(), LogLevel.DEBUG);
43
44
                input = new FileInputStream(new File(imagePath));
45
                byte[] bytes = IOUtils.toByteArray(input);
46 1 1. createReplacedElement : removed call to java/io/InputStream::close → SURVIVED
                input.close();
47
                FSImage fsImage = new ITextFSImage(
48
                        Image.getInstance(bytes)
49
                );
50
                LOGGER.log("IMAGE: " + fsImage.toString(), LogLevel.DEBUG);
51
52 2 1. createReplacedElement : negated conditional → SURVIVED
2. createReplacedElement : negated conditional → SURVIVED
                if (cssWidth != -1 || cssHeight != -1) {
53
                    this.height = cssWidth;
54
                    this.widht = cssHeight;
55
                } else {
56
                    this.height = fsImage.getWidth();
57
                    this.widht = fsImage.getHeight();
58
                }
59
//                fsImage.scale(this.widht, this.height);
60
                LOGGER.log("Image Dimension = H:" + this.height + " W:" + this.widht,
61
                        LogLevel.DEBUG);
62
63 1 1. createReplacedElement : replaced return value with null for org/europa/together/application/internal/PdfReplacedElementFactory::createReplacedElement → KILLED
                return new ITextImageElement(fsImage);
64
            } catch (Exception ex) {
65
                LOGGER.catchException(ex);
66
            }
67
        }
68
        return null;
69
    }
70
71
    @Override
72
    public void reset() {
73
        // Auto-generated method stub
74
    }
75
76
    @Override
77
    public void remove(final Element e) {
78
        // Auto-generated method stub
79
    }
80
81
    @Override
82
    public void setFormSubmissionListener(final FormSubmissionListener listener) {
83
        // Auto-generated method stub
84
    }
85
}

Mutations

37

1.1
Location : createReplacedElement
Killed by : org.europa.together.application.OpenPdfRendererTest.[engine:junit-jupiter]/[class:org.europa.together.application.OpenPdfRendererTest]/[method:includeLocalImages()]
negated conditional → KILLED

46

1.1
Location : createReplacedElement
Killed by : none
removed call to java/io/InputStream::close → SURVIVED
Covering tests

52

1.1
Location : createReplacedElement
Killed by : none
negated conditional → SURVIVED
Covering tests

2.2
Location : createReplacedElement
Killed by : none
negated conditional → SURVIVED Covering tests

63

1.1
Location : createReplacedElement
Killed by : org.europa.together.application.OpenPdfRendererTest.[engine:junit-jupiter]/[class:org.europa.together.application.OpenPdfRendererTest]/[method:includeLocalImages()]
replaced return value with null for org/europa/together/application/internal/PdfReplacedElementFactory::createReplacedElement → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0