1 |
|
package org.europa.together.application.internal; |
2 |
|
|
3 |
|
import java.io.FileInputStream; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStream; |
6 |
|
import org.w3c.dom.Element; |
7 |
|
import org.xhtmlrenderer.extend.FSImage; |
8 |
|
import org.xhtmlrenderer.extend.ReplacedElement; |
9 |
|
import org.xhtmlrenderer.extend.ReplacedElementFactory; |
10 |
|
import org.xhtmlrenderer.extend.UserAgentCallback; |
11 |
|
import org.xhtmlrenderer.layout.LayoutContext; |
12 |
|
import org.xhtmlrenderer.pdf.ITextFSImage; |
13 |
|
import org.xhtmlrenderer.pdf.ITextImageElement; |
14 |
|
import org.xhtmlrenderer.render.BlockBox; |
15 |
|
import org.xhtmlrenderer.simple.extend.FormSubmissionListener; |
16 |
|
import com.lowagie.text.BadElementException; |
17 |
|
import com.lowagie.text.Image; |
18 |
|
import org.europa.together.application.LogbackLogger; |
19 |
|
import org.europa.together.business.Logger; |
20 |
|
import org.europa.together.domain.LogLevel; |
21 |
|
import org.europa.together.utils.FileUtils; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
|
|
| 23.7% |
Uncovered Elements: 29 (38) |
Complexity: 12 |
Complexity Density: 0.48 |
|
26 |
|
public class PdfReplacedElementFactory implements ReplacedElementFactory { |
27 |
|
|
28 |
|
private static final Logger LOGGER = new LogbackLogger(PdfReplacedElementFactory.class); |
29 |
|
|
30 |
|
private final int height = 250; |
31 |
|
private final int width = 150; |
32 |
|
|
|
|
| 26.9% |
Uncovered Elements: 19 (26) |
Complexity: 8 |
Complexity Density: 0.44 |
|
33 |
6 |
@Override... |
34 |
|
public ReplacedElement createReplacedElement(final LayoutContext c, final BlockBox box, |
35 |
|
final UserAgentCallback uac, final int cssWidth, final int cssHeight) { |
36 |
6 |
Element e = box.getElement(); |
37 |
6 |
if (e == null) { |
38 |
0 |
return null; |
39 |
|
} |
40 |
6 |
String nodeName = e.getNodeName(); |
41 |
6 |
if (nodeName.equals("img")) { |
42 |
0 |
String imagePath = e.getAttribute("src"); |
43 |
0 |
LOGGER.log("imagePath-- " + imagePath.substring(imagePath.indexOf("/") + 1), |
44 |
|
LogLevel.DEBUG); |
45 |
0 |
FSImage fsImage; |
46 |
0 |
try { |
47 |
0 |
fsImage = getImageInstance(imagePath); |
48 |
|
} catch (BadElementException e1) { |
49 |
0 |
fsImage = null; |
50 |
|
} catch (IOException e1) { |
51 |
0 |
fsImage = null; |
52 |
|
} |
53 |
0 |
if (fsImage != null) { |
54 |
0 |
if (cssWidth != -1 || cssHeight != -1) { |
55 |
0 |
fsImage.scale(cssWidth, cssHeight); |
56 |
|
} else { |
57 |
0 |
fsImage.scale(height, width); |
58 |
|
} |
59 |
0 |
return new ITextImageElement(fsImage); |
60 |
|
} |
61 |
|
} |
62 |
6 |
return null; |
63 |
|
} |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
65 |
0 |
private FSImage getImageInstance(final String imagePath)... |
66 |
|
throws IOException, BadElementException { |
67 |
0 |
InputStream input = null; |
68 |
0 |
FSImage fsImage; |
69 |
|
|
70 |
0 |
input = new FileInputStream(getClass().getClassLoader().getResource( |
71 |
|
imagePath.substring(imagePath.indexOf("/") + 1)).getFile()); |
72 |
0 |
final byte[] bytes = FileUtils.inputStreamToByteArray(input); |
73 |
0 |
final Image image = Image.getInstance(bytes); |
74 |
0 |
fsImage = new ITextFSImage(image); |
75 |
0 |
return fsImage; |
76 |
|
} |
77 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
78 |
2 |
@Override... |
79 |
|
public void reset() { |
80 |
|
|
81 |
|
} |
82 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
83 |
0 |
@Override... |
84 |
|
public void remove(final Element e) { |
85 |
|
|
86 |
|
} |
87 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
88 |
0 |
@Override... |
89 |
|
public void setFormSubmissionListener(final FormSubmissionListener listener) { |
90 |
|
|
91 |
|
} |
92 |
|
} |