]> gerrit.simantics Code Review - simantics/platform.git/blob
c513654df6aae68705f78f3eeb3ca1d232d20e84
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.export.core.pdf;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Collections;\r
16 import java.util.HashMap;\r
17 import java.util.Map;\r
18 import java.util.function.Consumer;\r
19 \r
20 import org.osgi.framework.BundleContext;\r
21 import org.osgi.framework.InvalidSyntaxException;\r
22 import org.osgi.framework.ServiceReference;\r
23 import org.simantics.Logger;\r
24 import org.simantics.export.core.internal.Activator;\r
25 \r
26 import com.lowagie.text.Document;\r
27 import com.lowagie.text.Paragraph;\r
28 import com.lowagie.text.Rectangle;\r
29 import com.lowagie.text.pdf.PdfPageEventHelper;\r
30 import com.lowagie.text.pdf.PdfWriter;\r
31 \r
32 /**\r
33  * @author Tuukka Lehtonen\r
34  * @since 1.22.2\r
35  */\r
36 public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper {\r
37 \r
38         Map<String, PdfExportPageEvent> events;\r
39         \r
40         public ServiceBasedPdfExportPageEvent() {\r
41                 // Read all page event contributions from the OSGi service\r
42                 events = collectEvents();\r
43         }\r
44 \r
45         private static Map<String, PdfExportPageEvent> collectEvents() {\r
46                 BundleContext context = Activator.getContext();\r
47                 Collection<ServiceReference<PdfExportPageEvent>> serviceReferences;\r
48                 try {\r
49                         serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null);\r
50                 } catch (InvalidSyntaxException e) {\r
51                         e.printStackTrace();\r
52                         serviceReferences = Collections.emptyList();\r
53                 }\r
54                 Map<String, PdfExportPageEvent> events = new HashMap<>(serviceReferences.size());\r
55                 for (ServiceReference<PdfExportPageEvent> reference : serviceReferences) {\r
56                         PdfExportPageEvent event = context.getService(reference);\r
57                         String eventName = event.toString();\r
58                         events.put(eventName, event);\r
59                 }\r
60                 return events;\r
61         }\r
62 \r
63         private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer<PdfExportPageEvent> r) {\r
64                 try {\r
65                         r.accept(event);\r
66                 } catch (Exception e) {\r
67                         Logger.defaultLogError("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e);\r
68                 }\r
69         }\r
70 \r
71         @Override\r
72         public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {\r
73                 events.values().forEach(e -> safeInvoke("onChapter", e, ee -> ee.onChapter(writer, document, paragraphPosition, title)));\r
74         }\r
75 \r
76         @Override\r
77         public void onChapterEnd(PdfWriter writer, Document document, float position) {\r
78                 events.values().forEach(e -> safeInvoke("onChapterEnd", e, ee -> ee.onChapterEnd(writer, document, position)));\r
79         }\r
80 \r
81         @Override\r
82         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {\r
83                 events.values().forEach(e -> safeInvoke("onGenericTag", e, ee -> ee.onGenericTag(writer, document, rect, text)));\r
84         }\r
85 \r
86         @Override\r
87         public void onOpenDocument(PdfWriter writer, Document document) {\r
88                 events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document)));\r
89         }\r
90 \r
91         @Override\r
92         public void onCloseDocument(PdfWriter writer, Document document) {\r
93                 events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document)));\r
94         }\r
95 \r
96         @Override\r
97         public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {\r
98                 events.values().forEach(e -> safeInvoke("onParagraph", e, ee -> ee.onParagraph(writer, document, paragraphPosition)));\r
99         }\r
100 \r
101         @Override\r
102         public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {\r
103                 events.values().forEach(e -> safeInvoke("onParagraphEnd", e, ee -> ee.onParagraphEnd(writer, document, paragraphPosition)));\r
104         }\r
105 \r
106         @Override\r
107         public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {\r
108                 events.values().forEach(e -> safeInvoke("onSection", e, ee -> ee.onSection(writer, document, paragraphPosition, depth, title)));\r
109         }\r
110 \r
111         @Override\r
112         public void onSectionEnd(PdfWriter writer, Document document, float position) {\r
113                 events.values().forEach(e -> safeInvoke("onSectionEnd", e, ee -> ee.onSectionEnd(writer, document, position)));\r
114         }\r
115 \r
116         @Override\r
117         public void onStartPage(PdfWriter writer, Document document) {\r
118                 events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document)));\r
119         }\r
120 \r
121         @Override\r
122         public void onEndPage(PdfWriter writer, Document document) {\r
123                 events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document)));\r
124         }\r
125 \r
126 }\r