1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.maintainer; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
|
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.xwiki.annotation.Annotation; |
32 |
|
import org.xwiki.annotation.content.AlteredContent; |
33 |
|
import org.xwiki.annotation.content.ContentAlterer; |
34 |
|
import org.xwiki.annotation.io.IOService; |
35 |
|
import org.xwiki.annotation.io.IOTargetService; |
36 |
|
import org.xwiki.component.manager.ComponentManager; |
37 |
|
import org.xwiki.rendering.block.XDOM; |
38 |
|
import org.xwiki.rendering.parser.Parser; |
39 |
|
import org.xwiki.rendering.renderer.PrintRenderer; |
40 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
41 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
42 |
|
import org.xwiki.rendering.syntax.Syntax; |
43 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
44 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
|
|
| 94.6% |
Uncovered Elements: 11 (205) |
Complexity: 51 |
Complexity Density: 0.36 |
|
52 |
|
public abstract class AbstractAnnotationMaintainer implements AnnotationMaintainer |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
protected IOService ioService; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@Inject |
64 |
|
protected IOTargetService ioContentService; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("whitespace") |
72 |
|
protected ContentAlterer spaceStripperContentAlterer; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
protected ComponentManager componentManager; |
79 |
|
|
|
|
| 87.5% |
Uncovered Elements: 3 (24) |
Complexity: 5 |
Complexity Density: 0.28 |
|
80 |
49 |
@Override... |
81 |
|
public void updateAnnotations(String target, String previousContent, String currentContent) |
82 |
|
throws MaintainerServiceException |
83 |
|
{ |
84 |
49 |
Collection<Annotation> annotations; |
85 |
49 |
try { |
86 |
49 |
annotations = ioService.getAnnotations(target); |
87 |
|
|
88 |
49 |
if (annotations.size() == 0) { |
89 |
|
|
90 |
0 |
return; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
49 |
List<Annotation> toUpdate = new ArrayList<Annotation>(); |
95 |
|
|
96 |
|
|
97 |
49 |
String syntaxId = ioContentService.getSourceSyntax(target); |
98 |
49 |
String renderedPreviousContent = renderPlainText(previousContent, syntaxId); |
99 |
49 |
String renderedCurrentContent = renderPlainText(currentContent, syntaxId); |
100 |
|
|
101 |
|
|
102 |
49 |
Collection<XDelta> differences = |
103 |
|
getDiffService().getDifferences(renderedPreviousContent, renderedCurrentContent); |
104 |
|
|
105 |
|
|
106 |
49 |
if (differences.size() > 0) { |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
46 |
AlteredContent spacelessRenderedPreviousContent = |
111 |
|
spaceStripperContentAlterer.alter(renderedPreviousContent); |
112 |
|
|
113 |
46 |
for (Annotation annotation : annotations) { |
114 |
46 |
boolean wasUpdated = recomputeProperties(annotation, differences, renderedPreviousContent, |
115 |
|
spacelessRenderedPreviousContent, renderedCurrentContent); |
116 |
46 |
if (wasUpdated) { |
117 |
35 |
toUpdate.add(annotation); |
118 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
49 |
ioService.updateAnnotations(target, toUpdate); |
124 |
|
} catch (Exception e) { |
125 |
0 |
throw new MaintainerServiceException("An exception occurred while updating annotations for content at " |
126 |
|
+ target, e); |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
@param |
135 |
|
@throws |
136 |
|
@return |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
138 |
98 |
private String renderPlainText(String content, String syntaxId) throws Exception... |
139 |
|
{ |
140 |
98 |
PrintRenderer renderer = componentManager.getInstance(PrintRenderer.class, "normalizer-plain/1.0"); |
141 |
|
|
142 |
|
|
143 |
98 |
Parser parser = componentManager.getInstance(Parser.class, syntaxId); |
144 |
98 |
XDOM xdom = parser.parse(new StringReader(content)); |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
98 |
SyntaxFactory syntaxFactory = componentManager.getInstance(SyntaxFactory.class); |
149 |
98 |
Syntax sourceSyntax = syntaxFactory.createSyntaxFromIdString(syntaxId); |
150 |
98 |
TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class); |
151 |
98 |
transformationManager.performTransformations(xdom, sourceSyntax); |
152 |
|
|
153 |
|
|
154 |
98 |
WikiPrinter printer = new DefaultWikiPrinter(); |
155 |
98 |
renderer.setPrinter(printer); |
156 |
|
|
157 |
98 |
xdom.traverse(renderer); |
158 |
|
|
159 |
98 |
return printer.toString(); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
@param |
169 |
|
@param |
170 |
|
@param |
171 |
|
@param |
172 |
|
|
173 |
|
@param |
174 |
|
@return |
175 |
|
|
176 |
|
|
|
|
| 93.9% |
Uncovered Elements: 5 (82) |
Complexity: 22 |
Complexity Density: 0.38 |
|
177 |
46 |
protected boolean recomputeProperties(Annotation annotation, Collection<XDelta> differences,... |
178 |
|
String renderedPreviousContent, AlteredContent spacelessPreviousContent, String renderedCurrentContent) |
179 |
|
{ |
180 |
46 |
boolean updated = false; |
181 |
|
|
182 |
|
|
183 |
46 |
if (annotation.getState().equals(AnnotationState.ALTERED)) { |
184 |
0 |
return updated; |
185 |
|
} |
186 |
|
|
187 |
46 |
String spacelessLeftContext = |
188 |
46 |
StringUtils.isEmpty(annotation.getSelectionLeftContext()) ? "" : spaceStripperContentAlterer.alter( |
189 |
|
annotation.getSelectionLeftContext()).getContent().toString(); |
190 |
46 |
String spacelessRightContext = |
191 |
46 |
StringUtils.isEmpty(annotation.getSelectionRightContext()) ? "" : spaceStripperContentAlterer.alter( |
192 |
|
annotation.getSelectionRightContext()).getContent().toString(); |
193 |
46 |
String spacelessSelection = |
194 |
46 |
StringUtils.isEmpty(annotation.getSelection()) ? "" : spaceStripperContentAlterer.alter( |
195 |
|
annotation.getSelection()).getContent().toString(); |
196 |
46 |
String spacelessContext = spacelessLeftContext + spacelessSelection + spacelessRightContext; |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
46 |
int selectionIndex = spacelessLeftContext.length(); |
201 |
46 |
int lastSelectionIndex = selectionIndex + spacelessSelection.length() - 1; |
202 |
|
|
203 |
|
|
204 |
46 |
int cStart = spacelessPreviousContent.getContent().toString().indexOf(spacelessContext); |
205 |
|
|
206 |
46 |
if (spacelessContext.length() == 0 || cStart < 0) { |
207 |
|
|
208 |
|
|
209 |
0 |
return updated; |
210 |
|
} |
211 |
|
|
212 |
46 |
int cEnd = cStart + spacelessContext.length(); |
213 |
46 |
int sStart = cStart + selectionIndex; |
214 |
46 |
int sEnd = cStart + lastSelectionIndex; |
215 |
|
|
216 |
|
|
217 |
46 |
cStart = spacelessPreviousContent.getInitialOffset(cStart); |
218 |
|
|
219 |
|
|
220 |
46 |
cEnd = spacelessPreviousContent.getInitialOffset(cEnd - 1) + 1; |
221 |
46 |
sStart = spacelessPreviousContent.getInitialOffset(sStart); |
222 |
|
|
223 |
46 |
sEnd = spacelessPreviousContent.getInitialOffset(sEnd) + 1; |
224 |
|
|
225 |
|
|
226 |
46 |
AnnotationState initialState = annotation.getState(); |
227 |
|
|
228 |
|
|
229 |
46 |
int alteredCStart = cStart; |
230 |
46 |
int alteredSLength = sEnd - sStart; |
231 |
|
|
232 |
46 |
for (XDelta diff : differences) { |
233 |
62 |
int dStart = diff.getOffset(); |
234 |
62 |
int dEnd = diff.getOffset() + diff.getOriginal().length(); |
235 |
|
|
236 |
|
|
237 |
62 |
if (dEnd <= sStart) { |
238 |
13 |
alteredCStart += diff.getSignedDelta(); |
239 |
|
} |
240 |
|
|
241 |
62 |
if (dEnd > sStart && dStart >= sStart && dStart < sEnd && dEnd <= sEnd) { |
242 |
|
|
243 |
24 |
alteredSLength += diff.getSignedDelta(); |
244 |
24 |
annotation.setState(AnnotationState.UPDATED); |
245 |
24 |
updated = true; |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
62 |
if (dStart <= sStart && dEnd >= sEnd) { |
250 |
|
|
251 |
2 |
annotation.setState(AnnotationState.ALTERED); |
252 |
2 |
updated = true; |
253 |
2 |
break; |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
60 |
if (dStart < sStart && dEnd > sStart && dEnd <= sEnd) { |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
7 |
alteredCStart += diff.getSignedDelta(); |
262 |
7 |
annotation.setState(AnnotationState.UPDATED); |
263 |
7 |
updated = true; |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
60 |
if (dStart < sEnd && dEnd > sEnd) { |
268 |
|
|
269 |
7 |
annotation.setState(AnnotationState.UPDATED); |
270 |
7 |
updated = true; |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
46 |
if (annotation.getState() != AnnotationState.ALTERED) { |
275 |
|
|
276 |
44 |
int cLeftSize = sStart - cStart; |
277 |
44 |
int cRightSize = cEnd - sEnd; |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
44 |
if (annotation.getState() == AnnotationState.UPDATED && initialState == AnnotationState.SAFE) { |
282 |
28 |
annotation.setOriginalSelection(annotation.getSelection()); |
283 |
|
|
284 |
28 |
updated = true; |
285 |
|
} |
286 |
|
|
287 |
44 |
String originalLeftContext = annotation.getSelectionLeftContext(); |
288 |
44 |
String originalSelection = annotation.getSelection(); |
289 |
44 |
String originalRightContext = annotation.getSelectionRightContext(); |
290 |
|
|
291 |
44 |
String contextLeft = renderedCurrentContent.substring(alteredCStart, alteredCStart + cLeftSize); |
292 |
44 |
String selection = |
293 |
|
renderedCurrentContent.substring(alteredCStart + cLeftSize, alteredCStart + cLeftSize + alteredSLength); |
294 |
44 |
String contextRight = |
295 |
|
renderedCurrentContent.substring(alteredCStart + cLeftSize + alteredSLength, alteredCStart + cLeftSize |
296 |
|
+ alteredSLength + cRightSize); |
297 |
|
|
298 |
44 |
annotation.setSelection(selection, contextLeft, contextRight); |
299 |
|
|
300 |
|
|
301 |
44 |
ensureUnique(annotation, renderedCurrentContent, alteredCStart, cLeftSize, alteredSLength, cRightSize); |
302 |
|
|
303 |
|
|
304 |
44 |
updated = |
305 |
|
updated |
306 |
|
|| !(selection.equals(originalSelection) && contextLeft.equals(originalLeftContext) && contextRight |
307 |
|
.equals(originalRightContext)); |
308 |
|
} |
309 |
|
|
310 |
46 |
return updated; |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
@param |
318 |
|
@param |
319 |
|
@param |
320 |
|
|
321 |
|
@param |
322 |
|
@param |
323 |
|
|
324 |
|
|
|
|
| 94.3% |
Uncovered Elements: 3 (53) |
Complexity: 13 |
Complexity Density: 0.35 |
|
325 |
44 |
private void ensureUnique(Annotation annotation, String content, int cStart, int cLeftSize, int sLength,... |
326 |
|
int cRightSize) |
327 |
|
{ |
328 |
|
|
329 |
44 |
List<Integer> occurrences = getOccurrences(content, annotation.getSelectionInContext(), cStart); |
330 |
44 |
if (occurrences.size() == 0) { |
331 |
|
|
332 |
35 |
return; |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
9 |
boolean isUnique = false; |
337 |
9 |
int cLength = cLeftSize + sLength + cRightSize; |
338 |
|
|
339 |
9 |
int expansionLeft = 0; |
340 |
9 |
int expansionRight = 0; |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
9 |
char charLeft = content.charAt(cStart - expansionLeft); |
347 |
9 |
char charRight = content.charAt(cStart + cLength + expansionRight - 1); |
348 |
44 |
while (!isUnique) { |
349 |
35 |
boolean updated = false; |
350 |
|
|
351 |
|
|
352 |
35 |
if (cStart - expansionLeft - 1 > 0) { |
353 |
33 |
expansionLeft++; |
354 |
33 |
charLeft = content.charAt(cStart - expansionLeft); |
355 |
33 |
updated = true; |
356 |
|
} |
357 |
35 |
if (cStart + cLength + expansionRight + 1 <= content.length()) { |
358 |
26 |
expansionRight++; |
359 |
26 |
charRight = content.charAt(cStart + cLength + expansionRight - 1); |
360 |
26 |
updated = true; |
361 |
|
} |
362 |
35 |
if (!updated) { |
363 |
|
|
364 |
0 |
break; |
365 |
|
} |
366 |
35 |
if (charLeft == ' ' || charRight == ' ') { |
367 |
|
|
368 |
13 |
continue; |
369 |
|
} |
370 |
|
|
371 |
22 |
isUnique = true; |
372 |
|
|
373 |
22 |
for (int occurence : occurrences) { |
374 |
|
|
375 |
|
|
376 |
24 |
Character occurenceCharLeft = getSafeCharacter(content, occurence - expansionLeft); |
377 |
24 |
Character occurenceCharRight = getSafeCharacter(content, occurence + cLength + expansionRight - 1); |
378 |
24 |
if ((occurenceCharLeft != null && occurenceCharLeft.charValue() == charLeft) |
379 |
|
&& (occurenceCharRight != null && occurenceCharRight.charValue() == charRight)) { |
380 |
13 |
isUnique = false; |
381 |
13 |
break; |
382 |
|
} |
383 |
|
} |
384 |
|
} |
385 |
9 |
if (isUnique) { |
386 |
|
|
387 |
|
|
388 |
|
|
389 |
9 |
expansionLeft = expansionLeft + toNextWord(content, cStart - expansionLeft, true); |
390 |
9 |
expansionRight = expansionRight + toNextWord(content, cStart + cLength + expansionRight, false); |
391 |
|
|
392 |
9 |
String contextLeft = content.substring(cStart - expansionLeft, cStart + cLeftSize); |
393 |
9 |
String selection = content.substring(cStart + cLeftSize, cStart + cLeftSize + sLength); |
394 |
9 |
String contextRight = content.substring(cStart + cLeftSize + sLength, cStart + cLength + expansionRight); |
395 |
|
|
396 |
9 |
annotation.setSelection(selection, contextLeft, contextRight); |
397 |
|
} else { |
398 |
|
|
399 |
|
|
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
@param |
407 |
|
@param |
408 |
|
@param |
409 |
|
@return |
410 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
411 |
44 |
private List<Integer> getOccurrences(String subject, String pattern, int exclude)... |
412 |
|
{ |
413 |
44 |
List<Integer> indexes = new ArrayList<Integer>(); |
414 |
44 |
int lastIndex = subject.indexOf(pattern); |
415 |
99 |
while (lastIndex != -1) { |
416 |
55 |
if (lastIndex != exclude) { |
417 |
11 |
indexes.add(lastIndex); |
418 |
|
} |
419 |
55 |
lastIndex = subject.indexOf(pattern, lastIndex + 1); |
420 |
|
} |
421 |
|
|
422 |
44 |
return indexes; |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
@param |
431 |
|
@param |
432 |
|
@param |
433 |
|
@return |
434 |
|
|
435 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
436 |
18 |
private int toNextWord(String subject, int position, boolean toLeft)... |
437 |
|
{ |
438 |
18 |
int expansion = 1; |
439 |
|
|
440 |
|
|
441 |
18 |
boolean isSpaceOrEnd = |
442 |
18 |
toLeft ? position - expansion < 0 || subject.charAt(position - expansion) == ' ' |
443 |
|
: position + expansion > subject.length() || subject.charAt(position + expansion - 1) == ' '; |
444 |
61 |
while (!isSpaceOrEnd) { |
445 |
43 |
expansion++; |
446 |
43 |
isSpaceOrEnd = |
447 |
43 |
toLeft ? position - expansion < 0 || subject.charAt(position - expansion) == ' ' |
448 |
|
: position + expansion > subject.length() || subject.charAt(position + expansion - 1) == ' '; |
449 |
|
} |
450 |
|
|
451 |
18 |
return expansion - 1; |
452 |
|
} |
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
|
458 |
|
@param |
459 |
|
@param |
460 |
|
@return |
461 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
462 |
48 |
private Character getSafeCharacter(String content, int position)... |
463 |
|
{ |
464 |
48 |
if (position >= 0 && position < content.length()) { |
465 |
46 |
return content.charAt(position); |
466 |
|
} else { |
467 |
2 |
return null; |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
|
@return |
473 |
|
|
474 |
|
public abstract DiffService getDiffService(); |
475 |
|
} |