Coverage Report - org.apache.maven.doxia.book.services.renderer.LatexBookRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
LatexBookRenderer
80%
52/65
66%
12/18
7,333
LatexBookRenderer$SectionInfo
100%
1/1
N/A
7,333
 
 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.book.BookDoxiaException;
 23  
 import org.apache.maven.doxia.book.services.renderer.latex.LatexBookSink;
 24  
 import org.apache.maven.doxia.book.context.BookContext;
 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.parser.manager.ParserNotFoundException;
 29  
 import org.apache.maven.doxia.parser.ParseException;
 30  
 import org.apache.maven.doxia.Doxia;
 31  
 import org.codehaus.plexus.util.ReaderFactory;
 32  
 import org.codehaus.plexus.util.StringUtils;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.codehaus.plexus.util.WriterFactory;
 35  
 
 36  
 import java.io.File;
 37  
 import java.io.FileWriter;
 38  
 import java.io.IOException;
 39  
 import java.io.PrintWriter;
 40  
 import java.io.FileNotFoundException;
 41  
 import java.io.Reader;
 42  
 import java.io.Writer;
 43  
 import java.util.Map;
 44  
 import java.util.HashMap;
 45  
 
 46  
 /**
 47  
  * <p>LatexBookRenderer class.</p>
 48  
  *
 49  
  * @plexus.component role-hint="latex"
 50  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 51  
  * @version $Id: LatexBookRenderer.java 1090706 2011-04-09 23:15:28Z hboutemy $
 52  
  */
 53  1
 public class LatexBookRenderer
 54  
     implements BookRenderer
 55  
 {
 56  
     /**
 57  
      * @plexus.requirement
 58  
      */
 59  
     private Doxia doxia;
 60  
 
 61  
     // ----------------------------------------------------------------------
 62  
     // BookRenderer Implementatino
 63  
     // ----------------------------------------------------------------------
 64  
 
 65  
     /** {@inheritDoc} */
 66  
     public void renderBook( BookContext context )
 67  
         throws BookDoxiaException
 68  
     {
 69  1
         BookModel book = context.getBook();
 70  
 
 71  1
         if ( !context.getOutputDirectory().exists() )
 72  
         {
 73  0
             if ( !context.getOutputDirectory().mkdirs() )
 74  
             {
 75  0
                 throw new BookDoxiaException(
 76  
                     "Could not make directory: " + context.getOutputDirectory().getAbsolutePath() + "." );
 77  
             }
 78  
         }
 79  
 
 80  1
         File bookFile = new File( context.getOutputDirectory(), book.getId() + ".tex" );
 81  
 
 82  1
         FileWriter fileWriter = null;
 83  
 
 84  
         try
 85  
         {
 86  1
             fileWriter = new FileWriter( bookFile );
 87  
 
 88  1
             PrintWriter writer = new PrintWriter( fileWriter );
 89  
 
 90  1
             writeBook( book, context, writer );
 91  
         }
 92  0
         catch ( IOException e )
 93  
         {
 94  0
             throw new BookDoxiaException( "Error while opening file.", e );
 95  
         }
 96  
         finally
 97  
         {
 98  1
             IOUtil.close( fileWriter );
 99  1
         }
 100  1
     }
 101  
 
 102  
     // ----------------------------------------------------------------------
 103  
     // Private
 104  
     // ----------------------------------------------------------------------
 105  
 
 106  
     /** SectionInfo: id and title. */
 107  4
     static class SectionInfo
 108  
     {
 109  
         /** id. */
 110  
         String id;
 111  
 
 112  
         /** title. */
 113  
         String title;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Write a book.
 118  
      *
 119  
      * @param book the BookModel to write.
 120  
      * @param context the BookContext.
 121  
      * @param writer the writer to use.
 122  
      * @throws IOException if any.
 123  
      * @throws BookDoxiaException if the section cannot be written.
 124  
      */
 125  
     private void writeBook( BookModel book, BookContext context, PrintWriter writer )
 126  
         throws IOException, BookDoxiaException
 127  
     {
 128  
         // ----------------------------------------------------------------------
 129  
         // Process all the section documents and collect their names
 130  
         // ----------------------------------------------------------------------
 131  
 
 132  1
         Map<String, SectionInfo> sectionInfos = new HashMap<String, SectionInfo>();
 133  
 
 134  1
         for ( Chapter chapter : book.getChapters() )
 135  
         {
 136  2
             for ( Section section : chapter.getSections() )
 137  
             {
 138  4
                 SectionInfo info = writeSection( section, context );
 139  
 
 140  4
                 sectionInfos.put( info.id, info );
 141  4
             }
 142  
         }
 143  
 
 144  
         // ----------------------------------------------------------------------
 145  
         // Write the main .tex file
 146  
         // ----------------------------------------------------------------------
 147  
 
 148  1
         writer.println( "\\documentclass{book}" );
 149  1
         writer.println( "\\title{" + book.getTitle() + "}" );
 150  
 
 151  1
         if ( StringUtils.isNotEmpty( book.getAuthor() ) )
 152  
         {
 153  0
             writer.println( "\\author{" + book.getAuthor() + "}" );
 154  
         }
 155  
 
 156  1
         if ( StringUtils.isNotEmpty( book.getDate() ) )
 157  
         {
 158  0
             writer.println( "\\date{" + book.getDate() + "}" );
 159  
         }
 160  
 
 161  1
         LatexBookSink sink = new LatexBookSink( writer );
 162  1
         sink.defaultBookPreamble();
 163  
 
 164  1
         writer.println( "\\begin{document}" );
 165  1
         writer.println( "\\maketitle" );
 166  1
         writer.println( "\\tableofcontents" );
 167  
 //        writer.println( "\\listoffigures" );
 168  
 
 169  1
         for ( Chapter chapter : book.getChapters() )
 170  
         {
 171  2
             writer.println( "\\chapter{" + chapter.getTitle() + "}" );
 172  
 
 173  2
             for ( Section section : chapter.getSections() )
 174  
             {
 175  4
                 SectionInfo info = sectionInfos.get( section.getId() );
 176  
 
 177  4
                 writer.println( "\\input{" + info.id + "}" );
 178  4
             }
 179  
         }
 180  
 
 181  1
         writer.println( "\\end{document}" );
 182  1
     }
 183  
 
 184  
     /**
 185  
      * Write a section.
 186  
      *
 187  
      * @param section the Section to write.
 188  
      * @param context the BookContext.
 189  
      * @return SectionInfo
 190  
      * @throws IOException if any.
 191  
      * @throws BookDoxiaException if the section cannot be written.
 192  
      */
 193  
     private SectionInfo writeSection( Section section, BookContext context )
 194  
         throws IOException, BookDoxiaException
 195  
     {
 196  4
         File file = new File( context.getOutputDirectory(), ( section.getId() + ".tex" ) );
 197  
 
 198  4
         Writer writer = WriterFactory.newWriter( file, context.getOutputEncoding() );
 199  
 
 200  4
         LatexBookSink sink = new LatexBookSink( writer );
 201  
 
 202  4
         BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() );
 203  
 
 204  4
         if ( bookFile == null )
 205  
         {
 206  0
             throw new BookDoxiaException( "No document that matches section with id="
 207  
                         + section.getId() + "." );
 208  
         }
 209  
 
 210  4
         Reader reader = null;
 211  
         try
 212  
         {
 213  4
             reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
 214  4
             doxia.parse( reader, bookFile.getParserId(), sink );
 215  
         }
 216  0
         catch ( ParserNotFoundException e )
 217  
         {
 218  0
             throw new BookDoxiaException( "Parser not found: "
 219  
                         + bookFile.getParserId() + ".", e );
 220  
         }
 221  0
         catch ( ParseException e )
 222  
         {
 223  0
             throw new BookDoxiaException( "Error while parsing document: "
 224  
                         + bookFile.getFile().getAbsolutePath() + ".", e );
 225  
         }
 226  0
         catch ( FileNotFoundException e )
 227  
         {
 228  0
             throw new BookDoxiaException( "Could not find document: "
 229  
                         + bookFile.getFile().getAbsolutePath() + ".", e );
 230  
         }
 231  
         finally
 232  
         {
 233  4
             sink.flush();
 234  4
             sink.close();
 235  
 
 236  4
             IOUtil.close( reader );
 237  4
             IOUtil.close( writer );
 238  4
         }
 239  
 
 240  4
         SectionInfo info = new SectionInfo();
 241  4
         info.id = section.getId();
 242  4
         info.title = sink.getTitle();
 243  
 
 244  4
         return info;
 245  
     }
 246  
 }