Coverage Report - org.apache.maven.doxia.module.apt.AptSink
 
Classes in this File Line Coverage Branch Coverage Complexity
AptSink
92%
309/333
67%
72/106
1,713
 
 1  
 package org.apache.maven.doxia.module.apt;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.PrintWriter;
 23  
 import java.io.Writer;
 24  
 import java.util.Stack;
 25  
 
 26  
 import org.apache.maven.doxia.sink.AbstractTextSink;
 27  
 import org.apache.maven.doxia.sink.SinkEventAttributes;
 28  
 
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 
 31  
 /**
 32  
  * APT generator implementation.
 33  
  * <br/>
 34  
  * <b>Note</b>: The encoding used is UTF-8.
 35  
  *
 36  
  * @author eredmond
 37  
  * @version $Id: AptSink.java 1091053 2011-04-11 12:55:07Z ltheussl $
 38  
  * @since 1.0
 39  
  */
 40  
 public class AptSink
 41  
     extends AbstractTextSink
 42  
     implements AptMarkup
 43  
 {
 44  
     // ----------------------------------------------------------------------
 45  
     // Instance fields
 46  
     // ----------------------------------------------------------------------
 47  
 
 48  
     /**  A buffer that holds the current text when headerFlag or bufferFlag set to <code>true</code>. */
 49  
     private StringBuffer buffer;
 50  
 
 51  
     /**  A buffer that holds the table caption. */
 52  
     private StringBuffer tableCaptionBuffer;
 53  
 
 54  
     /**  author. */
 55  
     private String author;
 56  
 
 57  
     /**  title. */
 58  
     private String title;
 59  
 
 60  
     /**  date. */
 61  
     private String date;
 62  
 
 63  
     /** startFlag. */
 64  
     private boolean startFlag;
 65  
 
 66  
     /**  tableCaptionFlag. */
 67  
     private boolean tableCaptionFlag;
 68  
 
 69  
     /**  headerFlag. */
 70  
     private boolean headerFlag;
 71  
 
 72  
     /**  bufferFlag. */
 73  
     private boolean bufferFlag;
 74  
 
 75  
     /**  itemFlag. */
 76  
     private boolean itemFlag;
 77  
 
 78  
     /**  verbatimFlag. */
 79  
     private boolean verbatimFlag;
 80  
 
 81  
     /**  boxed verbatim. */
 82  
     private boolean isBoxed;
 83  
 
 84  
     /**  gridFlag for tables. */
 85  
     private boolean gridFlag;
 86  
 
 87  
     /**  number of cells in a table. */
 88  
     private int cellCount;
 89  
 
 90  
     /**  The writer to use. */
 91  
     private final PrintWriter writer;
 92  
 
 93  
     /**  justification of table cells. */
 94  
     private int cellJustif[];
 95  
 
 96  
     /**  a line of a row in a table. */
 97  
     private String rowLine;
 98  
 
 99  
     /**  listNestingIndent. */
 100  
     private String listNestingIndent;
 101  
 
 102  
     /**  listStyles. */
 103  
     private final Stack<String> listStyles;
 104  
 
 105  
     // ----------------------------------------------------------------------
 106  
     // Public protected methods
 107  
     // ----------------------------------------------------------------------
 108  
 
 109  
     /**
 110  
      * Constructor, initialize the Writer and the variables.
 111  
      *
 112  
      * @param writer not null writer to write the result. <b>Should</b> be an UTF-8 Writer.
 113  
      * You could use <code>newWriter</code> methods from {@link org.codehaus.plexus.util.WriterFactory}.
 114  
      */
 115  
     protected AptSink( Writer writer )
 116  76
     {
 117  76
         this.writer = new PrintWriter( writer );
 118  76
         this.listStyles = new Stack<String>();
 119  
 
 120  76
         init();
 121  76
     }
 122  
 
 123  
     /**
 124  
      * Returns the buffer that holds the current text.
 125  
      *
 126  
      * @return A StringBuffer.
 127  
      */
 128  
     protected StringBuffer getBuffer()
 129  
     {
 130  0
         return buffer;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Used to determine whether we are in head mode.
 135  
      *
 136  
      * @param headFlag True for head mode.
 137  
      */
 138  
     protected void setHeadFlag( boolean headFlag )
 139  
     {
 140  0
         this.headerFlag = headFlag;
 141  0
     }
 142  
 
 143  
     /**
 144  
      * Reset all variables.
 145  
      *
 146  
      * @deprecated since 1.1.2, use {@link #init()} instead of.
 147  
      */
 148  
     protected void resetState()
 149  
     {
 150  0
         init();
 151  0
     }
 152  
 
 153  
     /** {@inheritDoc} */
 154  
     protected void init()
 155  
     {
 156  156
         super.init();
 157  
 
 158  156
         resetBuffer();
 159  
 
 160  156
         this.tableCaptionBuffer = new StringBuffer();
 161  156
         this.listNestingIndent = "";
 162  
 
 163  156
         this.author = null;
 164  156
         this.title = null;
 165  156
         this.date = null;
 166  156
         this.startFlag = true;
 167  156
         this.tableCaptionFlag = false;
 168  156
         this.headerFlag = false;
 169  156
         this.bufferFlag = false;
 170  156
         this.itemFlag = false;
 171  156
         this.verbatimFlag = false;
 172  156
         this.isBoxed = false;
 173  156
         this.gridFlag = false;
 174  156
         this.cellCount = 0;
 175  156
         this.cellJustif = null;
 176  156
         this.rowLine = null;
 177  156
         this.listStyles.clear();
 178  156
     }
 179  
 
 180  
     /**
 181  
      * Reset the StringBuffer.
 182  
      */
 183  
     protected void resetBuffer()
 184  
     {
 185  230
         buffer = new StringBuffer();
 186  230
     }
 187  
 
 188  
     /**
 189  
      * Reset the TableCaptionBuffer.
 190  
      */
 191  
     protected void resetTableCaptionBuffer()
 192  
     {
 193  20
         tableCaptionBuffer = new StringBuffer();
 194  20
     }
 195  
 
 196  
     /** {@inheritDoc} */
 197  
     public void head()
 198  
     {
 199  16
         boolean startFlag = this.startFlag;
 200  
 
 201  16
         init();
 202  
 
 203  16
         headerFlag = true;
 204  16
         this.startFlag = startFlag;
 205  16
     }
 206  
 
 207  
     /** {@inheritDoc} */
 208  
     public void head_()
 209  
     {
 210  16
         headerFlag = false;
 211  
 
 212  16
         if ( ! startFlag )
 213  
         {
 214  2
             write( EOL );
 215  
         }
 216  16
         write( HEADER_START_MARKUP + EOL );
 217  16
         if ( title != null )
 218  
         {
 219  14
             write( " " + title + EOL );
 220  
         }
 221  16
         write( HEADER_START_MARKUP + EOL );
 222  16
         if ( author != null )
 223  
         {
 224  14
             write( " " + author + EOL );
 225  
         }
 226  16
         write( HEADER_START_MARKUP + EOL );
 227  16
         if ( date != null )
 228  
         {
 229  8
             write( " " + date + EOL );
 230  
         }
 231  16
         write( HEADER_START_MARKUP + EOL );
 232  16
     }
 233  
 
 234  
     /** {@inheritDoc} */
 235  
     public void title_()
 236  
     {
 237  16
         if ( buffer.length() > 0 )
 238  
         {
 239  14
             title = buffer.toString();
 240  14
             resetBuffer();
 241  
         }
 242  16
     }
 243  
 
 244  
     /** {@inheritDoc} */
 245  
     public void author_()
 246  
     {
 247  16
         if ( buffer.length() > 0 )
 248  
         {
 249  14
             author = buffer.toString();
 250  14
             resetBuffer();
 251  
         }
 252  16
     }
 253  
 
 254  
     /** {@inheritDoc} */
 255  
     public void date_()
 256  
     {
 257  10
         if ( buffer.length() > 0 )
 258  
         {
 259  8
             date = buffer.toString();
 260  8
             resetBuffer();
 261  
         }
 262  10
     }
 263  
 
 264  
     /** {@inheritDoc} */
 265  
     public void section1_()
 266  
     {
 267  20
         write( EOL );
 268  20
     }
 269  
 
 270  
     /** {@inheritDoc} */
 271  
     public void section2_()
 272  
     {
 273  18
         write( EOL );
 274  18
     }
 275  
 
 276  
     /** {@inheritDoc} */
 277  
     public void section3_()
 278  
     {
 279  12
         write( EOL );
 280  12
     }
 281  
 
 282  
     /** {@inheritDoc} */
 283  
     public void section4_()
 284  
     {
 285  12
         write( EOL );
 286  12
     }
 287  
 
 288  
     /** {@inheritDoc} */
 289  
     public void section5_()
 290  
     {
 291  10
         write( EOL );
 292  10
     }
 293  
 
 294  
     /** {@inheritDoc} */
 295  
     public void sectionTitle1()
 296  
     {
 297  20
         write( EOL );
 298  20
     }
 299  
 
 300  
     /** {@inheritDoc} */
 301  
     public void sectionTitle1_()
 302  
     {
 303  20
         write( EOL + EOL );
 304  20
     }
 305  
 
 306  
     /** {@inheritDoc} */
 307  
     public void sectionTitle2()
 308  
     {
 309  18
         write( EOL + SECTION_TITLE_START_MARKUP );
 310  18
     }
 311  
 
 312  
     /** {@inheritDoc} */
 313  
     public void sectionTitle2_()
 314  
     {
 315  18
         write( EOL + EOL );
 316  18
     }
 317  
 
 318  
     /** {@inheritDoc} */
 319  
     public void sectionTitle3()
 320  
     {
 321  12
         write( EOL + StringUtils.repeat( SECTION_TITLE_START_MARKUP, 2 ) );
 322  12
     }
 323  
 
 324  
     /** {@inheritDoc} */
 325  
     public void sectionTitle3_()
 326  
     {
 327  12
         write( EOL + EOL );
 328  12
     }
 329  
 
 330  
     /** {@inheritDoc} */
 331  
     public void sectionTitle4()
 332  
     {
 333  12
         write( EOL + StringUtils.repeat( SECTION_TITLE_START_MARKUP, 3 ) );
 334  12
     }
 335  
 
 336  
     /** {@inheritDoc} */
 337  
     public void sectionTitle4_()
 338  
     {
 339  12
         write( EOL + EOL );
 340  12
     }
 341  
 
 342  
     /** {@inheritDoc} */
 343  
     public void sectionTitle5()
 344  
     {
 345  10
         write( EOL + StringUtils.repeat( SECTION_TITLE_START_MARKUP, 4 ) );
 346  10
     }
 347  
 
 348  
     /** {@inheritDoc} */
 349  
     public void sectionTitle5_()
 350  
     {
 351  10
         write( EOL + EOL );
 352  10
     }
 353  
 
 354  
     /** {@inheritDoc} */
 355  
     public void list()
 356  
     {
 357  20
         listNestingIndent += " ";
 358  20
         listStyles.push( LIST_START_MARKUP );
 359  20
         write( EOL );
 360  20
     }
 361  
 
 362  
     /** {@inheritDoc} */
 363  
     public void list_()
 364  
     {
 365  20
         if ( listNestingIndent.length() <= 1 )
 366  
         {
 367  10
             write( EOL + listNestingIndent + LIST_END_MARKUP + EOL );
 368  
         }
 369  
         else
 370  
         {
 371  10
             write( EOL );
 372  
         }
 373  20
         listNestingIndent = StringUtils.chomp( listNestingIndent, " " );
 374  20
         listStyles.pop();
 375  20
         itemFlag = false;
 376  20
     }
 377  
 
 378  
     /** {@inheritDoc} */
 379  
     public void listItem()
 380  
     {
 381  
         //if ( !numberedList )
 382  
         //write( EOL + listNestingIndent + "*" );
 383  
         //else
 384  48
         numberedListItem();
 385  48
         itemFlag = true;
 386  48
     }
 387  
 
 388  
     /** {@inheritDoc} */
 389  
     public void listItem_()
 390  
     {
 391  48
         write( EOL );
 392  48
         itemFlag = false;
 393  48
     }
 394  
 
 395  
     /** {@inheritDoc} */
 396  
     public void numberedList( int numbering )
 397  
     {
 398  14
         listNestingIndent += " ";
 399  14
         write( EOL );
 400  
 
 401  
         String style;
 402  14
         switch ( numbering )
 403  
         {
 404  
             case NUMBERING_UPPER_ALPHA:
 405  6
                 style = String.valueOf( NUMBERING_UPPER_ALPHA_CHAR );
 406  6
                 break;
 407  
             case NUMBERING_LOWER_ALPHA:
 408  0
                 style = String.valueOf( NUMBERING_LOWER_ALPHA_CHAR );
 409  0
                 break;
 410  
             case NUMBERING_UPPER_ROMAN:
 411  0
                 style = String.valueOf( NUMBERING_UPPER_ROMAN_CHAR );
 412  0
                 break;
 413  
             case NUMBERING_LOWER_ROMAN:
 414  2
                 style = String.valueOf( NUMBERING_LOWER_ROMAN_CHAR );
 415  2
                 break;
 416  
             case NUMBERING_DECIMAL:
 417  
             default:
 418  6
                 style = String.valueOf( NUMBERING );
 419  
         }
 420  
 
 421  14
         listStyles.push( style );
 422  14
     }
 423  
 
 424  
     /** {@inheritDoc} */
 425  
     public void numberedList_()
 426  
     {
 427  14
         if ( listNestingIndent.length() <= 1 )
 428  
         {
 429  8
             write( EOL + listNestingIndent + LIST_END_MARKUP + EOL );
 430  
         }
 431  
         else
 432  
         {
 433  6
             write( EOL );
 434  
         }
 435  14
         listNestingIndent = StringUtils.chomp( listNestingIndent, " " );
 436  14
         listStyles.pop();
 437  14
         itemFlag = false;
 438  14
     }
 439  
 
 440  
     /** {@inheritDoc} */
 441  
     public void numberedListItem()
 442  
     {
 443  74
         String style = listStyles.peek();
 444  74
         if ( style.equals( String.valueOf( STAR ) ) )
 445  
         {
 446  48
             write( EOL + listNestingIndent + String.valueOf( STAR ) + String.valueOf( SPACE ) );
 447  
         }
 448  
         else
 449  
         {
 450  26
             write( EOL + listNestingIndent + String.valueOf( LEFT_SQUARE_BRACKET )
 451  
                 + String.valueOf( LEFT_SQUARE_BRACKET ) + style + String.valueOf( RIGHT_SQUARE_BRACKET )
 452  
                 + String.valueOf( RIGHT_SQUARE_BRACKET ) + String.valueOf( SPACE ) );
 453  
         }
 454  74
         itemFlag = true;
 455  74
     }
 456  
 
 457  
     /** {@inheritDoc} */
 458  
     public void numberedListItem_()
 459  
     {
 460  26
         write( EOL );
 461  26
         itemFlag = false;
 462  26
     }
 463  
 
 464  
     /** {@inheritDoc} */
 465  
     public void definitionList()
 466  
     {
 467  8
         listNestingIndent += " ";
 468  8
         listStyles.push( "" );
 469  8
         write( EOL );
 470  8
     }
 471  
 
 472  
     /** {@inheritDoc} */
 473  
     public void definitionList_()
 474  
     {
 475  8
         if ( listNestingIndent.length() <= 1 )
 476  
         {
 477  8
             write( EOL + listNestingIndent + LIST_END_MARKUP + EOL );
 478  
         }
 479  
         else
 480  
         {
 481  0
             write( EOL );
 482  
         }
 483  8
         listNestingIndent = StringUtils.chomp( listNestingIndent, " " );
 484  8
         listStyles.pop();
 485  8
         itemFlag = false;
 486  8
     }
 487  
 
 488  
     /** {@inheritDoc} */
 489  
     public void definedTerm()
 490  
     {
 491  14
         write( EOL + " [" );
 492  14
     }
 493  
 
 494  
     /** {@inheritDoc} */
 495  
     public void definedTerm_()
 496  
     {
 497  14
         write( "] " );
 498  14
     }
 499  
 
 500  
     /** {@inheritDoc} */
 501  
     public void definition()
 502  
     {
 503  14
         itemFlag = true;
 504  14
     }
 505  
 
 506  
     /** {@inheritDoc} */
 507  
     public void definition_()
 508  
     {
 509  14
         write( EOL );
 510  14
         itemFlag = false;
 511  14
     }
 512  
 
 513  
     /** {@inheritDoc} */
 514  
     public void pageBreak()
 515  
     {
 516  8
         write( EOL + PAGE_BREAK + EOL );
 517  8
     }
 518  
 
 519  
     /** {@inheritDoc} */
 520  
     public void paragraph()
 521  
     {
 522  106
         if ( itemFlag )
 523  
         {
 524  6
             write( EOL + EOL + "  " + listNestingIndent );
 525  
         }
 526  
         else
 527  
         {
 528  100
             write( EOL + " " );
 529  
         }
 530  106
     }
 531  
 
 532  
     /** {@inheritDoc} */
 533  
     public void paragraph_()
 534  
     {
 535  106
         write( EOL + EOL );
 536  106
     }
 537  
 
 538  
     /** {@inheritDoc} */
 539  
     public void verbatim( boolean boxed )
 540  
     {
 541  16
         verbatimFlag = true;
 542  16
         this.isBoxed = boxed;
 543  16
         write( EOL );
 544  16
         if ( boxed )
 545  
         {
 546  16
             write( EOL + BOXED_VERBATIM_START_MARKUP + EOL );
 547  
         }
 548  
         else
 549  
         {
 550  0
             write( EOL + NON_BOXED_VERBATIM_START_MARKUP + EOL );
 551  
         }
 552  16
     }
 553  
 
 554  
     /** {@inheritDoc} */
 555  
     public void verbatim_()
 556  
     {
 557  16
         if ( isBoxed )
 558  
         {
 559  16
             write( EOL + BOXED_VERBATIM_END_MARKUP + EOL );
 560  
         }
 561  
         else
 562  
         {
 563  0
             write( EOL + NON_BOXED_VERBATIM_END_MARKUP + EOL );
 564  
         }
 565  16
         isBoxed = false;
 566  16
         verbatimFlag = false;
 567  16
     }
 568  
 
 569  
     /** {@inheritDoc} */
 570  
     public void horizontalRule()
 571  
     {
 572  8
         write( EOL + HORIZONTAL_RULE_MARKUP + EOL );
 573  8
     }
 574  
 
 575  
     /** {@inheritDoc} */
 576  
     public void table()
 577  
     {
 578  20
         write( EOL );
 579  20
     }
 580  
 
 581  
     /** {@inheritDoc} */
 582  
     public void table_()
 583  
     {
 584  20
         if ( rowLine != null )
 585  
         {
 586  20
             write( rowLine );
 587  
         }
 588  20
         rowLine = null;
 589  
 
 590  20
         if ( tableCaptionBuffer.length() > 0 )
 591  
         {
 592  8
             text( tableCaptionBuffer.toString() + EOL );
 593  
         }
 594  
 
 595  20
         resetTableCaptionBuffer();
 596  20
     }
 597  
 
 598  
     /** {@inheritDoc} */
 599  
     public void tableRows( int justification[], boolean grid )
 600  
     {
 601  20
         cellJustif = justification;
 602  20
         gridFlag = grid;
 603  20
     }
 604  
 
 605  
     /** {@inheritDoc} */
 606  
     public void tableRows_()
 607  
     {
 608  20
         cellJustif = null;
 609  20
         gridFlag = false;
 610  20
     }
 611  
 
 612  
     /** {@inheritDoc} */
 613  
     public void tableRow()
 614  
     {
 615  38
         bufferFlag = true;
 616  38
         cellCount = 0;
 617  38
     }
 618  
 
 619  
     /** {@inheritDoc} */
 620  
     public void tableRow_()
 621  
     {
 622  38
         bufferFlag = false;
 623  
 
 624  
         // write out the header row first, then the data in the buffer
 625  38
         buildRowLine();
 626  
 
 627  38
         write( rowLine );
 628  
 
 629  
         // TODO: This will need to be more clever, for multi-line cells
 630  38
         if ( gridFlag )
 631  
         {
 632  24
             write( TABLE_ROW_SEPARATOR_MARKUP );
 633  
         }
 634  
 
 635  38
         write( buffer.toString() );
 636  
 
 637  38
         resetBuffer();
 638  
 
 639  38
         write( EOL );
 640  
 
 641  
         // only reset cell count if this is the last row
 642  38
         cellCount = 0;
 643  38
     }
 644  
 
 645  
     /** Construct a table row. */
 646  
     private void buildRowLine()
 647  
     {
 648  38
         StringBuffer rLine = new StringBuffer();
 649  38
         rLine.append( TABLE_ROW_START_MARKUP );
 650  
 
 651  124
         for ( int i = 0; i < cellCount; i++ )
 652  
         {
 653  86
             if ( cellJustif != null )
 654  
             {
 655  86
                 switch ( cellJustif[i] )
 656  
                 {
 657  
                 case 1:
 658  12
                     rLine.append( TABLE_COL_LEFT_ALIGNED_MARKUP );
 659  12
                     break;
 660  
                 case 2:
 661  12
                     rLine.append( TABLE_COL_RIGHT_ALIGNED_MARKUP );
 662  12
                     break;
 663  
                 default:
 664  62
                     rLine.append( TABLE_COL_CENTERED_ALIGNED_MARKUP );
 665  
                 }
 666  
             }
 667  
             else
 668  
             {
 669  0
                 rLine.append( TABLE_COL_CENTERED_ALIGNED_MARKUP );
 670  
             }
 671  
         }
 672  38
         rLine.append( EOL );
 673  
 
 674  38
         this.rowLine = rLine.toString();
 675  38
     }
 676  
 
 677  
     /** {@inheritDoc} */
 678  
     public void tableCell()
 679  
     {
 680  74
         tableCell( false );
 681  74
     }
 682  
 
 683  
     /** {@inheritDoc} */
 684  
     public void tableHeaderCell()
 685  
     {
 686  12
         tableCell( true );
 687  12
     }
 688  
 
 689  
     /**
 690  
      * Starts a table cell.
 691  
      *
 692  
      * @param headerRow If this cell is part of a header row.
 693  
      */
 694  
     public void tableCell( boolean headerRow )
 695  
     {
 696  86
         if ( headerRow )
 697  
         {
 698  12
             buffer.append( TABLE_CELL_SEPARATOR_MARKUP );
 699  
         }
 700  86
     }
 701  
 
 702  
     /** {@inheritDoc} */
 703  
     public void tableCell_()
 704  
     {
 705  74
         endTableCell();
 706  74
     }
 707  
 
 708  
     /** {@inheritDoc} */
 709  
     public void tableHeaderCell_()
 710  
     {
 711  12
         endTableCell();
 712  12
     }
 713  
 
 714  
     /**
 715  
      * Ends a table cell.
 716  
      */
 717  
     private void endTableCell()
 718  
     {
 719  86
         buffer.append( TABLE_CELL_SEPARATOR_MARKUP );
 720  86
         cellCount++;
 721  86
     }
 722  
 
 723  
     /** {@inheritDoc} */
 724  
     public void tableCaption()
 725  
     {
 726  8
         tableCaptionFlag = true;
 727  8
     }
 728  
 
 729  
     /** {@inheritDoc} */
 730  
     public void tableCaption_()
 731  
     {
 732  8
         tableCaptionFlag = false;
 733  8
     }
 734  
 
 735  
     /** {@inheritDoc} */
 736  
     public void figureCaption_()
 737  
     {
 738  8
         write( EOL );
 739  8
     }
 740  
 
 741  
     /** {@inheritDoc} */
 742  
     public void figureGraphics( String name )
 743  
     {
 744  8
         write( EOL + "[" + name + "] " );
 745  8
     }
 746  
 
 747  
     /** {@inheritDoc} */
 748  
     public void anchor( String name )
 749  
     {
 750  28
         write( ANCHOR_START_MARKUP );
 751  28
     }
 752  
 
 753  
     /** {@inheritDoc} */
 754  
     public void anchor_()
 755  
     {
 756  28
         write( ANCHOR_END_MARKUP );
 757  28
     }
 758  
 
 759  
     /** {@inheritDoc} */
 760  
     public void link( String name )
 761  
     {
 762  42
         if ( !headerFlag )
 763  
         {
 764  42
             write( LINK_START_1_MARKUP );
 765  42
             text( name.startsWith( "#" ) ? name.substring( 1 ) : name );
 766  42
             write( LINK_START_2_MARKUP );
 767  
         }
 768  42
     }
 769  
 
 770  
     /** {@inheritDoc} */
 771  
     public void link_()
 772  
     {
 773  42
         if ( !headerFlag )
 774  
         {
 775  42
             write( LINK_END_MARKUP );
 776  
         }
 777  42
     }
 778  
 
 779  
     /**
 780  
      * A link with a target.
 781  
      *
 782  
      * @param name The name of the link.
 783  
      * @param target The link target.
 784  
      */
 785  
     public void link( String name, String target )
 786  
     {
 787  0
         if ( !headerFlag )
 788  
         {
 789  0
             write( LINK_START_1_MARKUP );
 790  0
             text( target );
 791  0
             write( LINK_START_2_MARKUP );
 792  0
             text( name );
 793  
         }
 794  0
     }
 795  
 
 796  
     /** {@inheritDoc} */
 797  
     public void italic()
 798  
     {
 799  8
         if ( !headerFlag )
 800  
         {
 801  8
             write( ITALIC_START_MARKUP );
 802  
         }
 803  8
     }
 804  
 
 805  
     /** {@inheritDoc} */
 806  
     public void italic_()
 807  
     {
 808  8
         if ( !headerFlag )
 809  
         {
 810  8
             write( ITALIC_END_MARKUP );
 811  
         }
 812  8
     }
 813  
 
 814  
     /** {@inheritDoc} */
 815  
     public void bold()
 816  
     {
 817  8
         if ( !headerFlag )
 818  
         {
 819  8
             write( BOLD_START_MARKUP );
 820  
         }
 821  8
     }
 822  
 
 823  
     /** {@inheritDoc} */
 824  
     public void bold_()
 825  
     {
 826  8
         if ( !headerFlag )
 827  
         {
 828  8
             write( BOLD_END_MARKUP );
 829  
         }
 830  8
     }
 831  
 
 832  
     /** {@inheritDoc} */
 833  
     public void monospaced()
 834  
     {
 835  8
         if ( !headerFlag )
 836  
         {
 837  8
             write( MONOSPACED_START_MARKUP );
 838  
         }
 839  8
     }
 840  
 
 841  
     /** {@inheritDoc} */
 842  
     public void monospaced_()
 843  
     {
 844  8
         if ( !headerFlag )
 845  
         {
 846  8
             write( MONOSPACED_END_MARKUP );
 847  
         }
 848  8
     }
 849  
 
 850  
     /** {@inheritDoc} */
 851  
     public void lineBreak()
 852  
     {
 853  74
         if ( headerFlag || bufferFlag )
 854  
         {
 855  12
             buffer.append( EOL );
 856  
         }
 857  62
         else if ( verbatimFlag )
 858  
         {
 859  0
             write( EOL );
 860  
         }
 861  
         else
 862  
         {
 863  62
             write( "\\" + EOL );
 864  
         }
 865  74
     }
 866  
 
 867  
     /** {@inheritDoc} */
 868  
     public void nonBreakingSpace()
 869  
     {
 870  14
         if ( headerFlag || bufferFlag )
 871  
         {
 872  0
             buffer.append( NON_BREAKING_SPACE_MARKUP );
 873  
         }
 874  
         else
 875  
         {
 876  14
             write( NON_BREAKING_SPACE_MARKUP );
 877  
         }
 878  14
     }
 879  
 
 880  
     /** {@inheritDoc} */
 881  
     public void text( String text )
 882  
     {
 883  672
         if ( tableCaptionFlag )
 884  
         {
 885  8
             tableCaptionBuffer.append( text );
 886  
         }
 887  664
         else if ( headerFlag || bufferFlag )
 888  
         {
 889  134
             buffer.append( text );
 890  
         }
 891  530
         else if ( verbatimFlag )
 892  
         {
 893  16
             verbatimContent( text );
 894  
         }
 895  
         else
 896  
         {
 897  514
             content( text );
 898  
         }
 899  672
     }
 900  
 
 901  
     /** {@inheritDoc} */
 902  
     public void rawText( String text )
 903  
     {
 904  14
         write( text );
 905  14
     }
 906  
 
 907  
     /** {@inheritDoc} */
 908  
     public void comment( String comment )
 909  
     {
 910  12
         rawText( ( startFlag ? "" : EOL ) + COMMENT + COMMENT + SPACE + comment.trim() );
 911  12
     }
 912  
 
 913  
     /**
 914  
      * {@inheritDoc}
 915  
      *
 916  
      * Unkown events just log a warning message but are ignored otherwise.
 917  
      * @see org.apache.maven.doxia.sink.Sink#unknown(String,Object[],SinkEventAttributes)
 918  
      */
 919  
     public void unknown( String name, Object[] requiredParams, SinkEventAttributes attributes )
 920  
     {
 921  0
         getLog().warn( "[Apt Sink] Unknown Sink event: '" + name + "', ignoring!" );
 922  0
     }
 923  
 
 924  
     /**
 925  
      * Write text to output.
 926  
      *
 927  
      * @param text The text to write.
 928  
      */
 929  
     protected void write( String text )
 930  
     {
 931  1912
         startFlag = false;
 932  1912
         writer.write( unifyEOLs( text ) );
 933  1912
     }
 934  
 
 935  
     /**
 936  
      * Write Apt escaped text to output.
 937  
      *
 938  
      * @param text The text to write.
 939  
      */
 940  
     protected void content( String text )
 941  
     {
 942  514
         write( escapeAPT( text ) );
 943  514
     }
 944  
 
 945  
     /**
 946  
      * Write Apt escaped text to output.
 947  
      *
 948  
      * @param text The text to write.
 949  
      */
 950  
     protected void verbatimContent( String text )
 951  
     {
 952  16
         write( escapeAPT( text ) );
 953  16
     }
 954  
 
 955  
     /** {@inheritDoc} */
 956  
     public void flush()
 957  
     {
 958  64
         writer.flush();
 959  64
     }
 960  
 
 961  
     /** {@inheritDoc} */
 962  
     public void close()
 963  
     {
 964  64
         writer.close();
 965  
 
 966  64
         init();
 967  64
     }
 968  
 
 969  
     // ----------------------------------------------------------------------
 970  
     // Private methods
 971  
     // ----------------------------------------------------------------------
 972  
 
 973  
     /**
 974  
      * Escape special characters in a text in APT:
 975  
      *
 976  
      * <pre>
 977  
      * \~, \=, \-, \+, \*, \[, \], \<, \>, \{, \}, \\
 978  
      * </pre>
 979  
      *
 980  
      * @param text the String to escape, may be null
 981  
      * @return the text escaped, "" if null String input
 982  
      */
 983  
     private static String escapeAPT( String text )
 984  
     {
 985  530
         if ( text == null )
 986  
         {
 987  0
             return "";
 988  
         }
 989  
 
 990  530
         int length = text.length();
 991  530
         StringBuffer buffer = new StringBuffer( length );
 992  
 
 993  17378
         for ( int i = 0; i < length; ++i )
 994  
         {
 995  16848
             char c = text.charAt( i );
 996  16848
             switch ( c )
 997  
             { // 0080
 998  
                 case '\\':
 999  
                 case '~':
 1000  
                 case '=':
 1001  
                 case '-':
 1002  
                 case '+':
 1003  
                 case '*':
 1004  
                 case '[':
 1005  
                 case ']':
 1006  
                 case '<':
 1007  
                 case '>':
 1008  
                 case '{':
 1009  
                 case '}':
 1010  1312
                     buffer.append( '\\' );
 1011  1312
                     buffer.append( c );
 1012  1312
                     break;
 1013  
                 default:
 1014  15536
                     buffer.append( c );
 1015  
             }
 1016  
         }
 1017  
 
 1018  530
         return buffer.toString();
 1019  
     }
 1020  
 }