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 | 1 | 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 | 1 | BookModel book = context.getBook(); |
69 | |
|
70 | 1 | 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 | 1 | File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xml" ); |
80 | |
|
81 | |
Writer fileWriter; |
82 | |
try |
83 | |
{ |
84 | 1 | fileWriter = WriterFactory.newXmlWriter( bookFile ); |
85 | |
} |
86 | 0 | catch ( IOException e ) |
87 | |
{ |
88 | 0 | throw new BookDoxiaException( "Error while opening file.", e ); |
89 | 1 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 1 | DocBookBookSink sink = new DocBookBookSink( fileWriter ); |
98 | |
|
99 | |
try |
100 | |
{ |
101 | 1 | sink.book(); |
102 | |
|
103 | |
|
104 | |
|
105 | 1 | if ( StringUtils.isNotEmpty( book.getTitle() ) ) |
106 | |
{ |
107 | 1 | sink.bookTitle(); |
108 | 1 | sink.text( book.getTitle() ); |
109 | 1 | sink.bookTitle_(); |
110 | |
} |
111 | |
|
112 | 1 | if ( StringUtils.isNotEmpty( book.getDate() ) ) |
113 | |
{ |
114 | 0 | sink.bookDate(); |
115 | 0 | sink.text( book.getDate() ); |
116 | 0 | sink.bookDate_(); |
117 | |
} |
118 | |
|
119 | 1 | if ( StringUtils.isNotEmpty( book.getAuthor() ) ) |
120 | |
{ |
121 | 0 | sink.bookAuthor(); |
122 | 0 | sink.text( book.getAuthor() ); |
123 | 0 | sink.bookAuthor_(); |
124 | |
} |
125 | |
|
126 | 1 | sink.bookHead_(); |
127 | |
|
128 | 1 | for ( Chapter chapter : book.getChapters() ) |
129 | |
{ |
130 | 2 | sink.chapter(); |
131 | |
|
132 | 2 | if ( StringUtils.isNotEmpty( chapter.getTitle() ) ) |
133 | |
{ |
134 | 2 | sink.chapterTitle(); |
135 | 2 | sink.text( chapter.getTitle() ); |
136 | 2 | sink.chapterTitle_(); |
137 | |
} |
138 | |
|
139 | 2 | renderChapter( chapter, context, sink ); |
140 | |
|
141 | 2 | sink.chapter_(); |
142 | |
} |
143 | |
|
144 | 1 | sink.book_(); |
145 | |
} |
146 | |
finally |
147 | |
{ |
148 | 1 | sink.flush(); |
149 | |
|
150 | 1 | sink.close(); |
151 | |
|
152 | 1 | IOUtil.close( fileWriter ); |
153 | 1 | } |
154 | 1 | } |
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 | 2 | for ( Section section : chapter.getSections() ) |
169 | |
{ |
170 | 4 | renderSection( section, context, sink ); |
171 | |
} |
172 | 2 | } |
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 | 4 | BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() ); |
187 | |
|
188 | 4 | if ( bookFile == null ) |
189 | |
{ |
190 | 0 | throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." ); |
191 | |
} |
192 | |
|
193 | 4 | Reader reader = null; |
194 | |
try |
195 | |
{ |
196 | 4 | reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() ); |
197 | 4 | 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 | 4 | IOUtil.close( reader ); |
221 | 4 | } |
222 | 4 | } |
223 | |
} |