1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.office.viewer.script; |
21 |
|
|
22 |
|
import java.util.Collections; |
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.DocumentFormat; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.manager.ComponentManager; |
34 |
|
import org.xwiki.context.Execution; |
35 |
|
import org.xwiki.model.reference.AttachmentReference; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.office.viewer.OfficeViewer; |
38 |
|
import org.xwiki.office.viewer.OfficeViewerScriptService; |
39 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
40 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
41 |
|
import org.xwiki.rendering.block.XDOM; |
42 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
43 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
44 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
45 |
|
import org.xwiki.rendering.syntax.Syntax; |
46 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
47 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
@since |
53 |
|
@version |
54 |
|
|
55 |
|
@Component |
56 |
|
@Named("officeviewer") |
57 |
|
@Singleton |
|
|
| 89.5% |
Uncovered Elements: 4 (38) |
Complexity: 9 |
Complexity Density: 0.32 |
|
58 |
|
public class DefaultOfficeViewerScriptService implements OfficeViewerScriptService |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final String OFFICE_VIEW_EXCEPTION = "officeView.caughtException"; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private OfficeViewer officeViewer; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@see |
75 |
|
|
76 |
|
@Inject |
77 |
|
private OfficeServer officeServer; |
78 |
|
|
79 |
|
|
80 |
|
@link |
81 |
|
|
82 |
|
@Inject |
83 |
|
private ComponentManager componentManager; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
@Inject |
89 |
|
private Execution execution; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
@Inject |
95 |
|
private DocumentAccessBridge documentAccessBridge; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@Inject |
101 |
|
private TransformationManager transformationManager; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@Inject |
107 |
|
private Logger logger; |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
109 |
1 |
@Override... |
110 |
|
public Exception getCaughtException() |
111 |
|
{ |
112 |
1 |
return (Exception) this.execution.getContext().getProperty(OFFICE_VIEW_EXCEPTION); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
115 |
2 |
@Override... |
116 |
|
public String view(AttachmentReference attachmentReference) |
117 |
|
{ |
118 |
2 |
Map<String, String> parameters = Collections.emptyMap(); |
119 |
2 |
return view(attachmentReference, parameters); |
120 |
|
} |
121 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
122 |
2 |
@Override... |
123 |
|
public String view(AttachmentReference attachmentReference, Map<String, String> parameters) |
124 |
|
{ |
125 |
|
|
126 |
2 |
this.execution.getContext().removeProperty(OFFICE_VIEW_EXCEPTION); |
127 |
2 |
try { |
128 |
2 |
DocumentReference documentReference = attachmentReference.getDocumentReference(); |
129 |
|
|
130 |
1 |
if (!this.documentAccessBridge.isDocumentViewable(documentReference)) { |
131 |
0 |
throw new RuntimeException("Inadequate privileges."); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
1 |
Syntax fromSyntax = this.documentAccessBridge.getDocument(documentReference).getSyntax(); |
136 |
1 |
Syntax toSyntax = Syntax.XHTML_1_0; |
137 |
1 |
return render(this.officeViewer.createView(attachmentReference, parameters), fromSyntax, toSyntax); |
138 |
|
} catch (Exception e) { |
139 |
|
|
140 |
1 |
this.execution.getContext().setProperty(OFFICE_VIEW_EXCEPTION, e); |
141 |
1 |
this.logger.error("Failed to view office document: " + attachmentReference, e); |
142 |
1 |
return null; |
143 |
|
} |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
8 |
@Override... |
147 |
|
public boolean isMimeTypeSupported(String mimeType) |
148 |
|
{ |
149 |
8 |
return isConversionSupported(mimeType, "text/html"); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
@param |
157 |
|
@param |
158 |
|
@return |
159 |
|
|
160 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
161 |
8 |
private boolean isConversionSupported(String inputMediaType, String outputMediaType)... |
162 |
|
{ |
163 |
8 |
OfficeConverter converter = this.officeServer.getConverter(); |
164 |
8 |
if (converter != null) { |
165 |
8 |
DocumentFormat inputFormat = converter.getFormatRegistry().getFormatByMediaType(inputMediaType); |
166 |
8 |
DocumentFormat outputFormat = converter.getFormatRegistry().getFormatByMediaType(outputMediaType); |
167 |
8 |
return inputFormat != null && outputFormat != null |
168 |
|
&& outputFormat.getStoreProperties(inputFormat.getInputFamily()) != null; |
169 |
|
} else { |
170 |
0 |
return false; |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
@param@link |
178 |
|
@param |
179 |
|
@param |
180 |
|
@return |
181 |
|
@throws |
182 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
183 |
1 |
private String render(XDOM xdom, Syntax fromSyntax, Syntax toSyntax) throws Exception... |
184 |
|
{ |
185 |
|
|
186 |
|
|
187 |
1 |
TransformationContext context = new TransformationContext(xdom, fromSyntax); |
188 |
1 |
context.setTargetSyntax(toSyntax); |
189 |
1 |
this.transformationManager.performTransformations(xdom, context); |
190 |
|
|
191 |
1 |
WikiPrinter printer = new DefaultWikiPrinter(); |
192 |
1 |
BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, toSyntax.toIdString()); |
193 |
1 |
renderer.render(xdom, printer); |
194 |
1 |
return printer.toString(); |
195 |
|
} |
196 |
|
} |