Coverage Report - org.apache.maven.doxia.siterenderer.sink.SiteRendererSink
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteRendererSink
98%
74/75
80%
24/30
1,714
 
 1  
 package org.apache.maven.doxia.siterenderer.sink;
 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.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  
  * Sink for site rendering.
 41  
  *
 42  
  * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
 43  
  * @version $Id: SiteRendererSink.java 1087124 2011-03-30 22:45:41Z hboutemy $
 44  
  */
 45  
 public class SiteRendererSink
 46  
     extends XhtmlSink
 47  
     implements Sink, org.codehaus.doxia.sink.Sink
 48  
 {
 49  204
     private String date = "";
 50  
 
 51  204
     private String title = "";
 52  
 
 53  204
     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  
      * Construct a new SiteRendererSink.
 71  
      *
 72  
      * @param renderingContext the RenderingContext.
 73  
      */
 74  
     public SiteRendererSink( RenderingContext renderingContext )
 75  
     {
 76  204
         this( new StringWriter(), renderingContext );
 77  204
     }
 78  
 
 79  
     /**
 80  
      * Construct a new SiteRendererSink.
 81  
      *
 82  
      * @param writer the writer for the sink.
 83  
      * @param renderingContext the RenderingContext.
 84  
      */
 85  
     private SiteRendererSink( StringWriter writer, RenderingContext renderingContext )
 86  
     {
 87  204
         super( writer );
 88  
 
 89  204
         this.writer = writer;
 90  204
         this.headWriter = new StringWriter();
 91  204
         this.renderingContext = renderingContext;
 92  204
     }
 93  
 
 94  
     /** {@inheritDoc} */
 95  
     public void title_()
 96  
     {
 97  126
         if ( getTextBuffer().length() > 0 )
 98  
         {
 99  126
             title = getTextBuffer().toString();
 100  
         }
 101  
 
 102  126
         resetTextBuffer();
 103  126
     }
 104  
 
 105  
     /**
 106  
      * {@inheritDoc}
 107  
      *
 108  
      * Do nothing.
 109  
      * @see org.apache.maven.doxia.module.xhtml.XhtmlSink#title()
 110  
      */
 111  
     public void title()
 112  
     {
 113  
         // nop
 114  126
     }
 115  
 
 116  
     /**
 117  
      * <p>Getter for the field <code>title</code>.</p>
 118  
      *
 119  
      * @return a {@link java.lang.String} object.
 120  
      */
 121  
     public String getTitle()
 122  
     {
 123  204
         return title;
 124  
     }
 125  
 
 126  
     /** {@inheritDoc} */
 127  
     public void author_()
 128  
     {
 129  96
         if ( getTextBuffer().length() > 0 )
 130  
         {
 131  96
             String text = HtmlTools.escapeHTML( getTextBuffer().toString() );
 132  96
             text = StringUtils.replace( text, "&amp;#", "&#" );
 133  96
             authors.add( text.trim() );
 134  
         }
 135  
 
 136  96
         resetTextBuffer();
 137  96
     }
 138  
 
 139  
     /**
 140  
      * <p>Getter for the field <code>authors</code>.</p>
 141  
      *
 142  
      * @return a {@link java.util.List} object.
 143  
      */
 144  
     public List<String> getAuthors()
 145  
     {
 146  204
         return authors;
 147  
     }
 148  
 
 149  
     /** {@inheritDoc} */
 150  
     public void date_()
 151  
     {
 152  30
         if ( getTextBuffer().length() > 0 )
 153  
         {
 154  30
             date = getTextBuffer().toString().trim();
 155  
         }
 156  
 
 157  30
         resetTextBuffer();
 158  30
     }
 159  
 
 160  
     /**
 161  
      * <p>Getter for the field <code>date</code>.</p>
 162  
      *
 163  
      * @return a {@link java.lang.String} object.
 164  
      */
 165  
     public String getDate()
 166  
     {
 167  240
         return date;
 168  
     }
 169  
 
 170  
     /**
 171  
      * {@inheritDoc}
 172  
      *
 173  
      * Do nothing.
 174  
      * @see org.apache.maven.doxia.module.xhtml.XhtmlSink#body_()
 175  
      */
 176  
     public void body_()
 177  
     {
 178  
         // nop
 179  204
     }
 180  
 
 181  
     /**
 182  
      * {@inheritDoc}
 183  
      *
 184  
      * Do nothing.
 185  
      * @see org.apache.maven.doxia.module.xhtml.XhtmlSink#body()
 186  
      */
 187  
     public void body()
 188  
     {
 189  
         // nop
 190  204
     }
 191  
 
 192  
     /**
 193  
      * <p>getBody.</p>
 194  
      *
 195  
      * @return a {@link java.lang.String} object.
 196  
      */
 197  
     public String getBody()
 198  
     {
 199  204
         return writer.toString();
 200  
     }
 201  
 
 202  
     /**
 203  
      * <p>getHead.</p>
 204  
      *
 205  
      * @return a {@link java.lang.String} object.
 206  
      *
 207  
      * @since 1.1.1
 208  
      */
 209  
     public String getHead()
 210  
     {
 211  204
         return headWriter.toString();
 212  
     }
 213  
 
 214  
     /** {@inheritDoc} */
 215  
     public void head_()
 216  
     {
 217  192
         setHeadFlag( false );
 218  192
     }
 219  
 
 220  
     /** {@inheritDoc} */
 221  
     public void head()
 222  
     {
 223  192
         setHeadFlag( true );
 224  192
     }
 225  
 
 226  
     /** {@inheritDoc} */
 227  
     public void anchor( String name, SinkEventAttributes attributes )
 228  
     {
 229  516
         super.anchor( name, attributes );
 230  516
         if ( sectionTitle )
 231  
         {
 232  432
             if ( anchorsInSectionTitle == null )
 233  
             {
 234  426
                 anchorsInSectionTitle = new HashSet<String>();
 235  
             }
 236  432
             anchorsInSectionTitle.add( name );
 237  
         }
 238  516
     }
 239  
     
 240  
     /** {@inheritDoc} */
 241  
     protected void onSectionTitle( int depth, SinkEventAttributes attributes )
 242  
     {
 243  438
         this.sectionTitleBuffer = new StringBuffer();
 244  438
         sectionHasID = ( attributes != null && attributes.isDefined ( Attribute.ID.toString() ) );
 245  438
         sectionTitle = true;
 246  
 
 247  438
         super.onSectionTitle( depth, attributes );
 248  438
     }
 249  
 
 250  
     /** {@inheritDoc} */
 251  
     protected void onSectionTitle_( int depth )
 252  
     {
 253  438
         String sectionTitle = sectionTitleBuffer.toString();
 254  438
         this.sectionTitleBuffer = null;
 255  
 
 256  438
         if ( !sectionHasID && !StringUtils.isEmpty( sectionTitle ) )
 257  
         {
 258  426
             String id = HtmlTools.encodeId( sectionTitle );
 259  426
             if ( ( anchorsInSectionTitle == null ) || (! anchorsInSectionTitle.contains( id ) ) )
 260  
             {
 261  420
                 anchor( id );
 262  420
                 anchor_();
 263  
             }
 264  426
         }
 265  
         else
 266  
         {
 267  12
             sectionHasID = false;
 268  
         }
 269  
 
 270  438
         this.sectionTitle = false;
 271  438
         anchorsInSectionTitle = null;
 272  438
         super.onSectionTitle_( depth );
 273  438
     }
 274  
 
 275  
     /**
 276  
      * <p>Getter for the field <code>renderingContext</code>.</p>
 277  
      *
 278  
      * @return the current rendering context
 279  
      * @since 1.1
 280  
      */
 281  
     public RenderingContext getRenderingContext()
 282  
     {
 283  204
         return renderingContext;
 284  
     }
 285  
 
 286  
     /** {@inheritDoc} */
 287  
     public void text( String text )
 288  
     {
 289  8442
         if ( sectionTitleBuffer != null )
 290  
         {
 291  
             // this implies we're inside a section title, collect text events for anchor generation
 292  636
             sectionTitleBuffer.append( text );
 293  
         }
 294  
 
 295  8442
         super.text( text );
 296  8442
     }
 297  
 
 298  
     /** {@inheritDoc} */
 299  
     protected void write( String text )
 300  
     {
 301  19530
         if ( isHeadFlag() )
 302  
         {
 303  84
             headWriter.write( unifyEOLs( text ) );
 304  
 
 305  84
             return;
 306  
         }
 307  
 
 308  19446
         if ( renderingContext != null )
 309  
         {
 310  19446
             String relativePathToBasedir = renderingContext.getRelativePath();
 311  
 
 312  19446
             if ( relativePathToBasedir == null )
 313  
             {
 314  0
                 text = StringUtils.replace( text, "$relativePath", "." );
 315  
             }
 316  
             else
 317  
             {
 318  19446
                 text = StringUtils.replace( text, "$relativePath", relativePathToBasedir );
 319  
             }
 320  
         }
 321  
 
 322  19446
         super.write( text );
 323  19446
     }
 324  
 }