1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.macro.script; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.xwiki.context.Execution; |
31 |
|
import org.xwiki.observation.ObservationManager; |
32 |
|
import org.xwiki.rendering.block.Block; |
33 |
|
import org.xwiki.rendering.block.MacroBlock; |
34 |
|
import org.xwiki.rendering.macro.AbstractSignableMacro; |
35 |
|
import org.xwiki.rendering.macro.MacroContentParser; |
36 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
37 |
|
import org.xwiki.rendering.macro.descriptor.ContentDescriptor; |
38 |
|
import org.xwiki.rendering.parser.ParseException; |
39 |
|
import org.xwiki.rendering.parser.Parser; |
40 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
41 |
|
import org.xwiki.rendering.util.ParserUtils; |
42 |
|
import org.xwiki.script.event.ScriptEvaluatedEvent; |
43 |
|
import org.xwiki.script.event.ScriptEvaluatingEvent; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@link |
50 |
|
@link |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@param |
55 |
|
@version |
56 |
|
@since |
57 |
|
|
|
|
| 74.6% |
Uncovered Elements: 17 (67) |
Complexity: 21 |
Complexity Density: 0.5 |
|
58 |
|
public abstract class AbstractScriptMacro<P extends ScriptMacroParameters> extends AbstractSignableMacro<P> implements |
59 |
|
ScriptMacro |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
protected static final String CONTENT_DESCRIPTION = "the script to execute"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@deprecated |
70 |
|
|
71 |
|
@Inject |
72 |
|
@Deprecated |
73 |
|
protected org.xwiki.bridge.DocumentAccessBridge documentAccessBridge; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@Inject |
79 |
|
protected Execution execution; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
@Named("plain/1.0") |
87 |
|
private Parser plainTextParser; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@Inject |
93 |
|
private MacroContentParser contentParser; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@Inject |
99 |
|
private ObservationManager observation; |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
private ParserUtils parserUtils = new ParserUtils(); |
105 |
|
|
106 |
|
|
107 |
|
@param |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
109 |
0 |
public AbstractScriptMacro(String macroName)... |
110 |
|
{ |
111 |
0 |
super(macroName, null, ScriptMacroParameters.class); |
112 |
|
|
113 |
0 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
@param |
118 |
|
@param |
119 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
120 |
0 |
public AbstractScriptMacro(String macroName, String macroDescription)... |
121 |
|
{ |
122 |
0 |
super(macroName, macroDescription, ScriptMacroParameters.class); |
123 |
|
|
124 |
0 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
@param |
129 |
|
@param |
130 |
|
@param |
131 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
132 |
0 |
public AbstractScriptMacro(String macroName, String macroDescription, ContentDescriptor contentDescriptor)... |
133 |
|
{ |
134 |
0 |
super(macroName, macroDescription, contentDescriptor, ScriptMacroParameters.class); |
135 |
|
|
136 |
0 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
@param |
141 |
|
@param |
142 |
|
@param |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
144 |
0 |
public AbstractScriptMacro(String macroName, String macroDescription,... |
145 |
|
Class< ? extends ScriptMacroParameters> parametersBeanClass) |
146 |
|
{ |
147 |
0 |
super(macroName, macroDescription, parametersBeanClass); |
148 |
|
|
149 |
0 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
@param |
154 |
|
@param |
155 |
|
@param |
156 |
|
@param |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
158 |
93 |
public AbstractScriptMacro(String macroName, String macroDescription, ContentDescriptor contentDescriptor,... |
159 |
|
Class< ? extends ScriptMacroParameters> parametersBeanClass) |
160 |
|
{ |
161 |
93 |
super(macroName, macroDescription, contentDescriptor, parametersBeanClass); |
162 |
|
|
163 |
93 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
166 |
8754 |
@Override... |
167 |
|
public List<Block> execute(P parameters, String content, MacroTransformationContext context) |
168 |
|
throws MacroExecutionException |
169 |
|
{ |
170 |
8754 |
List<Block> result = Collections.emptyList(); |
171 |
|
|
172 |
8754 |
if (StringUtils.isNotEmpty(content)) { |
173 |
8750 |
try { |
174 |
|
|
175 |
8750 |
ScriptEvaluatingEvent event = new ScriptEvaluatingEvent(getDescriptor().getId().getId()); |
176 |
8750 |
this.observation.notify(event, context, parameters); |
177 |
8750 |
if (event.isCanceled()) { |
178 |
7 |
throw new MacroExecutionException(event.getReason()); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
8743 |
List<Block> blocks = evaluateBlock(parameters, content, context); |
183 |
|
|
184 |
8742 |
if (parameters.isOutput()) { |
185 |
7941 |
result = blocks; |
186 |
|
} |
187 |
|
} finally { |
188 |
|
|
189 |
8750 |
this.observation.notify(new ScriptEvaluatedEvent(getDescriptor().getId().getId()), context, parameters); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
8746 |
return result; |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
|
@link |
198 |
|
|
199 |
|
@param |
200 |
|
@param |
201 |
|
@param |
202 |
|
@return@link |
203 |
|
@throws |
204 |
|
@since |
205 |
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 7 |
Complexity Density: 0.58 |
|
206 |
7942 |
protected List<Block> parseScriptResult(String content, P parameters, MacroTransformationContext context)... |
207 |
|
throws MacroExecutionException |
208 |
|
{ |
209 |
7942 |
List<Block> result; |
210 |
|
|
211 |
7943 |
if (parameters.isWiki()) { |
212 |
7786 |
result = parseSourceSyntax(content, context); |
213 |
|
} else { |
214 |
157 |
try { |
215 |
157 |
result = this.plainTextParser.parse(new StringReader(content)).getChildren(); |
216 |
|
} catch (ParseException e) { |
217 |
|
|
218 |
|
|
219 |
0 |
throw new MacroExecutionException("Failed to parse link label as plain text", e); |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
7943 |
if (context.isInline()) { |
225 |
114 |
this.parserUtils.removeTopLevelParagraph(result); |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
114 |
if (!result.isEmpty() && result.get(0) instanceof MacroBlock && !((MacroBlock) result.get(0)).isInline()) { |
230 |
34 |
MacroBlock macro = (MacroBlock) result.get(0); |
231 |
34 |
result.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true)); |
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
7941 |
return result; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@param |
242 |
|
@param |
243 |
|
@param |
244 |
|
@return |
245 |
|
@throws |
246 |
|
@deprecated@link |
247 |
|
|
248 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
249 |
0 |
@Deprecated... |
250 |
|
protected String evaluate(P parameters, String content, MacroTransformationContext context) |
251 |
|
throws MacroExecutionException |
252 |
|
{ |
253 |
0 |
return ""; |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
@link |
258 |
|
|
259 |
|
@param |
260 |
|
@param |
261 |
|
@param |
262 |
|
@return |
263 |
|
@throws |
264 |
|
@since |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0 |
protected String evaluateString(P parameters, String content, MacroTransformationContext context)... |
267 |
|
throws MacroExecutionException |
268 |
|
{ |
269 |
|
|
270 |
0 |
return evaluate(parameters, content, context); |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
|
@link |
275 |
|
|
276 |
|
@param |
277 |
|
@param |
278 |
|
@param |
279 |
|
@return |
280 |
|
@throws |
281 |
|
@since |
282 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
283 |
8630 |
protected List<Block> evaluateBlock(P parameters, String content, MacroTransformationContext context)... |
284 |
|
throws MacroExecutionException |
285 |
|
{ |
286 |
8630 |
String scriptResult = evaluateString(parameters, content, context); |
287 |
|
|
288 |
8630 |
List<Block> result = Collections.emptyList(); |
289 |
8630 |
if (parameters.isOutput()) { |
290 |
|
|
291 |
7832 |
result = parseScriptResult(scriptResult, parameters, context); |
292 |
|
} |
293 |
|
|
294 |
8630 |
return result; |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
@param |
301 |
|
@param |
302 |
|
@return |
303 |
|
@throws |
304 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
305 |
7785 |
protected List<Block> parseSourceSyntax(String content, MacroTransformationContext context)... |
306 |
|
throws MacroExecutionException |
307 |
|
{ |
308 |
7786 |
return this.contentParser.parse(content, context, false, false).getChildren(); |
309 |
|
} |
310 |
|
} |