1 | |
package org.apache.maven.plugins.pdf; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Date; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
|
29 | |
import org.apache.maven.doxia.document.DocumentAuthor; |
30 | |
import org.apache.maven.doxia.document.DocumentCover; |
31 | |
import org.apache.maven.doxia.document.DocumentMeta; |
32 | |
import org.apache.maven.doxia.document.DocumentModel; |
33 | |
import org.apache.maven.doxia.document.DocumentTOC; |
34 | |
import org.apache.maven.doxia.document.DocumentTOCItem; |
35 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
36 | |
import org.apache.maven.doxia.site.decoration.Menu; |
37 | |
import org.apache.maven.doxia.site.decoration.MenuItem; |
38 | |
import org.apache.maven.model.Developer; |
39 | |
import org.apache.maven.project.MavenProject; |
40 | |
import org.codehaus.plexus.util.IOUtil; |
41 | |
import org.codehaus.plexus.util.ReaderFactory; |
42 | |
import org.codehaus.plexus.util.StringUtils; |
43 | |
import org.apache.commons.io.input.XmlStreamReader; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class DocumentModelBuilder |
52 | |
{ |
53 | |
|
54 | |
private final MavenProject project; |
55 | |
|
56 | |
|
57 | |
private final DecorationModel decorationModel; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public DocumentModelBuilder( MavenProject project ) |
65 | |
{ |
66 | 2 | this( project, null ); |
67 | 2 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public DocumentModelBuilder( MavenProject project, DecorationModel decorationModel ) |
76 | 4 | { |
77 | 4 | this.project = project; |
78 | 4 | this.decorationModel = decorationModel; |
79 | 4 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public DocumentModel getDocumentModel() |
87 | |
{ |
88 | 4 | return getDocumentModel( project, decorationModel, null ); |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public DocumentModel getDocumentModel( Date date ) |
98 | |
{ |
99 | 0 | return getDocumentModel( project, decorationModel, date ); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private static DocumentModel getDocumentModel( MavenProject project, |
116 | |
DecorationModel decorationModel, Date date ) |
117 | |
{ |
118 | 4 | final Date now = ( date == null ? new Date() : date ); |
119 | |
|
120 | 4 | final DocumentModel docModel = new DocumentModel(); |
121 | |
|
122 | 4 | docModel.setModelEncoding( getProjectModelEncoding( project ) ); |
123 | 4 | docModel.setOutputName( project == null || project.getArtifactId() == null |
124 | |
? "unnamed" : project.getArtifactId() ); |
125 | 4 | docModel.setMeta( getDocumentMeta( project, now ) ); |
126 | 4 | docModel.setCover( getDocumentCover( project, now ) ); |
127 | 4 | docModel.setToc( getDocumentTOC( decorationModel ) ); |
128 | |
|
129 | 4 | return docModel; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
private static DocumentTOC getDocumentTOC( DecorationModel decorationModel ) |
139 | |
{ |
140 | 4 | final DocumentTOC toc = new DocumentTOC(); |
141 | |
|
142 | 4 | if ( decorationModel != null && decorationModel.getMenus() != null ) |
143 | |
{ |
144 | 2 | for ( final Iterator it = decorationModel.getMenus().iterator(); it.hasNext(); ) |
145 | |
{ |
146 | 2 | final Menu menu = (Menu) it.next(); |
147 | |
|
148 | 2 | for ( final Iterator it2 = menu.getItems().iterator(); it2.hasNext(); ) |
149 | |
{ |
150 | 5 | final MenuItem item = (MenuItem) it2.next(); |
151 | |
|
152 | 5 | final DocumentTOCItem documentTOCItem = new DocumentTOCItem(); |
153 | 5 | documentTOCItem.setName( item.getName() ); |
154 | 5 | documentTOCItem.setRef( item.getHref() ); |
155 | 5 | toc.addItem( documentTOCItem ); |
156 | 5 | } |
157 | 2 | } |
158 | |
} |
159 | |
|
160 | 4 | return toc; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
private static DocumentMeta getDocumentMeta( MavenProject project, Date date ) |
172 | |
{ |
173 | 4 | final DocumentMeta meta = new DocumentMeta(); |
174 | |
|
175 | 4 | meta.setAuthors( getAuthors( project ) ); |
176 | 4 | meta.setCreationDate( date ); |
177 | 4 | meta.setCreator( System.getProperty( "user.name" ) ); |
178 | 4 | meta.setDate( date ); |
179 | 4 | meta.setDescription( project == null ? null : project.getDescription() ); |
180 | |
|
181 | 4 | meta.setInitialCreator( System.getProperty( "user.name" ) ); |
182 | |
|
183 | |
|
184 | 4 | meta.setSubject( getProjectName( project ) ); |
185 | 4 | meta.setTitle( getProjectName( project ) ); |
186 | |
|
187 | 4 | return meta; |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
private static DocumentCover getDocumentCover( MavenProject project, Date date ) |
199 | |
{ |
200 | 4 | final DocumentCover cover = new DocumentCover(); |
201 | |
|
202 | 4 | cover.setAuthors( getAuthors( project ) ); |
203 | |
|
204 | 4 | cover.setCompanyName( getProjectOrganizationName( project ) ); |
205 | 4 | cover.setCoverDate( date ); |
206 | 4 | cover.setCoverSubTitle( project == null ? null : "v. " + project.getVersion() ); |
207 | 4 | cover.setCoverTitle( getProjectName( project ) ); |
208 | |
|
209 | 4 | cover.setCoverVersion( project == null ? null : project.getVersion() ); |
210 | |
|
211 | 4 | cover.setProjectName( getProjectName( project ) ); |
212 | |
|
213 | 4 | return cover; |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
private static List getAuthors( MavenProject project ) |
224 | |
{ |
225 | 8 | if ( project == null || project.getDevelopers() == null ) |
226 | |
{ |
227 | 2 | return null; |
228 | |
} |
229 | |
|
230 | 6 | final List ret = new ArrayList( 4 ); |
231 | |
|
232 | 6 | for ( final Iterator it = project.getDevelopers().iterator(); it.hasNext(); ) |
233 | |
{ |
234 | 8 | final Developer developer = (Developer) it.next(); |
235 | |
|
236 | 8 | final DocumentAuthor author = new DocumentAuthor(); |
237 | 8 | author.setName( developer.getName() ); |
238 | 8 | author.setEmail( developer.getEmail() ); |
239 | 8 | author.setCompanyName( developer.getOrganization() ); |
240 | 8 | StringBuilder roles = null; |
241 | |
|
242 | 8 | for ( final Iterator it2 = developer.getRoles().iterator(); it2.hasNext(); ) |
243 | |
{ |
244 | 8 | final String role = (String) it2.next(); |
245 | |
|
246 | 8 | if ( roles == null ) |
247 | |
{ |
248 | 4 | roles = new StringBuilder( 32 ); |
249 | |
} |
250 | |
|
251 | 8 | roles.append( role ); |
252 | |
|
253 | 8 | if ( it2.hasNext() ) |
254 | |
{ |
255 | 4 | roles.append( ',' ).append( ' ' ); |
256 | |
} |
257 | 8 | } |
258 | 8 | if ( roles != null ) |
259 | |
{ |
260 | 4 | author.setPosition( roles.toString() ); |
261 | |
} |
262 | |
|
263 | 8 | ret.add( author ); |
264 | 8 | } |
265 | |
|
266 | 6 | return ret; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
private static String getProjectOrganizationName( MavenProject project ) |
274 | |
{ |
275 | 4 | if ( project != null && project.getOrganization() != null |
276 | |
&& StringUtils.isNotEmpty( project.getOrganization().getName() ) ) |
277 | |
{ |
278 | 2 | return project.getOrganization().getName(); |
279 | |
} |
280 | |
|
281 | 2 | return System.getProperty( "user.name" ); |
282 | |
} |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
private static String getProjectName( MavenProject project ) |
292 | |
{ |
293 | 16 | if ( project == null ) |
294 | |
{ |
295 | 4 | return null; |
296 | |
} |
297 | |
|
298 | 12 | if ( StringUtils.isEmpty( project.getName() ) ) |
299 | |
{ |
300 | 4 | return project.getGroupId() + ":" + project.getArtifactId(); |
301 | |
} |
302 | |
|
303 | 8 | return project.getName(); |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
private static String getProjectModelEncoding( MavenProject project ) |
313 | |
{ |
314 | 4 | if ( project == null ) |
315 | |
{ |
316 | 1 | return null; |
317 | |
} |
318 | |
|
319 | 3 | String encoding = project.getModel().getModelEncoding(); |
320 | |
|
321 | 3 | XmlStreamReader reader = null; |
322 | |
try |
323 | |
{ |
324 | 3 | reader = new XmlStreamReader( project.getFile() ); |
325 | 3 | encoding = reader.getEncoding(); |
326 | |
} |
327 | 0 | catch ( IOException e ) |
328 | |
{ |
329 | |
|
330 | |
} |
331 | |
finally |
332 | |
{ |
333 | 3 | IOUtil.close( reader ); |
334 | 3 | } |
335 | |
|
336 | 3 | if ( StringUtils.isEmpty( encoding ) ) |
337 | |
{ |
338 | 0 | return "UTF-8"; |
339 | |
} |
340 | |
|
341 | 3 | return encoding; |
342 | |
} |
343 | |
} |