a74a2a6d375df13342a9d16b647a61734931b419
[simantics/platform.git] / bundles / org.simantics.document / src / org / simantics / document / export / ExportWikiPdf.java
1 package org.simantics.document.export;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
9 import org.eclipse.mylyn.wikitext.mediawiki.core.MediaWikiLanguage;
10 import org.osgi.service.prefs.Preferences;
11 import org.simantics.databoard.binding.mutable.Variant;
12 import org.simantics.databoard.type.RecordType;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.Queries;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.document.DocumentSettings;
17 import org.simantics.document.DocumentUtils;
18 import org.simantics.export.core.ExportContext;
19 import org.simantics.export.core.error.ExportException;
20 import org.simantics.export.core.intf.ExportClass;
21 import org.simantics.export.core.manager.Content;
22 import org.simantics.export.core.pdf.ExportPdfWriter;
23 import org.simantics.export.core.pdf.ExportPdfWriter.Page;
24 import org.simantics.utils.datastructures.MapList;
25 import org.simantics.utils.page.PageDesc;
26
27 import com.lowagie.text.DocumentException;
28
29 public class ExportWikiPdf implements ExportClass {
30
31         static RecordType wikiOptions = new RecordType();
32         
33         MarkupParser markupParser;
34         MediaWikiLanguage language;
35         
36         public ExportWikiPdf() {
37                 markupParser = new MarkupParser();
38                 language = new MediaWikiLanguage();
39                 markupParser.setMarkupLanguage(language);
40         }
41         
42         @Override
43         public RecordType options(ExportContext ctx, Collection<String> contentUris) throws ExportException {
44                 return wikiOptions;
45         }
46
47         @Override
48         public List<String> validate(String contentUri, ExportContext context, Variant options) {
49                 return Collections.emptyList();
50         }
51
52         @Override
53         public void export(List<Content> contents, Object handle, ExportContext ctx, Variant options, IProgressMonitor monitor, MapList<Content, Content> attachmentMap) throws ExportException {
54                 
55                 final ExportPdfWriter writer = (ExportPdfWriter) handle;
56                 
57                 try {
58                         
59                         for(Content content : contents) {
60                                 
61                                 Resource res = ctx.session.syncRequest( Queries.resource(content.url) );
62                         
63                                 StringBuilder wikiSb = new StringBuilder();
64                                 StringBuilder cssSb = new StringBuilder();
65
66                                 DocumentUtils du = new DocumentUtils();
67                                 du.getDocumentWikiTextRecursive(ctx.session, res, wikiSb, cssSb);
68                                 DocumentSettings settings = du.getDocumentSettings(ctx.session, res);
69
70                                 if ( wikiSb.length()>0 ) {
71                                         Page page = writer.createPage(PageDesc.A4);
72                                         du.print(ctx.session, res, wikiSb.toString(), cssSb.toString(), settings, page.pdfWriter, page.document);
73                                 }
74
75                         }
76
77                 } catch (DatabaseException e) {
78                         throw new ExportException( e ); 
79                 } catch (DocumentException e) {
80                         throw new ExportException( e ); 
81                 }
82         }
83         
84         Content findClosestContent(List<Content> contents, String uri) {
85                 Content result = null;
86                 int match = -1;
87                 for ( Content c : contents ) {
88                         if ( uri.startsWith(c.url) ) {
89                                 if ( c.url.length()>match ) {
90                                         match = c.url.length();
91                                         result = c;
92                                 }
93                         }
94                 }
95                 return result;
96         }
97
98         @Override
99         public void fillDefaultPrefs(ExportContext ctx, Variant options) throws ExportException {
100         }
101
102         @Override
103         public void savePref(Variant options, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
104         }
105
106         @Override
107         public void loadPref(Variant options, Preferences contentScopeNode, Preferences workspaceScopePrefs) throws ExportException {
108         }
109         
110 }