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 private IndexEntry currentEntry;
75
76
77
78
79
80
81 public IndexingSink( IndexEntry sectionEntry )
82 {
83 stack = new Stack<IndexEntry>();
84 stack.push( sectionEntry );
85
86 init();
87 }
88
89
90
91
92
93
94 public String getTitle()
95 {
96 return title;
97 }
98
99
100
101
102
103
104 public void title()
105 {
106 this.type = TITLE;
107 }
108
109
110 public void sectionTitle1()
111 {
112 this.currentEntry = null;
113 this.type = TYPE_SECTION_1;
114 }
115
116
117 public void title_()
118 {
119 this.type = 0;
120 }
121
122 public void sectionTitle1_()
123 {
124 this.type = 0;
125 }
126
127
128 public void section1_()
129 {
130 pop();
131 }
132
133
134 public void sectionTitle2()
135 {
136 this.currentEntry = null;
137 this.type = TYPE_SECTION_2;
138 }
139
140 public void sectionTitle2_()
141 {
142 this.type = 0;
143 }
144
145
146 public void section2_()
147 {
148 pop();
149 }
150
151
152 public void sectionTitle3()
153 {
154 this.currentEntry = null;
155 this.type = TYPE_SECTION_3;
156 }
157
158 public void sectionTitle3_()
159 {
160 this.type = 0;
161 }
162
163
164 public void section3_()
165 {
166 pop();
167 }
168
169
170 public void sectionTitle4()
171 {
172 this.currentEntry = null;
173 this.type = TYPE_SECTION_4;
174 }
175
176 public void sectionTitle4_()
177 {
178 this.type = 0;
179 }
180
181
182 public void section4_()
183 {
184 pop();
185 }
186
187
188 public void sectionTitle5()
189 {
190 this.currentEntry = null;
191 this.type = TYPE_SECTION_5;
192 }
193
194 public void sectionTitle5_()
195 {
196 this.type = 0;
197 }
198
199
200 public void section5_()
201 {
202 pop();
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public void text( String text )
222 {
223 switch ( this.type )
224 {
225 case TITLE:
226 this.title = text;
227 break;
228 case TYPE_SECTION_1:
229 case TYPE_SECTION_2:
230 case TYPE_SECTION_3:
231 case TYPE_SECTION_4:
232 case TYPE_SECTION_5:
233
234
235
236
237 if ( this.currentEntry == null )
238 {
239 this.currentEntry = new IndexEntry( peek(), HtmlTools.encodeId( text ) );
240
241 this.currentEntry.setTitle( text );
242
243 push( currentEntry );
244 }
245 else
246 {
247 IndexEntry entry = (IndexEntry) stack.lastElement();
248
249 String title = currentEntry.getTitle() + text;
250 title = title.replaceAll( "[\\r\\n]+", "" );
251
252 entry.setId( HtmlTools.encodeId( title ) );
253
254 entry.setTitle( title );
255 }
256
257 break;
258
259 case TYPE_DEFINED_TERM:
260 case TYPE_FIGURE:
261 case TYPE_TABLE:
262 default:
263 break;
264 }
265 }
266
267
268
269
270
271
272 public void push( IndexEntry entry )
273 {
274 stack.push( entry );
275 }
276
277
278
279
280 public void pop()
281 {
282 stack.pop();
283 }
284
285
286
287
288
289
290 public IndexEntry peek()
291 {
292 return stack.peek();
293 }
294
295
296 public void close()
297 {
298 super.close();
299
300 init();
301 }
302
303
304 protected void init()
305 {
306 this.type = 0;
307 this.title = null;
308 }
309 }