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 | 1 | 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 | 1 | BookModel book = context.getBook(); |
68 | |
|
69 | 1 | 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 | 1 | File bookFile = new File( context.getOutputDirectory(), book.getId() + ".xhtml" ); |
79 | |
|
80 | |
Writer fileWriter; |
81 | |
|
82 | |
try |
83 | |
{ |
84 | 1 | fileWriter = new FileWriter( bookFile ); |
85 | |
} |
86 | 0 | catch ( IOException e ) |
87 | |
{ |
88 | 0 | throw new BookDoxiaException( "Error while opening file.", e ); |
89 | 1 | } |
90 | |
|
91 | 1 | XhtmlBookSink sink = new XhtmlBookSink( fileWriter, |
92 | |
new RenderingContext( context.getOutputDirectory(), bookFile.getAbsolutePath() ) ); |
93 | |
|
94 | |
try |
95 | |
{ |
96 | 1 | sink.bookHead(); |
97 | 1 | sink.bookTitle(); |
98 | 1 | sink.text( context.getBook().getTitle() ); |
99 | 1 | sink.bookTitle_(); |
100 | 1 | sink.bookAuthor(); |
101 | 1 | sink.text( context.getBook().getAuthor() ); |
102 | 1 | sink.bookAuthor_(); |
103 | 1 | sink.bookDate(); |
104 | 1 | sink.text( context.getBook().getDate() ); |
105 | 1 | sink.bookDate_(); |
106 | 1 | sink.bookHead_(); |
107 | 1 | sink.bookBody(); |
108 | |
|
109 | 1 | int chapterNumber = 1; |
110 | |
|
111 | 1 | for ( Chapter chapter : book.getChapters() ) |
112 | |
{ |
113 | 2 | sink.sectionTitle(); |
114 | 2 | sink.text( Integer.toString( chapterNumber ) + ". " + chapter.getTitle() ); |
115 | 2 | sink.sectionTitle_(); |
116 | |
|
117 | 2 | renderChapter( sink, chapter, context ); |
118 | |
|
119 | 2 | chapterNumber++; |
120 | |
} |
121 | |
|
122 | 1 | sink.bookBody_(); |
123 | |
} |
124 | |
finally |
125 | |
{ |
126 | 1 | sink.flush(); |
127 | |
|
128 | 1 | sink.close(); |
129 | |
|
130 | 1 | IOUtil.close( fileWriter ); |
131 | 1 | } |
132 | 1 | } |
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 | 2 | for ( Section section : chapter.getSections() ) |
150 | |
{ |
151 | 4 | renderSection( sink, section, context ); |
152 | |
} |
153 | 2 | } |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
private void renderSection( XhtmlBookSink sink, Section section, BookContext context ) |
164 | |
throws BookDoxiaException |
165 | |
{ |
166 | 4 | sink.section2(); |
167 | |
|
168 | 4 | BookContext.BookFile bookFile = context.getFiles().get( section.getId() ); |
169 | |
|
170 | 4 | if ( bookFile == null ) |
171 | |
{ |
172 | 0 | throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." ); |
173 | |
} |
174 | |
|
175 | 4 | Reader reader = null; |
176 | |
try |
177 | |
{ |
178 | 4 | reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() ); |
179 | 4 | 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 | 4 | IOUtil.close( reader ); |
204 | 4 | } |
205 | |
|
206 | 4 | sink.section2_(); |
207 | 4 | } |
208 | |
} |