Coverage Report - org.apache.maven.doxia.book.services.renderer.XHtmlBookRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
XHtmlBookRenderer
77%
45/58
60%
6/10
6,667
 
 1  
 package org.apache.maven.doxia.book.services.renderer;
 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 org.apache.maven.doxia.Doxia;
 23  
 import org.apache.maven.doxia.book.context.BookContext;
 24  
 import org.apache.maven.doxia.book.BookDoxiaException;
 25  
 import org.apache.maven.doxia.book.model.BookModel;
 26  
 import org.apache.maven.doxia.book.model.Chapter;
 27  
 import org.apache.maven.doxia.book.model.Section;
 28  
 import org.apache.maven.doxia.book.services.renderer.xhtml.XhtmlBookSink;
 29  
 import org.apache.maven.doxia.sink.render.RenderingContext;
 30  
 import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
 31  
 import org.apache.maven.doxia.parser.ParseException;
 32  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.codehaus.plexus.util.ReaderFactory;
 35  
 
 36  
 import java.io.File;
 37  
 import java.io.FileNotFoundException;
 38  
 import java.io.FileWriter;
 39  
 import java.io.IOException;
 40  
 import java.io.Reader;
 41  
 import java.io.Writer;
 42  
 
 43  
 /**
 44  
  * <p>XHtmlBookRenderer class.</p>
 45  
  *
 46  
  * @plexus.component role-hint="xhtml"
 47  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 48  
  * @version $Id: XHtmlBookRenderer.java 1090706 2011-04-09 23:15:28Z hboutemy $
 49  
  */
 50  1
 public class XHtmlBookRenderer
 51  
     extends AbstractLogEnabled
 52  
     implements BookRenderer
 53  
 {
 54  
     /**
 55  
      * @plexus.requirement
 56  
      */
 57  
     private Doxia doxia;
 58  
 
 59  
     // ----------------------------------------------------------------------
 60  
     // BookRenderer Implementation
 61  
     // ----------------------------------------------------------------------
 62  
 
 63  
     /** {@inheritDoc} */
 64  
     public void renderBook( BookContext context )
 65  
         throws BookDoxiaException
 66  
     {
 67  1
         BookModel book = context.getBook();
 68  
 
 69  1
         if ( !context.getOutputDirectory().exists() )
 70  
         {
 71  0
             if ( !context.getOutputDirectory().mkdirs() )
 72  
             {
 73  0
                 throw new BookDoxiaException( "Could not make directory: "
 74  
                             + context.getOutputDirectory().getAbsolutePath() + "." );
 75  
             }
 76  
         }
 77  
 
 78  1
         File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xhtml" );
 79  
 
 80  
         Writer fileWriter;
 81  
 
 82  
         try
 83  
         {
 84  1
             fileWriter = new FileWriter( bookFile );
 85  
         }
 86  0
         catch ( IOException e )
 87  
         {
 88  0
             throw new BookDoxiaException( "Error while opening file.", e );
 89  1
         }
 90  
 
 91  1
         XhtmlBookSink sink = new XhtmlBookSink( fileWriter,
 92  
               new RenderingContext( context.getOutputDirectory(), bookFile.getAbsolutePath() ) );
 93  
 
 94  
         try
 95  
         {
 96  1
             sink.bookHead();
 97  1
             sink.bookTitle();
 98  1
             sink.text( context.getBook().getTitle() );
 99  1
             sink.bookTitle_();
 100  1
             sink.bookAuthor();
 101  1
             sink.text( context.getBook().getAuthor() );
 102  1
             sink.bookAuthor_();
 103  1
             sink.bookDate();
 104  1
             sink.text( context.getBook().getDate() );
 105  1
             sink.bookDate_();
 106  1
             sink.bookHead_();
 107  1
             sink.bookBody();
 108  
 
 109  1
             int chapterNumber = 1;
 110  
 
 111  1
             for ( Chapter chapter : book.getChapters() )
 112  
             {
 113  2
                 sink.sectionTitle();
 114  2
                 sink.text( Integer.toString( chapterNumber ) + ". " + chapter.getTitle() );
 115  2
                 sink.sectionTitle_();
 116  
 
 117  2
                 renderChapter( sink, chapter, context );
 118  
 
 119  2
                 chapterNumber++;
 120  
             }
 121  
 
 122  1
             sink.bookBody_();
 123  
         }
 124  
         finally
 125  
         {
 126  1
             sink.flush();
 127  
 
 128  1
             sink.close();
 129  
 
 130  1
             IOUtil.close( fileWriter );
 131  1
         }
 132  1
     }
 133  
 
 134  
     // ----------------------------------------------------------------------
 135  
     // Private
 136  
     // ----------------------------------------------------------------------
 137  
 
 138  
     /**
 139  
      * Write a chapter.
 140  
      *
 141  
      * @param sink the XhtmlBookSink.
 142  
      * @param chapter the Chapter.
 143  
      * @param context the BookContext.
 144  
      * @throws BookDoxiaException if the chapter cannot be written.
 145  
      */
 146  
     private void renderChapter( XhtmlBookSink sink, Chapter chapter, BookContext context )
 147  
         throws BookDoxiaException
 148  
     {
 149  2
         for ( Section section : chapter.getSections() )
 150  
         {
 151  4
             renderSection( sink, section, context );
 152  
         }
 153  2
     }
 154  
 
 155  
     /**
 156  
      * Write a section.
 157  
      *
 158  
      * @param sink the XhtmlBookSink.
 159  
      * @param section the Section.
 160  
      * @param context the BookContext.
 161  
      * @throws BookDoxiaException if the section cannot be written.
 162  
      */
 163  
     private void renderSection( XhtmlBookSink sink, Section section, BookContext context )
 164  
         throws BookDoxiaException
 165  
     {
 166  4
         sink.section2();
 167  
 
 168  4
         BookContext.BookFile bookFile = context.getFiles().get( section.getId() );
 169  
 
 170  4
         if ( bookFile == null )
 171  
         {
 172  0
             throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." );
 173  
         }
 174  
 
 175  4
         Reader reader = null;
 176  
         try
 177  
         {
 178  4
             reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
 179  4
             doxia.parse( reader, bookFile.getParserId(), sink );
 180  
         }
 181  0
         catch ( ParserNotFoundException e )
 182  
         {
 183  0
             throw new BookDoxiaException( "Parser not found: "
 184  
                       + bookFile.getParserId() + ".", e );
 185  
         }
 186  0
         catch ( ParseException e )
 187  
         {
 188  0
             throw new BookDoxiaException( "Error while parsing document: "
 189  
                       + bookFile.getFile().getAbsolutePath() + ".", e );
 190  
         }
 191  0
         catch ( FileNotFoundException e )
 192  
         {
 193  0
             throw new BookDoxiaException( "Could not find document: "
 194  
                       + bookFile.getFile().getAbsolutePath() + ".", e );
 195  
         }
 196  0
         catch ( IOException e )
 197  
         {
 198  0
             throw new BookDoxiaException( "Error while rendering book: "
 199  
                       + bookFile.getFile().getAbsolutePath() + ".", e );
 200  
         }
 201  
         finally
 202  
         {
 203  4
             IOUtil.close( reader );
 204  4
         }
 205  
 
 206  4
         sink.section2_();
 207  4
     }
 208  
 }