Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IndexingSink |
|
| 1.4;1,4 |
1 | package org.apache.maven.doxia.index; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.util.Stack; | |
23 | ||
24 | import org.apache.maven.doxia.util.HtmlTools; | |
25 | import org.apache.maven.doxia.sink.SinkAdapter; | |
26 | ||
27 | /** | |
28 | * A sink implementation for index. | |
29 | * | |
30 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> | |
31 | * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> | |
32 | * @version $Id: IndexingSink.java 1090706 2011-04-09 23:15:28Z hboutemy $ | |
33 | */ | |
34 | public class IndexingSink | |
35 | extends SinkAdapter | |
36 | { | |
37 | /** Section 1. */ | |
38 | private static final int TYPE_SECTION_1 = 1; | |
39 | ||
40 | /** Section 2. */ | |
41 | private static final int TYPE_SECTION_2 = 2; | |
42 | ||
43 | /** Section 3. */ | |
44 | private static final int TYPE_SECTION_3 = 3; | |
45 | ||
46 | /** Section 4. */ | |
47 | private static final int TYPE_SECTION_4 = 4; | |
48 | ||
49 | /** Section 5. */ | |
50 | private static final int TYPE_SECTION_5 = 5; | |
51 | ||
52 | /** Defined term. */ | |
53 | private static final int TYPE_DEFINED_TERM = 6; | |
54 | ||
55 | /** Figure. */ | |
56 | private static final int TYPE_FIGURE = 7; | |
57 | ||
58 | /** Table. */ | |
59 | private static final int TYPE_TABLE = 8; | |
60 | ||
61 | /** Title. */ | |
62 | private static final int TITLE = 9; | |
63 | ||
64 | /** The current type. */ | |
65 | private int type; | |
66 | ||
67 | /** The current title. */ | |
68 | private String title; | |
69 | ||
70 | /** The stack. */ | |
71 | private final Stack<IndexEntry> stack; | |
72 | ||
73 | /** The current type. */ | |
74 | private IndexEntry currentEntry; | |
75 | ||
76 | /** | |
77 | * Default constructor. | |
78 | * | |
79 | * @param sectionEntry The first index entry. | |
80 | */ | |
81 | public IndexingSink( IndexEntry sectionEntry ) | |
82 | 6 | { |
83 | 6 | stack = new Stack<IndexEntry>(); |
84 | 6 | stack.push( sectionEntry ); |
85 | ||
86 | 6 | init(); |
87 | 6 | } |
88 | ||
89 | /** | |
90 | * <p>Getter for the field <code>title</code>.</p> | |
91 | * | |
92 | * @return the title | |
93 | */ | |
94 | public String getTitle() | |
95 | { | |
96 | 0 | return title; |
97 | } | |
98 | ||
99 | // ---------------------------------------------------------------------- | |
100 | // Sink Overrides | |
101 | // ---------------------------------------------------------------------- | |
102 | ||
103 | /** {@inheritDoc} */ | |
104 | public void title() | |
105 | { | |
106 | 0 | this.type = TITLE; |
107 | 0 | } |
108 | ||
109 | /** {@inheritDoc} */ | |
110 | public void sectionTitle1() | |
111 | { | |
112 | 18 | this.currentEntry = null; |
113 | 18 | this.type = TYPE_SECTION_1; |
114 | 18 | } |
115 | ||
116 | /** {@inheritDoc} */ | |
117 | public void title_() | |
118 | { | |
119 | 0 | this.type = 0; |
120 | 0 | } |
121 | ||
122 | public void sectionTitle1_() | |
123 | { | |
124 | 18 | this.type = 0; |
125 | 18 | } |
126 | ||
127 | /** {@inheritDoc} */ | |
128 | public void section1_() | |
129 | { | |
130 | 12 | pop(); |
131 | 12 | } |
132 | ||
133 | /** {@inheritDoc} */ | |
134 | public void sectionTitle2() | |
135 | { | |
136 | 6 | this.currentEntry = null; |
137 | 6 | this.type = TYPE_SECTION_2; |
138 | 6 | } |
139 | ||
140 | public void sectionTitle2_() | |
141 | { | |
142 | 6 | this.type = 0; |
143 | 6 | } |
144 | ||
145 | /** {@inheritDoc} */ | |
146 | public void section2_() | |
147 | { | |
148 | 6 | pop(); |
149 | 6 | } |
150 | ||
151 | /** {@inheritDoc} */ | |
152 | public void sectionTitle3() | |
153 | { | |
154 | 6 | this.currentEntry = null; |
155 | 6 | this.type = TYPE_SECTION_3; |
156 | 6 | } |
157 | ||
158 | public void sectionTitle3_() | |
159 | { | |
160 | 6 | this.type = 0; |
161 | 6 | } |
162 | ||
163 | /** {@inheritDoc} */ | |
164 | public void section3_() | |
165 | { | |
166 | 6 | pop(); |
167 | 6 | } |
168 | ||
169 | /** {@inheritDoc} */ | |
170 | public void sectionTitle4() | |
171 | { | |
172 | 0 | this.currentEntry = null; |
173 | 0 | this.type = TYPE_SECTION_4; |
174 | 0 | } |
175 | ||
176 | public void sectionTitle4_() | |
177 | { | |
178 | 0 | this.type = 0; |
179 | 0 | } |
180 | ||
181 | /** {@inheritDoc} */ | |
182 | public void section4_() | |
183 | { | |
184 | 0 | pop(); |
185 | 0 | } |
186 | ||
187 | /** {@inheritDoc} */ | |
188 | public void sectionTitle5() | |
189 | { | |
190 | 0 | this.currentEntry = null; |
191 | 0 | this.type = TYPE_SECTION_5; |
192 | 0 | } |
193 | ||
194 | public void sectionTitle5_() | |
195 | { | |
196 | 0 | this.type = 0; |
197 | 0 | } |
198 | ||
199 | /** {@inheritDoc} */ | |
200 | public void section5_() | |
201 | { | |
202 | 0 | pop(); |
203 | 0 | } |
204 | ||
205 | // public void definedTerm() | |
206 | // { | |
207 | // type = TYPE_DEFINED_TERM; | |
208 | // } | |
209 | // | |
210 | // public void figureCaption() | |
211 | // { | |
212 | // type = TYPE_FIGURE; | |
213 | // } | |
214 | // | |
215 | // public void tableCaption() | |
216 | // { | |
217 | // type = TYPE_TABLE; | |
218 | // } | |
219 | ||
220 | /** {@inheritDoc} */ | |
221 | public void text( String text ) | |
222 | { | |
223 | 36 | switch ( this.type ) |
224 | { | |
225 | case TITLE: | |
226 | 0 | this.title = text; |
227 | 0 | 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 | // Sanitize the id. The most important step is to remove any blanks | |
235 | // ----------------------------------------------------------------------- | |
236 | ||
237 | 36 | if ( this.currentEntry == null ) |
238 | { | |
239 | 30 | this.currentEntry = new IndexEntry( peek(), HtmlTools.encodeId( text ) ); |
240 | ||
241 | 30 | this.currentEntry.setTitle( text ); |
242 | ||
243 | 30 | push( currentEntry ); |
244 | } | |
245 | else | |
246 | { | |
247 | 6 | IndexEntry entry = (IndexEntry) stack.lastElement(); |
248 | ||
249 | 6 | String title = currentEntry.getTitle() + text; |
250 | 6 | title = title.replaceAll( "[\\r\\n]+", "" ); |
251 | ||
252 | 6 | entry.setId( HtmlTools.encodeId( title ) ); |
253 | ||
254 | 6 | entry.setTitle( title ); |
255 | } | |
256 | ||
257 | 6 | break; |
258 | // Dunno how to handle these yet | |
259 | case TYPE_DEFINED_TERM: | |
260 | case TYPE_FIGURE: | |
261 | case TYPE_TABLE: | |
262 | default: | |
263 | break; | |
264 | } | |
265 | 36 | } |
266 | ||
267 | /** | |
268 | * Pushes an IndexEntry onto the top of this stack. | |
269 | * | |
270 | * @param entry to put. | |
271 | */ | |
272 | public void push( IndexEntry entry ) | |
273 | { | |
274 | 30 | stack.push( entry ); |
275 | 30 | } |
276 | ||
277 | /** | |
278 | * Removes the IndexEntry at the top of this stack. | |
279 | */ | |
280 | public void pop() | |
281 | { | |
282 | 24 | stack.pop(); |
283 | 24 | } |
284 | ||
285 | /** | |
286 | * <p>peek.</p> | |
287 | * | |
288 | * @return Looks at the IndexEntry at the top of this stack. | |
289 | */ | |
290 | public IndexEntry peek() | |
291 | { | |
292 | 30 | return stack.peek(); |
293 | } | |
294 | ||
295 | /** {@inheritDoc} */ | |
296 | public void close() | |
297 | { | |
298 | 0 | super.close(); |
299 | ||
300 | 0 | init(); |
301 | 0 | } |
302 | ||
303 | /** {@inheritDoc} */ | |
304 | protected void init() | |
305 | { | |
306 | 6 | this.type = 0; |
307 | 6 | this.title = null; |
308 | 6 | } |
309 | } |