1 | |
package org.apache.maven.doxia.siterenderer.sink; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.StringWriter; |
23 | |
import java.io.Writer; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import javax.swing.text.html.HTML.Attribute; |
30 | |
|
31 | |
import org.apache.maven.doxia.module.xhtml.XhtmlSink; |
32 | |
import org.apache.maven.doxia.sink.render.RenderingContext; |
33 | |
import org.apache.maven.doxia.sink.Sink; |
34 | |
import org.apache.maven.doxia.sink.SinkEventAttributes; |
35 | |
import org.apache.maven.doxia.util.HtmlTools; |
36 | |
|
37 | |
import org.codehaus.plexus.util.StringUtils; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class SiteRendererSink |
46 | |
extends XhtmlSink |
47 | |
implements Sink, org.codehaus.doxia.sink.Sink |
48 | |
{ |
49 | 204 | private String date = ""; |
50 | |
|
51 | 204 | private String title = ""; |
52 | |
|
53 | 204 | private List<String> authors = new ArrayList<String>(); |
54 | |
|
55 | |
private final StringWriter headWriter; |
56 | |
|
57 | |
private StringBuffer sectionTitleBuffer; |
58 | |
|
59 | |
private boolean sectionHasID; |
60 | |
|
61 | |
private boolean sectionTitle; |
62 | |
|
63 | |
private Set<String> anchorsInSectionTitle; |
64 | |
|
65 | |
private final Writer writer; |
66 | |
|
67 | |
private RenderingContext renderingContext; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public SiteRendererSink( RenderingContext renderingContext ) |
75 | |
{ |
76 | 204 | this( new StringWriter(), renderingContext ); |
77 | 204 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private SiteRendererSink( StringWriter writer, RenderingContext renderingContext ) |
86 | |
{ |
87 | 204 | super( writer ); |
88 | |
|
89 | 204 | this.writer = writer; |
90 | 204 | this.headWriter = new StringWriter(); |
91 | 204 | this.renderingContext = renderingContext; |
92 | 204 | } |
93 | |
|
94 | |
|
95 | |
public void title_() |
96 | |
{ |
97 | 126 | if ( getTextBuffer().length() > 0 ) |
98 | |
{ |
99 | 126 | title = getTextBuffer().toString(); |
100 | |
} |
101 | |
|
102 | 126 | resetTextBuffer(); |
103 | 126 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void title() |
112 | |
{ |
113 | |
|
114 | 126 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public String getTitle() |
122 | |
{ |
123 | 204 | return title; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
public void author_() |
128 | |
{ |
129 | 96 | if ( getTextBuffer().length() > 0 ) |
130 | |
{ |
131 | 96 | String text = HtmlTools.escapeHTML( getTextBuffer().toString() ); |
132 | 96 | text = StringUtils.replace( text, "&#", "&#" ); |
133 | 96 | authors.add( text.trim() ); |
134 | |
} |
135 | |
|
136 | 96 | resetTextBuffer(); |
137 | 96 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public List<String> getAuthors() |
145 | |
{ |
146 | 204 | return authors; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
public void date_() |
151 | |
{ |
152 | 30 | if ( getTextBuffer().length() > 0 ) |
153 | |
{ |
154 | 30 | date = getTextBuffer().toString().trim(); |
155 | |
} |
156 | |
|
157 | 30 | resetTextBuffer(); |
158 | 30 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public String getDate() |
166 | |
{ |
167 | 240 | return date; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public void body_() |
177 | |
{ |
178 | |
|
179 | 204 | } |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public void body() |
188 | |
{ |
189 | |
|
190 | 204 | } |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public String getBody() |
198 | |
{ |
199 | 204 | return writer.toString(); |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public String getHead() |
210 | |
{ |
211 | 204 | return headWriter.toString(); |
212 | |
} |
213 | |
|
214 | |
|
215 | |
public void head_() |
216 | |
{ |
217 | 192 | setHeadFlag( false ); |
218 | 192 | } |
219 | |
|
220 | |
|
221 | |
public void head() |
222 | |
{ |
223 | 192 | setHeadFlag( true ); |
224 | 192 | } |
225 | |
|
226 | |
|
227 | |
public void anchor( String name, SinkEventAttributes attributes ) |
228 | |
{ |
229 | 516 | super.anchor( name, attributes ); |
230 | 516 | if ( sectionTitle ) |
231 | |
{ |
232 | 432 | if ( anchorsInSectionTitle == null ) |
233 | |
{ |
234 | 426 | anchorsInSectionTitle = new HashSet<String>(); |
235 | |
} |
236 | 432 | anchorsInSectionTitle.add( name ); |
237 | |
} |
238 | 516 | } |
239 | |
|
240 | |
|
241 | |
protected void onSectionTitle( int depth, SinkEventAttributes attributes ) |
242 | |
{ |
243 | 438 | this.sectionTitleBuffer = new StringBuffer(); |
244 | 438 | sectionHasID = ( attributes != null && attributes.isDefined ( Attribute.ID.toString() ) ); |
245 | 438 | sectionTitle = true; |
246 | |
|
247 | 438 | super.onSectionTitle( depth, attributes ); |
248 | 438 | } |
249 | |
|
250 | |
|
251 | |
protected void onSectionTitle_( int depth ) |
252 | |
{ |
253 | 438 | String sectionTitle = sectionTitleBuffer.toString(); |
254 | 438 | this.sectionTitleBuffer = null; |
255 | |
|
256 | 438 | if ( !sectionHasID && !StringUtils.isEmpty( sectionTitle ) ) |
257 | |
{ |
258 | 426 | String id = HtmlTools.encodeId( sectionTitle ); |
259 | 426 | if ( ( anchorsInSectionTitle == null ) || (! anchorsInSectionTitle.contains( id ) ) ) |
260 | |
{ |
261 | 420 | anchor( id ); |
262 | 420 | anchor_(); |
263 | |
} |
264 | 426 | } |
265 | |
else |
266 | |
{ |
267 | 12 | sectionHasID = false; |
268 | |
} |
269 | |
|
270 | 438 | this.sectionTitle = false; |
271 | 438 | anchorsInSectionTitle = null; |
272 | 438 | super.onSectionTitle_( depth ); |
273 | 438 | } |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public RenderingContext getRenderingContext() |
282 | |
{ |
283 | 204 | return renderingContext; |
284 | |
} |
285 | |
|
286 | |
|
287 | |
public void text( String text ) |
288 | |
{ |
289 | 8442 | if ( sectionTitleBuffer != null ) |
290 | |
{ |
291 | |
|
292 | 636 | sectionTitleBuffer.append( text ); |
293 | |
} |
294 | |
|
295 | 8442 | super.text( text ); |
296 | 8442 | } |
297 | |
|
298 | |
|
299 | |
protected void write( String text ) |
300 | |
{ |
301 | 19530 | if ( isHeadFlag() ) |
302 | |
{ |
303 | 84 | headWriter.write( unifyEOLs( text ) ); |
304 | |
|
305 | 84 | return; |
306 | |
} |
307 | |
|
308 | 19446 | if ( renderingContext != null ) |
309 | |
{ |
310 | 19446 | String relativePathToBasedir = renderingContext.getRelativePath(); |
311 | |
|
312 | 19446 | if ( relativePathToBasedir == null ) |
313 | |
{ |
314 | 0 | text = StringUtils.replace( text, "$relativePath", "." ); |
315 | |
} |
316 | |
else |
317 | |
{ |
318 | 19446 | text = StringUtils.replace( text, "$relativePath", relativePathToBasedir ); |
319 | |
} |
320 | |
} |
321 | |
|
322 | 19446 | super.write( text ); |
323 | 19446 | } |
324 | |
} |