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 java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.Writer;
27
28 import org.apache.maven.doxia.Doxia;
29 import org.apache.maven.doxia.book.BookDoxiaException;
30 import org.apache.maven.doxia.book.context.BookContext;
31 import org.apache.maven.doxia.book.model.BookModel;
32 import org.apache.maven.doxia.book.model.Chapter;
33 import org.apache.maven.doxia.book.model.Section;
34 import org.apache.maven.doxia.book.services.renderer.docbook.DocBookBookSink;
35 import org.apache.maven.doxia.parser.ParseException;
36 import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
37 import org.apache.maven.doxia.sink.Sink;
38 import org.codehaus.plexus.logging.AbstractLogEnabled;
39 import org.codehaus.plexus.util.IOUtil;
40 import org.codehaus.plexus.util.ReaderFactory;
41 import org.codehaus.plexus.util.StringUtils;
42 import org.codehaus.plexus.util.WriterFactory;
43
44
45
46
47
48
49
50
51 public class DocbookBookRenderer
52 extends AbstractLogEnabled
53 implements BookRenderer
54 {
55
56
57
58 private Doxia doxia;
59
60
61
62
63
64
65 public void renderBook( BookContext context )
66 throws BookDoxiaException
67 {
68 BookModel book = context.getBook();
69
70 if ( !context.getOutputDirectory().exists() )
71 {
72 if ( !context.getOutputDirectory().mkdirs() )
73 {
74 throw new BookDoxiaException( "Could not make directory: "
75 + context.getOutputDirectory().getAbsolutePath() + "." );
76 }
77 }
78
79 File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xml" );
80
81 Writer fileWriter;
82 try
83 {
84 fileWriter = WriterFactory.newXmlWriter( bookFile );
85 }
86 catch ( IOException e )
87 {
88 throw new BookDoxiaException( "Error while opening file.", e );
89 }
90
91
92
93
94
95
96
97 DocBookBookSink sink = new DocBookBookSink( fileWriter );
98
99 try
100 {
101 sink.book();
102
103
104
105 if ( StringUtils.isNotEmpty( book.getTitle() ) )
106 {
107 sink.bookTitle();
108 sink.text( book.getTitle() );
109 sink.bookTitle_();
110 }
111
112 if ( StringUtils.isNotEmpty( book.getDate() ) )
113 {
114 sink.bookDate();
115 sink.text( book.getDate() );
116 sink.bookDate_();
117 }
118
119 if ( StringUtils.isNotEmpty( book.getAuthor() ) )
120 {
121 sink.bookAuthor();
122 sink.text( book.getAuthor() );
123 sink.bookAuthor_();
124 }
125
126 sink.bookHead_();
127
128 for ( Chapter chapter : book.getChapters() )
129 {
130 sink.chapter();
131
132 if ( StringUtils.isNotEmpty( chapter.getTitle() ) )
133 {
134 sink.chapterTitle();
135 sink.text( chapter.getTitle() );
136 sink.chapterTitle_();
137 }
138
139 renderChapter( chapter, context, sink );
140
141 sink.chapter_();
142 }
143
144 sink.book_();
145 }
146 finally
147 {
148 sink.flush();
149
150 sink.close();
151
152 IOUtil.close( fileWriter );
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165 private void renderChapter( Chapter chapter, BookContext context, Sink sink )
166 throws BookDoxiaException
167 {
168 for ( Section section : chapter.getSections() )
169 {
170 renderSection( section, context, sink );
171 }
172 }
173
174
175
176
177
178
179
180
181
182
183 private void renderSection( Section section, BookContext context, Sink sink )
184 throws BookDoxiaException
185 {
186 BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() );
187
188 if ( bookFile == null )
189 {
190 throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." );
191 }
192
193 Reader reader = null;
194 try
195 {
196 reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
197 doxia.parse( reader, bookFile.getParserId(), sink );
198 }
199 catch ( ParserNotFoundException e )
200 {
201 throw new BookDoxiaException( "Parser not found: " + bookFile.getParserId() + ".", e );
202 }
203 catch ( ParseException e )
204 {
205 throw new BookDoxiaException(
206 "Error while parsing document: " + bookFile.getFile().getAbsolutePath() + ".",
207 e );
208 }
209 catch ( FileNotFoundException e )
210 {
211 throw new BookDoxiaException( "Could not find document: " + bookFile.getFile().getAbsolutePath() + ".", e );
212 }
213 catch ( IOException e )
214 {
215 throw new BookDoxiaException( "Error while rendering book: "
216 + bookFile.getFile().getAbsolutePath() + ".", e );
217 }
218 finally
219 {
220 IOUtil.close( reader );
221 }
222 }
223 }