1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.internal.renderer; |
21 |
|
|
22 |
|
import java.util.Collection; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.LinkedList; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
import java.util.SortedMap; |
29 |
|
import java.util.TreeMap; |
30 |
|
|
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.xwiki.annotation.Annotation; |
33 |
|
import org.xwiki.annotation.content.AlteredContent; |
34 |
|
import org.xwiki.annotation.content.ContentAlterer; |
35 |
|
import org.xwiki.annotation.renderer.AnnotationEvent; |
36 |
|
import org.xwiki.annotation.renderer.AnnotationEvent.AnnotationEventType; |
37 |
|
import org.xwiki.rendering.listener.Listener; |
38 |
|
import org.xwiki.rendering.listener.MetaData; |
39 |
|
import org.xwiki.rendering.listener.QueueListener; |
40 |
|
import org.xwiki.rendering.listener.chaining.ChainingListener; |
41 |
|
import org.xwiki.rendering.listener.chaining.ListenerChain; |
42 |
|
import org.xwiki.rendering.syntax.Syntax; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
|
|
| 91.2% |
Uncovered Elements: 11 (125) |
Complexity: 34 |
Complexity Density: 0.44 |
|
55 |
|
public class AnnotationGeneratorChainingListener extends QueueListener implements ChainingListener |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private static final long serialVersionUID = -2790330640900288463L; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private ListenerChain chain; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private StringBuffer plainTextContent = new StringBuffer(); |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private SortedMap<Integer, Event> eventsMapping = new TreeMap<Integer, Event>(); |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private Map<Event, AlteredContent> alteredEventsContent = new HashMap<Event, AlteredContent>(); |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
private Collection<Annotation> annotations = Collections.<Annotation> emptyList(); |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
private ContentAlterer selectionAlterer; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
private Map<Event, SortedMap<Integer, List<AnnotationEvent>>> bookmarks = |
99 |
|
new HashMap<Event, SortedMap<Integer, List<AnnotationEvent>>>(); |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@param |
105 |
|
@param |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
107 |
132 |
public AnnotationGeneratorChainingListener(ContentAlterer selectionAlterer, ListenerChain listenerChain)... |
108 |
|
{ |
109 |
132 |
this.chain = listenerChain; |
110 |
132 |
this.selectionAlterer = selectionAlterer; |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
113 |
1716 |
@Override... |
114 |
|
public void onWord(String word) |
115 |
|
{ |
116 |
|
|
117 |
1716 |
super.onWord(word); |
118 |
|
|
119 |
1716 |
plainTextContent.append(word); |
120 |
|
|
121 |
1716 |
eventsMapping.put(plainTextContent.length() - 1, getLast()); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
124 |
506 |
@Override... |
125 |
|
public void onSpecialSymbol(char symbol) |
126 |
|
{ |
127 |
506 |
super.onSpecialSymbol(symbol); |
128 |
506 |
plainTextContent.append("" + symbol); |
129 |
506 |
eventsMapping.put(plainTextContent.length() - 1, getLast()); |
130 |
|
} |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
132 |
22 |
@Override... |
133 |
|
public void onVerbatim(String content, boolean inline, Map<String, String> parameters) |
134 |
|
{ |
135 |
22 |
super.onVerbatim(content, inline, parameters); |
136 |
22 |
handleRawText(content); |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
@param |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
144 |
30 |
private void handleRawText(String text)... |
145 |
|
{ |
146 |
|
|
147 |
30 |
AlteredContent cleanedContent = selectionAlterer.alter(text); |
148 |
|
|
149 |
30 |
String cleanedContentString = cleanedContent.getContent().toString(); |
150 |
30 |
if (!StringUtils.isEmpty(cleanedContentString)) { |
151 |
14 |
plainTextContent.append(cleanedContentString); |
152 |
14 |
eventsMapping.put(plainTextContent.length() - 1, getLast()); |
153 |
|
|
154 |
14 |
alteredEventsContent.put(getLast(), cleanedContent); |
155 |
|
} |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
158 |
8 |
@Override... |
159 |
|
public void onRawText(String text, Syntax syntax) |
160 |
|
{ |
161 |
|
|
162 |
8 |
super.onRawText(text, syntax); |
163 |
|
|
164 |
|
|
165 |
8 |
handleRawText(text); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
@inheritDoc |
170 |
|
|
171 |
|
@since |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
173 |
132 |
@Override... |
174 |
|
public void endDocument(MetaData metadata) |
175 |
|
{ |
176 |
132 |
super.endDocument(metadata); |
177 |
|
|
178 |
|
|
179 |
132 |
mapAnnotations(); |
180 |
|
|
181 |
|
|
182 |
132 |
ChainingListener renderer = chain.getNextListener(getClass()); |
183 |
|
|
184 |
|
|
185 |
132 |
consumeEvents(renderer); |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
|
|
| 77.4% |
Uncovered Elements: 7 (31) |
Complexity: 7 |
Complexity Density: 0.37 |
|
192 |
132 |
private void mapAnnotations()... |
193 |
|
{ |
194 |
132 |
for (Annotation ann : annotations) { |
195 |
|
|
196 |
88 |
String annotationContext = ann.getSelectionInContext(); |
197 |
88 |
if (StringUtils.isEmpty(annotationContext)) { |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
0 |
continue; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
88 |
String alteredsLeftContext = |
206 |
88 |
StringUtils.isEmpty(ann.getSelectionLeftContext()) ? "" : selectionAlterer.alter( |
207 |
|
ann.getSelectionLeftContext()).getContent().toString(); |
208 |
88 |
String alteredRightContext = |
209 |
88 |
StringUtils.isEmpty(ann.getSelectionRightContext()) ? "" : selectionAlterer.alter( |
210 |
|
ann.getSelectionRightContext()).getContent().toString(); |
211 |
88 |
String alteredSelection = |
212 |
88 |
StringUtils.isEmpty(ann.getSelection()) ? "" : selectionAlterer.alter(ann.getSelection()).getContent() |
213 |
|
.toString(); |
214 |
88 |
String cleanedContext = alteredsLeftContext + alteredSelection + alteredRightContext; |
215 |
|
|
216 |
88 |
int contextIndex = plainTextContent.indexOf(cleanedContext); |
217 |
88 |
if (contextIndex >= 0) { |
218 |
|
|
219 |
88 |
int alteredSelectionStartIndex = alteredsLeftContext.length(); |
220 |
88 |
int alteredSelectionEndIndex = alteredSelectionStartIndex + alteredSelection.length() - 1; |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
88 |
Object[] startEvt = getEventAndOffset(contextIndex + alteredSelectionStartIndex, false); |
225 |
88 |
Object[] endEvt = getEventAndOffset(contextIndex + alteredSelectionEndIndex, true); |
226 |
88 |
if (startEvt != null & endEvt != null) { |
227 |
|
|
228 |
88 |
addBookmark((Event) startEvt[0], new AnnotationEvent(AnnotationEventType.START, ann), |
229 |
|
(Integer) startEvt[1]); |
230 |
88 |
addBookmark((Event) endEvt[0], new AnnotationEvent(AnnotationEventType.END, ann), |
231 |
|
(Integer) endEvt[1]); |
232 |
|
} else { |
233 |
|
|
234 |
|
|
235 |
0 |
continue; |
236 |
|
} |
237 |
|
} else { |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
0 |
continue; |
242 |
|
} |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
@param |
251 |
|
@param |
252 |
|
@return |
253 |
|
|
254 |
|
|
|
|
| 95.8% |
Uncovered Elements: 1 (24) |
Complexity: 5 |
Complexity Density: 0.31 |
|
255 |
176 |
private Object[] getEventAndOffset(int index, boolean isEnd)... |
256 |
|
{ |
257 |
176 |
Map.Entry<Integer, Event> previous = null; |
258 |
176 |
for (Map.Entry<Integer, Event> range : eventsMapping.entrySet()) { |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
1768 |
if (index <= range.getKey()) { |
263 |
|
|
264 |
176 |
Event evt = range.getValue(); |
265 |
|
|
266 |
176 |
int startIndex = 0; |
267 |
176 |
if (previous != null) { |
268 |
162 |
startIndex = previous.getKey() + 1; |
269 |
|
} |
270 |
|
|
271 |
176 |
int offset = index - startIndex; |
272 |
|
|
273 |
|
|
274 |
176 |
AlteredContent alteredEventContent = alteredEventsContent.get(evt); |
275 |
176 |
if (alteredEventContent != null) { |
276 |
8 |
offset = alteredEventContent.getInitialOffset(offset); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
176 |
if (isEnd) { |
281 |
88 |
offset += 1; |
282 |
|
} |
283 |
|
|
284 |
|
|
285 |
176 |
return new Object[] {evt, offset}; |
286 |
|
} |
287 |
|
|
288 |
1592 |
previous = range; |
289 |
|
} |
290 |
|
|
291 |
0 |
return null; |
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
@param |
298 |
|
@param |
299 |
|
@param |
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
301 |
176 |
protected void addBookmark(Event renderingEvent, AnnotationEvent annotationEvent, int offset)... |
302 |
|
{ |
303 |
176 |
SortedMap<Integer, List<AnnotationEvent>> mappings = bookmarks.get(renderingEvent); |
304 |
176 |
if (mappings == null) { |
305 |
144 |
mappings = new TreeMap<Integer, List<AnnotationEvent>>(); |
306 |
144 |
bookmarks.put(renderingEvent, mappings); |
307 |
|
} |
308 |
176 |
List<AnnotationEvent> events = mappings.get(offset); |
309 |
176 |
if (events == null) { |
310 |
173 |
events = new LinkedList<AnnotationEvent>(); |
311 |
173 |
mappings.put(offset, events); |
312 |
|
} |
313 |
|
|
314 |
176 |
addAnnotationEvent(annotationEvent, events); |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
@param |
322 |
|
@param |
323 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 6 |
Complexity Density: 1.2 |
|
324 |
176 |
protected void addAnnotationEvent(AnnotationEvent evt, List<AnnotationEvent> list)... |
325 |
|
{ |
326 |
|
|
327 |
|
|
328 |
176 |
if (list.size() == 0 || evt.getType() == AnnotationEventType.START |
329 |
|
|| list.get(list.size() - 1).getType() == AnnotationEventType.END) { |
330 |
175 |
list.add(evt); |
331 |
|
} else { |
332 |
|
|
333 |
1 |
int index = 0; |
334 |
1 |
for (index = 0; index < list.size() && list.get(index).getType() != AnnotationEventType.START; index++) { |
335 |
|
|
336 |
|
} |
337 |
1 |
list.add(index, evt); |
338 |
|
} |
339 |
|
} |
340 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
341 |
132 |
@Override... |
342 |
|
public void consumeEvents(Listener listener) |
343 |
|
{ |
344 |
|
|
345 |
|
|
346 |
4686 |
while (!isEmpty()) { |
347 |
|
|
348 |
4554 |
Event event = getFirst(); |
349 |
|
|
350 |
4554 |
event.eventType.fireEvent(listener, event.eventParameters); |
351 |
|
|
352 |
4554 |
remove(); |
353 |
|
} |
354 |
|
} |
355 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
356 |
0 |
@Override... |
357 |
|
public ListenerChain getListenerChain() |
358 |
|
{ |
359 |
0 |
return chain; |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
@param |
366 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
367 |
70 |
public void setAnnotations(Collection<Annotation> annotations)... |
368 |
|
{ |
369 |
70 |
this.annotations = annotations; |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
|
@return |
374 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
375 |
2252 |
public SortedMap<Integer, List<AnnotationEvent>> getAnnotationEvents()... |
376 |
|
{ |
377 |
2252 |
return bookmarks.get(getFirst()); |
378 |
|
} |
379 |
|
} |