1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 /**
36  * Rapport pdf (avec iText v2.1.7).
37  * @author Emeric Vernat
38  */

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         // Rq: le constructeur de IOException avec message et cause n'existe qu'en jdk 1.6
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             // il serait possible d'ouvrir la boîte de dialogue Imprimer de Adobe Reader
87             //              if (writer instanceof PdfWriter) {
88             //                ((PdfWriter) writer).addJavaScript("this.print(true);"false);
89             //              }
90
91             pdfCoreReport.toPdf();
92         } catch (final DocumentException e) {
93             throw createIOException(e);
94         }
95
96         document.close();
97     }
98
99     // cette méthode est utilisée dans l'ihm Swing
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     // cette méthode est utilisée dans l'ihm Swing
106     public void setCounterRange(Range counterRange) {
107         pdfCoreReport.setCounterRange(counterRange);
108     }
109
110     // cette méthode est utilisée dans l'ihm Swing
111     public void setCurrentRequests(List<CounterRequestContext> currentRequests) {
112         pdfCoreReport.setCurrentRequests(currentRequests);
113     }
114
115     // cette méthode est utilisée dans l'ihm Swing
116     public void close() throws IOException {
117         output.close();
118     }
119 }
120
by