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 | |
import java.util.Iterator; |
28 | |
import java.util.Locale; |
29 | |
|
30 | |
import org.apache.maven.doxia.Doxia; |
31 | |
import org.apache.maven.doxia.book.BookDoxiaException; |
32 | |
import org.apache.maven.doxia.book.context.BookContext; |
33 | |
import org.apache.maven.doxia.book.model.BookModel; |
34 | |
import org.apache.maven.doxia.book.model.Chapter; |
35 | |
import org.apache.maven.doxia.book.model.Section; |
36 | |
import org.apache.maven.doxia.book.services.renderer.xdoc.ChapterXdocBookSink; |
37 | |
import org.apache.maven.doxia.book.services.renderer.xdoc.IndexXdocBookSink; |
38 | |
import org.apache.maven.doxia.book.services.renderer.xdoc.SectionXdocBookSink; |
39 | |
import org.apache.maven.doxia.index.IndexEntry; |
40 | |
import org.apache.maven.doxia.module.xdoc.XdocSink; |
41 | |
import org.apache.maven.doxia.parser.ParseException; |
42 | |
import org.apache.maven.doxia.parser.manager.ParserNotFoundException; |
43 | |
import org.apache.maven.doxia.util.HtmlTools; |
44 | |
import org.codehaus.plexus.i18n.I18N; |
45 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
46 | |
import org.codehaus.plexus.util.IOUtil; |
47 | |
import org.codehaus.plexus.util.ReaderFactory; |
48 | |
import org.codehaus.plexus.util.StringUtils; |
49 | |
import org.codehaus.plexus.util.WriterFactory; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 2 | public class XdocBookRenderer |
60 | |
extends AbstractLogEnabled |
61 | |
implements BookRenderer |
62 | |
{ |
63 | |
|
64 | |
|
65 | |
|
66 | |
private Doxia doxia; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private I18N i18n; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void renderBook( BookContext context ) |
79 | |
throws BookDoxiaException |
80 | |
{ |
81 | 2 | BookModel book = context.getBook(); |
82 | |
|
83 | 2 | if ( !context.getOutputDirectory().exists() ) |
84 | |
{ |
85 | 0 | if ( !context.getOutputDirectory().mkdirs() ) |
86 | |
{ |
87 | 0 | throw new BookDoxiaException( "Could not make directory: " |
88 | |
+ context.getOutputDirectory().getAbsolutePath() + "." ); |
89 | |
} |
90 | |
} |
91 | |
|
92 | 2 | renderBook( book, context ); |
93 | 2 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
protected String getString( Locale locale, String key ) |
107 | |
{ |
108 | 4 | if ( StringUtils.isEmpty( key ) ) |
109 | |
{ |
110 | 0 | throw new IllegalArgumentException( "The key cannot be empty" ); |
111 | |
} |
112 | |
|
113 | 4 | return i18n.getString( "book-renderer", locale, key ).trim(); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
private void renderBook( BookModel book, BookContext context ) |
128 | |
throws BookDoxiaException |
129 | |
{ |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | 2 | File index = new File( context.getOutputDirectory(), "index.xml" ); |
135 | |
|
136 | |
try |
137 | |
{ |
138 | 2 | writeBookIndex( index, book, context ); |
139 | |
} |
140 | 0 | catch ( IOException e ) |
141 | |
{ |
142 | 0 | throw new BookDoxiaException( "Error while rendering index page to: '" |
143 | |
+ index.getAbsolutePath() + "'.", e ); |
144 | 2 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 2 | Iterator<IndexEntry> ii = context.getIndex().getChildEntries().iterator(); |
151 | |
|
152 | 2 | for ( Chapter chapter : book.getChapters() ) |
153 | |
{ |
154 | 4 | renderChapter( chapter, context, ii.next() ); |
155 | |
} |
156 | 2 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private void writeBookIndex( File index, BookModel book, BookContext context ) |
167 | |
throws IOException |
168 | |
{ |
169 | 2 | Writer writer = WriterFactory.newXmlWriter( index ); |
170 | |
|
171 | 2 | XdocSink sink = new IndexXdocBookSink( writer, context.getIndex().getFirstEntry(), i18n, context.getLocale() ); |
172 | |
|
173 | |
try |
174 | |
{ |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | 2 | sink.head(); |
180 | |
|
181 | 2 | sink.title(); |
182 | 2 | sink.text( book.getTitle() + " - " + getString( context.getLocale(), "toc" ) ); |
183 | 2 | sink.title_(); |
184 | |
|
185 | 2 | sink.head_(); |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | 2 | sink.body(); |
192 | |
|
193 | 2 | sink.section1(); |
194 | 2 | sink.sectionTitle1(); |
195 | 2 | sink.text( book.getTitle() + " - " + getString( context.getLocale(), "toc" ) ); |
196 | 2 | sink.sectionTitle1_(); |
197 | |
|
198 | 2 | sink.list(); |
199 | 2 | for ( IndexEntry entry : context.getIndex().getChildEntries() ) |
200 | |
{ |
201 | 4 | writeChapterIndexForBookIndex( sink, entry ); |
202 | |
} |
203 | 2 | sink.list_(); |
204 | |
|
205 | 2 | sink.section1_(); |
206 | |
|
207 | 2 | sink.body_(); |
208 | |
} |
209 | |
finally |
210 | |
{ |
211 | 2 | sink.flush(); |
212 | |
|
213 | 2 | sink.close(); |
214 | |
|
215 | 2 | IOUtil.close( writer ); |
216 | 2 | } |
217 | 2 | } |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
private void writeChapterIndexForBookIndex( XdocSink sink, IndexEntry chapterEntry ) |
226 | |
{ |
227 | 4 | sink.listItem(); |
228 | 4 | sink.link( chapterEntry.getId() + ".html" ); |
229 | 4 | sink.text( chapterEntry.getTitle() ); |
230 | 4 | sink.link_(); |
231 | |
|
232 | 4 | sink.list(); |
233 | 4 | for ( IndexEntry sectionIndex : chapterEntry.getChildEntries() ) |
234 | |
{ |
235 | 8 | writeSectionIndexForBookIndex( sink, sectionIndex ); |
236 | |
} |
237 | 4 | sink.list_(); |
238 | |
|
239 | 4 | sink.listItem_(); |
240 | 4 | } |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
private void writeSectionIndexForBookIndex( XdocSink sink, IndexEntry sectionIndex ) |
249 | |
{ |
250 | 16 | sink.listItem(); |
251 | 16 | sink.link( sectionIndex.getId() + ".html" ); |
252 | 16 | sink.text( sectionIndex.getTitle() ); |
253 | 16 | sink.link_(); |
254 | |
|
255 | 16 | sink.list(); |
256 | 16 | for ( IndexEntry subsectionIndex : sectionIndex.getChildEntries() ) |
257 | |
{ |
258 | 32 | writeSubsectionIndexForBookIndex( sink, sectionIndex, subsectionIndex ); |
259 | |
} |
260 | 16 | sink.list_(); |
261 | |
|
262 | 16 | sink.listItem_(); |
263 | 16 | } |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
private void writeSubsectionIndexForBookIndex( XdocSink sink, IndexEntry sectionIndex, IndexEntry subsectionIndex ) |
273 | |
{ |
274 | 32 | sink.listItem(); |
275 | 32 | sink.link( sectionIndex.getId() + ".html#" + HtmlTools.encodeId( subsectionIndex.getId() ) ); |
276 | 32 | sink.text( subsectionIndex.getTitle() ); |
277 | 32 | sink.link_(); |
278 | 32 | sink.listItem_(); |
279 | 32 | } |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
private void renderChapter( Chapter chapter, BookContext context, IndexEntry chapterIndex ) |
294 | |
throws BookDoxiaException |
295 | |
{ |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | 4 | File index = new File( context.getOutputDirectory(), chapter.getId() + ".xml" ); |
301 | |
|
302 | |
try |
303 | |
{ |
304 | 4 | writeChapterIndex( index, chapter, chapterIndex, context ); |
305 | |
} |
306 | 0 | catch ( IOException e ) |
307 | |
{ |
308 | 0 | throw new BookDoxiaException( "Error while rendering index page to: '" |
309 | |
+ index.getAbsolutePath() + "'.", e ); |
310 | 4 | } |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | 4 | Iterator<IndexEntry> ii = chapterIndex.getChildEntries().iterator(); |
317 | |
|
318 | 4 | for ( Section section : chapter.getSections() ) |
319 | |
{ |
320 | 8 | renderSection( context, section, ii.next() ); |
321 | |
} |
322 | 4 | } |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
private void writeChapterIndex( File index, Chapter chapter, IndexEntry chapterIndex, BookContext context ) |
334 | |
throws IOException |
335 | |
{ |
336 | 4 | Writer writer = WriterFactory.newXmlWriter( index ); |
337 | |
|
338 | 4 | ChapterXdocBookSink sink = new ChapterXdocBookSink( writer, chapterIndex, i18n, context.getLocale() ); |
339 | |
|
340 | |
try |
341 | |
{ |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | 4 | sink.head(); |
347 | |
|
348 | 4 | sink.title(); |
349 | 4 | sink.text( chapter.getTitle() ); |
350 | 4 | sink.title_(); |
351 | |
|
352 | 4 | sink.head_(); |
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | 4 | sink.body(); |
359 | |
|
360 | 4 | sink.section1(); |
361 | 4 | sink.sectionTitle1(); |
362 | 4 | sink.text( chapter.getTitle() ); |
363 | 4 | sink.sectionTitle1_(); |
364 | |
|
365 | 4 | sink.list(); |
366 | 4 | for ( IndexEntry sectionIndex : chapterIndex.getChildEntries() ) |
367 | |
{ |
368 | 8 | writeSectionIndexForBookIndex( sink, sectionIndex ); |
369 | |
} |
370 | 4 | sink.list_(); |
371 | |
|
372 | 4 | sink.section1_(); |
373 | |
|
374 | 4 | sink.body_(); |
375 | |
} |
376 | |
finally |
377 | |
{ |
378 | 4 | sink.flush(); |
379 | |
|
380 | 4 | sink.close(); |
381 | |
|
382 | 4 | IOUtil.close( writer ); |
383 | 4 | } |
384 | 4 | } |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
private void renderSection( BookContext context, Section section, IndexEntry sectionIndex ) |
395 | |
throws BookDoxiaException |
396 | |
{ |
397 | |
try |
398 | |
{ |
399 | 8 | Writer writer = WriterFactory.newXmlWriter( new File( context.getOutputDirectory() |
400 | |
+ "/" + section.getId() + ".xml" ) ); |
401 | |
|
402 | 8 | SectionXdocBookSink sink = new SectionXdocBookSink( writer, sectionIndex, i18n, context.getLocale() ); |
403 | |
|
404 | 8 | BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() ); |
405 | |
|
406 | 8 | if ( bookFile == null ) |
407 | |
{ |
408 | 0 | throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." ); |
409 | |
} |
410 | |
|
411 | 8 | Reader reader = null; |
412 | |
try |
413 | |
{ |
414 | 8 | reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() ); |
415 | 8 | doxia.parse( reader, bookFile.getParserId(), sink ); |
416 | |
} |
417 | 0 | catch ( ParserNotFoundException e ) |
418 | |
{ |
419 | 0 | throw new BookDoxiaException( "Parser not found: " + bookFile.getParserId() + ".", e ); |
420 | |
} |
421 | 0 | catch ( ParseException e ) |
422 | |
{ |
423 | 0 | throw new BookDoxiaException( "Error while parsing document: " + bookFile.getFile().getAbsolutePath() |
424 | |
+ ".", e ); |
425 | |
} |
426 | 0 | catch ( FileNotFoundException e ) |
427 | |
{ |
428 | 0 | throw new BookDoxiaException( "Could not find document: " + bookFile.getFile().getAbsolutePath() + ".", |
429 | |
e ); |
430 | |
} |
431 | |
finally |
432 | |
{ |
433 | 8 | sink.flush(); |
434 | 8 | sink.close(); |
435 | |
|
436 | 8 | IOUtil.close( reader ); |
437 | 8 | IOUtil.close( writer ); |
438 | 8 | } |
439 | |
} |
440 | 0 | catch ( IOException e ) |
441 | |
{ |
442 | 0 | throw new BookDoxiaException( "Error while rendering book.", e ); |
443 | 8 | } |
444 | 8 | } |
445 | |
} |