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