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