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