1 package org.apache.maven.doxia.module.xdoc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Writer;
23
24 import javax.swing.text.MutableAttributeSet;
25 import javax.swing.text.html.HTML.Attribute;
26
27 import org.apache.maven.doxia.sink.SinkEventAttributes;
28 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
29 import org.apache.maven.doxia.sink.impl.SinkUtils;
30 import org.apache.maven.doxia.sink.impl.XhtmlBaseSink;
31 import org.apache.maven.doxia.util.HtmlTools;
32
33 import org.codehaus.plexus.util.StringUtils;
34
35
36
37
38
39
40
41
42
43
44
45 public class XdocSink
46 extends XhtmlBaseSink
47 implements XdocMarkup
48 {
49
50
51
52
53
54 private boolean boxedFlag;
55
56 private String encoding;
57
58 private String languageId;
59
60
61
62
63
64
65
66
67
68
69
70 protected XdocSink( Writer writer )
71 {
72 super( writer );
73 }
74
75
76
77
78
79
80
81
82
83 protected XdocSink( Writer writer, String encoding )
84 {
85 this( writer );
86 this.encoding = encoding;
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100 protected XdocSink( Writer writer, String encoding, String languageId )
101 {
102 this( writer, encoding );
103
104 this.languageId = languageId;
105 }
106
107
108
109
110
111
112 protected void init()
113 {
114 super.init();
115
116 boxedFlag = false;
117 }
118
119
120
121
122
123 public void head()
124 {
125 head( null );
126 }
127
128
129
130
131
132
133 public void head( SinkEventAttributes attributes )
134 {
135 init();
136
137 setHeadFlag( true );
138
139 write( "<?xml version=\"1.0\"" );
140 if ( encoding != null )
141 {
142 write( " encoding=\"" + encoding + "\"" );
143 }
144 write( "?>" );
145
146 MutableAttributeSet atts = new SinkEventAttributeSet();
147 atts.addAttribute( "xmlns", XDOC_NAMESPACE );
148 atts.addAttribute( "xmlns:xsi", XML_NAMESPACE );
149 atts.addAttribute( "xsi:schemaLocation", XDOC_NAMESPACE + " " + XDOC_SYSTEM_ID );
150
151 if ( languageId != null )
152 {
153 atts.addAttribute( Attribute.LANG.toString(), languageId );
154 atts.addAttribute( "xml:lang", languageId );
155 }
156
157 if ( attributes != null )
158 {
159 atts.addAttributes( attributes );
160 }
161
162 writeStartTag( DOCUMENT_TAG, atts );
163
164 writeStartTag( PROPERTIES_TAG );
165 }
166
167
168
169
170
171
172 public void head_()
173 {
174 setHeadFlag( false );
175
176 writeEndTag( PROPERTIES_TAG );
177 }
178
179
180
181
182
183 public void title()
184 {
185 writeStartTag( TITLE );
186 }
187
188
189
190
191
192 public void title_()
193 {
194 content( getTextBuffer().toString() );
195
196 writeEndTag( TITLE );
197
198 resetTextBuffer();
199 }
200
201
202
203
204
205 public void author_()
206 {
207 if ( getTextBuffer().length() > 0 )
208 {
209 writeStartTag( AUTHOR_TAG );
210 String text = HtmlTools.escapeHTML( getTextBuffer().toString() );
211
212
213 text = StringUtils.replace( text, "&#", "&#" );
214 write( text );
215 writeEndTag( AUTHOR_TAG );
216 resetTextBuffer();
217 }
218 }
219
220
221
222
223
224 public void date_()
225 {
226 if ( getTextBuffer().length() > 0 )
227 {
228 writeStartTag( DATE_TAG );
229 content( getTextBuffer().toString() );
230 writeEndTag( DATE_TAG );
231 resetTextBuffer();
232 }
233 }
234
235
236
237
238
239 public void body()
240 {
241 body( null );
242 }
243
244
245
246
247
248 public void body( SinkEventAttributes attributes )
249 {
250 writeStartTag( BODY, attributes );
251 }
252
253
254
255
256
257
258 public void body_()
259 {
260 writeEndTag( BODY );
261
262 writeEndTag( DOCUMENT_TAG );
263
264 flush();
265
266 init();
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280 protected void onSection( int depth, SinkEventAttributes attributes )
281 {
282 if ( depth == SECTION_LEVEL_1 )
283 {
284 write( String.valueOf( LESS_THAN ) + SECTION_TAG.toString()
285 + SinkUtils.getAttributeString(
286 SinkUtils.filterAttributes( attributes, SinkUtils.SINK_BASE_ATTRIBUTES ) )
287 + String.valueOf( SPACE ) + Attribute.NAME + String.valueOf( EQUAL ) + String.valueOf( QUOTE ) );
288 }
289 else if ( depth == SECTION_LEVEL_2 )
290 {
291 write( String.valueOf( LESS_THAN ) + SUBSECTION_TAG.toString()
292 + SinkUtils.getAttributeString(
293 SinkUtils.filterAttributes( attributes, SinkUtils.SINK_BASE_ATTRIBUTES ) )
294 + String.valueOf( SPACE ) + Attribute.NAME + String.valueOf( EQUAL ) + String.valueOf( QUOTE ) );
295 }
296 }
297
298
299
300
301
302
303
304
305 protected void onSection_( int depth )
306 {
307 if ( depth == SECTION_LEVEL_1 )
308 {
309 writeEndTag( SECTION_TAG );
310 }
311 else if ( depth == SECTION_LEVEL_2 )
312 {
313 writeEndTag( SUBSECTION_TAG );
314 }
315 }
316
317
318
319
320
321
322
323
324
325 protected void onSectionTitle( int depth, SinkEventAttributes attributes )
326 {
327 MutableAttributeSet atts = SinkUtils.filterAttributes(
328 attributes, SinkUtils.SINK_SECTION_ATTRIBUTES );
329
330 if ( depth == SECTION_LEVEL_3 )
331 {
332 writeStartTag( H4, atts );
333 }
334 else if ( depth == SECTION_LEVEL_4 )
335 {
336 writeStartTag( H5, atts );
337 }
338 else if ( depth == SECTION_LEVEL_5 )
339 {
340 writeStartTag( H6, atts );
341 }
342 }
343
344
345
346
347
348
349
350
351
352 protected void onSectionTitle_( int depth )
353 {
354 if ( depth == SECTION_LEVEL_1 || depth == SECTION_LEVEL_2 )
355 {
356 write( String.valueOf( QUOTE ) + String.valueOf( GREATER_THAN ) );
357 }
358 else if ( depth == SECTION_LEVEL_3 )
359 {
360 writeEndTag( H4 );
361 }
362 else if ( depth == SECTION_LEVEL_4 )
363 {
364 writeEndTag( H5 );
365 }
366 else if ( depth == SECTION_LEVEL_5 )
367 {
368 writeEndTag( H6 );
369 }
370 }
371
372
373
374
375
376
377
378
379
380
381 public void verbatim( SinkEventAttributes attributes )
382 {
383 setVerbatimFlag( true );
384
385 MutableAttributeSet atts = SinkUtils.filterAttributes(
386 attributes, SinkUtils.SINK_VERBATIM_ATTRIBUTES );
387
388
389 if ( atts == null )
390 {
391 atts = new SinkEventAttributeSet();
392 }
393
394 boolean boxed = false;
395
396 if ( atts.isDefined( SinkEventAttributes.DECORATION ) )
397 {
398 boxed = "boxed".equals(
399 (String) atts.getAttribute( SinkEventAttributes.DECORATION ) );
400 }
401
402 boxedFlag = boxed;
403 atts.removeAttribute( SinkEventAttributes.DECORATION );
404
405 if ( boxed )
406 {
407 writeStartTag( SOURCE_TAG, atts );
408 }
409 else
410 {
411 atts.removeAttribute( Attribute.ALIGN.toString() );
412 writeStartTag( PRE, atts );
413 }
414 }
415
416
417
418
419
420
421 public void verbatim_()
422 {
423 if ( boxedFlag )
424 {
425 writeEndTag( SOURCE_TAG );
426 }
427 else
428 {
429 writeEndTag( PRE );
430 }
431
432 setVerbatimFlag( false );
433
434 boxedFlag = false;
435 }
436
437
438
439
440
441
442
443 public void tableRows( int[] justification, boolean grid )
444 {
445
446
447 this.tableRows = true;
448
449 setCellJustif( justification );
450
451 if ( this.tableAttributes == null )
452 {
453 this.tableAttributes = new SinkEventAttributeSet( 0 );
454 }
455
456 MutableAttributeSet att = new SinkEventAttributeSet();
457
458 if ( !tableAttributes.isDefined( Attribute.BORDER.toString() ) )
459 {
460 att.addAttribute( Attribute.BORDER, ( grid ? "1" : "0" ) );
461 }
462
463 att.addAttributes( tableAttributes );
464
465 tableAttributes.removeAttributes( tableAttributes );
466
467 writeStartTag( TABLE, att );
468 }
469
470
471
472
473
474
475
476 public void tableRow()
477 {
478 MutableAttributeSet att = new SinkEventAttributeSet();
479 att.addAttribute( Attribute.VALIGN, "top" );
480
481 writeStartTag( TR, att );
482
483 setCellCount( 0 );
484 }
485
486 public void close()
487 {
488 super.close();
489
490 init();
491 }
492
493
494
495
496
497
498
499 public void link( String name, String target )
500 {
501 if ( isHeadFlag() )
502 {
503 return;
504 }
505
506 MutableAttributeSet att = new SinkEventAttributeSet();
507
508 att.addAttribute( Attribute.HREF, HtmlTools.escapeHTML( name ) );
509
510 if ( target != null )
511 {
512 att.addAttribute( Attribute.TARGET, target );
513 }
514
515 writeStartTag( A, att );
516 }
517
518
519
520
521
522
523
524
525
526
527
528 protected void markup( String text )
529 {
530 write( text );
531 }
532 }