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.util.ArrayList; |
24 | |
import java.util.Date; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import org.apache.maven.doxia.document.DocumentAuthor; |
29 | |
import org.apache.maven.doxia.document.DocumentCover; |
30 | |
import org.apache.maven.doxia.document.DocumentMeta; |
31 | |
import org.apache.maven.doxia.document.DocumentModel; |
32 | |
import org.apache.maven.doxia.document.DocumentTOC; |
33 | |
import org.apache.maven.doxia.document.DocumentTOCItem; |
34 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
35 | |
import org.apache.maven.doxia.site.decoration.Menu; |
36 | |
import org.apache.maven.doxia.site.decoration.MenuItem; |
37 | |
import org.apache.maven.model.Developer; |
38 | |
import org.apache.maven.project.MavenProject; |
39 | |
|
40 | |
import org.codehaus.plexus.util.StringUtils; |
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 Iterator it = decorationModel.getMenus().iterator(); it.hasNext(); ) |
142 | |
{ |
143 | 2 | final Menu menu = (Menu) it.next(); |
144 | |
|
145 | 2 | for ( final Iterator it2 = menu.getItems().iterator(); it2.hasNext(); ) |
146 | |
{ |
147 | 6 | final MenuItem item = (MenuItem) it2.next(); |
148 | |
|
149 | 6 | final DocumentTOCItem documentTOCItem = new DocumentTOCItem(); |
150 | 6 | documentTOCItem.setName( item.getName() ); |
151 | 6 | documentTOCItem.setRef( item.getHref() ); |
152 | 6 | toc.addItem( documentTOCItem ); |
153 | |
} |
154 | |
} |
155 | |
} |
156 | |
|
157 | 4 | return toc; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
private static DocumentMeta getDocumentMeta( MavenProject project, Date date ) |
169 | |
{ |
170 | 4 | final DocumentMeta meta = new DocumentMeta(); |
171 | |
|
172 | 4 | meta.setAuthors( getAuthors( project ) ); |
173 | 4 | meta.setCreationDate( date ); |
174 | 4 | meta.setCreator( System.getProperty( "user.name" ) ); |
175 | 4 | meta.setDate( date ); |
176 | 4 | meta.setDescription( project == null ? null : project.getDescription() ); |
177 | |
|
178 | 4 | meta.setInitialCreator( System.getProperty( "user.name" ) ); |
179 | |
|
180 | |
|
181 | 4 | meta.setSubject( getProjectName( project ) ); |
182 | 4 | meta.setTitle( getProjectName( project ) ); |
183 | |
|
184 | 4 | return meta; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
private static DocumentCover getDocumentCover( MavenProject project, Date date ) |
196 | |
{ |
197 | 4 | final DocumentCover cover = new DocumentCover(); |
198 | |
|
199 | 4 | cover.setAuthors( getAuthors( project ) ); |
200 | |
|
201 | 4 | cover.setCompanyName( getProjectOrganizationName( project ) ); |
202 | 4 | cover.setCoverDate( date ); |
203 | 4 | cover.setCoverSubTitle( project == null ? null : "v. " + project.getVersion() ); |
204 | 4 | cover.setCoverTitle( getProjectName( project ) ); |
205 | |
|
206 | 4 | cover.setCoverVersion( project == null ? null : project.getVersion() ); |
207 | |
|
208 | 4 | cover.setProjectName( getProjectName( project ) ); |
209 | |
|
210 | 4 | return cover; |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
private static List getAuthors( MavenProject project ) |
221 | |
{ |
222 | 8 | if ( project == null || project.getDevelopers() == null ) |
223 | |
{ |
224 | 2 | return null; |
225 | |
} |
226 | |
|
227 | 6 | final List ret = new ArrayList(); |
228 | |
|
229 | 6 | for ( final Iterator it = project.getDevelopers().iterator(); it.hasNext(); ) |
230 | |
{ |
231 | 8 | final Developer developer = (Developer) it.next(); |
232 | |
|
233 | 8 | final DocumentAuthor author = new DocumentAuthor(); |
234 | 8 | author.setName( developer.getName() ); |
235 | 8 | author.setEmail( developer.getEmail() ); |
236 | 8 | author.setCompanyName( developer.getOrganization() ); |
237 | 8 | StringBuffer roles = null; |
238 | |
|
239 | 8 | for ( final Iterator it2 = developer.getRoles().iterator(); it2.hasNext(); ) |
240 | |
{ |
241 | 8 | final String role = (String) it2.next(); |
242 | |
|
243 | 8 | if ( roles == null ) |
244 | |
{ |
245 | 4 | roles = new StringBuffer(); |
246 | |
} |
247 | |
|
248 | 8 | roles.append( role ); |
249 | |
|
250 | 8 | if ( it2.hasNext() ) |
251 | |
{ |
252 | 4 | roles.append( ',' ).append( ' ' ); |
253 | |
} |
254 | |
} |
255 | 8 | if ( roles != null ) |
256 | |
{ |
257 | 4 | author.setPosition( roles.toString() ); |
258 | |
} |
259 | |
|
260 | 8 | ret.add( author ); |
261 | |
} |
262 | |
|
263 | 6 | return ret; |
264 | |
} |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
private static String getProjectOrganizationName( MavenProject project ) |
271 | |
{ |
272 | 4 | if ( project != null && project.getOrganization() != null |
273 | |
&& StringUtils.isNotEmpty( project.getOrganization().getName() ) ) |
274 | |
{ |
275 | 2 | return project.getOrganization().getName(); |
276 | |
} |
277 | |
|
278 | 2 | return System.getProperty( "user.name" ); |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
private static String getProjectName( MavenProject project ) |
289 | |
{ |
290 | 16 | if ( project == null ) |
291 | |
{ |
292 | 4 | return null; |
293 | |
} |
294 | |
|
295 | 12 | if ( StringUtils.isEmpty( project.getName() ) ) |
296 | |
{ |
297 | 4 | return project.getGroupId() + ":" + project.getArtifactId(); |
298 | |
} |
299 | |
|
300 | 8 | return project.getName(); |
301 | |
} |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
private static String getProjectModelEncoding( MavenProject project ) |
310 | |
{ |
311 | 4 | if ( project == null ) |
312 | |
{ |
313 | 1 | return null; |
314 | |
} |
315 | |
|
316 | 3 | if ( StringUtils.isEmpty( project.getModel().getModelEncoding() ) ) |
317 | |
{ |
318 | 0 | return "UTF-8"; |
319 | |
} |
320 | |
|
321 | 3 | return project.getModel().getModelEncoding(); |
322 | |
} |
323 | |
} |