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