View Javadoc
1   package org.apache.maven.plugins.pdf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Construct a DocumentModel from a MavenProject and related information.
43   *
44   * @author ltheussl
45   */
46  public class DocumentModelBuilder
47  {
48      /** A MavenProject to extract the information. */
49      private final MavenProject project;
50  
51      /** A DecorationModel to extract additional information. */
52      private final DecorationModel decorationModel;
53  
54      /**
55       * Constructor. Initialize a MavenProject to extract information from.
56       *
57       * @param project a MavenProject. May be null.
58       */
59      public DocumentModelBuilder( MavenProject project )
60      {
61          this( project, null );
62      }
63  
64      /**
65       * Constructor. Initialize a MavenProject and a DecorationModel to extract information from.
66       *
67       * @param project a MavenProject. May be null.
68       * @param decorationModel a DecorationModel. May be null.
69       */
70      public DocumentModelBuilder( MavenProject project, DecorationModel decorationModel )
71      {
72          this.project = project;
73          this.decorationModel = decorationModel;
74      }
75  
76      /**
77       * Get a DocumentModel.
78       *
79       * @return a DocumentModel. Always non-null.
80       */
81      public DocumentModel getDocumentModel()
82      {
83          return getDocumentModel( project, decorationModel, null );
84      }
85  
86      /**
87       * Get a DocumentModel.
88       *
89       * @param date overrides the default date in meta- and cover information.
90       * @return a DocumentModel. Always non-null.
91       */
92      public DocumentModel getDocumentModel( Date date )
93      {
94          return getDocumentModel( project, decorationModel, date );
95      }
96  
97      /**
98       * Extract a DocumentModel from a MavenProject.
99       *
100      * @param project a MavenProject. May be null.
101      * @param decorationModel a DecorationModel. May be null.
102      * @param date the date of the TOC. May be null in which case the build date will be used.
103      *
104      * @return a DocumentModel. Always non-null.
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      * Extract a DocumentTOC from a DecorationModel.
125      *
126      * @param decorationModel a DecorationModel. May be null.
127      * @return a DocumentTOC, always non-null.
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      * Extract meta information from a MavenProject.
152      *
153      * @param project a MavenProject. May be null.
154      * @param date the date to use in meta. May be null.
155      *
156      * @return a DocumentMeta object. Always non-null.
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         //meta.setGenerator( generator );
168         meta.setInitialCreator( System.getProperty( "user.name" ) );
169         //meta.setLanguage( locale == null ? null : locale.getLanguage() );
170         //meta.setPageSize( pageSize );
171         meta.setSubject( getProjectName( project ) );
172         meta.setTitle( getProjectName( project ) );
173 
174         return meta;
175     }
176 
177     /**
178      * Extract information for a DocumentCover from a MavenProject.
179      *
180      * @param project a MavenProject. May be null.
181      * @param date the cover date. May be null.
182      *
183      * @return a DocumentCover object. Always non-null.
184      */
185     private static DocumentCover getDocumentCover( MavenProject project, Date date )
186     {
187         final DocumentCover cover = new DocumentCover();
188 
189         cover.setAuthors( getAuthors( project ) );
190         //cover.setCompanyLogo( companyLogo );
191         cover.setCompanyName( getProjectOrganizationName( project ) );
192         cover.setCoverDate( date );
193         cover.setCoverSubTitle( project == null ? null : "v. " + project.getVersion() );
194         cover.setCoverTitle( getProjectName( project ) );
195         //cover.setCoverType( type );
196         cover.setCoverVersion( project == null ? null : project.getVersion() );
197         //cover.setProjectLogo( projectLogo );
198         cover.setProjectName( getProjectName( project ) );
199 
200         return cover;
201     }
202 
203     /**
204      * Wrap the list of project {@link Developer} to a list of {@link DocumentAuthor}.
205      *
206      * @param project the MavenProject to extract the authors from.
207      * @return a list of DocumentAuthors from the project developers.
208      * Returns null if project is null or contains no developers.
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      * @param project the MavenProject to extract the project organization name from.
252      * @return the project organization name if not empty, or the current System user name otherwise.
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      * Extract the name of the project.
267      *
268      * @param project the MavenProject to extract the project name from.
269      * @return the project name, or the project groupId and artifactId if
270      * the project name is empty, or null if project is null.
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      * Extract the encoding.
289      *
290      * @param project the MavenProject to extract the encoding name from.
291      * @return the project encoding if defined, or UTF-8 otherwise, or null if project is null.
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         // Workaround for MNG-4289
303         try ( XmlStreamReader reader = new XmlStreamReader( project.getFile() ) )
304         {
305             encoding = reader.getEncoding();
306         }
307         catch ( IOException e )
308         {
309             // nop
310         }
311 
312         if ( StringUtils.isEmpty( encoding ) )
313         {
314             return "UTF-8";
315         }
316 
317         return encoding;
318     }
319 }