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

File TocBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

12
29
11
1
137
87
17
0.59
2.64
11
1.55

Classes

Class Line # Actions
TocBuilder 31 29 0% 17 52
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.wikimodel.util;
21   
22    import java.util.ArrayDeque;
23    import java.util.Deque;
24   
25    /**
26    * This is an utility class which is used to build tables of content (TOCs).
27    *
28    * @version $Id: 425d0497458df380b6ebb0e009c16c18e22d0942 $
29    * @since 4.0M1
30    */
 
31    public class TocBuilder
32    {
33    protected int fBaseLevel;
34   
35    protected Deque<Integer> fBaseLevelStack = new ArrayDeque<Integer>();
36   
37    protected int fLevel;
38   
39    private ITocListener fListener;
40   
41    private final int fMaxHeaderDepth;
42   
43    private final int fMaxSectionDepth;
44   
45    private int fTotalDepth;
46   
 
47  0 toggle public TocBuilder(ITocListener listener)
48    {
49  0 this(listener, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
50    }
51   
 
52  0 toggle public TocBuilder(ITocListener listener, int totalDepth)
53    {
54  0 this(listener, Integer.MAX_VALUE, Integer.MAX_VALUE, totalDepth);
55    }
56   
 
57  0 toggle public TocBuilder(ITocListener listener, int documentDepth, int headerDepth)
58    {
59  0 this(listener, documentDepth, headerDepth, Integer.MAX_VALUE);
60    }
61   
 
62  0 toggle public TocBuilder(
63    ITocListener listener,
64    int documentDepth,
65    int headerDepth,
66    int totalDepth)
67    {
68  0 fListener = listener;
69  0 fMaxSectionDepth = documentDepth;
70  0 fMaxHeaderDepth = headerDepth;
71  0 fTotalDepth = totalDepth;
72    }
73   
 
74  0 toggle public void beginDocument()
75    {
76  0 fBaseLevelStack.push(fBaseLevel);
77  0 fBaseLevel = fLevel;
78    }
79   
 
80  0 toggle public void beginHeader(int level)
81    {
82  0 setHeaderLevel(level);
83  0 if (checkDepth()) {
84  0 fListener.beginItem();
85    }
86    }
87   
88    /**
89    * @return <code>true</code> if the current element should be shown
90    */
 
91  0 toggle public boolean checkDepth()
92    {
93  0 int documentDepth = fBaseLevelStack.size();
94  0 int headerLevel = getHeaderLevel();
95  0 return documentDepth <= fMaxSectionDepth
96    && headerLevel <= fMaxHeaderDepth
97    && (documentDepth + headerLevel) <= fTotalDepth;
98    }
99   
 
100  0 toggle public void endDocument()
101    {
102  0 setHeaderLevel(0);
103  0 Integer level = fBaseLevelStack.pop();
104  0 fBaseLevel = level.intValue();
105    }
106   
 
107  0 toggle public void endHeader()
108    {
109  0 if (checkDepth()) {
110  0 fListener.endItem();
111    }
112    }
113   
114    /**
115    * @return the current level of headers
116    */
 
117  0 toggle protected int getHeaderLevel()
118    {
119  0 return fLevel - fBaseLevel;
120    }
121   
 
122  0 toggle protected void setHeaderLevel(int level)
123    {
124  0 while (fLevel > level + fBaseLevel) {
125  0 if (checkDepth()) {
126  0 fListener.endLevel(getHeaderLevel());
127    }
128  0 fLevel--;
129    }
130  0 while (fLevel < level + fBaseLevel) {
131  0 fLevel++;
132  0 if (checkDepth()) {
133  0 fListener.beginLevel(getHeaderLevel());
134    }
135    }
136    }
137    }