1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.display.internal; |
21 |
|
|
22 |
|
import java.util.HashMap; |
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.apache.commons.lang.exception.ExceptionUtils; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
32 |
|
import org.xwiki.bridge.DocumentModelBridge; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.context.Execution; |
35 |
|
import org.xwiki.model.ModelContext; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.EntityReference; |
38 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
39 |
|
import org.xwiki.rendering.block.Block; |
40 |
|
import org.xwiki.rendering.block.HeaderBlock; |
41 |
|
import org.xwiki.rendering.block.XDOM; |
42 |
|
import org.xwiki.rendering.block.match.BlockMatcher; |
43 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
44 |
|
import org.xwiki.rendering.block.match.CompositeBlockMatcher; |
45 |
|
import org.xwiki.rendering.listener.MetaData; |
46 |
|
import org.xwiki.rendering.parser.ContentParser; |
47 |
|
import org.xwiki.rendering.syntax.Syntax; |
48 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
49 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
50 |
|
import org.xwiki.velocity.VelocityManager; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
|
58 |
|
@Component |
59 |
|
@Named("content") |
60 |
|
@Singleton |
|
|
| 82.8% |
Uncovered Elements: 16 (93) |
Complexity: 33 |
Complexity Density: 0.55 |
|
61 |
|
public class DocumentContentDisplayer implements DocumentDisplayer |
62 |
|
{ |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private static final String IS_IN_RENDERING_ENGINE = "isInRenderingEngine"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private Logger logger; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@Inject |
79 |
|
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@Inject |
85 |
|
private DocumentAccessBridge documentAccessBridge; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@Inject |
91 |
|
private Execution execution; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
private VelocityManager velocityManager; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@Inject |
103 |
|
private TransformationManager transformationManager; |
104 |
|
|
105 |
|
@Inject |
106 |
|
private ModelContext modelContext; |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
@Inject |
112 |
|
private ContentParser parser; |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
114 |
7320 |
@Override... |
115 |
|
public XDOM display(DocumentModelBridge document, DocumentDisplayerParameters parameters) |
116 |
|
{ |
117 |
7320 |
String nameSpace = |
118 |
7320 |
defaultEntityReferenceSerializer.serialize(parameters.isContentTransformed() |
119 |
|
&& parameters.isTransformationContextIsolated() ? document.getDocumentReference() |
120 |
|
: documentAccessBridge.getCurrentDocumentReference()); |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
7320 |
Map<Object, Object> xwikiContext = getXWikiContextMap(); |
126 |
7320 |
Object isInRenderingEngine = xwikiContext.put(IS_IN_RENDERING_ENGINE, true); |
127 |
|
|
128 |
7320 |
maybeOpenNameSpace(nameSpace, parameters.isTransformationContextIsolated(), isInRenderingEngine); |
129 |
|
|
130 |
7320 |
try { |
131 |
7320 |
XDOM result = |
132 |
7320 |
parameters.isExecutionContextIsolated() ? displayInIsolatedExecutionContext(document, nameSpace, |
133 |
|
parameters) : display(document, nameSpace, parameters); |
134 |
7320 |
return result; |
135 |
|
} finally { |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
7320 |
maybeCloseNameSpace(nameSpace, parameters.isTransformationContextIsolated(), isInRenderingEngine); |
142 |
|
|
143 |
7320 |
if (isInRenderingEngine != null) { |
144 |
1208 |
xwikiContext.put(IS_IN_RENDERING_ENGINE, isInRenderingEngine); |
145 |
|
} else { |
146 |
6112 |
xwikiContext.remove(IS_IN_RENDERING_ENGINE); |
147 |
|
} |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
@return |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
7320 |
@SuppressWarnings("unchecked")... |
155 |
|
private Map<Object, Object> getXWikiContextMap() |
156 |
|
{ |
157 |
7320 |
return (Map<Object, Object>) execution.getContext().getProperty("xwikicontext"); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
@param |
165 |
|
@param |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 5 |
Complexity Density: 1 |
|
167 |
7320 |
private void maybeOpenNameSpace(String nameSpace, boolean transformationContextIsolated, Object isInRenderingEngine)... |
168 |
|
{ |
169 |
7320 |
if (transformationContextIsolated && (isInRenderingEngine == null || isInRenderingEngine == Boolean.FALSE)) { |
170 |
6001 |
try { |
171 |
|
|
172 |
6001 |
velocityManager.getVelocityEngine().startedUsingMacroNamespace(nameSpace); |
173 |
6000 |
logger.debug("Started using velocity macro namespace [{}].", nameSpace); |
174 |
|
} catch (Exception e) { |
175 |
|
|
176 |
|
|
177 |
1 |
logger.warn("Failed to notify Velocity Macro cache for opening the [{}] namespace. Reason = [{}]", |
178 |
|
nameSpace, ExceptionUtils.getRootCauseMessage(e)); |
179 |
|
} |
180 |
|
} |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
@param |
187 |
|
@param |
188 |
|
@param |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 5 |
Complexity Density: 1 |
|
190 |
7320 |
private void maybeCloseNameSpace(String nameSpace, boolean transformationContextIsolated,... |
191 |
|
Object isInRenderingEngine) |
192 |
|
{ |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
7320 |
if (transformationContextIsolated && (isInRenderingEngine == null || isInRenderingEngine == Boolean.FALSE)) { |
197 |
6001 |
try { |
198 |
6001 |
velocityManager.getVelocityEngine().stoppedUsingMacroNamespace(nameSpace); |
199 |
6000 |
logger.debug("Stopped using velocity macro namespace [{}].", nameSpace); |
200 |
|
} catch (Exception e) { |
201 |
|
|
202 |
|
|
203 |
1 |
logger.warn("Failed to notify Velocity Macro cache for closing the [{}] namespace. Reason = [{}]", |
204 |
|
nameSpace, e.getMessage()); |
205 |
|
} |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
@param |
213 |
|
@param |
214 |
|
@param |
215 |
|
@return |
216 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
217 |
8 |
private XDOM displayInIsolatedExecutionContext(DocumentModelBridge document, String nameSpace,... |
218 |
|
DocumentDisplayerParameters parameters) |
219 |
|
{ |
220 |
8 |
Map<String, Object> backupObjects = new HashMap<String, Object>(); |
221 |
8 |
EntityReference currentWikiReference = this.modelContext.getCurrentEntityReference(); |
222 |
8 |
try { |
223 |
|
|
224 |
8 |
documentAccessBridge.pushDocumentInContext(backupObjects, document); |
225 |
|
|
226 |
8 |
modelContext.setCurrentEntityReference(document.getDocumentReference().getWikiReference()); |
227 |
8 |
return display(document, nameSpace, parameters); |
228 |
|
} catch (Exception e) { |
229 |
0 |
throw new RuntimeException(e.getMessage(), e); |
230 |
|
} finally { |
231 |
8 |
documentAccessBridge.popDocumentFromContext(backupObjects); |
232 |
|
|
233 |
8 |
this.modelContext.setCurrentEntityReference(currentWikiReference); |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
@param |
241 |
|
@param |
242 |
|
@param |
243 |
|
@return |
244 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
245 |
7320 |
protected XDOM display(DocumentModelBridge document, String nameSpace, DocumentDisplayerParameters parameters)... |
246 |
|
{ |
247 |
|
|
248 |
7320 |
XDOM content = getContent(document, parameters); |
249 |
|
|
250 |
7320 |
if (!parameters.isContentTransformed()) { |
251 |
955 |
return content; |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
6364 |
content.getMetaData().addMetaData(MetaData.BASE, |
257 |
|
defaultEntityReferenceSerializer.serialize(documentAccessBridge.getCurrentDocumentReference())); |
258 |
|
|
259 |
6365 |
TransformationContext txContext = |
260 |
|
new TransformationContext(content, document.getSyntax(), parameters.isTransformationContextRestricted()); |
261 |
6364 |
txContext.setId(nameSpace); |
262 |
6364 |
try { |
263 |
6365 |
transformationManager.performTransformations(content, txContext); |
264 |
|
} catch (Exception e) { |
265 |
0 |
throw new RuntimeException(e); |
266 |
|
} |
267 |
|
|
268 |
6365 |
return content; |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
@param |
275 |
|
@param |
276 |
|
@return |
277 |
|
|
|
|
| 46.2% |
Uncovered Elements: 7 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
278 |
7320 |
private XDOM getContent(DocumentModelBridge document, final DocumentDisplayerParameters parameters)... |
279 |
|
{ |
280 |
7320 |
XDOM content = parameters.isContentTranslated() ? getTranslatedContent(document) : document.getXDOM(); |
281 |
|
|
282 |
7320 |
if (parameters.getSectionId() != null) { |
283 |
0 |
HeaderBlock headerBlock = |
284 |
|
content.getFirstBlock(new CompositeBlockMatcher(new ClassBlockMatcher(HeaderBlock.class), |
285 |
|
new BlockMatcher() |
286 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
287 |
0 |
@Override... |
288 |
|
public boolean match(Block block) |
289 |
|
{ |
290 |
0 |
return ((HeaderBlock) block).getId().equals(parameters.getSectionId()); |
291 |
|
} |
292 |
|
}), Block.Axes.DESCENDANT); |
293 |
0 |
if (headerBlock == null) { |
294 |
0 |
throw new RuntimeException("Cannot find section [" + parameters.getSectionId() + "] in document [" |
295 |
|
+ this.defaultEntityReferenceSerializer.serialize(document.getDocumentReference()) + "]"); |
296 |
|
} else { |
297 |
0 |
content = new XDOM(headerBlock.getSection().getChildren(), content.getMetaData()); |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
7320 |
return content; |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
@param |
311 |
|
@return |
312 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
313 |
175 |
private XDOM getTranslatedContent(DocumentModelBridge document)... |
314 |
|
{ |
315 |
175 |
try { |
316 |
175 |
DocumentModelBridge translatedDocument = documentAccessBridge.getDocument(document.getDocumentReference()); |
317 |
|
|
318 |
|
|
319 |
|
|
320 |
175 |
if (!document.getRealLanguage().equals(translatedDocument.getRealLanguage())) { |
321 |
|
|
322 |
2 |
if (document.getSyntax().equals(translatedDocument.getSyntax())) { |
323 |
|
|
324 |
0 |
return translatedDocument.getXDOM(); |
325 |
|
} else { |
326 |
|
|
327 |
|
|
328 |
2 |
return parseContent(translatedDocument.getContent(), document.getSyntax(), |
329 |
|
document.getDocumentReference()); |
330 |
|
} |
331 |
|
} |
332 |
|
} catch (Exception e) { |
333 |
|
|
334 |
|
} |
335 |
173 |
return document.getXDOM(); |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
@param |
342 |
|
@param |
343 |
|
@return |
344 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
345 |
2 |
private XDOM parseContent(String content, Syntax syntax, DocumentReference source)... |
346 |
|
{ |
347 |
2 |
try { |
348 |
2 |
return parser.parse(content, syntax, source); |
349 |
|
} catch (Exception e) { |
350 |
0 |
throw new RuntimeException(e); |
351 |
|
} |
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
@return |
362 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
363 |
0 |
protected Execution getExecution()... |
364 |
|
{ |
365 |
0 |
return execution; |
366 |
|
} |
367 |
|
} |