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