1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application.internal

File PdfReplacedElementFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
93% of files have more coverage

Code metrics

8
25
5
1
92
76
12
0.48
5
5
2.4

Classes

Class Line # Actions
PdfReplacedElementFactory 26 25 0% 12 29
0.2368421123.7%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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    * Helper class to replace elements fürm HTML content to include them in PDF.
25    */
 
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   
 
33  6 toggle @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   
 
65  0 toggle private FSImage getImageInstance(final String imagePath)
66    throws IOException, BadElementException {
67  0 InputStream input = null;
68  0 FSImage fsImage;
69    // Removing "../" from image path like "../images/ExceptionPropagation.png"
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   
 
78  2 toggle @Override
79    public void reset() {
80    // Auto-generated method stub
81    }
82   
 
83  0 toggle @Override
84    public void remove(final Element e) {
85    // Auto-generated method stub
86    }
87   
 
88  0 toggle @Override
89    public void setFormSubmissionListener(final FormSubmissionListener listener) {
90    // Auto-generated method stub
91    }
92    }