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
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.export.core.pdf;
\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
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
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
33 * @author Tuukka Lehtonen
\r
36 public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper {
\r
38 Map<String, PdfExportPageEvent> events;
\r
40 public ServiceBasedPdfExportPageEvent() {
\r
41 // Read all page event contributions from the OSGi service
\r
42 events = collectEvents();
\r
45 private static Map<String, PdfExportPageEvent> collectEvents() {
\r
46 BundleContext context = Activator.getContext();
\r
47 Collection<ServiceReference<PdfExportPageEvent>> serviceReferences;
\r
49 serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null);
\r
50 } catch (InvalidSyntaxException e) {
\r
51 e.printStackTrace();
\r
52 serviceReferences = Collections.emptyList();
\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
63 private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer<PdfExportPageEvent> r) {
\r
66 } catch (Exception e) {
\r
67 Logger.defaultLogError("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e);
\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
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
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
87 public void onOpenDocument(PdfWriter writer, Document document) {
\r
88 events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document)));
\r
92 public void onCloseDocument(PdfWriter writer, Document document) {
\r
93 events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document)));
\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
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
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
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
117 public void onStartPage(PdfWriter writer, Document document) {
\r
118 events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document)));
\r
122 public void onEndPage(PdfWriter writer, Document document) {
\r
123 events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document)));
\r