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 | 2 | 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 | 2 | BookModel book = context.getBook(); |
69 | |
|
70 | 2 | if ( !context.getOutputDirectory().exists() ) |
71 | |
{ |
72 | 0 | if ( !context.getOutputDirectory().mkdirs() ) |
73 | |
{ |
74 | 0 | throw new BookDoxiaException( "Could not make directory: " |
75 | |
+ context.getOutputDirectory().getAbsolutePath() + "." ); |
76 | |
} |
77 | |
} |
78 | |
|
79 | 2 | File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xml" ); |
80 | |
|
81 | |
Writer fileWriter; |
82 | |
try |
83 | |
{ |
84 | 2 | fileWriter = WriterFactory.newXmlWriter( bookFile ); |
85 | |
} |
86 | 0 | catch ( IOException e ) |
87 | |
{ |
88 | 0 | throw new BookDoxiaException( "Error while opening file.", e ); |
89 | 2 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 2 | DocBookBookSink sink = new DocBookBookSink( fileWriter ); |
98 | |
|
99 | |
try |
100 | |
{ |
101 | 2 | sink.book(); |
102 | |
|
103 | |
|
104 | |
|
105 | 2 | if ( StringUtils.isNotEmpty( book.getTitle() ) ) |
106 | |
{ |
107 | 2 | sink.bookTitle(); |
108 | 2 | sink.text( book.getTitle() ); |
109 | 2 | sink.bookTitle_(); |
110 | |
} |
111 | |
|
112 | 2 | if ( StringUtils.isNotEmpty( book.getDate() ) ) |
113 | |
{ |
114 | 0 | sink.bookDate(); |
115 | 0 | sink.text( book.getDate() ); |
116 | 0 | sink.bookDate_(); |
117 | |
} |
118 | |
|
119 | 2 | if ( StringUtils.isNotEmpty( book.getAuthor() ) ) |
120 | |
{ |
121 | 0 | sink.bookAuthor(); |
122 | 0 | sink.text( book.getAuthor() ); |
123 | 0 | sink.bookAuthor_(); |
124 | |
} |
125 | |
|
126 | 2 | sink.bookHead_(); |
127 | |
|
128 | 2 | for ( Chapter chapter : book.getChapters() ) |
129 | |
{ |
130 | 4 | sink.chapter(); |
131 | |
|
132 | 4 | if ( StringUtils.isNotEmpty( chapter.getTitle() ) ) |
133 | |
{ |
134 | 4 | sink.chapterTitle(); |
135 | 4 | sink.text( chapter.getTitle() ); |
136 | 4 | sink.chapterTitle_(); |
137 | |
} |
138 | |
|
139 | 4 | renderChapter( chapter, context, sink ); |
140 | |
|
141 | 4 | sink.chapter_(); |
142 | |
} |
143 | |
|
144 | 2 | sink.book_(); |
145 | |
} |
146 | |
finally |
147 | |
{ |
148 | 2 | sink.flush(); |
149 | |
|
150 | 2 | sink.close(); |
151 | |
|
152 | 2 | IOUtil.close( fileWriter ); |
153 | 2 | } |
154 | 2 | } |
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 | 4 | for ( Section section : chapter.getSections() ) |
169 | |
{ |
170 | 8 | renderSection( section, context, sink ); |
171 | |
} |
172 | 4 | } |
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 | 8 | BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() ); |
187 | |
|
188 | 8 | if ( bookFile == null ) |
189 | |
{ |
190 | 0 | throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." ); |
191 | |
} |
192 | |
|
193 | 8 | Reader reader = null; |
194 | |
try |
195 | |
{ |
196 | 8 | reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() ); |
197 | 8 | doxia.parse( reader, bookFile.getParserId(), sink ); |
198 | |
} |
199 | 0 | catch ( ParserNotFoundException e ) |
200 | |
{ |
201 | 0 | throw new BookDoxiaException( "Parser not found: " + bookFile.getParserId() + ".", e ); |
202 | |
} |
203 | 0 | catch ( ParseException e ) |
204 | |
{ |
205 | 0 | throw new BookDoxiaException( |
206 | |
"Error while parsing document: " + bookFile.getFile().getAbsolutePath() + ".", |
207 | |
e ); |
208 | |
} |
209 | 0 | catch ( FileNotFoundException e ) |
210 | |
{ |
211 | 0 | throw new BookDoxiaException( "Could not find document: " + bookFile.getFile().getAbsolutePath() + ".", e ); |
212 | |
} |
213 | 0 | catch ( IOException e ) |
214 | |
{ |
215 | 0 | throw new BookDoxiaException( "Error while rendering book: " |
216 | |
+ bookFile.getFile().getAbsolutePath() + ".", e ); |
217 | |
} |
218 | |
finally |
219 | |
{ |
220 | 8 | IOUtil.close( reader ); |
221 | 8 | } |
222 | 8 | } |
223 | |
} |