| 1 | package org.europa.together.application; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | import java.io.FileNotFoundException; | |
| 5 | import java.io.FileOutputStream; | |
| 6 | import java.io.IOException; | |
| 7 | import java.io.OutputStream; | |
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.List; | |
| 10 | import org.europa.together.application.internal.PdfDocument; | |
| 11 | import org.europa.together.application.internal.PdfReplacedElementFactory; | |
| 12 | import org.europa.together.business.Logger; | |
| 13 | import org.europa.together.business.PdfRenderer; | |
| 14 | import org.europa.together.domain.LogLevel; | |
| 15 | import org.europa.together.utils.StringUtils; | |
| 16 | import org.jsoup.Jsoup; | |
| 17 | import org.jsoup.nodes.Document; | |
| 18 | import org.openpdf.text.pdf.PdfStamper; | |
| 19 | import org.springframework.stereotype.Repository; | |
| 20 | import org.xhtmlrenderer.layout.SharedContext; | |
| 21 | import org.xhtmlrenderer.pdf.ITextRenderer; | |
| 22 | ||
| 23 | /** | |
| 24 | * Implementation of a simple PDF Renderer. | |
| 25 | * <br> | |
| 26 | * https://knpcode.com/java-programs/generating-pdf-java-using-openpdf-tutorial/#HelloWorldOpenPDF | |
| 27 | */ | |
| 28 | @Repository | |
| 29 | public class OpenPdfRenderer implements PdfRenderer { | |
| 30 | ||
| 31 | private static final long serialVersionUID = 11L; | |
| 32 | private static final Logger LOGGER = new LogbackLogger(OpenPdfRenderer.class); | |
| 33 | ||
| 34 | //A4 | |
| 35 | private String format = "size: 21cm 29.7cm; margin: 20mm 20mm;"; | |
| 36 | ||
| 37 | private String title = ""; | |
| 38 | private String subject = ""; | |
| 39 | private String author = ""; | |
| 40 | private String keywords = ""; | |
| 41 | ||
| 42 | @Override | |
| 43 | public PdfDocument loadDocument(final File pdfDocument) | |
| 44 | throws IOException { | |
| 45 |
1
1. loadDocument : negated conditional → KILLED |
if (!pdfDocument.exists()) { |
| 46 | throw new FileNotFoundException(); | |
| 47 | } | |
| 48 |
1
1. loadDocument : replaced return value with null for org/europa/together/application/OpenPdfRenderer::loadDocument → KILLED |
return new PdfDocument(pdfDocument.getAbsolutePath()); |
| 49 | } | |
| 50 | ||
| 51 | @Override | |
| 52 | public void writeDocument(final PdfDocument pdf, final String destination) | |
| 53 | throws IOException, FileNotFoundException { | |
| 54 | PdfStamper pdfStamper = new PdfStamper(pdf, new FileOutputStream(destination)); | |
| 55 |
1
1. writeDocument : removed call to org/openpdf/text/pdf/PdfStamper::close → SURVIVED |
pdfStamper.close(); |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | public PdfDocument removePage(final PdfDocument pdf, final int... pages) | |
| 60 | throws IOException { | |
| 61 | PdfDocument newPDF = new PdfDocument(pdf); | |
| 62 | int pagesTotal = newPDF.getNumberOfPages(); | |
| 63 | List<Integer> allPages = new ArrayList<>(pagesTotal); | |
| 64 |
2
1. removePage : changed conditional boundary → SURVIVED 2. removePage : negated conditional → KILLED |
for (int i = 1; i <= pagesTotal; i++) { |
| 65 | allPages.add(i); | |
| 66 | } | |
| 67 | for (Integer page : pages) { | |
| 68 | allPages.remove(page); | |
| 69 | } | |
| 70 |
1
1. removePage : removed call to org/europa/together/application/internal/PdfDocument::selectPages → KILLED |
newPDF.selectPages(allPages); |
| 71 | LOGGER.log("Document contains " + newPDF.getNumberOfPages() + " Pages", | |
| 72 | LogLevel.DEBUG); | |
| 73 |
1
1. removePage : replaced return value with null for org/europa/together/application/OpenPdfRenderer::removePage → KILLED |
return newPDF; |
| 74 | } | |
| 75 | ||
| 76 | @Override | |
| 77 | public void renderDocumentFromHtml(final String file, final String template) | |
| 78 | throws FileNotFoundException, IOException { | |
| 79 | StringBuilder html = new StringBuilder(); | |
| 80 | html.append("<html>") | |
| 81 | .append("<head>") | |
| 82 | .append("<meta charset=\"UTF-8\" />") | |
| 83 | .append("<title>").append( | |
| 84 | StringUtils.escapeXmlCharacters(getTitle())).append("</title>") | |
| 85 | .append("<meta name=\"author\" content=\"").append( | |
| 86 | StringUtils.escapeXmlCharacters(getAuthor())).append("\" />") | |
| 87 | .append("<meta name=\"subject\" content=\"").append( | |
| 88 | StringUtils.escapeXmlCharacters(getSubject())).append("\" />") | |
| 89 | .append("<meta name=\"keywords\" content=\"").append( | |
| 90 | StringUtils.escapeXmlCharacters(getKeywords())).append("\" />") | |
| 91 | .append("<style>") | |
| 92 | .append("@page {").append(format).append("}") | |
| 93 | .append("</style>") | |
| 94 | .append("</head>") | |
| 95 | .append("<body>") | |
| 96 | .append(template) | |
| 97 | .append("</body></html>"); | |
| 98 | ||
| 99 | Document document = Jsoup.parse(html.toString(), "UTF-8"); | |
| 100 | document.outputSettings().syntax(Document.OutputSettings.Syntax.xml); | |
| 101 | LOGGER.log("test", LogLevel.DEBUG); | |
| 102 | try (OutputStream os = new FileOutputStream(file)) { | |
| 103 | ITextRenderer renderer = new ITextRenderer(); | |
| 104 | SharedContext sharedContext = renderer.getSharedContext(); | |
| 105 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/layout/SharedContext::setPrint → SURVIVED |
sharedContext.setPrint(true); |
| 106 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/layout/SharedContext::setInteractive → SURVIVED |
sharedContext.setInteractive(false); |
| 107 | //IMAGE | |
| 108 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/layout/SharedContext::setReplacedElementFactory → KILLED |
sharedContext.setReplacedElementFactory(new PdfReplacedElementFactory()); |
| 109 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/extend/TextRenderer::setSmoothingThreshold → SURVIVED |
sharedContext.getTextRenderer().setSmoothingThreshold(0); |
| 110 | ||
| 111 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/pdf/ITextRenderer::setDocumentFromString → KILLED |
renderer.setDocumentFromString(createWellFormedHtml(html.toString())); |
| 112 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/pdf/ITextRenderer::layout → KILLED |
renderer.layout(); |
| 113 |
1
1. renderDocumentFromHtml : removed call to org/xhtmlrenderer/pdf/ITextRenderer::createPDF → KILLED |
renderer.createPDF(os); |
| 114 | } | |
| 115 | } | |
| 116 | ||
| 117 | //<editor-fold defaultstate="collapsed" desc="Getter / Setter"> | |
| 118 | @Override | |
| 119 | public void setAuthor(final String author) { | |
| 120 | this.author = author; | |
| 121 | } | |
| 122 | ||
| 123 | @Override | |
| 124 | public void setKeywords(final String keywords) { | |
| 125 | this.keywords = keywords; | |
| 126 | } | |
| 127 | ||
| 128 | @Override | |
| 129 | public void setSubject(final String subject) { | |
| 130 | this.subject = subject; | |
| 131 | } | |
| 132 | ||
| 133 | @Override | |
| 134 | public void setTitle(final String title) { | |
| 135 | this.title = title; | |
| 136 | } | |
| 137 | ||
| 138 | @Override | |
| 139 | public void setFormat(final String format) { | |
| 140 | this.format = format; | |
| 141 | } | |
| 142 | ||
| 143 | @Override | |
| 144 | public String getAuthor() { | |
| 145 |
1
1. getAuthor : replaced return value with "" for org/europa/together/application/OpenPdfRenderer::getAuthor → KILLED |
return author; |
| 146 | } | |
| 147 | ||
| 148 | @Override | |
| 149 | public String getKeywords() { | |
| 150 |
1
1. getKeywords : replaced return value with "" for org/europa/together/application/OpenPdfRenderer::getKeywords → KILLED |
return keywords; |
| 151 | } | |
| 152 | ||
| 153 | @Override | |
| 154 | public String getSubject() { | |
| 155 |
1
1. getSubject : replaced return value with "" for org/europa/together/application/OpenPdfRenderer::getSubject → KILLED |
return subject; |
| 156 | } | |
| 157 | ||
| 158 | @Override | |
| 159 | public String getTitle() { | |
| 160 |
1
1. getTitle : replaced return value with "" for org/europa/together/application/OpenPdfRenderer::getTitle → KILLED |
return title; |
| 161 | } | |
| 162 | ||
| 163 | @Override | |
| 164 | public String getFormat() { | |
| 165 |
1
1. getFormat : replaced return value with "" for org/europa/together/application/OpenPdfRenderer::getFormat → KILLED |
return format; |
| 166 | } | |
| 167 | //</editor-fold> | |
| 168 | ||
| 169 | private String createWellFormedHtml(final String html) { | |
| 170 | Document content = Jsoup.parse(html, "UTF-8"); | |
| 171 | content.outputSettings().syntax( | |
| 172 | Document.OutputSettings.Syntax.xml); | |
| 173 |
1
1. createWellFormedHtml : replaced return value with "" for org/europa/together/application/OpenPdfRenderer::createWellFormedHtml → KILLED |
return content.html(); |
| 174 | } | |
| 175 | ||
| 176 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 48 |
1.1 |
|
| 55 |
1.1 |
|
| 64 |
1.1 2.2 |
|
| 70 |
1.1 |
|
| 73 |
1.1 |
|
| 105 |
1.1 |
|
| 106 |
1.1 |
|
| 108 |
1.1 |
|
| 109 |
1.1 |
|
| 111 |
1.1 |
|
| 112 |
1.1 |
|
| 113 |
1.1 |
|
| 145 |
1.1 |
|
| 150 |
1.1 |
|
| 155 |
1.1 |
|
| 160 |
1.1 |
|
| 165 |
1.1 |
|
| 173 |
1.1 |