1
18 package net.bull.javamelody.internal.web.pdf;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.List;
23 import java.util.Map;
24
25 import com.lowagie.text.Document;
26 import com.lowagie.text.DocumentException;
27
28 import net.bull.javamelody.internal.common.I18N;
29 import net.bull.javamelody.internal.model.Collector;
30 import net.bull.javamelody.internal.model.CounterRequestContext;
31 import net.bull.javamelody.internal.model.JavaInformations;
32 import net.bull.javamelody.internal.model.Period;
33 import net.bull.javamelody.internal.model.Range;
34
35
39 public class PdfReport {
40 private final Document document;
41 private final OutputStream output;
42 private final PdfCoreReport pdfCoreReport;
43
44 public PdfReport(Collector collector, boolean collectorServer,
45 List<JavaInformations> javaInformationsList, Range range, OutputStream output)
46 throws IOException {
47 super();
48 assert output != null;
49 this.output = output;
50
51 try {
52 final PdfDocumentFactory pdfDocumentFactory = new PdfDocumentFactory(
53 collector.getApplication(), range, output);
54 this.document = pdfDocumentFactory.createDocument();
55 this.pdfCoreReport = new PdfCoreReport(collector, collectorServer, javaInformationsList,
56 range, pdfDocumentFactory, document);
57 } catch (final DocumentException e) {
58 throw createIOException(e);
59 }
60 }
61
62 public PdfReport(Collector collector, boolean collectorServer,
63 List<JavaInformations> javaInformationsList, Period period, OutputStream output)
64 throws IOException {
65 this(collector, collectorServer, javaInformationsList, period.getRange(), output);
66 }
67
68 private static IOException createIOException(DocumentException e) {
69
70 return new IOException(e.getMessage(), e);
71 }
72
73 public static String getFileName(String application) {
74 return "JavaMelody_" + application.replace(' ', '_').replace("/", "") + '_'
75 + I18N.getCurrentDate().replace('/', '_') + ".pdf";
76 }
77
78 public static boolean shouldUseEnglishInsteadOfUkrainian() {
79 return PdfFonts.shouldUseEnglishInsteadOfUkrainian();
80 }
81
82 public void toPdf() throws IOException {
83 try {
84 document.open();
85
86
87
88
89
90
91 pdfCoreReport.toPdf();
92 } catch (final DocumentException e) {
93 throw createIOException(e);
94 }
95
96 document.close();
97 }
98
99
100 public void preInitGraphs(Map<String, byte[]> newSmallGraphs,
101 Map<String, byte[]> newSmallOtherGraphs, Map<String, byte[]> newLargeGraphs) {
102 pdfCoreReport.preInitGraphs(newSmallGraphs, newSmallOtherGraphs, newLargeGraphs);
103 }
104
105
106 public void setCounterRange(Range counterRange) {
107 pdfCoreReport.setCounterRange(counterRange);
108 }
109
110
111 public void setCurrentRequests(List<CounterRequestContext> currentRequests) {
112 pdfCoreReport.setCurrentRequests(currentRequests);
113 }
114
115
116 public void close() throws IOException {
117 output.close();
118 }
119 }
120