1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.wikibridge; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import org.xwiki.component.manager.ComponentLookupException; |
28 |
|
import org.xwiki.component.manager.ComponentManager; |
29 |
|
import org.xwiki.context.Execution; |
30 |
|
import org.xwiki.context.ExecutionContext; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.observation.ObservationManager; |
33 |
|
import org.xwiki.rendering.block.Block; |
34 |
|
import org.xwiki.rendering.block.MacroBlock; |
35 |
|
import org.xwiki.rendering.block.MacroMarkerBlock; |
36 |
|
import org.xwiki.rendering.block.MetaDataBlock; |
37 |
|
import org.xwiki.rendering.block.ParagraphBlock; |
38 |
|
import org.xwiki.rendering.block.XDOM; |
39 |
|
import org.xwiki.rendering.internal.macro.script.NestedScriptMacroEnabled; |
40 |
|
import org.xwiki.rendering.internal.transformation.MutableRenderingContext; |
41 |
|
import org.xwiki.rendering.macro.Macro; |
42 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
43 |
|
import org.xwiki.rendering.macro.descriptor.MacroDescriptor; |
44 |
|
import org.xwiki.rendering.macro.descriptor.ParameterDescriptor; |
45 |
|
import org.xwiki.rendering.macro.parameter.MacroParameterException; |
46 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacro; |
47 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroExecutionFinishedEvent; |
48 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroExecutionStartsEvent; |
49 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroParameters; |
50 |
|
import org.xwiki.rendering.syntax.Syntax; |
51 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
52 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
53 |
|
import org.xwiki.rendering.transformation.Transformation; |
54 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
55 |
|
|
56 |
|
|
57 |
|
@link |
58 |
|
|
59 |
|
@version |
60 |
|
@since |
61 |
|
|
|
|
| 90.1% |
Uncovered Elements: 13 (131) |
Complexity: 38 |
Complexity Density: 0.42 |
|
62 |
|
public class DefaultWikiMacro implements WikiMacro, NestedScriptMacroEnabled |
63 |
|
{ |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private static final String MACRO_KEY = "macro"; |
68 |
|
|
69 |
|
|
70 |
|
@link |
71 |
|
|
72 |
|
private static final String MACRO_HINT = MACRO_KEY; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
private static final String MACRO_CONTENT_KEY = "content"; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private static final String MACRO_PARAMS_KEY = "params"; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
private static final String MACRO_CONTEXT_KEY = "context"; |
88 |
|
|
89 |
|
|
90 |
|
@link@link |
91 |
|
|
92 |
|
private static final String MACRO_RESULT_KEY = "result"; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
private static final WikiMacroExecutionStartsEvent STARTEXECUTION_EVENT = new WikiMacroExecutionStartsEvent(); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
private static final WikiMacroExecutionFinishedEvent ENDEXECUTION_EVENT = new WikiMacroExecutionFinishedEvent(); |
103 |
|
|
104 |
|
|
105 |
|
@link |
106 |
|
|
107 |
|
private MacroDescriptor descriptor; |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
private DocumentReference macroDocumentReference; |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
private DocumentReference macroAuthor; |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
private boolean supportsInlineMode; |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
private XDOM content; |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
private Syntax syntax; |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
private ComponentManager componentManager; |
138 |
|
|
139 |
|
|
140 |
|
@link |
141 |
|
|
142 |
|
@param |
143 |
|
@param |
144 |
|
@param |
145 |
|
@param@link |
146 |
|
@param |
147 |
|
@param |
148 |
|
@param@link |
149 |
|
@since |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
151 |
248 |
public DefaultWikiMacro(DocumentReference macroDocumentReference, DocumentReference macroAuthor,... |
152 |
|
boolean supportsInlineMode, MacroDescriptor descriptor, XDOM macroContent, Syntax syntax, |
153 |
|
ComponentManager componentManager) |
154 |
|
{ |
155 |
248 |
this.macroDocumentReference = macroDocumentReference; |
156 |
248 |
this.macroAuthor = macroAuthor; |
157 |
248 |
this.supportsInlineMode = supportsInlineMode; |
158 |
248 |
this.descriptor = descriptor; |
159 |
248 |
this.content = macroContent; |
160 |
248 |
this.syntax = syntax; |
161 |
248 |
this.componentManager = componentManager; |
162 |
|
} |
163 |
|
|
|
|
| 89.1% |
Uncovered Elements: 6 (55) |
Complexity: 10 |
Complexity Density: 0.22 |
|
164 |
154 |
@Override... |
165 |
|
public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) |
166 |
|
throws MacroExecutionException |
167 |
|
{ |
168 |
154 |
validate(parameters, macroContent); |
169 |
|
|
170 |
|
|
171 |
154 |
XDOM xdom = prepareWikiMacroContent(context); |
172 |
|
|
173 |
|
|
174 |
154 |
Map<String, Object> macroBinding = new HashMap<String, Object>(); |
175 |
154 |
macroBinding.put(MACRO_PARAMS_KEY, parameters); |
176 |
154 |
macroBinding.put(MACRO_CONTENT_KEY, macroContent); |
177 |
154 |
macroBinding.put(MACRO_CONTEXT_KEY, context); |
178 |
154 |
macroBinding.put(MACRO_RESULT_KEY, null); |
179 |
|
|
180 |
|
|
181 |
154 |
try { |
182 |
154 |
List<WikiMacroBindingInitializer> bindingInitializers = |
183 |
|
this.componentManager.getInstanceList(WikiMacroBindingInitializer.class); |
184 |
|
|
185 |
154 |
for (WikiMacroBindingInitializer bindingInitializer : bindingInitializers) { |
186 |
146 |
bindingInitializer.initialize(this.macroDocumentReference, parameters, macroContent, context, |
187 |
|
macroBinding); |
188 |
|
} |
189 |
|
} catch (ComponentLookupException e) { |
190 |
|
|
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
154 |
ObservationManager observation = null; |
195 |
154 |
try { |
196 |
154 |
observation = this.componentManager.getInstance(ObservationManager.class); |
197 |
|
} catch (ComponentLookupException e) { |
198 |
|
|
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
154 |
Map<String, Object> xwikiContext = null; |
203 |
154 |
try { |
204 |
154 |
Execution execution = this.componentManager.getInstance(Execution.class); |
205 |
154 |
ExecutionContext econtext = execution.getContext(); |
206 |
154 |
if (econtext != null) { |
207 |
154 |
xwikiContext = (Map<String, Object>) execution.getContext().getProperty("xwikicontext"); |
208 |
|
} |
209 |
|
} catch (ComponentLookupException e) { |
210 |
|
|
211 |
|
} |
212 |
|
|
213 |
154 |
try { |
214 |
154 |
Transformation macroTransformation = this.componentManager.getInstance(Transformation.class, MACRO_HINT); |
215 |
|
|
216 |
154 |
if (xwikiContext != null) { |
217 |
|
|
218 |
154 |
xwikiContext.put(MACRO_KEY, macroBinding); |
219 |
|
} |
220 |
|
|
221 |
154 |
MacroBlock wikiMacroBlock = context.getCurrentMacroBlock(); |
222 |
|
|
223 |
154 |
MacroMarkerBlock wikiMacroMarker = |
224 |
|
new MacroMarkerBlock(wikiMacroBlock.getId(), wikiMacroBlock.getParameters(), |
225 |
|
wikiMacroBlock.getContent(), xdom.getChildren(), wikiMacroBlock.isInline()); |
226 |
|
|
227 |
|
|
228 |
154 |
MetaDataBlock metaDataBlock = |
229 |
|
new MetaDataBlock(Collections.<Block> singletonList(wikiMacroMarker), xdom.getMetaData()); |
230 |
|
|
231 |
|
|
232 |
154 |
wikiMacroBlock.getParent().replaceChild(metaDataBlock, wikiMacroBlock); |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
154 |
wikiMacroBlock.setParent(metaDataBlock.getParent()); |
238 |
154 |
wikiMacroBlock.setNextSiblingBlock(metaDataBlock.getNextSibling()); |
239 |
154 |
wikiMacroBlock.setPreviousSiblingBlock(metaDataBlock.getPreviousSibling()); |
240 |
|
|
241 |
154 |
try { |
242 |
154 |
if (observation != null) { |
243 |
154 |
observation.notify(STARTEXECUTION_EVENT, this, macroBinding); |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
154 |
TransformationContext txContext = new TransformationContext(context.getXDOM(), this.syntax); |
248 |
154 |
txContext.setId(context.getId()); |
249 |
|
|
250 |
154 |
RenderingContext renderingContext = componentManager.getInstance(RenderingContext.class); |
251 |
154 |
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, |
252 |
|
wikiMacroMarker); |
253 |
|
} finally { |
254 |
|
|
255 |
154 |
metaDataBlock.getParent().replaceChild(wikiMacroBlock, metaDataBlock); |
256 |
|
} |
257 |
|
|
258 |
154 |
return extractResult(wikiMacroMarker.getChildren(), macroBinding, context); |
259 |
|
} catch (Exception ex) { |
260 |
0 |
throw new MacroExecutionException("Error while performing internal macro transformations", ex); |
261 |
|
} finally { |
262 |
154 |
if (xwikiContext != null) { |
263 |
154 |
xwikiContext.remove(MACRO_KEY); |
264 |
|
} |
265 |
|
|
266 |
154 |
if (observation != null) { |
267 |
154 |
observation.notify(ENDEXECUTION_EVENT, this); |
268 |
|
} |
269 |
|
} |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
@param |
276 |
|
@param |
277 |
|
@param |
278 |
|
@return |
279 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
280 |
154 |
private List<Block> extractResult(List<Block> blocks, Map<String, Object> macroContext,... |
281 |
|
MacroTransformationContext context) |
282 |
|
{ |
283 |
154 |
Object resultObject = macroContext.get(MACRO_RESULT_KEY); |
284 |
|
|
285 |
154 |
List<Block> result; |
286 |
154 |
if (resultObject != null && resultObject instanceof List) { |
287 |
2 |
result = (List<Block>) macroContext.get(MACRO_RESULT_KEY); |
288 |
|
} else { |
289 |
152 |
result = blocks; |
290 |
|
|
291 |
152 |
if (context.isInline()) { |
292 |
2 |
removeTopLevelParagraph(result); |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
154 |
return result; |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
@param |
304 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
305 |
2 |
private void removeTopLevelParagraph(List<Block> blocks)... |
306 |
|
{ |
307 |
|
|
308 |
|
|
309 |
2 |
if ((blocks.size() == 1) && blocks.get(0) instanceof ParagraphBlock) { |
310 |
1 |
Block paragraphBlock = blocks.remove(0); |
311 |
1 |
blocks.addAll(0, paragraphBlock.getChildren()); |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
@param |
319 |
|
@return |
320 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
321 |
154 |
private XDOM prepareWikiMacroContent(MacroTransformationContext context)... |
322 |
|
{ |
323 |
154 |
XDOM xdom = this.content.clone(); |
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
154 |
if (context.isInline()) { |
331 |
3 |
List<Block> children = xdom.getChildren(); |
332 |
3 |
if (children.size() > 0 && children.get(0) instanceof MacroBlock) { |
333 |
2 |
MacroBlock old = (MacroBlock) children.get(0); |
334 |
2 |
MacroBlock replacement = new MacroBlock(old.getId(), old.getParameters(), old.getContent(), true); |
335 |
2 |
xdom.replaceChild(replacement, old); |
336 |
|
} |
337 |
|
} |
338 |
|
|
339 |
154 |
return xdom; |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
@param |
346 |
|
@param |
347 |
|
@throws |
348 |
|
|
|
|
| 65% |
Uncovered Elements: 7 (20) |
Complexity: 9 |
Complexity Density: 0.75 |
|
349 |
154 |
private void validate(WikiMacroParameters parameters, String macroContent) throws MacroExecutionException... |
350 |
|
{ |
351 |
|
|
352 |
|
|
353 |
|
|
354 |
154 |
Map<String, ParameterDescriptor> parameterDescriptors = getDescriptor().getParameterDescriptorMap(); |
355 |
154 |
for (String parameterName : parameterDescriptors.keySet()) { |
356 |
1225 |
ParameterDescriptor parameterDescriptor = parameterDescriptors.get(parameterName); |
357 |
1225 |
Object parameterValue = parameters.get(parameterName); |
358 |
1225 |
if (parameterDescriptor.isMandatory() && (null == parameterValue)) { |
359 |
0 |
throw new MacroParameterException(String.format("Parameter [%s] is mandatory", parameterName)); |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
1225 |
Object parameterDefaultValue = parameterDescriptor.getDefaultValue(); |
364 |
1225 |
if (parameterValue == null && parameterDefaultValue != null) { |
365 |
285 |
parameters.set(parameterName, parameterDefaultValue); |
366 |
|
} |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
154 |
if (getDescriptor().getContentDescriptor() != null && getDescriptor().getContentDescriptor().isMandatory()) { |
371 |
0 |
if (macroContent == null || macroContent.length() == 0) { |
372 |
0 |
throw new MacroExecutionException("Missing macro content: this macro requires content (a body)"); |
373 |
|
} |
374 |
|
} |
375 |
|
} |
376 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
377 |
1209 |
@Override... |
378 |
|
public MacroDescriptor getDescriptor() |
379 |
|
{ |
380 |
1209 |
return this.descriptor; |
381 |
|
} |
382 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
383 |
1683 |
@Override... |
384 |
|
public int getPriority() |
385 |
|
{ |
386 |
1683 |
return 1000; |
387 |
|
} |
388 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
389 |
1 |
@Override... |
390 |
|
public String getId() |
391 |
|
{ |
392 |
1 |
return this.descriptor.getId().getId(); |
393 |
|
} |
394 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
395 |
23 |
@Override... |
396 |
|
public DocumentReference getDocumentReference() |
397 |
|
{ |
398 |
23 |
return this.macroDocumentReference; |
399 |
|
} |
400 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
401 |
641 |
@Override... |
402 |
|
public DocumentReference getAuthorReference() |
403 |
|
{ |
404 |
641 |
return this.macroAuthor; |
405 |
|
} |
406 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
407 |
1633 |
@Override... |
408 |
|
public int compareTo(Macro< ? > macro) |
409 |
|
{ |
410 |
1633 |
return getPriority() - macro.getPriority(); |
411 |
|
} |
412 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
413 |
4 |
@Override... |
414 |
|
public boolean supportsInlineMode() |
415 |
|
{ |
416 |
4 |
return this.supportsInlineMode; |
417 |
|
} |
418 |
|
} |