1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.devacfr.maven.skins.reflow.model;
20
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23
24 import java.util.List;
25 import java.util.Set;
26
27 import com.google.common.collect.Sets;
28 import org.apache.commons.lang3.builder.ToStringBuilder;
29 import org.devacfr.maven.skins.reflow.HtmlTool;
30 import org.devacfr.maven.skins.reflow.HtmlTool.IdElement;
31 import org.devacfr.maven.skins.reflow.ISkinConfig;
32 import org.devacfr.maven.skins.reflow.Xpp3Utils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43
44 public abstract class Toc<T extends Toc<?>> extends BsComponent {
45
46 private static final Set<String> TOC_TYPES = Sets.newHashSet("sidebar", "top", "false");
47
48
49 public static final String COMPONENT = "toc";
50
51
52 private static final Logger LOGGER = LoggerFactory.getLogger(Toc.class);
53
54
55 private boolean enabled = true;
56
57
58 private final String type;
59
60
61
62
63
64
65
66
67 public static Toc<?> createToc(@Nonnull final ISkinConfig config, @Nullable final String preferredType) {
68 Toc<?> toc = null;
69 String type = config.getPropertyValue(COMPONENT, String.class, preferredType);
70 if (LOGGER.isTraceEnabled()) {
71 LOGGER.trace("Page '{}' Find Toc: {}", config.getFileId(), type);
72 }
73 if (!TOC_TYPES.contains(type)) {
74 type = preferredType;
75 }
76 if (type == null) {
77 type = "";
78 }
79 switch (type) {
80 case "sidebar":
81 toc = createSidebar(config);
82 break;
83 case "top":
84 toc = createTopBar(config);
85 break;
86 default:
87
88 toc = new Toc<Toc<?>>("", "") {};
89
90 toc.withEnabled(false);
91 break;
92 }
93
94 return toc;
95 }
96
97
98
99
100
101
102 public static Toc<?> createSidebar(@Nonnull final ISkinConfig config) {
103 return new TocSidebar(config);
104 }
105
106
107
108
109
110
111 public static Toc<?> createTopBar(@Nonnull final ISkinConfig config) {
112 return new TocTopBar(config);
113 }
114
115
116
117
118
119
120
121 protected Toc(final String type, final String component) {
122 super(component);
123 this.type = type;
124 }
125
126
127
128
129 @SuppressWarnings("unchecked")
130 protected T self() {
131 return (T) this;
132 }
133
134
135
136
137 public String getType() {
138 return type;
139 }
140
141
142
143
144
145
146 public boolean isEnabled() {
147 return enabled;
148 }
149
150
151
152
153
154
155
156 public List<? extends IdElement> getTocItems(final ISkinConfig skinConfig) {
157 final HtmlTool htmlTool = getHtmlTool(skinConfig);
158 final String bodyContent = getBodyContent(skinConfig);
159
160 final List<? extends IdElement> tocItems = htmlTool.headingTree(bodyContent,
161 Xpp3Utils.getChildren(skinConfig.get("sections")));
162 return tocItems;
163 }
164
165
166
167
168
169
170
171
172 protected T withEnabled(final boolean enabled) {
173 this.enabled = enabled;
174 return self();
175 }
176
177 @Override
178 protected String onPreRender(final @Nonnull ISkinConfig skinConfig, final @Nonnull String bodyContent) {
179 if (this.enabled) {
180 final HtmlTool htmlTool = getHtmlTool(skinConfig);
181 return htmlTool.ensureHeadingIds(skinConfig.getContext().getType(),
182 skinConfig.getFileId(),
183 bodyContent,
184 HtmlTool.DEFAULT_SLUG_SEPARATOR);
185 }
186 return bodyContent;
187 }
188
189
190
191
192 @Override
193 public String toString() {
194 return ToStringBuilder.reflectionToString(this);
195 }
196 }