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   * @version $Id$
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      /**
74       * Default constructor.
75       *
76       * @param sectionEntry The first index entry.
77       */
78      public IndexingSink( IndexEntry sectionEntry )
79      {
80          stack = new Stack<>();
81          stack.push( sectionEntry );
82  
83          init();
84      }
85  
86      /**
87       * <p>Getter for the field <code>title</code>.</p>
88       *
89       * @return the title
90       */
91      public String getTitle()
92      {
93          return title;
94      }
95  
96      // ----------------------------------------------------------------------
97      // Sink Overrides
98      // ----------------------------------------------------------------------
99  
100     /** {@inheritDoc} */
101     public void title()
102     {
103         this.type = TITLE;
104     }
105 
106     /** {@inheritDoc} */
107     public void section1()
108     {
109         pushNewEntry();
110     }
111 
112     /** {@inheritDoc} */
113     public void sectionTitle1()
114     {
115         this.type = TYPE_SECTION_1;
116     }
117 
118     /** {@inheritDoc} */
119     public void title_()
120     {
121         this.type = 0;
122     }
123 
124     public void sectionTitle1_()
125     {
126         this.type = 0;
127     }
128 
129     /** {@inheritDoc} */
130     public void section1_()
131     {
132         pop();
133     }
134 
135     /** {@inheritDoc} */
136     public void section2()
137     {
138         pushNewEntry();
139     }
140 
141     /** {@inheritDoc} */
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     /** {@inheritDoc} */
153     public void section2_()
154     {
155         pop();
156     }
157 
158     /** {@inheritDoc} */
159     public void section3()
160     {
161         pushNewEntry();
162     }
163 
164     /** {@inheritDoc} */
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     /** {@inheritDoc} */
176     public void section3_()
177     {
178         pop();
179     }
180 
181     /** {@inheritDoc} */
182     public void section4()
183     {
184         pushNewEntry();
185     }
186 
187     /** {@inheritDoc} */
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     /** {@inheritDoc} */
199     public void section4_()
200     {
201         pop();
202     }
203 
204     /** {@inheritDoc} */
205     public void section5()
206     {
207         pushNewEntry();
208     }
209 
210     /** {@inheritDoc} */
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     /** {@inheritDoc} */
222     public void section5_()
223     {
224         pop();
225     }
226 
227     // public void definedTerm()
228     // {
229     // type = TYPE_DEFINED_TERM;
230     // }
231     //
232     // public void figureCaption()
233     // {
234     // type = TYPE_FIGURE;
235     // }
236     //
237     // public void tableCaption()
238     // {
239     // type = TYPE_TABLE;
240     // }
241 
242     /** {@inheritDoc} */
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                 // Sanitize the id. The most important step is to remove any blanks
257                 // -----------------------------------------------------------------------
258 
259                 // append text to current entry
260                 IndexEntry entry = 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             // Dunno how to handle these yet
270             case TYPE_DEFINED_TERM:
271             case TYPE_FIGURE:
272             case TYPE_TABLE:
273             default:
274                 break;
275         }
276     }
277 
278     /**
279      * Creates and pushes a new IndexEntry onto the top of this stack.
280      */
281     public void pushNewEntry()
282     {
283         IndexEntry entry = new IndexEntry( peek(), "" );
284 
285         entry.setTitle( "" );
286 
287         stack.push( entry );
288     }
289 
290     /**
291      * Pushes an IndexEntry onto the top of this stack.
292      *
293      * @param entry to put.
294      */
295     public void push( IndexEntry entry )
296     {
297         stack.push( entry );
298     }
299 
300     /**
301      * Removes the IndexEntry at the top of this stack.
302      */
303     public void pop()
304     {
305         stack.pop();
306     }
307 
308     /**
309      * <p>peek.</p>
310      *
311      * @return Looks at the IndexEntry at the top of this stack.
312      */
313     public IndexEntry peek()
314     {
315         return stack.peek();
316     }
317 
318     /** {@inheritDoc} */
319     public void close()
320     {
321         super.close();
322 
323         init();
324     }
325 
326     /** {@inheritDoc} */
327     protected void init()
328     {
329         this.type = 0;
330         this.title = null;
331     }
332 }