1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.index;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Stack;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26 import org.apache.maven.doxia.sink.impl.SinkAdapter;
27 import org.apache.maven.doxia.util.HtmlTools;
28
29
30
31
32
33
34
35 public class IndexingSink extends SinkAdapter {
36
37 private static final int TYPE_SECTION_1 = 1;
38
39
40 private static final int TYPE_SECTION_2 = 2;
41
42
43 private static final int TYPE_SECTION_3 = 3;
44
45
46 private static final int TYPE_SECTION_4 = 4;
47
48
49 private static final int TYPE_SECTION_5 = 5;
50
51
52 private static final int TYPE_DEFINED_TERM = 6;
53
54
55 private static final int TYPE_FIGURE = 7;
56
57
58 private static final int TYPE_TABLE = 8;
59
60
61 private static final int TITLE = 9;
62
63
64 private int type;
65
66
67 private String title;
68
69
70 private final Stack<IndexEntry> stack;
71
72
73
74
75
76 private final Map<String, AtomicInteger> usedIds;
77
78
79
80
81
82
83 public IndexingSink(IndexEntry sectionEntry) {
84 stack = new Stack<>();
85 stack.push(sectionEntry);
86 usedIds = new HashMap<>();
87 usedIds.put(sectionEntry.getId(), new AtomicInteger());
88 init();
89 }
90
91
92
93
94
95
96 public String getTitle() {
97 return title;
98 }
99
100
101
102
103
104
105
106
107 public void title() {
108 this.type = TITLE;
109 }
110
111
112
113
114 public void section1() {
115 pushNewEntry();
116 }
117
118
119
120
121 public void sectionTitle1() {
122 this.type = TYPE_SECTION_1;
123 }
124
125
126
127
128 public void title_() {
129 this.type = 0;
130 }
131
132
133
134
135 public void sectionTitle1_() {
136 this.type = 0;
137 }
138
139
140
141
142 public void section1_() {
143 pop();
144 }
145
146
147
148
149 public void section2() {
150 pushNewEntry();
151 }
152
153
154
155
156 public void sectionTitle2() {
157 this.type = TYPE_SECTION_2;
158 }
159
160
161
162
163 public void sectionTitle2_() {
164 this.type = 0;
165 }
166
167
168
169
170 public void section2_() {
171 pop();
172 }
173
174
175
176
177 public void section3() {
178 pushNewEntry();
179 }
180
181
182
183
184 public void sectionTitle3() {
185 this.type = TYPE_SECTION_3;
186 }
187
188
189
190
191 public void sectionTitle3_() {
192 this.type = 0;
193 }
194
195
196
197
198 public void section3_() {
199 pop();
200 }
201
202
203
204
205 public void section4() {
206 pushNewEntry();
207 }
208
209
210
211
212 public void sectionTitle4() {
213 this.type = TYPE_SECTION_4;
214 }
215
216
217
218
219 public void sectionTitle4_() {
220 this.type = 0;
221 }
222
223
224
225
226 public void section4_() {
227 pop();
228 }
229
230
231
232
233 public void section5() {
234 pushNewEntry();
235 }
236
237
238
239
240 public void sectionTitle5() {
241 this.type = TYPE_SECTION_5;
242 }
243
244
245
246
247 public void sectionTitle5_() {
248 this.type = 0;
249 }
250
251
252
253
254 public void section5_() {
255 pop();
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 public void text(String text) {
275 switch (this.type) {
276 case TITLE:
277 this.title = text;
278 break;
279 case TYPE_SECTION_1:
280 case TYPE_SECTION_2:
281 case TYPE_SECTION_3:
282 case TYPE_SECTION_4:
283 case TYPE_SECTION_5:
284
285
286
287
288
289 IndexEntry entry = stack.lastElement();
290
291 String title = entry.getTitle() + text;
292 title = title.replaceAll("[\\r\\n]+", "");
293 entry.setTitle(title);
294
295 entry.setId(getUniqueId(HtmlTools.encodeId(title)));
296
297 break;
298
299 case TYPE_DEFINED_TERM:
300 case TYPE_FIGURE:
301 case TYPE_TABLE:
302 default:
303 break;
304 }
305 }
306
307
308
309
310
311
312
313 String getUniqueId(String id) {
314 final String uniqueId;
315
316 if (usedIds.containsKey(id)) {
317 uniqueId = id + "_" + usedIds.get(id).incrementAndGet();
318 } else {
319 usedIds.put(id, new AtomicInteger());
320 uniqueId = id;
321 }
322 return uniqueId;
323 }
324
325
326
327
328 public void pushNewEntry() {
329 IndexEntry entry = new IndexEntry(peek(), "");
330
331 entry.setTitle("");
332
333 stack.push(entry);
334 }
335
336
337
338
339
340
341 public void push(IndexEntry entry) {
342 stack.push(entry);
343 }
344
345
346
347
348 public void pop() {
349 stack.pop();
350 }
351
352
353
354
355
356
357 public IndexEntry peek() {
358 return stack.peek();
359 }
360
361
362
363
364 public void close() {
365 super.close();
366
367 init();
368 }
369
370
371
372
373 protected void init() {
374 this.type = 0;
375 this.title = null;
376 }
377 }