1 package org.apache.maven.doxia.module.confluence;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.io.Writer;
25 import java.util.Stack;
26
27 import javax.swing.text.html.HTML.Attribute;
28
29 import org.apache.maven.doxia.sink.AbstractTextSink;
30 import org.apache.maven.doxia.sink.SinkEventAttributes;
31 import org.apache.maven.doxia.util.HtmlTools;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39
40
41
42
43 public class ConfluenceSink
44 extends AbstractTextSink
45 implements ConfluenceMarkup
46 {
47
48 private final PrintWriter out;
49
50
51 private StringWriter writer;
52
53
54 private boolean headFlag;
55
56 private int levelList = 0;
57
58
59 private final Stack<String> listStyles;
60
61
62 private boolean verbatimBoxedFlag;
63
64
65 private boolean tableHeaderFlag;
66
67
68 private String linkName;
69
70
71
72
73
74
75
76 protected ConfluenceSink( Writer writer )
77 {
78 this.out = new PrintWriter( writer );
79 this.listStyles = new Stack<String>();
80
81 init();
82 }
83
84
85 public void anchor( String name )
86 {
87 write( ANCHOR_START_MARKUP + name + ANCHOR_END_MARKUP );
88 }
89
90
91 public void anchor( String name, SinkEventAttributes attributes )
92 {
93 anchor( name );
94 }
95
96
97
98
99
100 public void anchor_()
101 {
102
103 }
104
105
106
107
108
109 public void author()
110 {
111
112 }
113
114
115 public void author( SinkEventAttributes attributes )
116 {
117 author();
118 }
119
120
121
122
123
124 public void author_()
125 {
126
127 }
128
129
130
131
132
133 public void body()
134 {
135
136 }
137
138
139 public void body( SinkEventAttributes attributes )
140 {
141 body();
142 }
143
144
145
146
147
148 public void body_()
149 {
150
151 }
152
153
154 public void bold()
155 {
156 write( BOLD_START_MARKUP );
157 }
158
159
160 public void bold_()
161 {
162 write( BOLD_END_MARKUP );
163 }
164
165
166
167
168
169 public void comment( String comment )
170 {
171
172 }
173
174
175 public void close()
176 {
177 out.write( writer.toString() );
178 out.close();
179
180 init();
181 }
182
183
184
185
186
187 public void date()
188 {
189
190 }
191
192
193 public void date( SinkEventAttributes attributes )
194 {
195 date();
196 }
197
198
199
200
201
202 public void date_()
203 {
204
205 }
206
207
208 public void definedTerm()
209 {
210 write( " " );
211 }
212
213
214 public void definedTerm( SinkEventAttributes attributes )
215 {
216 definedTerm();
217 }
218
219
220
221
222
223 public void definedTerm_()
224 {
225
226 }
227
228
229
230
231
232 public void definition()
233 {
234
235 }
236
237
238
239
240
241 public void definition( SinkEventAttributes attributes )
242 {
243
244 }
245
246
247
248
249
250 public void definition_()
251 {
252
253 }
254
255
256
257
258
259 public void definitionList()
260 {
261
262 }
263
264
265
266
267
268 public void definitionList( SinkEventAttributes attributes )
269 {
270
271 }
272
273
274
275
276
277 public void definitionList_()
278 {
279
280 }
281
282
283
284
285
286 public void definitionListItem()
287 {
288
289 }
290
291
292
293
294
295 public void definitionListItem( SinkEventAttributes attributes )
296 {
297
298 }
299
300
301
302
303
304 public void definitionListItem_()
305 {
306
307 }
308
309
310
311
312
313 public void figure()
314 {
315
316 }
317
318
319 public void figure( SinkEventAttributes attributes )
320 {
321 figure();
322 }
323
324
325
326
327
328 public void figure_()
329 {
330
331 }
332
333
334
335
336
337 public void figureCaption()
338 {
339
340 }
341
342
343 public void figureCaption( SinkEventAttributes attributes )
344 {
345 figureCaption();
346 }
347
348
349
350
351
352 public void figureCaption_()
353 {
354
355 }
356
357
358 public void figureGraphics( String name )
359 {
360 writeEOL();
361 write( FIGURE_START_MARKUP + name + FIGURE_END_MARKUP );
362 }
363
364
365 public void figureGraphics( String src, SinkEventAttributes attributes )
366 {
367 figureGraphics( src );
368 if ( attributes != null && attributes.getAttribute( Attribute.ALT.toString() ) != null )
369 {
370 write( attributes.getAttribute( Attribute.ALT.toString() ).toString() );
371 writeEOL( true );
372 }
373 }
374
375
376 public void flush()
377 {
378 close();
379 writer.flush();
380 }
381
382
383 public void head()
384 {
385 init();
386
387 headFlag = true;
388 }
389
390
391 public void head( SinkEventAttributes attributes )
392 {
393 head();
394 }
395
396
397 public void head_()
398 {
399 headFlag = false;
400 }
401
402
403
404
405
406 public void horizontalRule()
407 {
408
409 }
410
411
412 public void horizontalRule( SinkEventAttributes attributes )
413 {
414 horizontalRule();
415 }
416
417
418 public void italic()
419 {
420 write( ITALIC_START_MARKUP );
421 }
422
423
424 public void italic_()
425 {
426 write( ITALIC_END_MARKUP );
427 }
428
429
430 public void lineBreak()
431 {
432 write( LINE_BREAK_MARKUP );
433 writeEOL();
434 }
435
436
437 public void lineBreak( SinkEventAttributes attributes )
438 {
439 lineBreak();
440 }
441
442
443 public void link( String name )
444 {
445 linkName = name;
446 }
447
448
449 public void link( String name, SinkEventAttributes attributes )
450 {
451 link( name );
452 }
453
454
455 public void link_()
456 {
457 linkName = null;
458 write( LINK_END_MARKUP );
459 }
460
461
462 public void list()
463 {
464 if ( !writer.toString().endsWith( EOL + EOL ) )
465 {
466 writeEOL( true );
467 }
468
469 levelList++;
470 }
471
472
473 public void list( SinkEventAttributes attributes )
474 {
475 list();
476 }
477
478
479 public void list_()
480 {
481 levelList--;
482 }
483
484
485 public void listItem()
486 {
487 write( StringUtils.repeat( "*", levelList ) + " " );
488 }
489
490
491 public void listItem( SinkEventAttributes attributes )
492 {
493 listItem();
494 }
495
496
497 public void listItem_()
498 {
499 writeEOL( true );
500 }
501
502
503 public void monospaced()
504 {
505 write( MONOSPACED_START_MARKUP );
506 }
507
508
509 public void monospaced_()
510 {
511 write( MONOSPACED_END_MARKUP );
512 }
513
514
515
516
517
518 public void nonBreakingSpace()
519 {
520
521 }
522
523
524 public void numberedList( int numbering )
525 {
526 levelList++;
527
528 String style;
529 switch ( numbering )
530 {
531 case NUMBERING_UPPER_ALPHA:
532 case NUMBERING_LOWER_ALPHA:
533 case NUMBERING_UPPER_ROMAN:
534 case NUMBERING_LOWER_ROMAN:
535 case NUMBERING_DECIMAL:
536 default:
537 style = NUMBERING_MARKUP;
538 }
539
540 listStyles.push( style );
541 }
542
543
544 public void numberedList( int numbering, SinkEventAttributes attributes )
545 {
546 numberedList( numbering );
547 }
548
549
550 public void numberedList_()
551 {
552 levelList--;
553 listStyles.pop();
554 }
555
556
557 public void numberedListItem()
558 {
559 writeEOL( true );
560 String style = listStyles.peek();
561 write( style + SPACE );
562 }
563
564
565 public void numberedListItem( SinkEventAttributes attributes )
566 {
567 numberedListItem();
568 }
569
570
571 public void numberedListItem_()
572 {
573 writeEOL( true );
574 }
575
576
577
578
579
580 public void pageBreak()
581 {
582
583 }
584
585
586
587
588
589 public void paragraph()
590 {
591
592 }
593
594
595 public void paragraph( SinkEventAttributes attributes )
596 {
597 paragraph();
598 }
599
600
601 public void paragraph_()
602 {
603 writeEOL( true );
604 writeEOL();
605 }
606
607
608
609
610
611 public void rawText( String text )
612 {
613
614 }
615
616
617
618
619
620 public void section( int level, SinkEventAttributes attributes )
621 {
622
623 }
624
625
626
627
628
629 public void section1()
630 {
631
632 }
633
634
635
636
637
638 public void section1_()
639 {
640
641 }
642
643
644
645
646
647 public void section2()
648 {
649
650 }
651
652
653
654
655
656 public void section2_()
657 {
658
659 }
660
661
662
663
664
665 public void section3()
666 {
667
668 }
669
670
671
672
673
674 public void section3_()
675 {
676
677 }
678
679
680
681
682
683 public void section4()
684 {
685
686 }
687
688
689
690
691
692 public void section4_()
693 {
694
695 }
696
697
698
699
700
701 public void section5()
702 {
703
704 }
705
706
707
708
709
710 public void section5_()
711 {
712
713 }
714
715
716
717
718
719 public void section_( int level )
720 {
721
722 }
723
724
725
726
727
728 public void sectionTitle()
729 {
730
731 }
732
733
734 public void sectionTitle( int level, SinkEventAttributes attributes )
735 {
736 if ( level > 0 && level < 6 )
737 {
738 write( "h" + level + ". " );
739 }
740 }
741
742
743 public void sectionTitle1()
744 {
745 sectionTitle( 1, null );
746 }
747
748
749 public void sectionTitle1_()
750 {
751 sectionTitle_( 1 );
752 }
753
754
755 public void sectionTitle2()
756 {
757 sectionTitle( 2, null );
758 }
759
760
761 public void sectionTitle2_()
762 {
763 sectionTitle_( 2 );
764 }
765
766
767 public void sectionTitle3()
768 {
769 sectionTitle( 3, null );
770 }
771
772
773 public void sectionTitle3_()
774 {
775 sectionTitle_( 3 );
776 }
777
778
779 public void sectionTitle4()
780 {
781 sectionTitle( 4, null );
782 }
783
784
785 public void sectionTitle4_()
786 {
787 sectionTitle_( 4 );
788 }
789
790
791 public void sectionTitle5()
792 {
793 sectionTitle( 5, null );
794 }
795
796
797 public void sectionTitle5_()
798 {
799 sectionTitle_( 5 );
800 }
801
802
803
804
805
806 public void sectionTitle_()
807 {
808
809 }
810
811
812 public void sectionTitle_( int level )
813 {
814 writeEOL( true );
815 writeEOL();
816 }
817
818
819 public void table()
820 {
821
822 writeEOL( true );
823 writeEOL();
824 }
825
826
827 public void table( SinkEventAttributes attributes )
828 {
829 table();
830 }
831
832
833 public void table_()
834 {
835 writeEOL( true );
836 writeEOL();
837 }
838
839
840
841
842
843 public void tableCaption()
844 {
845
846 }
847
848
849 public void tableCaption( SinkEventAttributes attributes )
850 {
851 tableCaption();
852 }
853
854
855
856
857
858 public void tableCaption_()
859 {
860
861 }
862
863
864 public void tableCell()
865 {
866 write( " " );
867 }
868
869
870 public void tableCell( SinkEventAttributes attributes )
871 {
872 tableCell();
873 }
874
875
876 public void tableCell( String width )
877 {
878 tableCell();
879 }
880
881
882 public void tableCell_()
883 {
884 write( " " );
885 write( TABLE_CELL_MARKUP );
886 }
887
888
889 public void tableHeaderCell()
890 {
891 tableHeaderFlag = true;
892 write( TABLE_CELL_HEADER_START_MARKUP );
893 }
894
895
896 public void tableHeaderCell( SinkEventAttributes attributes )
897 {
898 tableHeaderCell();
899 }
900
901
902 public void tableHeaderCell( String width )
903 {
904 tableHeaderCell();
905 }
906
907
908 public void tableHeaderCell_()
909 {
910 write( TABLE_CELL_HEADER_END_MARKUP );
911 }
912
913
914 public void tableRow()
915 {
916 write( TABLE_ROW_MARKUP );
917 }
918
919
920 public void tableRow( SinkEventAttributes attributes )
921 {
922 tableRow();
923 }
924
925
926 public void tableRow_()
927 {
928 if ( tableHeaderFlag )
929 {
930 tableHeaderFlag = false;
931 write( TABLE_ROW_MARKUP );
932 }
933 writeEOL( true );
934 }
935
936
937
938
939
940 public void tableRows( int[] justification, boolean grid )
941 {
942
943 }
944
945
946
947
948
949 public void tableRows_()
950 {
951
952 }
953
954
955 public void text( String text )
956 {
957 if ( headFlag )
958 {
959 return;
960 }
961
962 if ( linkName != null )
963 {
964 write( LINK_START_MARKUP );
965 }
966
967 content( text );
968
969 if ( linkName != null )
970 {
971 write( LINK_MIDDLE_MARKUP + linkName );
972 }
973 }
974
975
976 public void text( String text, SinkEventAttributes attributes )
977 {
978 if ( attributes == null )
979 {
980 text( text );
981 }
982 else
983 {
984 if ( attributes.containsAttribute( SinkEventAttributes.DECORATION, "underline" ) )
985 {
986 write( UNDERLINED_START_MARKUP );
987 }
988 else if ( attributes.containsAttribute( SinkEventAttributes.DECORATION, "line-through" ) )
989 {
990 write( STRIKETHROUGH_START_MARKUP );
991 }
992 if ( attributes.containsAttribute( SinkEventAttributes.VALIGN, "sub" ) )
993 {
994 write( SUBSCRIPT_START_MARKUP );
995 }
996 else if ( attributes.containsAttribute( SinkEventAttributes.VALIGN, "sup" ) )
997 {
998 write( SUPERSCRIPT_START_MARKUP );
999 }
1000
1001 text( text );
1002
1003 if ( attributes.containsAttribute( SinkEventAttributes.VALIGN, "sup" ) )
1004 {
1005 write( SUPERSCRIPT_END_MARKUP );
1006 }
1007 else if ( attributes.containsAttribute( SinkEventAttributes.VALIGN, "sub" ) )
1008 {
1009 write( SUBSCRIPT_END_MARKUP );
1010 }
1011 if ( attributes.containsAttribute( SinkEventAttributes.DECORATION, "line-through" ) )
1012 {
1013 write( STRIKETHROUGH_END_MARKUP );
1014 }
1015 else if ( attributes.containsAttribute( SinkEventAttributes.DECORATION, "underline" ) )
1016 {
1017 write( UNDERLINED_END_MARKUP );
1018 }
1019 }
1020 }
1021
1022
1023
1024
1025
1026 public void title()
1027 {
1028
1029 }
1030
1031
1032 public void title( SinkEventAttributes attributes )
1033 {
1034 title();
1035 }
1036
1037
1038
1039
1040
1041 public void title_()
1042 {
1043
1044 }
1045
1046
1047
1048
1049
1050 public void unknown( String name, Object[] requiredParams, SinkEventAttributes attributes )
1051 {
1052
1053 }
1054
1055
1056 public void verbatim( boolean boxed )
1057 {
1058 if ( boxed )
1059 {
1060 verbatimBoxedFlag = true;
1061 }
1062
1063 if ( verbatimBoxedFlag )
1064 {
1065 write( "{code|borderStyle=solid}" );
1066 }
1067 else
1068 {
1069 write( "{noformat}" );
1070 }
1071 writeEOL( true );
1072 }
1073
1074
1075 public void verbatim_()
1076 {
1077 if ( verbatimBoxedFlag )
1078 {
1079 write( "{code}" );
1080 }
1081 else
1082 {
1083 write( "{noformat}" );
1084 }
1085
1086 writeEOL( true );
1087 writeEOL();
1088 }
1089
1090
1091
1092
1093
1094 private void write( String text )
1095 {
1096 writer.write( unifyEOLs( text ) );
1097 }
1098
1099
1100
1101
1102 private void writeEOL()
1103 {
1104 write( EOL );
1105 }
1106
1107
1108
1109
1110 private void writeEOL( boolean trim )
1111 {
1112 if ( !trim )
1113 {
1114 writeEOL();
1115 return;
1116 }
1117
1118 String tmp = writer.toString().trim();
1119 writer = new StringWriter();
1120 writer.write( tmp );
1121 write( EOL );
1122 }
1123
1124
1125
1126
1127
1128
1129 protected void content( String text )
1130 {
1131 write( escapeHTML( text ) );
1132 }
1133
1134
1135 protected void init()
1136 {
1137 super.init();
1138
1139 this.writer = new StringWriter();
1140 this.headFlag = false;
1141 this.levelList = 0;
1142 this.listStyles.clear();
1143 this.verbatimBoxedFlag = false;
1144 this.tableHeaderFlag = false;
1145 this.linkName = null;
1146 }
1147
1148
1149
1150
1151
1152
1153
1154
1155 protected static String escapeHTML( String text )
1156 {
1157 return HtmlTools.escapeHTML( text );
1158 }
1159 }