| 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 |
|
|
| 24 |
|
|
| |
|
| 80% |
Uncovered Elements: 6 (30) |
Complexity: 8 |
Complexity Density: 0.36 |
|
| 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 |
|
|
| |
|
| 84.6% |
Uncovered Elements: 4 (26) |
Complexity: 5 |
Complexity Density: 0.23 |
|
| 32 |
48 |
@Override... |
| 33 |
|
public ReplacedElement createReplacedElement(final LayoutContext c, final BlockBox box, |
| 34 |
|
final UserAgentCallback uac, final int cssWidth, final int cssHeight) { |
| 35 |
48 |
Element e = box.getElement(); |
| 36 |
48 |
String nodeName = e.getNodeName(); |
| 37 |
48 |
if (nodeName.equals("img")) { |
| 38 |
1 |
String imagePath = e.getAttribute("src"); |
| 39 |
1 |
InputStream input = null; |
| 40 |
1 |
try { |
| 41 |
1 |
LOGGER.log("imagePath: " + imagePath, LogLevel.DEBUG); |
| 42 |
1 |
LOGGER.log("File exist: " + new File(imagePath).exists(), LogLevel.DEBUG); |
| 43 |
|
|
| 44 |
1 |
input = new FileInputStream(new File(imagePath)); |
| 45 |
1 |
byte[] bytes = IOUtils.toByteArray(input); |
| 46 |
1 |
input.close(); |
| 47 |
1 |
FSImage fsImage = new ITextFSImage( |
| 48 |
|
Image.getInstance(bytes) |
| 49 |
|
); |
| 50 |
1 |
LOGGER.log("IMAGE: " + fsImage.toString(), LogLevel.DEBUG); |
| 51 |
|
|
| 52 |
1 |
if (cssWidth != -1 || cssHeight != -1) { |
| 53 |
0 |
this.height = cssWidth; |
| 54 |
0 |
this.widht = cssHeight; |
| 55 |
|
} else { |
| 56 |
1 |
this.height = fsImage.getWidth(); |
| 57 |
1 |
this.widht = fsImage.getHeight(); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
1 |
LOGGER.log("Image Dimension = H:" + this.height + " W:" + this.widht, |
| 61 |
|
LogLevel.DEBUG); |
| 62 |
|
|
| 63 |
1 |
return new ITextImageElement(fsImage); |
| 64 |
|
} catch (Exception ex) { |
| 65 |
0 |
LOGGER.catchException(ex); |
| 66 |
|
} |
| 67 |
|
} |
| 68 |
47 |
return null; |
| 69 |
|
} |
| 70 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 71 |
3 |
@Override... |
| 72 |
|
public void reset() { |
| 73 |
|
|
| 74 |
|
} |
| 75 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 76 |
0 |
@Override... |
| 77 |
|
public void remove(final Element e) { |
| 78 |
|
|
| 79 |
|
} |
| 80 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 81 |
0 |
@Override... |
| 82 |
|
public void setFormSubmissionListener(final FormSubmissionListener listener) { |
| 83 |
|
|
| 84 |
|
} |
| 85 |
|
} |