1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.listener.chaining

File BlockStateChainingListener.java

 

Coverage histogram

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

Code metrics

6
150
72
4
677
482
75
0.5
2.08
18
1.04

Classes

Class Line # Actions
BlockStateChainingListener 39 150 0% 75 12
0.9473684494.7%
BlockStateChainingListener.Event 41 0 - 0 0
-1.0 -
BlockStateChainingListener.ListState 668 0 - 0 0
-1.0 -
BlockStateChainingListener.DefinitionListState 673 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 961 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.ArrayDeque;
23    import java.util.Deque;
24    import java.util.Map;
25   
26    import org.xwiki.rendering.listener.Format;
27    import org.xwiki.rendering.listener.HeaderLevel;
28    import org.xwiki.rendering.listener.ListType;
29    import org.xwiki.rendering.listener.MetaData;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.syntax.Syntax;
32   
33    /**
34    * Indicates block element for which we are inside and previous blocks.
35    *
36    * @version $Id: 532ef932636ee482db1b1afa142694b8fded1b71 $
37    * @since 1.8RC1
38    */
 
39    public class BlockStateChainingListener extends AbstractChainingListener implements StackableChainingListener
40    {
 
41    public enum Event
42    {
43    NONE,
44    DEFINITION_DESCRIPTION,
45    DEFINITION_TERM,
46    DEFINITION_LIST,
47    DOCUMENT,
48    FORMAT,
49    HEADER,
50    LINK,
51    LIST,
52    LIST_ITEM,
53    MACRO_MARKER,
54    PARAGRAPH,
55    QUOTATION,
56    QUOTATION_LINE,
57    SECTION,
58    TABLE,
59    TABLE_CELL,
60    TABLE_HEAD_CELL,
61    TABLE_ROW,
62    RAW_TEXT,
63    EMPTY_LINES,
64    HORIZONTAL_LINE,
65    ID,
66    IMAGE,
67    NEW_LINE,
68    SPACE,
69    SPECIAL_SYMBOL,
70    MACRO,
71    VERBATIM_INLINE,
72    VERBATIM_STANDALONE,
73    WORD
74    }
75   
76    private Event previousEvent = Event.NONE;
77   
78    private int inlineDepth;
79   
80    private boolean isInParagraph;
81   
82    private boolean isInHeader;
83   
84    private int linkDepth;
85   
86    private boolean isInTable;
87   
88    private boolean isInTableCell;
89   
90    private Deque<DefinitionListState> definitionListDepth = new ArrayDeque<DefinitionListState>();
91   
92    private Deque<ListState> listDepth = new ArrayDeque<ListState>();
93   
94    private int quotationDepth;
95   
96    private int quotationLineDepth;
97   
98    private int quotationLineIndex = -1;
99   
100    private int macroDepth;
101   
102    private int cellRow = -1;
103   
104    private int cellCol = -1;
105   
 
106  107020 toggle public BlockStateChainingListener(ListenerChain listenerChain)
107    {
108  107017 setListenerChain(listenerChain);
109    }
110   
 
111  29 toggle @Override
112    public StackableChainingListener createChainingListenerInstance()
113    {
114  29 return new BlockStateChainingListener(getListenerChain());
115    }
116   
 
117  24 toggle public Event getPreviousEvent()
118    {
119  24 return this.previousEvent;
120    }
121   
 
122  813 toggle public int getInlineDepth()
123    {
124  813 return this.inlineDepth;
125    }
126   
 
127  813 toggle public boolean isInLine()
128    {
129  813 return getInlineDepth() > 0;
130    }
131   
 
132  0 toggle public boolean isInParagraph()
133    {
134  0 return this.isInParagraph;
135    }
136   
 
137  680 toggle public boolean isInHeader()
138    {
139  680 return this.isInHeader;
140    }
141   
 
142  680 toggle public boolean isInTable()
143    {
144  680 return this.isInTable;
145    }
146   
 
147  0 toggle public boolean isInTableCell()
148    {
149  0 return this.isInTableCell;
150    }
151   
 
152  102 toggle public int getCellCol()
153    {
154  102 return this.cellCol;
155    }
156   
 
157  116 toggle public int getCellRow()
158    {
159  116 return this.cellRow;
160    }
161   
 
162  121 toggle public int getDefinitionListDepth()
163    {
164  121 return this.definitionListDepth.size();
165    }
166   
 
167  54 toggle public boolean isInDefinitionList()
168    {
169  54 return getDefinitionListDepth() > 0;
170    }
171   
 
172  54 toggle public int getDefinitionListItemIndex()
173    {
174  54 return isInDefinitionList() ? this.definitionListDepth.peek().definitionListItemIndex : -1;
175    }
176   
 
177  528 toggle public int getListDepth()
178    {
179  528 return this.listDepth.size();
180    }
181   
 
182  323 toggle public boolean isInList()
183    {
184  323 return getListDepth() > 0;
185    }
186   
 
187  302 toggle public int getListItemIndex()
188    {
189  302 return isInList() ? this.listDepth.peek().listItemIndex : -1;
190    }
191   
 
192  0 toggle public void pushLinkDepth()
193    {
194  0 ++this.linkDepth;
195    }
196   
 
197  0 toggle public void popLinkDepth()
198    {
199  0 --this.linkDepth;
200    }
201   
 
202  864 toggle public int getLinkDepth()
203    {
204  864 return this.linkDepth;
205    }
206   
 
207  0 toggle public boolean isInLink()
208    {
209  0 return getLinkDepth() > 0;
210    }
211   
 
212  41 toggle public int getQuotationDepth()
213    {
214  41 return this.quotationDepth;
215    }
216   
 
217  24 toggle public boolean isInQuotation()
218    {
219  24 return getQuotationDepth() > 0;
220    }
221   
 
222  43 toggle public int getQuotationLineDepth()
223    {
224  43 return this.quotationLineDepth;
225    }
226   
 
227  43 toggle public boolean isInQuotationLine()
228    {
229  43 return getQuotationLineDepth() > 0;
230    }
231   
 
232  21 toggle public int getQuotationLineIndex()
233    {
234  21 return this.quotationLineIndex;
235    }
236   
 
237  47998 toggle public int getMacroDepth()
238    {
239  47998 return this.macroDepth;
240    }
241   
 
242  47972 toggle public boolean isInMacro()
243    {
244  47972 return getMacroDepth() > 0;
245    }
246   
247    // Events
248   
 
249  141 toggle @Override
250    public void beginDefinitionDescription()
251    {
252  141 ++this.inlineDepth;
253  141 ++this.definitionListDepth.peek().definitionListItemIndex;
254   
255  141 super.beginDefinitionDescription();
256    }
257   
258    /**
259    * {@inheritDoc}
260    *
261    * @since 2.0RC1
262    */
 
263  77 toggle @Override
264    public void beginDefinitionList(Map<String, String> parameters)
265    {
266  77 this.definitionListDepth.push(new DefinitionListState());
267   
268  77 super.beginDefinitionList(parameters);
269    }
270   
 
271  132 toggle @Override
272    public void beginDefinitionTerm()
273    {
274  132 ++this.inlineDepth;
275  132 ++this.definitionListDepth.peek().definitionListItemIndex;
276   
277  132 super.beginDefinitionTerm();
278    }
279   
280    /**
281    * {@inheritDoc}
282    *
283    * @since 2.5RC1
284    */
 
285  1050 toggle @Override
286    public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
287    {
288  1050 ++this.linkDepth;
289   
290  1050 super.beginLink(reference, freestanding, parameters);
291    }
292   
 
293  1572 toggle @Override
294    public void beginList(ListType type, Map<String, String> parameters)
295    {
296  1572 this.listDepth.push(new ListState());
297   
298  1572 super.beginList(type, parameters);
299    }
300   
 
301  4710 toggle @Override
302    public void beginListItem()
303    {
304  4710 ++this.inlineDepth;
305  4710 ++this.listDepth.peek().listItemIndex;
306   
307  4710 super.beginListItem();
308    }
309   
 
310  17256 toggle @Override
311    public void beginMacroMarker(String name, Map<String, String> parameters, String content, boolean isInline)
312    {
313  17256 ++this.macroDepth;
314   
315  17256 super.beginMacroMarker(name, parameters, content, isInline);
316    }
317   
 
318  7220 toggle @Override
319    public void beginParagraph(Map<String, String> parameters)
320    {
321  7221 this.isInParagraph = true;
322  7220 ++this.inlineDepth;
323   
324  7221 super.beginParagraph(parameters);
325    }
326   
 
327  30 toggle @Override
328    public void beginQuotation(Map<String, String> parameters)
329    {
330  30 ++this.quotationDepth;
331   
332  30 super.beginQuotation(parameters);
333    }
334   
 
335  45 toggle @Override
336    public void beginQuotationLine()
337    {
338  45 ++this.quotationLineDepth;
339  45 ++this.inlineDepth;
340  45 ++this.quotationLineIndex;
341   
342  45 super.beginQuotationLine();
343    }
344   
 
345  665 toggle @Override
346    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
347    {
348  665 this.isInHeader = true;
349  665 ++this.inlineDepth;
350   
351  665 super.beginHeader(level, id, parameters);
352    }
353   
 
354  92 toggle @Override
355    public void beginTable(Map<String, String> parameters)
356    {
357  92 this.isInTable = true;
358   
359  92 super.beginTable(parameters);
360    }
361   
 
362  219 toggle @Override
363    public void beginTableRow(Map<String, String> parameters)
364    {
365  219 ++this.cellRow;
366   
367  219 super.beginTableRow(parameters);
368    }
369   
 
370  360 toggle @Override
371    public void beginTableCell(Map<String, String> parameters)
372    {
373  360 this.isInTableCell = true;
374  360 ++this.inlineDepth;
375  360 ++this.cellCol;
376   
377  360 super.beginTableCell(parameters);
378    }
379   
 
380  187 toggle @Override
381    public void beginTableHeadCell(Map<String, String> parameters)
382    {
383  187 this.isInTableCell = true;
384  187 ++this.inlineDepth;
385  187 ++this.cellCol;
386   
387  187 super.beginTableHeadCell(parameters);
388    }
389   
 
390  141 toggle @Override
391    public void endDefinitionDescription()
392    {
393  141 super.endDefinitionDescription();
394   
395  141 --this.inlineDepth;
396  141 this.previousEvent = Event.DEFINITION_DESCRIPTION;
397    }
398   
399    /**
400    * {@inheritDoc}
401    *
402    * @since 2.0RC1
403    */
 
404  77 toggle @Override
405    public void endDefinitionList(Map<String, String> parameters)
406    {
407  77 super.endDefinitionList(parameters);
408   
409  77 this.definitionListDepth.pop();
410   
411  77 this.previousEvent = Event.DEFINITION_LIST;
412    }
413   
 
414  132 toggle @Override
415    public void endDefinitionTerm()
416    {
417  132 super.endDefinitionTerm();
418   
419  132 --this.inlineDepth;
420  132 this.previousEvent = Event.DEFINITION_TERM;
421    }
422   
423    /**
424    * {@inheritDoc}
425    *
426    * @since 3.0M2
427    */
 
428  28224 toggle @Override
429    public void endDocument(MetaData metadata)
430    {
431  28223 this.previousEvent = Event.DOCUMENT;
432   
433  28222 super.endDocument(metadata);
434    }
435   
 
436  5136 toggle @Override
437    public void endFormat(Format format, Map<String, String> parameters)
438    {
439  5136 super.endFormat(format, parameters);
440   
441  5136 this.previousEvent = Event.FORMAT;
442    }
443   
444    /**
445    * {@inheritDoc}
446    *
447    * @since 2.5RC1
448    */
 
449  1048 toggle @Override
450    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
451    {
452  1048 super.endLink(reference, freestanding, parameters);
453   
454  1048 --this.linkDepth;
455  1048 this.previousEvent = Event.LINK;
456    }
457   
 
458  1572 toggle @Override
459    public void endList(ListType type, Map<String, String> parameters)
460    {
461  1572 super.endList(type, parameters);
462   
463  1572 this.listDepth.pop();
464   
465  1572 this.previousEvent = Event.LIST;
466    }
467   
 
468  4710 toggle @Override
469    public void endListItem()
470    {
471  4710 super.endListItem();
472   
473  4710 --this.inlineDepth;
474  4710 this.previousEvent = Event.LIST_ITEM;
475    }
476   
 
477  17255 toggle @Override
478    public void endMacroMarker(String name, Map<String, String> parameters, String content, boolean isInline)
479    {
480  17255 super.endMacroMarker(name, parameters, content, isInline);
481   
482  17256 this.previousEvent = Event.MACRO_MARKER;
483  17256 --this.macroDepth;
484    }
485   
 
486  7219 toggle @Override
487    public void endParagraph(Map<String, String> parameters)
488    {
489  7219 super.endParagraph(parameters);
490   
491  7219 this.isInParagraph = false;
492  7219 --this.inlineDepth;
493  7218 this.previousEvent = Event.PARAGRAPH;
494    }
495   
 
496  30 toggle @Override
497    public void endQuotation(Map<String, String> parameters)
498    {
499  30 super.endQuotation(parameters);
500   
501  30 --this.quotationDepth;
502  30 if (this.quotationDepth == 0) {
503  16 this.quotationLineIndex = -1;
504    }
505  30 this.previousEvent = Event.QUOTATION;
506    }
507   
 
508  45 toggle @Override
509    public void endQuotationLine()
510    {
511  45 super.endQuotationLine();
512   
513  45 --this.quotationLineDepth;
514  45 --this.inlineDepth;
515  45 this.previousEvent = Event.QUOTATION_LINE;
516    }
517   
 
518  775 toggle @Override
519    public void endSection(Map<String, String> parameters)
520    {
521  775 super.endSection(parameters);
522   
523  775 this.previousEvent = Event.SECTION;
524    }
525   
 
526  665 toggle @Override
527    public void endHeader(HeaderLevel level, String id, Map<String, String> parameters)
528    {
529  665 super.endHeader(level, id, parameters);
530   
531  665 this.isInHeader = false;
532  665 --this.inlineDepth;
533  665 this.previousEvent = Event.HEADER;
534    }
535   
 
536  92 toggle @Override
537    public void endTable(Map<String, String> parameters)
538    {
539  92 super.endTable(parameters);
540   
541  92 this.isInTable = false;
542  92 this.cellRow = -1;
543  92 this.previousEvent = Event.TABLE;
544    }
545   
 
546  360 toggle @Override
547    public void endTableCell(Map<String, String> parameters)
548    {
549  360 super.endTableCell(parameters);
550   
551  360 this.isInTableCell = false;
552  360 --this.inlineDepth;
553  360 this.previousEvent = Event.TABLE_CELL;
554    }
555   
 
556  187 toggle @Override
557    public void endTableHeadCell(Map<String, String> parameters)
558    {
559  187 super.endTableHeadCell(parameters);
560   
561  187 this.isInTableCell = false;
562  187 --this.inlineDepth;
563  187 this.previousEvent = Event.TABLE_HEAD_CELL;
564    }
565   
 
566  219 toggle @Override
567    public void endTableRow(Map<String, String> parameters)
568    {
569  219 super.endTableRow(parameters);
570   
571  219 this.previousEvent = Event.TABLE_ROW;
572  219 this.cellCol = -1;
573    }
574   
 
575  6711 toggle @Override
576    public void onRawText(String text, Syntax syntax)
577    {
578  6711 super.onRawText(text, syntax);
579   
580  6711 this.previousEvent = Event.RAW_TEXT;
581    }
582   
 
583  230 toggle @Override
584    public void onEmptyLines(int count)
585    {
586  230 this.previousEvent = Event.EMPTY_LINES;
587   
588  230 super.onEmptyLines(count);
589    }
590   
 
591  26 toggle @Override
592    public void onHorizontalLine(Map<String, String> parameters)
593    {
594  26 this.previousEvent = Event.HORIZONTAL_LINE;
595   
596  26 super.onHorizontalLine(parameters);
597    }
598   
 
599  9 toggle @Override
600    public void onId(String name)
601    {
602  9 this.previousEvent = Event.ID;
603   
604  9 super.onId(name);
605    }
606   
607    /**
608    * {@inheritDoc}
609    *
610    * @since 2.5RC1
611    */
 
612  572 toggle @Override
613    public void onImage(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
614    {
615  572 this.previousEvent = Event.IMAGE;
616   
617  572 super.onImage(reference, freestanding, parameters);
618    }
619   
 
620  9497 toggle @Override
621    public void onNewLine()
622    {
623  9497 this.previousEvent = Event.NEW_LINE;
624   
625  9497 super.onNewLine();
626    }
627   
 
628  435263 toggle @Override
629    public void onSpace()
630    {
631  435265 this.previousEvent = Event.SPACE;
632   
633  435265 super.onSpace();
634    }
635   
 
636  363520 toggle @Override
637    public void onSpecialSymbol(char symbol)
638    {
639  363519 this.previousEvent = Event.SPECIAL_SYMBOL;
640   
641  363518 super.onSpecialSymbol(symbol);
642    }
643   
 
644  92 toggle @Override
645    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
646    {
647  92 this.previousEvent = Event.VERBATIM_STANDALONE;
648   
649  92 super.onVerbatim(content, inline, parameters);
650    }
651   
 
652  464633 toggle @Override
653    public void onWord(String word)
654    {
655  464631 this.previousEvent = Event.WORD;
656   
657  464633 super.onWord(word);
658    }
659   
 
660  435 toggle @Override
661    public void onMacro(String id, Map<String, String> parameters, String content, boolean inline)
662    {
663  435 this.previousEvent = Event.MACRO;
664   
665  435 super.onMacro(id, parameters, content, inline);
666    }
667   
 
668    private static class ListState
669    {
670    public int listItemIndex = -1;
671    }
672   
 
673    private static class DefinitionListState
674    {
675    public int definitionListItemIndex = -1;
676    }
677    }