View Javadoc

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  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          BookModel book = context.getBook();
70  
71          if ( !context.getOutputDirectory().exists() )
72          {
73              if ( !context.getOutputDirectory().mkdirs() )
74              {
75                  throw new BookDoxiaException(
76                      "Could not make directory: " + context.getOutputDirectory().getAbsolutePath() + "." );
77              }
78          }
79  
80          File bookFile = new File( context.getOutputDirectory(), book.getId() + ".tex" );
81  
82          FileWriter fileWriter = null;
83  
84          try
85          {
86              fileWriter = new FileWriter( bookFile );
87  
88              PrintWriter writer = new PrintWriter( fileWriter );
89  
90              writeBook( book, context, writer );
91          }
92          catch ( IOException e )
93          {
94              throw new BookDoxiaException( "Error while opening file.", e );
95          }
96          finally
97          {
98              IOUtil.close( fileWriter );
99          }
100     }
101 
102     // ----------------------------------------------------------------------
103     // Private
104     // ----------------------------------------------------------------------
105 
106     /** SectionInfo: id and title. */
107     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         Map<String, SectionInfo> sectionInfos = new HashMap<String, SectionInfo>();
133 
134         for ( Chapter chapter : book.getChapters() )
135         {
136             for ( Section section : chapter.getSections() )
137             {
138                 SectionInfo info = writeSection( section, context );
139 
140                 sectionInfos.put( info.id, info );
141             }
142         }
143 
144         // ----------------------------------------------------------------------
145         // Write the main .tex file
146         // ----------------------------------------------------------------------
147 
148         writer.println( "\\documentclass{book}" );
149         writer.println( "\\title{" + book.getTitle() + "}" );
150 
151         if ( StringUtils.isNotEmpty( book.getAuthor() ) )
152         {
153             writer.println( "\\author{" + book.getAuthor() + "}" );
154         }
155 
156         if ( StringUtils.isNotEmpty( book.getDate() ) )
157         {
158             writer.println( "\\date{" + book.getDate() + "}" );
159         }
160 
161         LatexBookSink sink = new LatexBookSink( writer );
162         sink.defaultBookPreamble();
163 
164         writer.println( "\\begin{document}" );
165         writer.println( "\\maketitle" );
166         writer.println( "\\tableofcontents" );
167 //        writer.println( "\\listoffigures" );
168 
169         for ( Chapter chapter : book.getChapters() )
170         {
171             writer.println( "\\chapter{" + chapter.getTitle() + "}" );
172 
173             for ( Section section : chapter.getSections() )
174             {
175                 SectionInfo info = sectionInfos.get( section.getId() );
176 
177                 writer.println( "\\input{" + info.id + "}" );
178             }
179         }
180 
181         writer.println( "\\end{document}" );
182     }
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         File file = new File( context.getOutputDirectory(), ( section.getId() + ".tex" ) );
197 
198         Writer writer = WriterFactory.newWriter( file, context.getOutputEncoding() );
199 
200         LatexBookSink sink = new LatexBookSink( writer );
201 
202         BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() );
203 
204         if ( bookFile == null )
205         {
206             throw new BookDoxiaException( "No document that matches section with id="
207                         + section.getId() + "." );
208         }
209 
210         Reader reader = null;
211         try
212         {
213             reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
214             doxia.parse( reader, bookFile.getParserId(), sink );
215         }
216         catch ( ParserNotFoundException e )
217         {
218             throw new BookDoxiaException( "Parser not found: "
219                         + bookFile.getParserId() + ".", e );
220         }
221         catch ( ParseException e )
222         {
223             throw new BookDoxiaException( "Error while parsing document: "
224                         + bookFile.getFile().getAbsolutePath() + ".", e );
225         }
226         catch ( FileNotFoundException e )
227         {
228             throw new BookDoxiaException( "Could not find document: "
229                         + bookFile.getFile().getAbsolutePath() + ".", e );
230         }
231         finally
232         {
233             sink.flush();
234             sink.close();
235 
236             IOUtil.close( reader );
237             IOUtil.close( writer );
238         }
239 
240         SectionInfo info = new SectionInfo();
241         info.id = section.getId();
242         info.title = sink.getTitle();
243 
244         return info;
245     }
246 }