--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2016 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.export.core.pdf;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.function.Consumer;\r
+\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.InvalidSyntaxException;\r
+import org.osgi.framework.ServiceReference;\r
+import org.simantics.Logger;\r
+import org.simantics.export.core.internal.Activator;\r
+\r
+import com.lowagie.text.Document;\r
+import com.lowagie.text.Paragraph;\r
+import com.lowagie.text.Rectangle;\r
+import com.lowagie.text.pdf.PdfPageEventHelper;\r
+import com.lowagie.text.pdf.PdfWriter;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @since 1.22.2\r
+ */\r
+public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper {\r
+\r
+ Map<String, PdfExportPageEvent> events;\r
+ \r
+ public ServiceBasedPdfExportPageEvent() {\r
+ // Read all page event contributions from the OSGi service\r
+ events = collectEvents();\r
+ }\r
+\r
+ private static Map<String, PdfExportPageEvent> collectEvents() {\r
+ BundleContext context = Activator.getContext();\r
+ Collection<ServiceReference<PdfExportPageEvent>> serviceReferences;\r
+ try {\r
+ serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null);\r
+ } catch (InvalidSyntaxException e) {\r
+ e.printStackTrace();\r
+ serviceReferences = Collections.emptyList();\r
+ }\r
+ Map<String, PdfExportPageEvent> events = new HashMap<>(serviceReferences.size());\r
+ for (ServiceReference<PdfExportPageEvent> reference : serviceReferences) {\r
+ PdfExportPageEvent event = context.getService(reference);\r
+ String eventName = event.toString();\r
+ events.put(eventName, event);\r
+ }\r
+ return events;\r
+ }\r
+\r
+ private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer<PdfExportPageEvent> r) {\r
+ try {\r
+ r.accept(event);\r
+ } catch (Exception e) {\r
+ Logger.defaultLogError("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {\r
+ events.values().forEach(e -> safeInvoke("onChapter", e, ee -> ee.onChapter(writer, document, paragraphPosition, title)));\r
+ }\r
+\r
+ @Override\r
+ public void onChapterEnd(PdfWriter writer, Document document, float position) {\r
+ events.values().forEach(e -> safeInvoke("onChapterEnd", e, ee -> ee.onChapterEnd(writer, document, position)));\r
+ }\r
+\r
+ @Override\r
+ public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {\r
+ events.values().forEach(e -> safeInvoke("onGenericTag", e, ee -> ee.onGenericTag(writer, document, rect, text)));\r
+ }\r
+\r
+ @Override\r
+ public void onOpenDocument(PdfWriter writer, Document document) {\r
+ events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document)));\r
+ }\r
+\r
+ @Override\r
+ public void onCloseDocument(PdfWriter writer, Document document) {\r
+ events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document)));\r
+ }\r
+\r
+ @Override\r
+ public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {\r
+ events.values().forEach(e -> safeInvoke("onParagraph", e, ee -> ee.onParagraph(writer, document, paragraphPosition)));\r
+ }\r
+\r
+ @Override\r
+ public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {\r
+ events.values().forEach(e -> safeInvoke("onParagraphEnd", e, ee -> ee.onParagraphEnd(writer, document, paragraphPosition)));\r
+ }\r
+\r
+ @Override\r
+ public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {\r
+ events.values().forEach(e -> safeInvoke("onSection", e, ee -> ee.onSection(writer, document, paragraphPosition, depth, title)));\r
+ }\r
+\r
+ @Override\r
+ public void onSectionEnd(PdfWriter writer, Document document, float position) {\r
+ events.values().forEach(e -> safeInvoke("onSectionEnd", e, ee -> ee.onSectionEnd(writer, document, position)));\r
+ }\r
+\r
+ @Override\r
+ public void onStartPage(PdfWriter writer, Document document) {\r
+ events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document)));\r
+ }\r
+\r
+ @Override\r
+ public void onEndPage(PdfWriter writer, Document document) {\r
+ events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document)));\r
+ }\r
+\r
+}\r