View Javadoc
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.sink.impl.SinkAdapter;
25  import org.apache.maven.doxia.util.HtmlTools;
26  
27  /**
28   * A sink implementation for index.
29   *
30   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
31   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
32   */
33  public class IndexingSink
34      extends SinkAdapter
35  {
36      /** Section 1. */
37      private static final int TYPE_SECTION_1 = 1;
38  
39      /** Section 2. */
40      private static final int TYPE_SECTION_2 = 2;
41  
42      /** Section 3. */
43      private static final int TYPE_SECTION_3 = 3;
44  
45      /** Section 4. */
46      private static final int TYPE_SECTION_4 = 4;
47  
48      /** Section 5. */
49      private static final int TYPE_SECTION_5 = 5;
50  
51      /** Defined term. */
52      private static final int TYPE_DEFINED_TERM = 6;
53  
54      /** Figure. */
55      private static final int TYPE_FIGURE = 7;
56  
57      /** Table. */
58      private static final int TYPE_TABLE = 8;
59  
60      /** Title. */
61      private static final int TITLE = 9;
62  
63      /** The current type. */
64      private int type;
65  
66      /** The current title. */
67      private String title;
68  
69      /** The stack. */
70      private final Stack<IndexEntry> stack;
71  
72      /**
73       * Default constructor.
74       *
75       * @param sectionEntry The first index entry.
76       */
77      public IndexingSink( IndexEntry sectionEntry )
78      {
79          stack = new Stack<>();
80          stack.push( sectionEntry );
81  
82          init();
83      }
84  
85      /**
86       * <p>Getter for the field <code>title</code>.</p>
87       *
88       * @return the title
89       */
90      public String getTitle()
91      {
92          return title;
93      }
94  
95      // ----------------------------------------------------------------------
96      // Sink Overrides
97      // ----------------------------------------------------------------------
98  
99      /**
100      * {@inheritDoc}
101      */
102     public void title()
103     {
104         this.type = TITLE;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     public void section1()
111     {
112         pushNewEntry();
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     public void sectionTitle1()
119     {
120         this.type = TYPE_SECTION_1;
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     public void title_()
127     {
128         this.type = 0;
129     }
130 
131     /**
132      * <p>sectionTitle1_.</p>
133      */
134     public void sectionTitle1_()
135     {
136         this.type = 0;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public void section1_()
143     {
144         pop();
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     public void section2()
151     {
152         pushNewEntry();
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public void sectionTitle2()
159     {
160         this.type = TYPE_SECTION_2;
161     }
162 
163     /**
164      * <p>sectionTitle2_.</p>
165      */
166     public void sectionTitle2_()
167     {
168         this.type = 0;
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     public void section2_()
175     {
176         pop();
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     public void section3()
183     {
184         pushNewEntry();
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     public void sectionTitle3()
191     {
192         this.type = TYPE_SECTION_3;
193     }
194 
195     /**
196      * <p>sectionTitle3_.</p>
197      */
198     public void sectionTitle3_()
199     {
200         this.type = 0;
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     public void section3_()
207     {
208         pop();
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     public void section4()
215     {
216         pushNewEntry();
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     public void sectionTitle4()
223     {
224         this.type = TYPE_SECTION_4;
225     }
226 
227     /**
228      * <p>sectionTitle4_.</p>
229      */
230     public void sectionTitle4_()
231     {
232         this.type = 0;
233     }
234 
235     /**
236      * {@inheritDoc}
237      */
238     public void section4_()
239     {
240         pop();
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     public void section5()
247     {
248         pushNewEntry();
249     }
250 
251     /**
252      * {@inheritDoc}
253      */
254     public void sectionTitle5()
255     {
256         this.type = TYPE_SECTION_5;
257     }
258 
259     /**
260      * <p>sectionTitle5_.</p>
261      */
262     public void sectionTitle5_()
263     {
264         this.type = 0;
265     }
266 
267     /**
268      * {@inheritDoc}
269      */
270     public void section5_()
271     {
272         pop();
273     }
274 
275     // public void definedTerm()
276     // {
277     // type = TYPE_DEFINED_TERM;
278     // }
279     //
280     // public void figureCaption()
281     // {
282     // type = TYPE_FIGURE;
283     // }
284     //
285     // public void tableCaption()
286     // {
287     // type = TYPE_TABLE;
288     // }
289 
290     /** {@inheritDoc} */
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                 // Sanitize the id. The most important step is to remove any blanks
305                 // -----------------------------------------------------------------------
306 
307                 // append text to current entry
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             // Dunno how to handle these yet
318             case TYPE_DEFINED_TERM:
319             case TYPE_FIGURE:
320             case TYPE_TABLE:
321             default:
322                 break;
323         }
324     }
325 
326     /**
327      * Creates and pushes a new IndexEntry onto the top of this stack.
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      * Pushes an IndexEntry onto the top of this stack.
340      *
341      * @param entry to put.
342      */
343     public void push( IndexEntry entry )
344     {
345         stack.push( entry );
346     }
347 
348     /**
349      * Removes the IndexEntry at the top of this stack.
350      */
351     public void pop()
352     {
353         stack.pop();
354     }
355 
356     /**
357      * <p>peek.</p>
358      *
359      * @return Looks at the IndexEntry at the top of this stack.
360      */
361     public IndexEntry peek()
362     {
363         return stack.peek();
364     }
365 
366     /**
367      * {@inheritDoc}
368      */
369     public void close()
370     {
371         super.close();
372 
373         init();
374     }
375 
376     /**
377      * {@inheritDoc}
378      */
379     protected void init()
380     {
381         this.type = 0;
382         this.title = null;
383     }
384 }