1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.export.core.pdf;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
18 import java.util.function.Consumer;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.InvalidSyntaxException;
22 import org.osgi.framework.ServiceReference;
23 import org.simantics.export.core.internal.Activator;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.lowagie.text.Document;
28 import com.lowagie.text.Paragraph;
29 import com.lowagie.text.Rectangle;
30 import com.lowagie.text.pdf.PdfPageEventHelper;
31 import com.lowagie.text.pdf.PdfWriter;
34 * @author Tuukka Lehtonen
37 public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper {
38 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBasedPdfExportPageEvent.class);
40 Map<String, PdfExportPageEvent> events;
42 public ServiceBasedPdfExportPageEvent() {
43 // Read all page event contributions from the OSGi service
44 events = collectEvents();
47 private static Map<String, PdfExportPageEvent> collectEvents() {
48 BundleContext context = Activator.getContext();
49 Collection<ServiceReference<PdfExportPageEvent>> serviceReferences;
51 serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null);
52 } catch (InvalidSyntaxException e) {
54 serviceReferences = Collections.emptyList();
56 Map<String, PdfExportPageEvent> events = new HashMap<>(serviceReferences.size());
57 for (ServiceReference<PdfExportPageEvent> reference : serviceReferences) {
58 PdfExportPageEvent event = context.getService(reference);
59 String eventName = event.toString();
60 events.put(eventName, event);
65 private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer<PdfExportPageEvent> r) {
68 } catch (Exception e) {
69 LOGGER.error("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e);
74 public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
75 events.values().forEach(e -> safeInvoke("onChapter", e, ee -> ee.onChapter(writer, document, paragraphPosition, title)));
79 public void onChapterEnd(PdfWriter writer, Document document, float position) {
80 events.values().forEach(e -> safeInvoke("onChapterEnd", e, ee -> ee.onChapterEnd(writer, document, position)));
84 public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
85 events.values().forEach(e -> safeInvoke("onGenericTag", e, ee -> ee.onGenericTag(writer, document, rect, text)));
89 public void onOpenDocument(PdfWriter writer, Document document) {
90 events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document)));
94 public void onCloseDocument(PdfWriter writer, Document document) {
95 events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document)));
99 public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
100 events.values().forEach(e -> safeInvoke("onParagraph", e, ee -> ee.onParagraph(writer, document, paragraphPosition)));
104 public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
105 events.values().forEach(e -> safeInvoke("onParagraphEnd", e, ee -> ee.onParagraphEnd(writer, document, paragraphPosition)));
109 public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {
110 events.values().forEach(e -> safeInvoke("onSection", e, ee -> ee.onSection(writer, document, paragraphPosition, depth, title)));
114 public void onSectionEnd(PdfWriter writer, Document document, float position) {
115 events.values().forEach(e -> safeInvoke("onSectionEnd", e, ee -> ee.onSectionEnd(writer, document, position)));
119 public void onStartPage(PdfWriter writer, Document document) {
120 events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document)));
124 public void onEndPage(PdfWriter writer, Document document) {
125 events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document)));