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 | 6 | 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 | 6 | String source = (String) request.getParameter( "sourceContent" ); |
100 | 6 | Parser parser = (Parser) request.getParameter( "parser" ); |
101 | |
|
102 | 6 | section = getInt( request, "section", 0 ); |
103 | 6 | fromDepth = getInt( request, "fromDepth", 0 ); |
104 | 6 | toDepth = getInt( request, "toDepth", DEFAULT_DEPTH ); |
105 | |
|
106 | 6 | if ( fromDepth > toDepth ) |
107 | |
{ |
108 | 0 | return; |
109 | |
} |
110 | |
|
111 | 6 | IndexEntry index = new IndexEntry( "index" ); |
112 | 6 | IndexingSink tocSink = new IndexingSink( index ); |
113 | |
|
114 | |
try |
115 | |
{ |
116 | 6 | parser.parse( new StringReader( source ), tocSink ); |
117 | |
} |
118 | 0 | catch ( ParseException e ) |
119 | |
{ |
120 | 0 | throw new MacroExecutionException( "ParseException: " + e.getMessage(), e ); |
121 | 6 | } |
122 | |
|
123 | 6 | if ( index.getChildEntries().size() > 0 ) |
124 | |
{ |
125 | 6 | sink.list( getAttributesFromMap( request.getParameters() ) ); |
126 | |
|
127 | 6 | int i = 1; |
128 | |
|
129 | 6 | for ( IndexEntry sectionIndex : index.getChildEntries() ) |
130 | |
{ |
131 | 18 | if ( ( i == section ) || ( section == 0 ) ) |
132 | |
{ |
133 | 14 | writeSubSectionN( sink, sectionIndex, 1 ); |
134 | |
} |
135 | |
|
136 | 18 | i++; |
137 | |
} |
138 | |
|
139 | 6 | sink.list_(); |
140 | |
} |
141 | 6 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
private void writeSubSectionN( Sink sink, IndexEntry sectionIndex, int n ) |
149 | |
{ |
150 | 22 | if ( fromDepth <= n ) |
151 | |
{ |
152 | 22 | sink.listItem(); |
153 | 22 | sink.link( "#" + HtmlTools.encodeId( sectionIndex.getId() ) ); |
154 | 22 | sink.text( sectionIndex.getTitle() ); |
155 | 22 | sink.link_(); |
156 | |
} |
157 | |
|
158 | 22 | if ( toDepth > n ) |
159 | |
{ |
160 | 22 | if ( sectionIndex.getChildEntries().size() > 0 ) |
161 | |
{ |
162 | 10 | if ( fromDepth <= n ) |
163 | |
{ |
164 | 10 | sink.list(); |
165 | |
} |
166 | |
|
167 | 10 | for ( IndexEntry subsectionIndex : sectionIndex.getChildEntries() ) |
168 | |
{ |
169 | 10 | if ( n == toDepth - 1 ) |
170 | |
{ |
171 | 2 | sink.listItem(); |
172 | 2 | sink.link( "#" + HtmlTools.encodeId( subsectionIndex.getId() ) ); |
173 | 2 | sink.text( subsectionIndex.getTitle() ); |
174 | 2 | sink.link_(); |
175 | 2 | sink.listItem_(); |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | 8 | writeSubSectionN( sink, subsectionIndex, n + 1 ); |
180 | |
} |
181 | |
} |
182 | |
|
183 | 10 | if ( fromDepth <= n ) |
184 | |
{ |
185 | 10 | sink.list_(); |
186 | |
} |
187 | |
} |
188 | |
} |
189 | |
|
190 | 22 | if ( fromDepth <= n ) |
191 | |
{ |
192 | 22 | sink.listItem_(); |
193 | |
} |
194 | 22 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
private static int getInt( MacroRequest request, String parameter, int defaultValue ) |
204 | |
throws MacroExecutionException |
205 | |
{ |
206 | 18 | String value = (String) request.getParameter( parameter ); |
207 | |
|
208 | 18 | if ( StringUtils.isEmpty( value ) ) |
209 | |
{ |
210 | 8 | return defaultValue; |
211 | |
} |
212 | |
|
213 | |
int i; |
214 | |
|
215 | |
try |
216 | |
{ |
217 | 10 | i = Integer.parseInt( value ); |
218 | |
} |
219 | 4 | catch ( NumberFormatException e ) |
220 | |
{ |
221 | 4 | return defaultValue; |
222 | 6 | } |
223 | |
|
224 | 6 | if ( i < 0 ) |
225 | |
{ |
226 | 0 | throw new MacroExecutionException( "The " + parameter + "=" + i + " should be positive." ); |
227 | |
} |
228 | |
|
229 | 6 | return i; |
230 | |
} |
231 | |
} |