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.velocity; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.io.StringWriter; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.apache.velocity.VelocityContext; |
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
35 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
36 |
|
import org.xwiki.rendering.macro.script.AbstractScriptMacro; |
37 |
|
import org.xwiki.rendering.macro.velocity.VelocityMacroConfiguration; |
38 |
|
import org.xwiki.rendering.macro.velocity.VelocityMacroParameters; |
39 |
|
import org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter; |
40 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
41 |
|
import org.xwiki.script.ScriptContextManager; |
42 |
|
import org.xwiki.velocity.VelocityManager; |
43 |
|
import org.xwiki.velocity.XWikiVelocityException; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("velocity") |
54 |
|
@Singleton |
|
|
| 84.8% |
Uncovered Elements: 7 (46) |
Complexity: 12 |
Complexity Density: 0.4 |
|
55 |
|
public class VelocityMacro extends AbstractScriptMacro<VelocityMacroParameters> |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private static final String DESCRIPTION = "Executes a Velocity script."; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String CONTENT_DESCRIPTION = "the velocity script to execute"; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
private VelocityManager velocityManager; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
private VelocityMacroConfiguration configuration; |
78 |
|
|
79 |
|
@Inject |
80 |
|
private ScriptContextManager scriptContextManager; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
private Logger logger; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
48 |
public VelocityMacro()... |
92 |
|
{ |
93 |
48 |
super("Velocity", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), |
94 |
|
VelocityMacroParameters.class); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
114 |
@Override... |
98 |
|
public boolean supportsInlineMode() |
99 |
|
{ |
100 |
114 |
return true; |
101 |
|
} |
102 |
|
|
|
|
| 87% |
Uncovered Elements: 3 (23) |
Complexity: 5 |
Complexity Density: 0.29 |
|
103 |
8630 |
@Override... |
104 |
|
protected String evaluateString(VelocityMacroParameters parameters, String content, |
105 |
|
MacroTransformationContext context) throws MacroExecutionException |
106 |
|
{ |
107 |
8630 |
String result = ""; |
108 |
|
|
109 |
8630 |
try { |
110 |
8630 |
VelocityContext velocityContext = this.velocityManager.getCurrentVelocityContext(); |
111 |
|
|
112 |
8630 |
VelocityMacroFilter filter = getFilter(parameters); |
113 |
|
|
114 |
8630 |
String cleanedContent = content; |
115 |
|
|
116 |
|
|
117 |
8630 |
if (filter != null) { |
118 |
8630 |
cleanedContent = filter.before(cleanedContent, velocityContext); |
119 |
|
} |
120 |
|
|
121 |
8630 |
StringWriter writer = new StringWriter(); |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
8629 |
String key = context.getTransformationContext().getId(); |
126 |
8630 |
if (key == null) { |
127 |
1 |
key = "unknown namespace"; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
8630 |
this.velocityManager.evaluate(writer, key, new StringReader(cleanedContent)); |
132 |
8630 |
result = writer.toString(); |
133 |
|
|
134 |
|
|
135 |
8630 |
if (filter != null) { |
136 |
8630 |
result = filter.after(result, velocityContext); |
137 |
|
} |
138 |
|
} catch (XWikiVelocityException e) { |
139 |
0 |
throw new MacroExecutionException("Failed to evaluate Velocity Macro for content [" + content + "]", e); |
140 |
|
} |
141 |
|
|
142 |
8629 |
return result; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@return |
148 |
|
@since |
149 |
|
|
|
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
150 |
8629 |
private VelocityMacroFilter getFilter(VelocityMacroParameters parameters)... |
151 |
|
{ |
152 |
8630 |
String filterName = parameters.getFilter(); |
153 |
|
|
154 |
8630 |
if (StringUtils.isEmpty(filterName)) { |
155 |
8598 |
filterName = this.configuration.getFilter(); |
156 |
|
|
157 |
8599 |
if (StringUtils.isEmpty(filterName)) { |
158 |
0 |
filterName = null; |
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
8630 |
VelocityMacroFilter filter = null; |
163 |
8630 |
if (filterName != null) { |
164 |
8630 |
try { |
165 |
8630 |
filter = getComponentManager().getInstance(VelocityMacroFilter.class, filterName); |
166 |
|
} catch (ComponentLookupException e) { |
167 |
0 |
this.logger.error("Can't find velocity macro filter", e); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
8630 |
return filter; |
172 |
|
} |
173 |
|
} |