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.text.DateFormat;
28 import java.util.Date;
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.module.itext.ITextSinkFactory;
37 import org.apache.maven.doxia.parser.ParseException;
38 import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
39 import org.apache.maven.doxia.sink.Sink;
40 import org.codehaus.plexus.logging.AbstractLogEnabled;
41 import org.codehaus.plexus.util.IOUtil;
42 import org.codehaus.plexus.util.ReaderFactory;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.codehaus.plexus.util.WriterFactory;
45 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
46
47
48
49
50
51
52
53
54 public abstract class AbstractITextBookRenderer
55 extends AbstractLogEnabled
56 implements BookRenderer
57 {
58
59
60
61 private Doxia doxia;
62
63
64
65
66
67
68 public void renderBook( BookContext context )
69 throws BookDoxiaException
70 {
71 BookModel book = context.getBook();
72
73 if ( !context.getOutputDirectory().exists() )
74 {
75 if ( !context.getOutputDirectory().mkdirs() )
76 {
77 throw new BookDoxiaException( "Could not make directory: "
78 + context.getOutputDirectory().getAbsolutePath() + "." );
79 }
80 }
81
82 File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xml" );
83
84 Writer fileWriter;
85 try
86 {
87 fileWriter = WriterFactory.newXmlWriter( bookFile );
88 }
89 catch ( IOException e )
90 {
91 throw new BookDoxiaException( "Error while opening file.", e );
92 }
93
94
95
96
97
98 PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( fileWriter, "UTF-8", null );
99 writer.startElement( "itext" );
100 writer.addAttribute( "creationdate", DateFormat.getDateTimeInstance().format( new Date() ) );
101 writer.addAttribute( "producer", "Doxia iText" );
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 System.setProperty( "itext.basedir", bookFile.getParentFile().getAbsolutePath() );
137 Sink sink = new ITextSinkFactory().createSink( writer );
138
139 try
140 {
141 for ( Chapter chapter : book.getChapters() )
142 {
143 renderChapter( sink, writer, chapter, context );
144 }
145
146 writer.endElement();
147 }
148 finally
149 {
150 sink.flush();
151 sink.close();
152
153 IOUtil.close( fileWriter );
154 System.getProperties().remove( "itext.basedir" );
155 }
156
157
158
159
160 File outputFile = new File( context.getOutputDirectory(), book.getId() + "." + getOutputExtension() );
161 try
162 {
163 renderXML( bookFile, outputFile );
164 }
165 catch ( IOException e )
166 {
167 throw new BookDoxiaException( "Error while rendering file", e );
168 }
169 }
170
171
172
173
174
175
176 public abstract String getOutputExtension();
177
178
179
180
181
182
183
184
185 public abstract void renderXML( File iTextFile, File iTextOutput )
186 throws IOException;
187
188
189
190
191
192
193
194
195
196
197
198
199
200 private void renderChapter( Sink sink, PrettyPrintXMLWriter writer, Chapter chapter, BookContext context )
201 throws BookDoxiaException
202 {
203 writer.startElement( "chapter" );
204 writer.addAttribute( "numberdepth", "1" );
205 writer.addAttribute( "depth", "1" );
206 writer.addAttribute( "indent", "1" );
207
208 startTitle( writer, "36.0", "Helvetica", "24.0", "normal", "255", "0", "0" );
209 chunk( writer, chapter.getTitle(), "Helvetica", "24.0", "normal", "255", "0", "0" );
210 writer.endElement();
211
212
213 for ( Section section : chapter.getSections() )
214 {
215 renderSection( sink, writer, section, context );
216 }
217
218
219 writer.endElement();
220 }
221
222
223
224
225
226
227
228
229
230 private void renderSection( Sink sink, PrettyPrintXMLWriter writer, Section section, BookContext context )
231 throws BookDoxiaException
232 {
233
234
235
236
237
238
239 BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() );
240
241 if ( bookFile == null )
242 {
243 throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." );
244 }
245
246
247
248
249
250 Reader reader = null;
251 try
252 {
253 reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
254 doxia.parse( reader, bookFile.getParserId(), sink );
255 }
256 catch ( ParserNotFoundException e )
257 {
258 throw new BookDoxiaException( "Parser not found: " + bookFile.getParserId() + ".", e );
259 }
260 catch ( ParseException e )
261 {
262 throw new BookDoxiaException(
263 "Error while parsing document: " + bookFile.getFile().getAbsolutePath() + ".",
264 e );
265 }
266 catch ( FileNotFoundException e )
267 {
268 throw new BookDoxiaException( "Could not find document: " + bookFile.getFile().getAbsolutePath() + ".", e );
269 }
270 catch ( IOException e )
271 {
272 throw new BookDoxiaException( "Error while rendering book: "
273 + bookFile.getFile().getAbsolutePath() + ".", e );
274 }
275 finally
276 {
277 IOUtil.close( reader );
278 }
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293 private void startTitle( PrettyPrintXMLWriter writer, String leading, String font, String size, String fontstyle,
294 String red, String green, String blue )
295 {
296 writer.startElement( "title" );
297 writer.addAttribute( "leading", leading );
298 writer.addAttribute( "font", font );
299 writer.addAttribute( "size", size );
300 writer.addAttribute( "fontstyle", fontstyle );
301 writer.addAttribute( "red", red );
302 writer.addAttribute( "green", green );
303 writer.addAttribute( "blue", blue );
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317
318 private void chunk( PrettyPrintXMLWriter writer, String title, String font, String size, String fontstyle,
319 String red, String green, String blue )
320 {
321 writer.startElement( "chunk" );
322 writer.addAttribute( "font", font );
323 writer.addAttribute( "size", size );
324 writer.addAttribute( "fontstyle", fontstyle );
325 writer.addAttribute( "red", red );
326 writer.addAttribute( "green", green );
327 writer.addAttribute( "blue", blue );
328 if ( StringUtils.isNotEmpty( title ) )
329 {
330 writer.writeText( title );
331 }
332 else
333 {
334 writer.writeText( "<Missing title>" );
335 }
336 writer.endElement();
337 }
338 }