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 | 2 | 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 | 2 | BookModel book = context.getBook(); |
72 | |
|
73 | 2 | if ( !context.getOutputDirectory().exists() ) |
74 | |
{ |
75 | 0 | if ( !context.getOutputDirectory().mkdirs() ) |
76 | |
{ |
77 | 0 | throw new BookDoxiaException( "Could not make directory: " |
78 | |
+ context.getOutputDirectory().getAbsolutePath() + "." ); |
79 | |
} |
80 | |
} |
81 | |
|
82 | 2 | File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xml" ); |
83 | |
|
84 | |
Writer fileWriter; |
85 | |
try |
86 | |
{ |
87 | 2 | fileWriter = WriterFactory.newXmlWriter( bookFile ); |
88 | |
} |
89 | 0 | catch ( IOException e ) |
90 | |
{ |
91 | 0 | throw new BookDoxiaException( "Error while opening file.", e ); |
92 | 2 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 2 | PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( fileWriter, "UTF-8", null ); |
99 | 2 | writer.startElement( "itext" ); |
100 | 2 | writer.addAttribute( "creationdate", DateFormat.getDateTimeInstance().format( new Date() ) ); |
101 | 2 | 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 | 2 | System.setProperty( "itext.basedir", bookFile.getParentFile().getAbsolutePath() ); |
137 | 2 | Sink sink = new ITextSinkFactory().createSink( writer ); |
138 | |
|
139 | |
try |
140 | |
{ |
141 | 2 | for ( Chapter chapter : book.getChapters() ) |
142 | |
{ |
143 | 4 | renderChapter( sink, writer, chapter, context ); |
144 | |
} |
145 | |
|
146 | 2 | writer.endElement(); |
147 | |
} |
148 | |
finally |
149 | |
{ |
150 | 2 | sink.flush(); |
151 | 2 | sink.close(); |
152 | |
|
153 | 2 | IOUtil.close( fileWriter ); |
154 | 2 | System.getProperties().remove( "itext.basedir" ); |
155 | 2 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | 2 | File outputFile = new File( context.getOutputDirectory(), book.getId() + "." + getOutputExtension() ); |
161 | |
try |
162 | |
{ |
163 | 2 | renderXML( bookFile, outputFile ); |
164 | |
} |
165 | 0 | catch ( IOException e ) |
166 | |
{ |
167 | 0 | throw new BookDoxiaException( "Error while rendering file", e ); |
168 | 2 | } |
169 | 2 | } |
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 | 4 | writer.startElement( "chapter" ); |
204 | 4 | writer.addAttribute( "numberdepth", "1" ); |
205 | 4 | writer.addAttribute( "depth", "1" ); |
206 | 4 | writer.addAttribute( "indent", "1" ); |
207 | |
|
208 | 4 | startTitle( writer, "36.0", "Helvetica", "24.0", "normal", "255", "0", "0" ); |
209 | 4 | chunk( writer, chapter.getTitle(), "Helvetica", "24.0", "normal", "255", "0", "0" ); |
210 | 4 | writer.endElement(); |
211 | |
|
212 | |
|
213 | 4 | for ( Section section : chapter.getSections() ) |
214 | |
{ |
215 | 8 | renderSection( sink, writer, section, context ); |
216 | |
} |
217 | |
|
218 | |
|
219 | 4 | writer.endElement(); |
220 | 4 | } |
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 | 8 | BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() ); |
240 | |
|
241 | 8 | if ( bookFile == null ) |
242 | |
{ |
243 | 0 | throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." ); |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | 8 | Reader reader = null; |
251 | |
try |
252 | |
{ |
253 | 8 | reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() ); |
254 | 8 | doxia.parse( reader, bookFile.getParserId(), sink ); |
255 | |
} |
256 | 0 | catch ( ParserNotFoundException e ) |
257 | |
{ |
258 | 0 | throw new BookDoxiaException( "Parser not found: " + bookFile.getParserId() + ".", e ); |
259 | |
} |
260 | 0 | catch ( ParseException e ) |
261 | |
{ |
262 | 0 | throw new BookDoxiaException( |
263 | |
"Error while parsing document: " + bookFile.getFile().getAbsolutePath() + ".", |
264 | |
e ); |
265 | |
} |
266 | 0 | catch ( FileNotFoundException e ) |
267 | |
{ |
268 | 0 | throw new BookDoxiaException( "Could not find document: " + bookFile.getFile().getAbsolutePath() + ".", e ); |
269 | |
} |
270 | 0 | catch ( IOException e ) |
271 | |
{ |
272 | 0 | throw new BookDoxiaException( "Error while rendering book: " |
273 | |
+ bookFile.getFile().getAbsolutePath() + ".", e ); |
274 | |
} |
275 | |
finally |
276 | |
{ |
277 | 8 | IOUtil.close( reader ); |
278 | 8 | } |
279 | 8 | } |
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 | 4 | writer.startElement( "title" ); |
297 | 4 | writer.addAttribute( "leading", leading ); |
298 | 4 | writer.addAttribute( "font", font ); |
299 | 4 | writer.addAttribute( "size", size ); |
300 | 4 | writer.addAttribute( "fontstyle", fontstyle ); |
301 | 4 | writer.addAttribute( "red", red ); |
302 | 4 | writer.addAttribute( "green", green ); |
303 | 4 | writer.addAttribute( "blue", blue ); |
304 | 4 | } |
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 | 4 | writer.startElement( "chunk" ); |
322 | 4 | writer.addAttribute( "font", font ); |
323 | 4 | writer.addAttribute( "size", size ); |
324 | 4 | writer.addAttribute( "fontstyle", fontstyle ); |
325 | 4 | writer.addAttribute( "red", red ); |
326 | 4 | writer.addAttribute( "green", green ); |
327 | 4 | writer.addAttribute( "blue", blue ); |
328 | 4 | if ( StringUtils.isNotEmpty( title ) ) |
329 | |
{ |
330 | 4 | writer.writeText( title ); |
331 | |
} |
332 | |
else |
333 | |
{ |
334 | 0 | writer.writeText( "<Missing title>" ); |
335 | |
} |
336 | 4 | writer.endElement(); |
337 | 4 | } |
338 | |
} |