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