1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.export; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.File; |
24 |
|
import java.io.FileInputStream; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.io.OutputStream; |
27 |
|
import java.nio.charset.Charset; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.Map; |
30 |
|
|
31 |
|
import org.artofsolving.jodconverter.document.DocumentFormat; |
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.slf4j.LoggerFactory; |
34 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
35 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
36 |
|
|
37 |
|
import com.xpn.xwiki.XWikiContext; |
38 |
|
import com.xpn.xwiki.XWikiException; |
39 |
|
import com.xpn.xwiki.pdf.impl.PdfExportImpl; |
40 |
|
import com.xpn.xwiki.web.Utils; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 9 |
Complexity Density: 0.38 |
|
51 |
|
public class OfficeExporter extends PdfExportImpl |
52 |
|
{ |
53 |
|
|
54 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(OfficeExporter.class); |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private OfficeServer officeServer = Utils.getComponent(OfficeServer.class); |
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@return |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
65 |
0 |
public ExportType getExportType(String extension)... |
66 |
|
{ |
67 |
0 |
if (this.officeServer.getState() == OfficeServer.ServerState.CONNECTED) { |
68 |
0 |
DocumentFormat format = |
69 |
|
this.officeServer.getConverter().getFormatRegistry().getFormatByExtension(extension); |
70 |
0 |
if (format != null) { |
71 |
0 |
return new ExportType(format.getMediaType(), format.getExtension()); |
72 |
|
} |
73 |
|
} |
74 |
0 |
return null; |
75 |
|
} |
76 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
0 |
@Override... |
78 |
|
protected void exportXHTML(String xhtml, OutputStream out, ExportType type, XWikiContext context) |
79 |
|
throws XWikiException |
80 |
|
{ |
81 |
|
|
82 |
0 |
exportXHTML(xhtml, out, |
83 |
|
this.officeServer.getConverter().getFormatRegistry().getFormatByExtension(type.getExtension()), context); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@param |
90 |
|
@param |
91 |
|
@param |
92 |
|
@param |
93 |
|
|
94 |
|
@throws |
95 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
|
96 |
0 |
private void exportXHTML(String xhtml, OutputStream out, DocumentFormat format, XWikiContext context)... |
97 |
|
throws XWikiException |
98 |
|
{ |
99 |
0 |
String html = applyXSLT(xhtml, getOfficeExportXSLT(context)); |
100 |
|
|
101 |
0 |
String inputFileName = "export_input.html"; |
102 |
0 |
String outputFileName = "export_output." + format.getExtension(); |
103 |
|
|
104 |
0 |
Map<String, InputStream> inputStreams = new HashMap<String, InputStream>(); |
105 |
|
|
106 |
0 |
Charset charset = Charset.forName(context.getWiki().getEncoding()); |
107 |
0 |
inputStreams.put(inputFileName, new ByteArrayInputStream(html.getBytes(charset))); |
108 |
0 |
addEmbeddedObjects(inputStreams, context); |
109 |
|
|
110 |
0 |
OfficeConverter officeConverter = this.officeServer.getConverter(); |
111 |
0 |
try { |
112 |
0 |
Map<String, byte[]> ouput = officeConverter.convert(inputStreams, inputFileName, outputFileName); |
113 |
|
|
114 |
0 |
out.write(ouput.values().iterator().next()); |
115 |
|
} catch (Exception e) { |
116 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_EXPORT, |
117 |
|
XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, String.format( |
118 |
|
"Exception while exporting to %s (%s).", format.getName(), format.getExtension()), e); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
@param |
128 |
|
|
129 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
130 |
0 |
private void addEmbeddedObjects(Map<String, InputStream> inputStreams, XWikiContext context)... |
131 |
|
{ |
132 |
0 |
@SuppressWarnings("unchecked") |
133 |
|
Map<String, File> fileMapping = (Map<String, File>) context.get("pdfexport-file-mapping"); |
134 |
0 |
for (File file : fileMapping.values()) { |
135 |
0 |
try { |
136 |
|
|
137 |
0 |
inputStreams.put(file.getName(), new FileInputStream(file)); |
138 |
|
} catch (Exception e) { |
139 |
0 |
LOGGER.warn(String.format("Failed to embed %s in the office export.", file.getName()), e); |
140 |
|
} |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@param |
148 |
|
@return |
149 |
|
@see |
150 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
151 |
0 |
private InputStream getOfficeExportXSLT(XWikiContext context)... |
152 |
|
{ |
153 |
0 |
return getXslt("officeExportXSLT", "officeExport.xsl", context); |
154 |
|
} |
155 |
|
} |