1 package org.apache.maven.doxia.book.services.renderer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50 public class XHtmlBookRenderer
51 extends AbstractLogEnabled
52 implements BookRenderer
53 {
54
55
56
57 private Doxia doxia;
58
59
60
61
62
63
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
136
137
138
139
140
141
142
143
144
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
157
158
159
160
161
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 }