1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.officeimporter.script; |
21 |
|
|
22 |
|
import java.io.InputStream; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.artofsolving.jodconverter.document.DocumentFamily; |
30 |
|
import org.artofsolving.jodconverter.document.DocumentFormat; |
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.context.Execution; |
35 |
|
import org.xwiki.model.reference.AttachmentReference; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
38 |
|
import org.xwiki.officeimporter.OfficeImporterException; |
39 |
|
import org.xwiki.officeimporter.builder.PresentationBuilder; |
40 |
|
import org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder; |
41 |
|
import org.xwiki.officeimporter.builder.XHTMLOfficeDocumentBuilder; |
42 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
43 |
|
import org.xwiki.officeimporter.document.XDOMOfficeDocument; |
44 |
|
import org.xwiki.officeimporter.document.XHTMLOfficeDocument; |
45 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
46 |
|
import org.xwiki.officeimporter.server.OfficeServer.ServerState; |
47 |
|
import org.xwiki.officeimporter.server.OfficeServerConfiguration; |
48 |
|
import org.xwiki.officeimporter.server.OfficeServerException; |
49 |
|
import org.xwiki.officeimporter.splitter.TargetDocumentDescriptor; |
50 |
|
import org.xwiki.officeimporter.splitter.XDOMOfficeDocumentSplitter; |
51 |
|
import org.xwiki.script.service.ScriptService; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@version |
57 |
|
@since |
58 |
|
|
59 |
|
@Component |
60 |
|
@Named("officeimporter") |
61 |
|
@Singleton |
|
|
| 25.6% |
Uncovered Elements: 87 (117) |
Complexity: 34 |
Complexity Density: 0.43 |
|
62 |
|
public class OfficeImporterScriptService implements ScriptService |
63 |
|
{ |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public static final String OFFICE_IMPORTER_ERROR = "OFFICE_IMPORTER_ERROR"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private Logger logger; |
74 |
|
|
75 |
|
|
76 |
|
@link |
77 |
|
|
78 |
|
@Inject |
79 |
|
private Execution execution; |
80 |
|
|
81 |
|
|
82 |
|
@link |
83 |
|
|
84 |
|
@Inject |
85 |
|
private DocumentAccessBridge docBridge; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@Inject |
91 |
|
@Named("currentmixed") |
92 |
|
private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@Inject |
98 |
|
private OfficeServer officeServer; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@Inject |
104 |
|
private OfficeServerConfiguration officeServerConfiguration; |
105 |
|
|
106 |
|
|
107 |
|
@link |
108 |
|
|
109 |
|
@Inject |
110 |
|
private XHTMLOfficeDocumentBuilder xhtmlBuilder; |
111 |
|
|
112 |
|
|
113 |
|
@link |
114 |
|
|
115 |
|
@Inject |
116 |
|
private XDOMOfficeDocumentBuilder xdomBuilder; |
117 |
|
|
118 |
|
|
119 |
|
@link |
120 |
|
|
121 |
|
@Inject |
122 |
|
private PresentationBuilder presentationBuilder; |
123 |
|
|
124 |
|
|
125 |
|
@link |
126 |
|
|
127 |
|
@Inject |
128 |
|
private XDOMOfficeDocumentSplitter xdomSplitter; |
129 |
|
|
130 |
|
|
131 |
|
@link |
132 |
|
|
133 |
|
@param |
134 |
|
@param |
135 |
|
|
136 |
|
@param |
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@return@link |
141 |
|
@since |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
143 |
0 |
public XHTMLOfficeDocument officeToXHTML(InputStream officeFileStream, String officeFileName,... |
144 |
|
DocumentReference targetDocumentReference, boolean filterStyles) |
145 |
|
{ |
146 |
0 |
try { |
147 |
0 |
assertConnected(); |
148 |
0 |
return this.xhtmlBuilder.build(officeFileStream, officeFileName, targetDocumentReference, filterStyles); |
149 |
|
} catch (Exception ex) { |
150 |
0 |
setErrorMessage(ex.getMessage()); |
151 |
0 |
logger.error(ex.getMessage(), ex); |
152 |
|
} |
153 |
0 |
return null; |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
@link |
158 |
|
|
159 |
|
@param |
160 |
|
@param |
161 |
|
|
162 |
|
@param |
163 |
|
|
164 |
|
|
165 |
|
@param |
166 |
|
@return@link |
167 |
|
@since |
168 |
|
@deprecated@link |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
0 |
public XHTMLOfficeDocument officeToXHTML(InputStream officeFileStream, String officeFileName,... |
171 |
|
String referenceDocument, boolean filterStyles) |
172 |
|
{ |
173 |
0 |
return officeToXHTML(officeFileStream, officeFileName, |
174 |
|
this.currentMixedDocumentReferenceResolver.resolve(referenceDocument), filterStyles); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
@link@link |
179 |
|
|
180 |
|
@param@link |
181 |
|
@return@link@link |
182 |
|
|
183 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
184 |
0 |
public XDOMOfficeDocument xhtmlToXDOM(XHTMLOfficeDocument xhtmlOfficeDocument)... |
185 |
|
{ |
186 |
0 |
try { |
187 |
0 |
return this.xdomBuilder.build(xhtmlOfficeDocument); |
188 |
|
} catch (OfficeImporterException ex) { |
189 |
0 |
setErrorMessage(ex.getMessage()); |
190 |
0 |
logger.error(ex.getMessage(), ex); |
191 |
|
} |
192 |
0 |
return null; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
@link |
197 |
|
|
198 |
|
@param |
199 |
|
@param |
200 |
|
|
201 |
|
@param |
202 |
|
|
203 |
|
|
204 |
|
@param |
205 |
|
@return@link@link |
206 |
|
|
207 |
|
@since |
208 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
209 |
0 |
public XDOMOfficeDocument officeToXDOM(InputStream officeFileStream, String officeFileName,... |
210 |
|
DocumentReference targetDocumentReference, boolean filterStyles) |
211 |
|
{ |
212 |
0 |
try { |
213 |
0 |
assertConnected(); |
214 |
0 |
if (isPresentation(officeFileName)) { |
215 |
0 |
return this.presentationBuilder.build(officeFileStream, officeFileName, targetDocumentReference); |
216 |
|
} else { |
217 |
0 |
return this.xdomBuilder.build(officeFileStream, officeFileName, targetDocumentReference, filterStyles); |
218 |
|
} |
219 |
|
} catch (Exception ex) { |
220 |
0 |
setErrorMessage(ex.getMessage()); |
221 |
0 |
logger.error(ex.getMessage(), ex); |
222 |
|
} |
223 |
0 |
return null; |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
@link |
228 |
|
|
229 |
|
@param |
230 |
|
@param |
231 |
|
|
232 |
|
@param |
233 |
|
|
234 |
|
|
235 |
|
@param |
236 |
|
@return@link@link |
237 |
|
|
238 |
|
@deprecated@link |
239 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
240 |
0 |
public XDOMOfficeDocument officeToXDOM(InputStream officeFileStream, String officeFileName,... |
241 |
|
String referenceDocument, boolean filterStyles) |
242 |
|
{ |
243 |
0 |
return officeToXDOM(officeFileStream, officeFileName, |
244 |
|
this.currentMixedDocumentReferenceResolver.resolve(referenceDocument), filterStyles); |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
@link@link |
249 |
|
|
250 |
|
|
251 |
|
@link |
252 |
|
|
253 |
|
@param@link |
254 |
|
@param |
255 |
|
@param |
256 |
|
@param |
257 |
|
@link |
258 |
|
|
259 |
|
@return@link@link |
260 |
|
|
261 |
|
@since |
262 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
263 |
0 |
public Map<TargetDocumentDescriptor, XDOMOfficeDocument> split(XDOMOfficeDocument xdomDocument,... |
264 |
|
String[] headingLevels, String namingCriterionHint, DocumentReference rootDocumentReference) |
265 |
|
{ |
266 |
0 |
int[] splitLevels = new int[headingLevels.length]; |
267 |
0 |
for (int i = 0; i < headingLevels.length; i++) { |
268 |
0 |
splitLevels[i] = Integer.parseInt(headingLevels[i]); |
269 |
|
} |
270 |
0 |
try { |
271 |
0 |
return this.xdomSplitter.split(xdomDocument, splitLevels, namingCriterionHint, rootDocumentReference); |
272 |
|
} catch (OfficeImporterException ex) { |
273 |
0 |
setErrorMessage(ex.getMessage()); |
274 |
0 |
logger.error(ex.getMessage(), ex); |
275 |
|
} |
276 |
0 |
return null; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
@link@link |
281 |
|
|
282 |
|
|
283 |
|
@link |
284 |
|
|
285 |
|
@param@link |
286 |
|
@param |
287 |
|
@param |
288 |
|
@param |
289 |
|
@link |
290 |
|
|
291 |
|
@return@link@link |
292 |
|
|
293 |
|
@deprecated@link |
294 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
295 |
0 |
public Map<TargetDocumentDescriptor, XDOMOfficeDocument> split(XDOMOfficeDocument xdomDocument,... |
296 |
|
String[] headingLevels, String namingCriterionHint, String rootDocumentName) |
297 |
|
{ |
298 |
0 |
return split(xdomDocument, headingLevels, namingCriterionHint, |
299 |
|
this.currentMixedDocumentReferenceResolver.resolve(rootDocumentName)); |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
@link |
304 |
|
|
305 |
|
@param@link |
306 |
|
@param |
307 |
|
@param |
308 |
|
@param |
309 |
|
@param |
310 |
|
@param |
311 |
|
@return |
312 |
|
@since |
313 |
|
|
|
|
| 60% |
Uncovered Elements: 16 (40) |
Complexity: 10 |
Complexity Density: 0.36 |
|
314 |
2 |
public boolean save(XDOMOfficeDocument doc, DocumentReference documentReference, String syntaxId,... |
315 |
|
DocumentReference parentReference, String title, boolean append) |
316 |
|
{ |
317 |
2 |
try { |
318 |
|
|
319 |
2 |
if (!this.docBridge.isDocumentEditable(documentReference)) { |
320 |
0 |
String message = "You do not have edit rights on [%s] document."; |
321 |
0 |
throw new OfficeImporterException(String.format(message, documentReference)); |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
2 |
if (this.docBridge.exists(documentReference) && append) { |
326 |
|
|
327 |
1 |
String currentSyntaxId = this.docBridge.getDocument(documentReference).getSyntax().toIdString(); |
328 |
1 |
if (!currentSyntaxId.equals(syntaxId)) { |
329 |
0 |
String message = |
330 |
|
"The target page [%s] exists but its syntax [%s] is different from the specified syntax [%s]"; |
331 |
0 |
throw new OfficeImporterException(String.format(message, documentReference, currentSyntaxId, |
332 |
|
syntaxId)); |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
1 |
String currentContent = this.docBridge.getDocumentContent(documentReference, null); |
337 |
1 |
String newContent = currentContent + "\n" + doc.getContentAsString(syntaxId); |
338 |
1 |
this.docBridge.setDocumentContent(documentReference, newContent, "Updated by office importer.", false); |
339 |
|
} else { |
340 |
1 |
this.docBridge.setDocumentSyntaxId(documentReference, syntaxId); |
341 |
1 |
this.docBridge.setDocumentContent(documentReference, doc.getContentAsString(syntaxId), |
342 |
|
"Created by office importer.", false); |
343 |
|
|
344 |
|
|
345 |
1 |
if (null != parentReference) { |
346 |
1 |
this.docBridge.setDocumentParentReference(documentReference, parentReference); |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
1 |
String docTitle = (null == title) ? doc.getTitle() : title; |
351 |
|
|
352 |
|
|
353 |
1 |
if (null != docTitle) { |
354 |
1 |
this.docBridge.setDocumentTitle(documentReference, docTitle); |
355 |
|
} |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
2 |
attachArtifacts(doc.getArtifacts(), documentReference); |
360 |
|
|
361 |
2 |
return true; |
362 |
|
} catch (OfficeImporterException ex) { |
363 |
0 |
setErrorMessage(ex.getMessage()); |
364 |
0 |
logger.error(ex.getMessage(), ex); |
365 |
|
} catch (Exception ex) { |
366 |
0 |
String message = "Error while saving document [%s]."; |
367 |
0 |
message = String.format(message, documentReference); |
368 |
0 |
setErrorMessage(message); |
369 |
0 |
logger.error(message, ex); |
370 |
|
} |
371 |
0 |
return false; |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
|
@link |
376 |
|
|
377 |
|
@param@link |
378 |
|
@param |
379 |
|
@param |
380 |
|
@param |
381 |
|
@param |
382 |
|
@param |
383 |
|
@return |
384 |
|
@deprecated@link |
385 |
|
|
386 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
387 |
0 |
public boolean save(XDOMOfficeDocument doc, String target, String syntaxId, String parent, String title,... |
388 |
|
boolean append) |
389 |
|
{ |
390 |
0 |
return save(doc, this.currentMixedDocumentReferenceResolver.resolve(target), syntaxId, |
391 |
|
this.currentMixedDocumentReferenceResolver.resolve(parent), title, append); |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
@return |
396 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
397 |
0 |
public String getErrorMessage()... |
398 |
|
{ |
399 |
0 |
return (String) this.execution.getContext().getProperty(OFFICE_IMPORTER_ERROR); |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
|
405 |
|
@param |
406 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
407 |
0 |
private void setErrorMessage(String message)... |
408 |
|
{ |
409 |
0 |
this.execution.getContext().setProperty(OFFICE_IMPORTER_ERROR, message); |
410 |
|
} |
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
@throws |
418 |
|
@throws |
419 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
420 |
0 |
private void assertConnected() throws OfficeImporterException, OfficeServerException... |
421 |
|
{ |
422 |
0 |
boolean connected = this.officeServer.getState().equals(ServerState.CONNECTED); |
423 |
0 |
if (!connected) { |
424 |
|
|
425 |
0 |
if (this.officeServerConfiguration.isAutoStart()) { |
426 |
|
|
427 |
|
|
428 |
0 |
this.officeServer.start(); |
429 |
0 |
connected = this.officeServer.getState().equals(ServerState.CONNECTED); |
430 |
|
} |
431 |
0 |
if (!connected) { |
432 |
0 |
throw new OfficeImporterException("Office server unavailable."); |
433 |
|
} |
434 |
|
} |
435 |
|
} |
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
@param |
441 |
|
@return |
442 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
443 |
0 |
private boolean isPresentation(String officeFileName)... |
444 |
|
{ |
445 |
0 |
String extension = officeFileName.substring(officeFileName.lastIndexOf('.') + 1); |
446 |
0 |
OfficeConverter officeConverter = officeServer.getConverter(); |
447 |
0 |
if (officeConverter != null) { |
448 |
0 |
DocumentFormat format = officeConverter.getFormatRegistry().getFormatByExtension(extension); |
449 |
0 |
return format != null && format.getInputFamily() == DocumentFamily.PRESENTATION; |
450 |
|
} |
451 |
0 |
return false; |
452 |
|
} |
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
@param |
458 |
|
@param |
459 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
460 |
2 |
private void attachArtifacts(Map<String, byte[]> artifacts, DocumentReference targetDocumentReference)... |
461 |
|
{ |
462 |
2 |
for (Map.Entry<String, byte[]> artifact : artifacts.entrySet()) { |
463 |
1 |
AttachmentReference attachmentReference = |
464 |
|
new AttachmentReference(artifact.getKey(), targetDocumentReference); |
465 |
1 |
try { |
466 |
1 |
this.docBridge.setAttachmentContent(attachmentReference, artifact.getValue()); |
467 |
|
} catch (Exception ex) { |
468 |
|
|
469 |
0 |
logger.error("Error while attaching artifact.", ex); |
470 |
|
} |
471 |
|
} |
472 |
|
} |
473 |
|
} |