| 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 | 4 | this( project, null ); |
| 67 | 4 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public DocumentModelBuilder( MavenProject project, DecorationModel decorationModel ) |
| 76 | 8 | { |
| 77 | 8 | this.project = project; |
| 78 | 8 | this.decorationModel = decorationModel; |
| 79 | 8 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public DocumentModel getDocumentModel() |
| 87 | |
{ |
| 88 | 8 | 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 | 8 | final Date now = ( date == null ? new Date() : date ); |
| 119 | |
|
| 120 | 8 | final DocumentModel docModel = new DocumentModel(); |
| 121 | |
|
| 122 | 8 | docModel.setModelEncoding( getProjectModelEncoding( project ) ); |
| 123 | 8 | docModel.setOutputName( project == null || project.getArtifactId() == null |
| 124 | |
? "unnamed" : project.getArtifactId() ); |
| 125 | 8 | docModel.setMeta( getDocumentMeta( project, now ) ); |
| 126 | 8 | docModel.setCover( getDocumentCover( project, now ) ); |
| 127 | 8 | docModel.setToc( getDocumentTOC( decorationModel ) ); |
| 128 | |
|
| 129 | 8 | return docModel; |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
private static DocumentTOC getDocumentTOC( DecorationModel decorationModel ) |
| 139 | |
{ |
| 140 | 8 | final DocumentTOC toc = new DocumentTOC(); |
| 141 | |
|
| 142 | 8 | if ( decorationModel != null && decorationModel.getMenus() != null ) |
| 143 | |
{ |
| 144 | 4 | for ( final Iterator it = decorationModel.getMenus().iterator(); it.hasNext(); ) |
| 145 | |
{ |
| 146 | 4 | final Menu menu = (Menu) it.next(); |
| 147 | |
|
| 148 | 4 | for ( final Iterator it2 = menu.getItems().iterator(); it2.hasNext(); ) |
| 149 | |
{ |
| 150 | 10 | final MenuItem item = (MenuItem) it2.next(); |
| 151 | |
|
| 152 | 10 | final DocumentTOCItem documentTOCItem = new DocumentTOCItem(); |
| 153 | 10 | documentTOCItem.setName( item.getName() ); |
| 154 | 10 | documentTOCItem.setRef( item.getHref() ); |
| 155 | 10 | toc.addItem( documentTOCItem ); |
| 156 | 10 | } |
| 157 | 4 | } |
| 158 | |
} |
| 159 | |
|
| 160 | 8 | return toc; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
private static DocumentMeta getDocumentMeta( MavenProject project, Date date ) |
| 172 | |
{ |
| 173 | 8 | final DocumentMeta meta = new DocumentMeta(); |
| 174 | |
|
| 175 | 8 | meta.setAuthors( getAuthors( project ) ); |
| 176 | 8 | meta.setCreationDate( date ); |
| 177 | 8 | meta.setCreator( System.getProperty( "user.name" ) ); |
| 178 | 8 | meta.setDate( date ); |
| 179 | 8 | meta.setDescription( project == null ? null : project.getDescription() ); |
| 180 | |
|
| 181 | 8 | meta.setInitialCreator( System.getProperty( "user.name" ) ); |
| 182 | |
|
| 183 | |
|
| 184 | 8 | meta.setSubject( getProjectName( project ) ); |
| 185 | 8 | meta.setTitle( getProjectName( project ) ); |
| 186 | |
|
| 187 | 8 | return meta; |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private static DocumentCover getDocumentCover( MavenProject project, Date date ) |
| 199 | |
{ |
| 200 | 8 | final DocumentCover cover = new DocumentCover(); |
| 201 | |
|
| 202 | 8 | cover.setAuthors( getAuthors( project ) ); |
| 203 | |
|
| 204 | 8 | cover.setCompanyName( getProjectOrganizationName( project ) ); |
| 205 | 8 | cover.setCoverDate( date ); |
| 206 | 8 | cover.setCoverSubTitle( project == null ? null : "v. " + project.getVersion() ); |
| 207 | 8 | cover.setCoverTitle( getProjectName( project ) ); |
| 208 | |
|
| 209 | 8 | cover.setCoverVersion( project == null ? null : project.getVersion() ); |
| 210 | |
|
| 211 | 8 | cover.setProjectName( getProjectName( project ) ); |
| 212 | |
|
| 213 | 8 | return cover; |
| 214 | |
} |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
private static List getAuthors( MavenProject project ) |
| 224 | |
{ |
| 225 | 16 | if ( project == null || project.getDevelopers() == null ) |
| 226 | |
{ |
| 227 | 4 | return null; |
| 228 | |
} |
| 229 | |
|
| 230 | 12 | final List ret = new ArrayList( 4 ); |
| 231 | |
|
| 232 | 12 | for ( final Iterator it = project.getDevelopers().iterator(); it.hasNext(); ) |
| 233 | |
{ |
| 234 | 16 | final Developer developer = (Developer) it.next(); |
| 235 | |
|
| 236 | 16 | final DocumentAuthor author = new DocumentAuthor(); |
| 237 | 16 | author.setName( developer.getName() ); |
| 238 | 16 | author.setEmail( developer.getEmail() ); |
| 239 | 16 | author.setCompanyName( developer.getOrganization() ); |
| 240 | 16 | StringBuilder roles = null; |
| 241 | |
|
| 242 | 16 | for ( final Iterator it2 = developer.getRoles().iterator(); it2.hasNext(); ) |
| 243 | |
{ |
| 244 | 16 | final String role = (String) it2.next(); |
| 245 | |
|
| 246 | 16 | if ( roles == null ) |
| 247 | |
{ |
| 248 | 8 | roles = new StringBuilder( 32 ); |
| 249 | |
} |
| 250 | |
|
| 251 | 16 | roles.append( role ); |
| 252 | |
|
| 253 | 16 | if ( it2.hasNext() ) |
| 254 | |
{ |
| 255 | 8 | roles.append( ',' ).append( ' ' ); |
| 256 | |
} |
| 257 | 16 | } |
| 258 | 16 | if ( roles != null ) |
| 259 | |
{ |
| 260 | 8 | author.setPosition( roles.toString() ); |
| 261 | |
} |
| 262 | |
|
| 263 | 16 | ret.add( author ); |
| 264 | 16 | } |
| 265 | |
|
| 266 | 12 | return ret; |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
private static String getProjectOrganizationName( MavenProject project ) |
| 274 | |
{ |
| 275 | 8 | if ( project != null && project.getOrganization() != null |
| 276 | |
&& StringUtils.isNotEmpty( project.getOrganization().getName() ) ) |
| 277 | |
{ |
| 278 | 4 | return project.getOrganization().getName(); |
| 279 | |
} |
| 280 | |
|
| 281 | 4 | return System.getProperty( "user.name" ); |
| 282 | |
} |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
private static String getProjectName( MavenProject project ) |
| 292 | |
{ |
| 293 | 32 | if ( project == null ) |
| 294 | |
{ |
| 295 | 8 | return null; |
| 296 | |
} |
| 297 | |
|
| 298 | 24 | if ( StringUtils.isEmpty( project.getName() ) ) |
| 299 | |
{ |
| 300 | 8 | return project.getGroupId() + ":" + project.getArtifactId(); |
| 301 | |
} |
| 302 | |
|
| 303 | 16 | return project.getName(); |
| 304 | |
} |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
private static String getProjectModelEncoding( MavenProject project ) |
| 313 | |
{ |
| 314 | 8 | if ( project == null ) |
| 315 | |
{ |
| 316 | 2 | return null; |
| 317 | |
} |
| 318 | |
|
| 319 | 6 | String encoding = project.getModel().getModelEncoding(); |
| 320 | |
|
| 321 | 6 | XmlStreamReader reader = null; |
| 322 | |
try |
| 323 | |
{ |
| 324 | 6 | reader = new XmlStreamReader( project.getFile() ); |
| 325 | 6 | encoding = reader.getEncoding(); |
| 326 | |
} |
| 327 | 0 | catch ( IOException e ) |
| 328 | |
{ |
| 329 | |
|
| 330 | |
} |
| 331 | |
finally |
| 332 | |
{ |
| 333 | 6 | IOUtil.close( reader ); |
| 334 | 6 | } |
| 335 | |
|
| 336 | 6 | if ( StringUtils.isEmpty( encoding ) ) |
| 337 | |
{ |
| 338 | 0 | return "UTF-8"; |
| 339 | |
} |
| 340 | |
|
| 341 | 6 | return encoding; |
| 342 | |
} |
| 343 | |
} |