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 private String date = "";
50
51 private String title = "";
52
53 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 isSectionTitle;
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 this( new StringWriter(), renderingContext );
77 }
78
79
80
81
82
83
84
85 private SiteRendererSink( StringWriter writer, RenderingContext renderingContext )
86 {
87 super( writer );
88
89 this.writer = writer;
90 this.headWriter = new StringWriter();
91 this.renderingContext = renderingContext;
92 }
93
94
95 @Override
96 public void title_()
97 {
98 if ( getTextBuffer().length() > 0 )
99 {
100 title = getTextBuffer().toString();
101 }
102
103 resetTextBuffer();
104 }
105
106
107
108
109
110
111
112 @Override
113 public void title()
114 {
115
116 }
117
118
119
120
121
122
123 public String getTitle()
124 {
125 return title;
126 }
127
128
129 @Override
130 public void author_()
131 {
132 if ( getTextBuffer().length() > 0 )
133 {
134 String text = HtmlTools.escapeHTML( getTextBuffer().toString() );
135 text = StringUtils.replace( text, "&#", "&#" );
136 authors.add( text.trim() );
137 }
138
139 resetTextBuffer();
140 }
141
142
143
144
145
146
147 public List<String> getAuthors()
148 {
149 return authors;
150 }
151
152
153 @Override
154 public void date_()
155 {
156 if ( getTextBuffer().length() > 0 )
157 {
158 date = getTextBuffer().toString().trim();
159 }
160
161 resetTextBuffer();
162 }
163
164
165
166
167
168
169 public String getDate()
170 {
171 return date;
172 }
173
174
175
176
177
178
179
180 @Override
181 public void body_()
182 {
183
184 }
185
186
187
188
189
190
191
192 @Override
193 public void body()
194 {
195
196 }
197
198
199
200
201
202
203 public String getBody()
204 {
205 return writer.toString();
206 }
207
208
209
210
211
212
213
214
215 public String getHead()
216 {
217 return headWriter.toString();
218 }
219
220
221 @Override
222 public void head_()
223 {
224 setHeadFlag( false );
225 }
226
227
228 @Override
229 public void head()
230 {
231 setHeadFlag( true );
232 }
233
234
235 @Override
236 public void anchor( String name, SinkEventAttributes attributes )
237 {
238 super.anchor( name, attributes );
239 if ( isSectionTitle )
240 {
241 if ( anchorsInSectionTitle == null )
242 {
243 anchorsInSectionTitle = new HashSet<String>();
244 }
245 anchorsInSectionTitle.add( name );
246 }
247 }
248
249
250 @Override
251 protected void onSectionTitle( int depth, SinkEventAttributes attributes )
252 {
253 this.sectionTitleBuffer = new StringBuffer();
254 sectionHasID = ( attributes != null && attributes.isDefined ( Attribute.ID.toString() ) );
255 isSectionTitle = true;
256
257 super.onSectionTitle( depth, attributes );
258 }
259
260
261 @Override
262 protected void onSectionTitle_( int depth )
263 {
264 String sectionTitle = sectionTitleBuffer.toString();
265 this.sectionTitleBuffer = null;
266
267 if ( !sectionHasID && !StringUtils.isEmpty( sectionTitle ) )
268 {
269 String id = HtmlTools.encodeId( sectionTitle );
270 if ( ( anchorsInSectionTitle == null ) || (! anchorsInSectionTitle.contains( id ) ) )
271 {
272 anchor( id );
273 anchor_();
274 }
275 }
276 else
277 {
278 sectionHasID = false;
279 }
280
281 this.isSectionTitle = false;
282 anchorsInSectionTitle = null;
283 super.onSectionTitle_( depth );
284 }
285
286
287
288
289
290
291
292 public RenderingContext getRenderingContext()
293 {
294 return renderingContext;
295 }
296
297
298 @Override
299 public void text( String text )
300 {
301 if ( sectionTitleBuffer != null )
302 {
303
304 sectionTitleBuffer.append( text );
305 }
306
307 super.text( text );
308 }
309
310
311 @Override
312 protected void write( String text )
313 {
314 String txt = text;
315
316 if ( isHeadFlag() )
317 {
318 headWriter.write( unifyEOLs( txt ) );
319
320 return;
321 }
322
323 if ( renderingContext != null )
324 {
325 String relativePathToBasedir = renderingContext.getRelativePath();
326
327 if ( relativePathToBasedir == null )
328 {
329 txt = StringUtils.replace( txt, "$relativePath", "." );
330 }
331 else
332 {
333 txt = StringUtils.replace( txt, "$relativePath", relativePathToBasedir );
334 }
335 }
336
337 super.write( txt );
338 }
339 }