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.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  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          BookModel book = context.getBook();
68  
69          if ( !context.getOutputDirectory().exists() )
70          {
71              if ( !context.getOutputDirectory().mkdirs() )
72              {
73                  throw new BookDoxiaException( "Could not make directory: "
74                              + context.getOutputDirectory().getAbsolutePath() + "." );
75              }
76          }
77  
78          File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xhtml" );
79  
80          Writer fileWriter;
81  
82          try
83          {
84              fileWriter = new FileWriter( bookFile );
85          }
86          catch ( IOException e )
87          {
88              throw new BookDoxiaException( "Error while opening file.", e );
89          }
90  
91          XhtmlBookSink sink = new XhtmlBookSink( fileWriter,
92                new RenderingContext( context.getOutputDirectory(), bookFile.getAbsolutePath() ) );
93  
94          try
95          {
96              sink.bookHead();
97              sink.bookTitle();
98              sink.text( context.getBook().getTitle() );
99              sink.bookTitle_();
100             sink.bookAuthor();
101             sink.text( context.getBook().getAuthor() );
102             sink.bookAuthor_();
103             sink.bookDate();
104             sink.text( context.getBook().getDate() );
105             sink.bookDate_();
106             sink.bookHead_();
107             sink.bookBody();
108 
109             int chapterNumber = 1;
110 
111             for ( Chapter chapter : book.getChapters() )
112             {
113                 sink.sectionTitle();
114                 sink.text( Integer.toString( chapterNumber ) + ". " + chapter.getTitle() );
115                 sink.sectionTitle_();
116 
117                 renderChapter( sink, chapter, context );
118 
119                 chapterNumber++;
120             }
121 
122             sink.bookBody_();
123         }
124         finally
125         {
126             sink.flush();
127 
128             sink.close();
129 
130             IOUtil.close( fileWriter );
131         }
132     }
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         for ( Section section : chapter.getSections() )
150         {
151             renderSection( sink, section, context );
152         }
153     }
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         sink.section2();
167 
168         BookContext.BookFile bookFile = context.getFiles().get( section.getId() );
169 
170         if ( bookFile == null )
171         {
172             throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." );
173         }
174 
175         Reader reader = null;
176         try
177         {
178             reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
179             doxia.parse( reader, bookFile.getParserId(), sink );
180         }
181         catch ( ParserNotFoundException e )
182         {
183             throw new BookDoxiaException( "Parser not found: "
184                       + bookFile.getParserId() + ".", e );
185         }
186         catch ( ParseException e )
187         {
188             throw new BookDoxiaException( "Error while parsing document: "
189                       + bookFile.getFile().getAbsolutePath() + ".", e );
190         }
191         catch ( FileNotFoundException e )
192         {
193             throw new BookDoxiaException( "Could not find document: "
194                       + bookFile.getFile().getAbsolutePath() + ".", e );
195         }
196         catch ( IOException e )
197         {
198             throw new BookDoxiaException( "Error while rendering book: "
199                       + bookFile.getFile().getAbsolutePath() + ".", e );
200         }
201         finally
202         {
203             IOUtil.close( reader );
204         }
205 
206         sink.section2_();
207     }
208 }