1 package org.apache.maven.doxia.macro.toc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringReader;
23
24 import org.apache.maven.doxia.index.IndexEntry;
25 import org.apache.maven.doxia.index.IndexingSink;
26 import org.apache.maven.doxia.macro.AbstractMacro;
27 import org.apache.maven.doxia.macro.MacroExecutionException;
28 import org.apache.maven.doxia.macro.MacroRequest;
29 import org.apache.maven.doxia.util.HtmlTools;
30 import org.apache.maven.doxia.parser.ParseException;
31 import org.apache.maven.doxia.parser.Parser;
32 import org.apache.maven.doxia.sink.Sink;
33
34 import org.codehaus.plexus.util.StringUtils;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public class TocMacro
81 extends AbstractMacro
82 {
83
84 private int section;
85
86
87 private int fromDepth;
88
89
90 private int toDepth;
91
92
93 private static final int DEFAULT_DEPTH = 5;
94
95
96 public void execute( Sink sink, MacroRequest request )
97 throws MacroExecutionException
98 {
99 String source = (String) request.getParameter( "sourceContent" );
100 Parser parser = (Parser) request.getParameter( "parser" );
101
102 section = getInt( request, "section", 0 );
103 fromDepth = getInt( request, "fromDepth", 0 );
104 toDepth = getInt( request, "toDepth", DEFAULT_DEPTH );
105
106 if ( fromDepth > toDepth )
107 {
108 return;
109 }
110
111 IndexEntry index = new IndexEntry( "index" );
112 IndexingSink tocSink = new IndexingSink( index );
113
114 try
115 {
116 parser.parse( new StringReader( source ), tocSink );
117 }
118 catch ( ParseException e )
119 {
120 throw new MacroExecutionException( "ParseException: " + e.getMessage(), e );
121 }
122
123 if ( index.getChildEntries().size() > 0 )
124 {
125 sink.list( getAttributesFromMap( request.getParameters() ) );
126
127 int i = 1;
128
129 for ( IndexEntry sectionIndex : index.getChildEntries() )
130 {
131 if ( ( i == section ) || ( section == 0 ) )
132 {
133 writeSubSectionN( sink, sectionIndex, 1 );
134 }
135
136 i++;
137 }
138
139 sink.list_();
140 }
141 }
142
143
144
145
146
147
148 private void writeSubSectionN( Sink sink, IndexEntry sectionIndex, int n )
149 {
150 if ( fromDepth <= n )
151 {
152 sink.listItem();
153 sink.link( "#" + HtmlTools.encodeId( sectionIndex.getId() ) );
154 sink.text( sectionIndex.getTitle() );
155 sink.link_();
156 }
157
158 if ( toDepth > n )
159 {
160 if ( sectionIndex.getChildEntries().size() > 0 )
161 {
162 if ( fromDepth <= n )
163 {
164 sink.list();
165 }
166
167 for ( IndexEntry subsectionIndex : sectionIndex.getChildEntries() )
168 {
169 if ( n == toDepth - 1 )
170 {
171 sink.listItem();
172 sink.link( "#" + HtmlTools.encodeId( subsectionIndex.getId() ) );
173 sink.text( subsectionIndex.getTitle() );
174 sink.link_();
175 sink.listItem_();
176 }
177 else
178 {
179 writeSubSectionN( sink, subsectionIndex, n + 1 );
180 }
181 }
182
183 if ( fromDepth <= n )
184 {
185 sink.list_();
186 }
187 }
188 }
189
190 if ( fromDepth <= n )
191 {
192 sink.listItem_();
193 }
194 }
195
196
197
198
199
200
201
202
203 private static int getInt( MacroRequest request, String parameter, int defaultValue )
204 throws MacroExecutionException
205 {
206 String value = (String) request.getParameter( parameter );
207
208 if ( StringUtils.isEmpty( value ) )
209 {
210 return defaultValue;
211 }
212
213 int i;
214
215 try
216 {
217 i = Integer.parseInt( value );
218 }
219 catch ( NumberFormatException e )
220 {
221 return defaultValue;
222 }
223
224 if ( i < 0 )
225 {
226 throw new MacroExecutionException( "The " + parameter + "=" + i + " should be positive." );
227 }
228
229 return i;
230 }
231 }