1. Project Clover database mar. janv. 20 2026 12:32:22 CET
  2. Package org.devacfr.maven.skins.reflow.model

File Toc.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
60% of files have more coverage

Code metrics

10
40
11
1
193
95
18
0,45
3,64
11
1,64

Classes

Class Line # Actions
Toc 39 40 0% 18 21
0.655737765,6%
 

Contributing tests

This file is covered by 19 tests. .

Source view

1    /*
2    * Copyright 2012-2025 Christophe Friederich
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package org.devacfr.maven.skins.reflow.model;
17   
18    import com.google.common.collect.Sets;
19    import java.util.List;
20    import java.util.Set;
21    import javax.annotation.Nonnull;
22    import javax.annotation.Nullable;
23    import org.apache.commons.lang3.builder.ToStringBuilder;
24    import org.devacfr.maven.skins.reflow.HtmlTool;
25    import org.devacfr.maven.skins.reflow.HtmlTool.IdElement;
26    import org.devacfr.maven.skins.reflow.ISkinConfig;
27    import org.devacfr.maven.skins.reflow.Xpp3Utils;
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30   
31    /**
32    * Represents the base of Table of content component.
33    *
34    * @author devacfr
35    * @since 2.0
36    * @param <T>
37    * the type of inherit of {@link Toc}.
38    */
 
39    public abstract class Toc<T extends Toc<?>> extends BsComponent {
40   
41    private static final Set<String> TOC_TYPES = Sets.newHashSet("sidebar", "top", "false");
42   
43    /** */
44    public static final String COMPONENT = "toc";
45   
46    /** */
47    private static final Logger LOGGER = LoggerFactory.getLogger(Toc.class);
48   
49    /** enable by default */
50    private boolean enabled = true;
51   
52    /** */
53    private final String type;
54   
55    /**
56    * @param config
57    * a config (can <b>not</b> be {@code null}).
58    * @param preferredType
59    * the default type of Toc to use.
60    * @return Returns new instance corresponding {@link Toc} to configuration.
61    */
 
62  26 toggle public static Toc<?> createToc(@Nonnull final ISkinConfig config, @Nullable final String preferredType) {
63  26 Toc<?> toc = null;
64  26 String type = config.getPropertyValue(COMPONENT, String.class, preferredType);
65  26 if (LOGGER.isTraceEnabled()) {
66  0 LOGGER.trace("Page '{}' Find Toc: {}", config.getFileId(), type);
67    }
68  26 if (!TOC_TYPES.contains(type)) {
69  14 type = preferredType;
70    }
71  26 if (type == null) {
72  14 type = "";
73    }
74  26 switch (type) {
75  0 case "sidebar":
76  0 toc = createSidebar(config);
77  0 break;
78  1 case "top":
79  1 toc = createTopBar(config);
80  1 break;
81  25 default:
82    // create a disabled empty toc
83  25 toc = new Toc<Toc<?>>(config, "", "") {};
84   
85  25 toc.withEnabled(false);
86  25 break;
87    }
88   
89  26 return toc;
90    }
91   
92    /**
93    * @param config
94    * a config (can <b>not</b> be {@code null}).
95    * @return Returns new instance of Toc sidebar.
96    */
 
97  0 toggle public static Toc<?> createSidebar(@Nonnull final ISkinConfig config) {
98  0 return new TocSidebar(config);
99    }
100   
101    /**
102    * @param config
103    * a config (can <b>not</b> be {@code null}).
104    * @return Returns new instance Toc top bar.
105    */
 
106  1 toggle public static Toc<?> createTopBar(@Nonnull final ISkinConfig config) {
107  1 return new TocTopBar(config);
108    }
109   
110    /**
111    * @param config
112    * a config (can <b>not</b> be {@code null}
113    * @param type
114    * the {@link String} representation of Toc.
115    * @param component
116    * the bootstrap component name.
117    */
 
118  27 toggle protected Toc(@Nonnull final ISkinConfig config, final String type, final String component) {
119  27 super(config, component);
120  27 this.type = type;
121    }
122   
123    /**
124    * @return Returns the fluent instance.
125    */
 
126  33 toggle @SuppressWarnings("unchecked")
127    protected T self() {
128  33 return (T) this;
129    }
130   
131    /**
132    * @return Returns the {@link String} reprensenting the type of {@link Toc}.
133    */
 
134  0 toggle public String getType() {
135  0 return type;
136    }
137   
138    /**
139    * Gets the indicating whether is enable.
140    *
141    * @return Returns {@code true} if is enable, otherwise {@code false}.
142    */
 
143  4 toggle public boolean isEnabled() {
144  4 return enabled;
145    }
146   
147    /**
148    * @return Returns a list of {@link IdElement} representing the heading tree containing in current page.
149    * @since 2.1
150    */
 
151  0 toggle public List<? extends IdElement> getTocItems() {
152  0 final HtmlTool htmlTool = config.getHtmlTool();
153  0 final String bodyContent = config.getBodyContent();
154  0 if (LOGGER.isTraceEnabled()) {
155  0 LOGGER.trace("Generating TOC items for page '{}', for content:{}", config.getFileId(), bodyContent);
156    }
157  0 final List<? extends IdElement> tocItems = htmlTool.headingTree(bodyContent,
158    Xpp3Utils.getChildren(config.get("sections")));
159  0 return tocItems;
160    }
161   
162    /**
163    * Sets the indicating whether is enable.
164    *
165    * @param enabled
166    * status to use.
167    * @return Returns the fluent instance.
168    */
 
169  27 toggle protected T withEnabled(final boolean enabled) {
170  27 this.enabled = enabled;
171  27 return self();
172    }
173   
 
174  5 toggle @Override
175    protected String onPreRender(final @Nonnull String bodyContent) {
176  5 if (this.enabled) {
177  0 final HtmlTool htmlTool = config.getHtmlTool();
178  0 return htmlTool.ensureHeadingIds(config.getContext().getType(),
179    config.getFileId(),
180    bodyContent,
181    HtmlTool.DEFAULT_SLUG_SEPARATOR);
182    }
183  5 return bodyContent;
184    }
185   
186    /**
187    * {@inheritDoc}
188    */
 
189  22 toggle @Override
190    public String toString() {
191  22 return ToStringBuilder.reflectionToString(this);
192    }
193    }