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.filter; |
21 |
|
|
22 |
|
import java.util.regex.Pattern; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.apache.velocity.VelocityContext; |
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.component.phase.Initializable; |
32 |
|
import org.xwiki.component.phase.InitializationException; |
33 |
|
import org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter; |
34 |
|
import org.xwiki.velocity.internal.util.InvalidVelocityException; |
35 |
|
import org.xwiki.velocity.internal.util.VelocityParser; |
36 |
|
import org.xwiki.velocity.internal.util.VelocityParserContext; |
37 |
|
import org.xwiki.velocity.internal.util.VelocityBlock.VelocityType; |
38 |
|
|
39 |
|
|
40 |
|
@link |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@Named("html") |
49 |
|
@Singleton |
|
|
| 95.6% |
Uncovered Elements: 4 (91) |
Complexity: 23 |
Complexity Density: 0.38 |
|
50 |
|
public class HTMLVelocityMacroFilter implements VelocityMacroFilter, Initializable |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private static final String BINDING_NEWLINE = "nl"; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private static final String NEWLINE = "\n"; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String BINDING_SPACE = "sp"; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String SPACE = " "; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private static final Pattern MSNEWLINE_PATTERN = Pattern.compile("\\r\\n|\\r"); |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
private Logger logger; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
private VelocityParser velocityParser; |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
10 |
@Override... |
89 |
|
public void initialize() throws InitializationException |
90 |
|
{ |
91 |
10 |
this.velocityParser = new VelocityParser(); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
94 |
46 |
@Override... |
95 |
|
public String before(String content, VelocityContext velocityContext) |
96 |
|
{ |
97 |
|
|
98 |
46 |
velocityContext.put(BINDING_NEWLINE, NEWLINE); |
99 |
46 |
velocityContext.put(BINDING_SPACE, SPACE); |
100 |
|
|
101 |
46 |
return clean(content); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
@param |
118 |
|
@return |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 8 |
Complexity Density: 0.33 |
|
120 |
46 |
public String clean(String content)... |
121 |
|
{ |
122 |
46 |
StringBuffer contentBuffer = new StringBuffer(); |
123 |
|
|
124 |
46 |
char[] array = MSNEWLINE_PATTERN.matcher(content).replaceAll(NEWLINE).toCharArray(); |
125 |
|
|
126 |
46 |
VelocityParserContext context = new VelocityParserContext(); |
127 |
46 |
FilterContext filterContext = new FilterContext(); |
128 |
|
|
129 |
46 |
int i = 0; |
130 |
286 |
while (i < array.length) { |
131 |
240 |
try { |
132 |
240 |
if (array[i] == '#') { |
133 |
40 |
i = cleanKeyWord(contentBuffer, array, i, context, filterContext); |
134 |
|
|
135 |
38 |
continue; |
136 |
200 |
} else if (array[i] == '$') { |
137 |
20 |
i = cleanVar(contentBuffer, array, i, context, filterContext); |
138 |
|
|
139 |
15 |
continue; |
140 |
180 |
} else if (Character.isWhitespace(array[i])) { |
141 |
94 |
if (!filterContext.removeWhiteSpaces && contentBuffer.length() > 0) { |
142 |
55 |
filterContext.foundWhiteSpace = true; |
143 |
|
} |
144 |
|
|
145 |
94 |
++i; |
146 |
|
|
147 |
94 |
continue; |
148 |
|
} |
149 |
|
} catch (InvalidVelocityException e) { |
150 |
7 |
this.logger.debug("Not a valid velocity keyword at char [" + i + "]", e); |
151 |
|
} |
152 |
|
|
153 |
93 |
flushWhiteSpaces(contentBuffer, filterContext, false); |
154 |
|
|
155 |
93 |
contentBuffer.append(array[i]); |
156 |
|
|
157 |
93 |
++i; |
158 |
|
} |
159 |
|
|
160 |
46 |
flushWhiteSpaces(contentBuffer, filterContext, true); |
161 |
|
|
162 |
46 |
return contentBuffer.toString(); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
@param |
169 |
|
@param |
170 |
|
@param |
171 |
|
@param |
172 |
|
@param |
173 |
|
@return |
174 |
|
@throws |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
176 |
40 |
private int cleanKeyWord(StringBuffer contentBuffer, char[] array, int currentIndex, VelocityParserContext context,... |
177 |
|
FilterContext filterContext) throws InvalidVelocityException |
178 |
|
{ |
179 |
40 |
int i = this.velocityParser.getKeyWord(array, currentIndex, null, context); |
180 |
|
|
181 |
38 |
if (context.getType() != VelocityType.COMMENT) { |
182 |
35 |
if (context.getType() == VelocityType.DIRECTIVE) { |
183 |
31 |
if (filterContext.wsGroup.length() == 0) { |
184 |
23 |
flushWhiteSpaces(filterContext.wsGroup, filterContext, false); |
185 |
|
} |
186 |
|
|
187 |
31 |
filterContext.wsGroup.append(array, currentIndex, i - currentIndex); |
188 |
|
|
189 |
31 |
filterContext.removeWhiteSpaces = true; |
190 |
|
} else { |
191 |
4 |
flushWhiteSpaces(contentBuffer, filterContext, false); |
192 |
|
|
193 |
4 |
contentBuffer.append(array, currentIndex, i - currentIndex); |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
38 |
return i; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@param |
205 |
|
@param |
206 |
|
@param |
207 |
|
@param |
208 |
|
@return |
209 |
|
@throws |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
211 |
20 |
private int cleanVar(StringBuffer contentBuffer, char[] array, int currentIndex, VelocityParserContext context,... |
212 |
|
FilterContext filterContext) throws InvalidVelocityException |
213 |
|
{ |
214 |
20 |
StringBuffer varName = new StringBuffer(); |
215 |
20 |
int i = this.velocityParser.getVar(array, currentIndex, varName, null, context); |
216 |
|
|
217 |
15 |
if (varName.toString().equals(BINDING_NEWLINE)) { |
218 |
8 |
flushWhiteSpaces(contentBuffer, filterContext, true); |
219 |
|
|
220 |
8 |
contentBuffer.append("${nl}"); |
221 |
8 |
filterContext.removeWhiteSpaces = true; |
222 |
|
} else { |
223 |
7 |
flushWhiteSpaces(contentBuffer, filterContext, false); |
224 |
|
|
225 |
7 |
contentBuffer.append(array, currentIndex, i - currentIndex); |
226 |
|
} |
227 |
|
|
228 |
15 |
return i; |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
@param |
238 |
|
@param |
239 |
|
@param@link |
240 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 6 |
Complexity Density: 0.6 |
|
241 |
181 |
private void flushWhiteSpaces(StringBuffer contentBuffer, FilterContext filterContext, boolean forceNoSpace)... |
242 |
|
{ |
243 |
181 |
if (filterContext.wsGroup.length() > 0) { |
244 |
23 |
boolean space = filterContext.wsGroup.charAt(0) == ' '; |
245 |
|
|
246 |
23 |
if (forceNoSpace && space) { |
247 |
2 |
contentBuffer.append(filterContext.wsGroup, 1, filterContext.wsGroup.length()); |
248 |
|
} else { |
249 |
21 |
contentBuffer.append(filterContext.wsGroup); |
250 |
|
} |
251 |
23 |
filterContext.wsGroup.setLength(0); |
252 |
|
} |
253 |
|
|
254 |
181 |
if (filterContext.foundWhiteSpace && !forceNoSpace) { |
255 |
24 |
contentBuffer.append(' '); |
256 |
|
} |
257 |
|
|
258 |
181 |
filterContext.foundWhiteSpace = false; |
259 |
181 |
filterContext.removeWhiteSpaces = false; |
260 |
|
} |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
262 |
0 |
@Override... |
263 |
|
public String after(String content, VelocityContext velocityContext) |
264 |
|
{ |
265 |
0 |
velocityContext.remove(BINDING_NEWLINE); |
266 |
0 |
velocityContext.remove(BINDING_SPACE); |
267 |
|
|
268 |
0 |
return content; |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
@version |
275 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 5 |
Complexity Density: 1 |
|
276 |
|
static class FilterContext |
277 |
|
{ |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
private boolean foundWhiteSpace; |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
private boolean removeWhiteSpaces; |
287 |
|
|
288 |
|
|
289 |
|
@see |
290 |
|
|
291 |
|
private StringBuffer wsGroup = new StringBuffer(); |
292 |
|
|
293 |
|
|
294 |
|
@return |
295 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
296 |
0 |
public boolean isFoundWhiteSpace()... |
297 |
|
{ |
298 |
0 |
return this.foundWhiteSpace; |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
|
@param |
303 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
304 |
0 |
public void setFoundWhiteSpace(boolean foundWhiteSpace)... |
305 |
|
{ |
306 |
0 |
this.foundWhiteSpace = foundWhiteSpace; |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
|
@return |
311 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
312 |
0 |
public boolean isRemoveWhiteSpaces()... |
313 |
|
{ |
314 |
0 |
return this.removeWhiteSpaces; |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
@param |
319 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
320 |
0 |
public void setRemoveWhiteSpaces(boolean removeWhiteSpaces)... |
321 |
|
{ |
322 |
0 |
this.removeWhiteSpaces = removeWhiteSpaces; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
@return |
327 |
|
|
328 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
329 |
0 |
public StringBuffer getWsGroup()... |
330 |
|
{ |
331 |
0 |
return this.wsGroup; |
332 |
|
} |
333 |
|
} |
334 |
|
} |