001package org.apache.maven.doxia.sink.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.IOException; 023import java.io.Writer; 024 025import org.apache.maven.doxia.sink.SinkEventAttributes; 026import org.apache.maven.doxia.sink.impl.AbstractSink; 027import org.apache.maven.doxia.sink.impl.SinkUtils; 028 029/** 030 * A simple text-based implementation of the <code>Sink</code> interface. 031 * Useful for testing purposes. 032 */ 033public class TextSink 034 extends AbstractSink 035{ 036 037 /** For writing the result. */ 038 private final Writer out; 039 040 /** Constructor. 041 * @param writer The writer for writing the result. 042 */ 043 public TextSink( Writer writer ) 044 { 045 this.out = writer; 046 } 047 048 049 @Override 050 public void head() 051 { 052 writeln( "begin:head" ); 053 } 054 055 @Override 056 public void head_() 057 { 058 writeln( "end:head" ); 059 } 060 061 @Override 062 public void body() 063 { 064 writeln( "begin:body" ); 065 } 066 067 @Override 068 public void body_() 069 { 070 writeln( "end:body" ); 071 } 072 073 @Override 074 public void section1() 075 { 076 write( "begin:section1" ); 077 } 078 079 @Override 080 public void section1_() 081 { 082 writeln( "end:section1" ); 083 } 084 085 @Override 086 public void section2() 087 { 088 write( "begin:section2" ); 089 } 090 091 @Override 092 public void section2_() 093 { 094 writeln( "end:section2" ); 095 } 096 097 @Override 098 public void section3() 099 { 100 write( "begin:section3" ); 101 } 102 103 @Override 104 public void section3_() 105 { 106 writeln( "end:section3" ); 107 } 108 109 @Override 110 public void section4() 111 { 112 write( "begin:section4" ); 113 } 114 115 @Override 116 public void section4_() 117 { 118 writeln( "end:section4" ); 119 } 120 121 @Override 122 public void section5() 123 { 124 write( "begin:section5" ); 125 } 126 127 @Override 128 public void section5_() 129 { 130 writeln( "end:section5" ); 131 } 132 133 @Override 134 public void section6() 135 { 136 write( "begin:section6" ); 137 } 138 139 @Override 140 public void section6_() 141 { 142 writeln( "end:section6" ); 143 } 144 145 @Override 146 public void list() 147 { 148 writeln( "begin:list" ); 149 } 150 151 @Override 152 public void list_() 153 { 154 writeln( "end:list" ); 155 } 156 157 @Override 158 public void listItem() 159 { 160 write( "begin:listItem" ); 161 } 162 163 @Override 164 public void listItem_() 165 { 166 writeln( "end:listItem" ); 167 } 168 169 @Override 170 public void numberedList( int numbering ) 171 { 172 writeln( "begin:numberedList, numbering: " + numbering ); 173 } 174 175 @Override 176 public void numberedList_() 177 { 178 writeln( "end:numberedList" ); 179 } 180 181 @Override 182 public void numberedListItem() 183 { 184 write( "begin:numberedListItem" ); 185 } 186 187 @Override 188 public void numberedListItem_() 189 { 190 writeln( "end:numberedListItem" ); 191 } 192 193 @Override 194 public void definitionList() 195 { 196 writeln( "begin:definitionList" ); 197 } 198 199 @Override 200 public void definitionList_() 201 { 202 writeln( "end:definitionList" ); 203 } 204 205 @Override 206 public void definitionListItem() 207 { 208 write( "begin:definitionListItem" ); 209 } 210 211 @Override 212 public void definitionListItem_() 213 { 214 writeln( "end:definitionListItem" ); 215 } 216 217 @Override 218 public void definition() 219 { 220 write( "begin:definition" ); 221 } 222 223 @Override 224 public void definition_() 225 { 226 writeln( "end:definition" ); 227 } 228 229 @Override 230 public void figure() 231 { 232 write( "begin:figure" ); 233 } 234 235 @Override 236 public void figure_() 237 { 238 writeln( "end:figure" ); 239 } 240 241 @Override 242 public void table() 243 { 244 writeln( "begin:table" ); 245 } 246 247 @Override 248 public void table_() 249 { 250 writeln( "end:table" ); 251 } 252 253 @Override 254 public void tableRows( int[] justification, boolean grid ) 255 { 256 writeln( "begin:tableRows" ); 257 } 258 259 @Override 260 public void tableRows_() 261 { 262 writeln( "end:tableRows" ); 263 } 264 265 @Override 266 public void tableRow() 267 { 268 write( "begin:tableRow" ); 269 } 270 271 @Override 272 public void tableRow_() 273 { 274 writeln( "end:tableRow" ); 275 } 276 277 @Override 278 public void title() 279 { 280 write( "begin:title" ); 281 } 282 283 @Override 284 public void title_() 285 { 286 writeln( "end:title" ); 287 } 288 289 @Override 290 public void author() 291 { 292 write( "begin:author" ); 293 } 294 295 @Override 296 public void author_() 297 { 298 writeln( "end:author" ); 299 } 300 301 @Override 302 public void date() 303 { 304 write( "begin:date" ); 305 } 306 307 @Override 308 public void date_() 309 { 310 writeln( "end:date" ); 311 } 312 313 @Override 314 public void sectionTitle() 315 { 316 write( "begin:sectionTitle" ); 317 } 318 319 @Override 320 public void sectionTitle_() 321 { 322 writeln( "end:sectionTitle" ); 323 } 324 325 @Override 326 public void sectionTitle1() 327 { 328 write( "begin:sectionTitle1" ); 329 } 330 331 @Override 332 public void sectionTitle1_() 333 { 334 writeln( "end:sectionTitle1" ); 335 } 336 337 @Override 338 public void sectionTitle2() 339 { 340 write( "begin:sectionTitle2" ); 341 } 342 343 @Override 344 public void sectionTitle2_() 345 { 346 writeln( "end:sectionTitle2" ); 347 } 348 349 @Override 350 public void sectionTitle3() 351 { 352 write( "begin:sectionTitle3" ); 353 } 354 355 @Override 356 public void sectionTitle3_() 357 { 358 writeln( "end:sectionTitle3" ); 359 } 360 361 @Override 362 public void sectionTitle4() 363 { 364 write( "begin:sectionTitle4" ); 365 } 366 367 @Override 368 public void sectionTitle4_() 369 { 370 writeln( "end:sectionTitle4" ); 371 } 372 373 @Override 374 public void sectionTitle5() 375 { 376 write( "begin:sectionTitle5" ); 377 } 378 379 @Override 380 public void sectionTitle5_() 381 { 382 writeln( "end:sectionTitle5" ); 383 } 384 385 @Override 386 public void sectionTitle6() 387 { 388 write( "begin:sectionTitle6" ); 389 } 390 391 @Override 392 public void sectionTitle6_() 393 { 394 writeln( "end:sectionTitle6" ); 395 } 396 397 @Override 398 public void paragraph() 399 { 400 write( "begin:paragraph" ); 401 } 402 403 @Override 404 public void paragraph_() 405 { 406 writeln( "end:paragraph" ); 407 } 408 409 @Override 410 public void verbatim( boolean boxed ) 411 { 412 write( "begin:verbatim, boxed: " + boxed ); 413 } 414 415 @Override 416 public void verbatim_() 417 { 418 writeln( "end:verbatim" ); 419 } 420 421 @Override 422 public void definedTerm() 423 { 424 write( "begin:definedTerm" ); 425 } 426 427 @Override 428 public void definedTerm_() 429 { 430 writeln( "end:definedTerm" ); 431 } 432 433 @Override 434 public void figureCaption() 435 { 436 write( "begin:figureCaption" ); 437 } 438 439 @Override 440 public void figureCaption_() 441 { 442 writeln( "end:figureCaption" ); 443 } 444 445 @Override 446 public void tableCell() 447 { 448 write( "begin:tableCell" ); 449 } 450 451 @Override 452 public void tableCell( String width ) 453 { 454 write( "begin:tableCell, width: " + width ); 455 } 456 457 @Override 458 public void tableCell_() 459 { 460 writeln( "end:tableCell" ); 461 } 462 463 @Override 464 public void tableHeaderCell() 465 { 466 write( "begin:tableHeaderCell" ); 467 } 468 469 @Override 470 public void tableHeaderCell( String width ) 471 { 472 write( "begin:tableHeaderCell, width: " + width ); 473 } 474 475 @Override 476 public void tableHeaderCell_() 477 { 478 writeln( "end:tableHeaderCell" ); 479 } 480 481 @Override 482 public void tableCaption() 483 { 484 write( "begin:tableCaption" ); 485 } 486 487 @Override 488 public void tableCaption_() 489 { 490 writeln( "end:tableCaption" ); 491 } 492 493 @Override 494 public void figureGraphics( String name ) 495 { 496 write( "figureGraphics, name: " + name ); 497 } 498 499 @Override 500 public void horizontalRule() 501 { 502 write( "horizontalRule" ); 503 } 504 505 @Override 506 public void pageBreak() 507 { 508 write( "pageBreak" ); 509 } 510 511 @Override 512 public void anchor( String name ) 513 { 514 write( "begin:anchor, name: " + name ); 515 } 516 517 @Override 518 public void anchor_() 519 { 520 writeln( "end:anchor" ); 521 } 522 523 @Override 524 public void link( String name ) 525 { 526 write( "begin:link, name: " + name ); 527 } 528 529 @Override 530 public void link_() 531 { 532 writeln( "end:link" ); 533 } 534 535 @Override 536 public void italic() 537 { 538 write( "begin:italic" ); 539 } 540 541 @Override 542 public void italic_() 543 { 544 writeln( "end:italic" ); 545 } 546 547 @Override 548 public void bold() 549 { 550 write( "begin:bold" ); 551 } 552 553 @Override 554 public void bold_() 555 { 556 writeln( "end:bold" ); 557 } 558 559 @Override 560 public void monospaced() 561 { 562 write( "begin:monospaced" ); 563 } 564 565 @Override 566 public void monospaced_() 567 { 568 writeln( "end:monospaced" ); 569 } 570 571 @Override 572 public void lineBreak() 573 { 574 write( "lineBreak" ); 575 } 576 577 @Override 578 public void nonBreakingSpace() 579 { 580 write( "nonBreakingSpace" ); 581 } 582 583 @Override 584 public void text( String text ) 585 { 586 write( "text: " + text ); 587 } 588 589 @Override 590 public void rawText( String text ) 591 { 592 write( "rawText: " + text ); 593 } 594 595 @Override 596 public void comment( String comment ) 597 { 598 write( "comment: " + comment ); 599 } 600 601 @Override 602 public void flush() 603 { 604 try 605 { 606 out.flush(); 607 } 608 catch ( IOException e ) 609 { 610 getLog().warn( "Could not flush sink: " + e.getMessage(), e ); 611 } 612 } 613 614 @Override 615 public void close() 616 { 617 try 618 { 619 out.close(); 620 } 621 catch ( IOException e ) 622 { 623 getLog().warn( "Could not close sink: " + e.getMessage(), e ); 624 } 625 } 626 627 @Override 628 public void head( SinkEventAttributes attributes ) 629 { 630 head(); 631 } 632 633 @Override 634 public void title( SinkEventAttributes attributes ) 635 { 636 title(); 637 } 638 639 @Override 640 public void author( SinkEventAttributes attributes ) 641 { 642 author(); 643 } 644 645 @Override 646 public void date( SinkEventAttributes attributes ) 647 { 648 date(); 649 } 650 651 @Override 652 public void body( SinkEventAttributes attributes ) 653 { 654 body(); 655 } 656 657 @Override 658 public void section( int level, SinkEventAttributes attributes ) 659 { 660 write( "begin:section" + level ); 661 } 662 663 @Override 664 public void section_( int level ) 665 { 666 writeln( "end:section" + level ); 667 } 668 669 @Override 670 public void sectionTitle( int level, SinkEventAttributes attributes ) 671 { 672 write( "begin:sectionTitle" + level ); 673 } 674 675 @Override 676 public void sectionTitle_( int level ) 677 { 678 writeln( "end:sectionTitle" + level ); 679 } 680 681 @Override 682 public void list( SinkEventAttributes attributes ) 683 { 684 list(); 685 } 686 687 @Override 688 public void listItem( SinkEventAttributes attributes ) 689 { 690 listItem(); 691 } 692 693 @Override 694 public void numberedList( int numbering, SinkEventAttributes attributes ) 695 { 696 numberedList( numbering ); 697 } 698 699 @Override 700 public void numberedListItem( SinkEventAttributes attributes ) 701 { 702 numberedListItem(); 703 } 704 705 @Override 706 public void definitionList( SinkEventAttributes attributes ) 707 { 708 definitionList(); 709 } 710 711 @Override 712 public void definitionListItem( SinkEventAttributes attributes ) 713 { 714 definitionListItem(); 715 } 716 717 @Override 718 public void definition( SinkEventAttributes attributes ) 719 { 720 definition(); 721 } 722 723 @Override 724 public void definedTerm( SinkEventAttributes attributes ) 725 { 726 definedTerm(); 727 } 728 729 @Override 730 public void figure( SinkEventAttributes attributes ) 731 { 732 write( "begin:figure" + SinkUtils.getAttributeString( attributes ) ); 733 } 734 735 @Override 736 public void figureCaption( SinkEventAttributes attributes ) 737 { 738 figureCaption(); 739 } 740 741 @Override 742 public void figureGraphics( String src, SinkEventAttributes attributes ) 743 { 744 figureGraphics( src ); 745 } 746 747 @Override 748 public void table( SinkEventAttributes attributes ) 749 { 750 table(); 751 } 752 753 @Override 754 public void tableRow( SinkEventAttributes attributes ) 755 { 756 tableRow(); 757 } 758 759 @Override 760 public void tableCell( SinkEventAttributes attributes ) 761 { 762 tableCell(); 763 } 764 765 @Override 766 public void tableHeaderCell( SinkEventAttributes attributes ) 767 { 768 tableHeaderCell(); 769 } 770 771 @Override 772 public void tableCaption( SinkEventAttributes attributes ) 773 { 774 tableCaption(); 775 } 776 777 @Override 778 public void paragraph( SinkEventAttributes attributes ) 779 { 780 paragraph(); 781 } 782 783 @Override 784 public void verbatim( SinkEventAttributes attributes ) 785 { 786 boolean boxed = false; 787 788 if ( attributes != null && attributes.isDefined( SinkEventAttributes.DECORATION ) ) 789 { 790 boxed = "boxed".equals( 791 attributes.getAttribute( SinkEventAttributes.DECORATION ).toString() ); 792 } 793 794 verbatim( boxed ); 795 } 796 797 @Override 798 public void horizontalRule( SinkEventAttributes attributes ) 799 { 800 horizontalRule(); 801 } 802 803 @Override 804 public void anchor( String name, SinkEventAttributes attributes ) 805 { 806 anchor( name ); 807 } 808 809 @Override 810 public void link( String name, SinkEventAttributes attributes ) 811 { 812 link( name ); 813 } 814 815 @Override 816 public void lineBreak( SinkEventAttributes attributes ) 817 { 818 lineBreak(); 819 } 820 821 @Override 822 public void text( String text, SinkEventAttributes attributes ) 823 { 824 text( text ); 825 } 826 827 @Override 828 public void unknown( String name, Object[] requiredParams, SinkEventAttributes attributes ) 829 { 830 write( "unknown: " + name ); 831 } 832 833 /** 834 * Writes the given string + EOL. 835 * 836 * @param text The text to write. 837 */ 838 private void write( String text ) 839 { 840 try 841 { 842 out.write( text + EOL ); 843 } 844 catch ( IOException e ) 845 { 846 getLog().warn( "Could not write to sink: " + e.getMessage(), e ); 847 } 848 } 849 850 /** 851 * Writes the given string + two EOLs. 852 * 853 * @param text The text to write. 854 */ 855 private void writeln( String text ) 856 { 857 write( text ); 858 write( EOL ); 859 } 860}