1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.listener.chaining

File LookaheadChainingListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
129
66
1
561
402
69
0.53
1.95
66
1.05

Classes

Class Line # Actions
LookaheadChainingListener 40 129 0% 69 15
0.9253731492.5%
 

Contributing tests

This file is covered by 296 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.listener.chaining;
21   
22    import java.util.Map;
23   
24    import org.xwiki.rendering.listener.Format;
25    import org.xwiki.rendering.listener.HeaderLevel;
26    import org.xwiki.rendering.listener.ListType;
27    import org.xwiki.rendering.listener.MetaData;
28    import org.xwiki.rendering.listener.QueueListener;
29    import org.xwiki.rendering.listener.QueueListener.Event;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.syntax.Syntax;
32   
33    /**
34    * Stores events without emitting them back in order to accumulate them and to provide a lookahead feature. The
35    * lookahead depth is configurable.
36    *
37    * @version $Id: 25d19fbbf266a01e7a8939e7ba6573992fcdc54a $
38    * @since 1.8RC1
39    */
 
40    public class LookaheadChainingListener extends AbstractChainingListener
41    {
42    private QueueListener previousEvents = new QueueListener();
43   
44    private int lookaheadDepth;
45   
 
46  597 toggle public LookaheadChainingListener(ListenerChain listenerChain, int lookaheadDepth)
47    {
48  597 setListenerChain(listenerChain);
49  597 setLookaheadDepth(lookaheadDepth);
50    }
51   
52    /**
53    * Redefine the stacking depth.
54    *
55    * @param lookaheadDepth the new depth to set
56    * @since 10.5RC1
57    */
 
58  780 toggle protected void setLookaheadDepth(int lookaheadDepth)
59    {
60  780 this.lookaheadDepth = lookaheadDepth;
61    }
62   
63    /**
64    * @return the list of stacked events
65    * @since 10.5RC1
66    */
 
67  183 toggle protected QueueListener getPreviousEvents()
68    {
69  183 return this.previousEvents;
70    }
71   
 
72  316 toggle public Event getNextEvent()
73    {
74  316 return getNextEvent(1);
75    }
76   
 
77  321 toggle public Event getNextEvent(int depth)
78    {
79  321 return this.previousEvents.getEvent(depth);
80    }
81   
 
82  28 toggle @Override
83    public void beginDefinitionDescription()
84    {
85  28 this.previousEvents.beginDefinitionDescription();
86  28 firePreviousEvent();
87    }
88   
89    /**
90    * {@inheritDoc}
91    *
92    * @since 2.0RC1
93    */
 
94  29 toggle @Override
95    public void beginDefinitionList(Map<String, String> parameters)
96    {
97  29 this.previousEvents.beginDefinitionList(parameters);
98  29 firePreviousEvent();
99    }
100   
 
101  24 toggle @Override
102    public void beginDefinitionTerm()
103    {
104  24 this.previousEvents.beginDefinitionTerm();
105  24 firePreviousEvent();
106    }
107   
108    /**
109    * {@inheritDoc}
110    *
111    * @since 3.0M2
112    */
 
113  349 toggle @Override
114    public void beginDocument(MetaData metadata)
115    {
116  349 this.previousEvents.beginDocument(metadata);
117  349 flush();
118    }
119   
 
120  71 toggle @Override
121    public void beginGroup(Map<String, String> parameters)
122    {
123  71 this.previousEvents.beginGroup(parameters);
124  71 firePreviousEvent();
125    }
126   
 
127  133 toggle @Override
128    public void beginFormat(Format format, Map<String, String> parameters)
129    {
130  133 this.previousEvents.beginFormat(format, parameters);
131  133 firePreviousEvent();
132    }
133   
 
134  90 toggle @Override
135    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
136    {
137  90 this.previousEvents.beginHeader(level, id, parameters);
138  90 firePreviousEvent();
139    }
140   
141    /**
142    * {@inheritDoc}
143    *
144    * @since 2.5RC1
145    */
 
146  91 toggle @Override
147    public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
148    {
149  91 this.previousEvents.beginLink(reference, freestanding, parameters);
150  91 firePreviousEvent();
151    }
152   
 
153  191 toggle @Override
154    public void beginList(ListType type, Map<String, String> parameters)
155    {
156  191 this.previousEvents.beginList(type, parameters);
157  191 firePreviousEvent();
158    }
159   
 
160  0 toggle @Override
161    public void beginListItem()
162    {
163  0 this.previousEvents.beginListItem();
164  0 firePreviousEvent();
165    }
166   
 
167  271 toggle @Override
168    public void beginListItem(Map<String, String> parameters)
169    {
170  271 this.previousEvents.beginListItem(parameters);
171  271 firePreviousEvent();
172    }
173   
 
174  49 toggle @Override
175    public void beginMacroMarker(String name, Map<String, String> parameters, String content, boolean isInline)
176    {
177  49 this.previousEvents.beginMacroMarker(name, parameters, content, isInline);
178  49 firePreviousEvent();
179    }
180   
181    /**
182    * {@inheritDoc}
183    *
184    * @since 3.0M2
185    */
 
186  13 toggle @Override
187    public void beginMetaData(MetaData metadata)
188    {
189  13 this.previousEvents.beginMetaData(metadata);
190  13 firePreviousEvent();
191    }
192   
 
193  334 toggle @Override
194    public void beginParagraph(Map<String, String> parameters)
195    {
196  334 this.previousEvents.beginParagraph(parameters);
197  334 firePreviousEvent();
198    }
199   
 
200  13 toggle @Override
201    public void beginQuotation(Map<String, String> parameters)
202    {
203  13 this.previousEvents.beginQuotation(parameters);
204  13 firePreviousEvent();
205    }
206   
 
207  21 toggle @Override
208    public void beginQuotationLine()
209    {
210  21 this.previousEvents.beginQuotationLine();
211  21 firePreviousEvent();
212    }
213   
 
214  95 toggle @Override
215    public void beginSection(Map<String, String> parameters)
216    {
217  95 this.previousEvents.beginSection(parameters);
218  95 firePreviousEvent();
219    }
220   
 
221  31 toggle @Override
222    public void beginTable(Map<String, String> parameters)
223    {
224  31 this.previousEvents.beginTable(parameters);
225  31 firePreviousEvent();
226    }
227   
 
228  51 toggle @Override
229    public void beginTableCell(Map<String, String> parameters)
230    {
231  51 this.previousEvents.beginTableCell(parameters);
232  51 firePreviousEvent();
233    }
234   
 
235  30 toggle @Override
236    public void beginTableHeadCell(Map<String, String> parameters)
237    {
238  30 this.previousEvents.beginTableHeadCell(parameters);
239  30 firePreviousEvent();
240    }
241   
 
242  46 toggle @Override
243    public void beginTableRow(Map<String, String> parameters)
244    {
245  46 this.previousEvents.beginTableRow(parameters);
246  46 firePreviousEvent();
247    }
248   
 
249  0 toggle @Override
250    public void beginFigure(Map<String, String> parameters)
251    {
252  0 this.previousEvents.beginFigure(parameters);
253  0 firePreviousEvent();
254    }
255   
 
256  0 toggle @Override
257    public void beginFigureCaption(Map<String, String> parameters)
258    {
259  0 this.previousEvents.beginFigureCaption(parameters);
260  0 firePreviousEvent();
261    }
262   
 
263  28 toggle @Override
264    public void endDefinitionDescription()
265    {
266  28 this.previousEvents.endDefinitionDescription();
267  28 firePreviousEvent();
268    }
269   
270    /**
271    * {@inheritDoc}
272    *
273    * @since 2.0RC1
274    */
 
275  29 toggle @Override
276    public void endDefinitionList(Map<String, String> parameters)
277    {
278  29 this.previousEvents.endDefinitionList(parameters);
279  29 firePreviousEvent();
280    }
281   
 
282  24 toggle @Override
283    public void endDefinitionTerm()
284    {
285  24 this.previousEvents.endDefinitionTerm();
286  24 firePreviousEvent();
287    }
288   
289    /**
290    * {@inheritDoc}
291    *
292    * @since 3.0M2
293    */
 
294  639 toggle @Override
295    public void endDocument(MetaData metadata)
296    {
297  639 this.previousEvents.endDocument(metadata);
298  639 flush();
299    }
300   
 
301  71 toggle @Override
302    public void endGroup(Map<String, String> parameters)
303    {
304  71 this.previousEvents.endGroup(parameters);
305  71 firePreviousEvent();
306    }
307   
 
308  133 toggle @Override
309    public void endFormat(Format format, Map<String, String> parameters)
310    {
311  133 this.previousEvents.endFormat(format, parameters);
312  133 firePreviousEvent();
313    }
314   
 
315  90 toggle @Override
316    public void endHeader(HeaderLevel level, String id, Map<String, String> parameters)
317    {
318  90 this.previousEvents.endHeader(level, id, parameters);
319  90 firePreviousEvent();
320    }
321   
322    /**
323    * {@inheritDoc}
324    *
325    * @since 2.5RC1
326    */
 
327  91 toggle @Override
328    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
329    {
330  91 this.previousEvents.endLink(reference, freestanding, parameters);
331  91 firePreviousEvent();
332    }
333   
 
334  191 toggle @Override
335    public void endList(ListType type, Map<String, String> parameters)
336    {
337  191 this.previousEvents.endList(type, parameters);
338  191 firePreviousEvent();
339    }
340   
 
341  322 toggle @Override
342    public void endListItem()
343    {
344  322 this.previousEvents.endListItem();
345  322 firePreviousEvent();
346    }
347   
 
348  104 toggle @Override
349    public void endListItem(Map<String, String> parameters)
350    {
351  104 this.previousEvents.endListItem(parameters);
352  104 firePreviousEvent();
353    }
354   
 
355  49 toggle @Override
356    public void endMacroMarker(String name, Map<String, String> parameters, String content, boolean isInline)
357    {
358  49 this.previousEvents.endMacroMarker(name, parameters, content, isInline);
359  49 firePreviousEvent();
360    }
361   
362    /**
363    * {@inheritDoc}
364    *
365    * @since 3.0M2
366    */
 
367  13 toggle @Override
368    public void endMetaData(MetaData metadata)
369    {
370  13 this.previousEvents.endMetaData(metadata);
371  13 firePreviousEvent();
372    }
373   
 
374  333 toggle @Override
375    public void endParagraph(Map<String, String> parameters)
376    {
377  333 this.previousEvents.endParagraph(parameters);
378  333 firePreviousEvent();
379    }
380   
 
381  13 toggle @Override
382    public void endQuotation(Map<String, String> parameters)
383    {
384  13 this.previousEvents.endQuotation(parameters);
385  13 firePreviousEvent();
386    }
387   
 
388  21 toggle @Override
389    public void endQuotationLine()
390    {
391  21 this.previousEvents.endQuotationLine();
392  21 firePreviousEvent();
393    }
394   
 
395  95 toggle @Override
396    public void endSection(Map<String, String> parameters)
397    {
398  95 this.previousEvents.endSection(parameters);
399  95 firePreviousEvent();
400    }
401   
 
402  31 toggle @Override
403    public void endTable(Map<String, String> parameters)
404    {
405  31 this.previousEvents.endTable(parameters);
406  31 firePreviousEvent();
407    }
408   
 
409  149 toggle @Override
410    public void endTableCell(Map<String, String> parameters)
411    {
412  149 this.previousEvents.endTableCell(parameters);
413  149 firePreviousEvent();
414    }
415   
 
416  90 toggle @Override
417    public void endTableHeadCell(Map<String, String> parameters)
418    {
419  90 this.previousEvents.endTableHeadCell(parameters);
420  90 firePreviousEvent();
421    }
422   
 
423  46 toggle @Override
424    public void endTableRow(Map<String, String> parameters)
425    {
426  46 this.previousEvents.endTableRow(parameters);
427  46 firePreviousEvent();
428    }
429   
 
430  0 toggle @Override
431    public void endFigure(Map<String, String> parameters)
432    {
433  0 this.previousEvents.endFigure(parameters);
434  0 firePreviousEvent();
435    }
436   
 
437  0 toggle @Override
438    public void endFigureCaption(Map<String, String> parameters)
439    {
440  0 this.previousEvents.endFigureCaption(parameters);
441  0 firePreviousEvent();
442    }
443   
 
444  16 toggle @Override
445    public void onRawText(String text, Syntax syntax)
446    {
447  16 this.previousEvents.onRawText(text, syntax);
448  16 firePreviousEvent();
449    }
450   
 
451  12 toggle @Override
452    public void onEmptyLines(int count)
453    {
454  12 this.previousEvents.onEmptyLines(count);
455  12 firePreviousEvent();
456    }
457   
 
458  10 toggle @Override
459    public void onHorizontalLine(Map<String, String> parameters)
460    {
461  10 this.previousEvents.onHorizontalLine(parameters);
462  10 firePreviousEvent();
463    }
464   
 
465  6 toggle @Override
466    public void onId(String name)
467    {
468  6 this.previousEvents.onId(name);
469  6 firePreviousEvent();
470    }
471   
472    /**
473    * {@inheritDoc}
474    *
475    * @since 2.5RC1
476    */
 
477  37 toggle @Override
478    public void onImage(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
479    {
480  37 this.previousEvents.onImage(reference, freestanding, parameters);
481  37 firePreviousEvent();
482    }
483   
 
484  32 toggle @Override
485    public void onMacro(String id, Map<String, String> parameters, String content, boolean inline)
486    {
487  32 this.previousEvents.onMacro(id, parameters, content, inline);
488  32 firePreviousEvent();
489    }
490   
 
491  133 toggle @Override
492    public void onNewLine()
493    {
494  133 this.previousEvents.onNewLine();
495  133 firePreviousEvent();
496    }
497   
 
498  973 toggle @Override
499    public void onSpace()
500    {
501  973 this.previousEvents.onSpace();
502  973 firePreviousEvent();
503    }
504   
 
505  327 toggle @Override
506    public void onSpecialSymbol(char symbol)
507    {
508  327 this.previousEvents.onSpecialSymbol(symbol);
509  327 firePreviousEvent();
510    }
511   
 
512  22 toggle @Override
513    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
514    {
515  22 this.previousEvents.onVerbatim(content, inline, parameters);
516  22 firePreviousEvent();
517    }
518   
 
519  2032 toggle @Override
520    public void onWord(String word)
521    {
522  2032 this.previousEvents.onWord(word);
523  2032 firePreviousEvent();
524    }
525   
 
526  7134 toggle private void firePreviousEvent()
527    {
528  7134 if (this.previousEvents.size() > this.lookaheadDepth) {
529  5004 fireEvent();
530    }
531    }
532   
 
533  988 toggle private void flush()
534    {
535    // Ensure that all remaining events are flushed
536  4122 while (!this.previousEvents.isEmpty()) {
537  3134 fireEvent();
538    }
539    }
540   
 
541  8138 toggle private void fireEvent()
542    {
543  8138 Event event = this.previousEvents.remove();
544  8138 event.eventType.fireEvent(getListenerChain().getNextListener(getClass()), event.eventParameters);
545    }
546   
547    /**
548    * Transfer all passed events by removing the from the passed parameter and moving them to the beginning of the
549    * event stack.
550    *
551    * @param eventsToTransfer the collection of events to move
552    * @since 10.5RC1
553    */
 
554  183 toggle public void transferStart(QueueListener eventsToTransfer)
555    {
556  1638 while (!eventsToTransfer.isEmpty()) {
557  1455 Event event = eventsToTransfer.removeLast();
558  1455 this.previousEvents.offerFirst(event);
559    }
560    }
561    }