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