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  
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   * Construct a DocumentModel from a MavenProject and related information.
47   *
48   * @author ltheussl
49   * @version $Id: DocumentModelBuilder.java 1396150 2012-10-09 18:15:29Z krosenvold $
50   */
51  public class DocumentModelBuilder
52  {
53      /** A MavenProject to extract the information. */
54      private final MavenProject project;
55  
56      /** A DecorationModel to extract additional information. */
57      private final DecorationModel decorationModel;
58  
59      /**
60       * Constructor. Initialize a MavenProject to extract information from.
61       *
62       * @param project a MavenProject. May be null.
63       */
64      public DocumentModelBuilder( MavenProject project )
65      {
66          this( project, null );
67      }
68  
69      /**
70       * Constructor. Initialize a MavenProject and a DecorationModel to extract information from.
71       *
72       * @param project a MavenProject. May be null.
73       * @param decorationModel a DecorationModel. May be null.
74       */
75      public DocumentModelBuilder( MavenProject project, DecorationModel decorationModel )
76      {
77          this.project = project;
78          this.decorationModel = decorationModel;
79      }
80  
81      /**
82       * Get a DocumentModel.
83       *
84       * @return a DocumentModel. Always non-null.
85       */
86      public DocumentModel getDocumentModel()
87      {
88          return getDocumentModel( project, decorationModel, null );
89      }
90  
91      /**
92       * Get a DocumentModel.
93       *
94       * @param date overrides the default date in meta- and cover information.
95       * @return a DocumentModel. Always non-null.
96       */
97      public DocumentModel getDocumentModel( Date date )
98      {
99          return getDocumentModel( project, decorationModel, date );
100     }
101 
102     // ----------------------------------------------------------------------
103     // Private methods
104     // ----------------------------------------------------------------------
105 
106     /**
107      * Extract a DocumentModel from a MavenProject.
108      *
109      * @param project a MavenProject. May be null.
110      * @param decorationModel a DecorationModel. May be null.
111      * @param date the date of the TOC. May be null in which case the build date will be used.
112      *
113      * @return a DocumentModel. Always non-null.
114      */
115     private static DocumentModel getDocumentModel( MavenProject project,
116             DecorationModel decorationModel, Date date )
117     {
118         final Date now = ( date == null ? new Date() : date );
119 
120         final DocumentModel docModel = new DocumentModel();
121 
122         docModel.setModelEncoding( getProjectModelEncoding( project ) );
123         docModel.setOutputName( project == null || project.getArtifactId() == null
124                 ? "unnamed" : project.getArtifactId() );
125         docModel.setMeta( getDocumentMeta( project, now ) );
126         docModel.setCover( getDocumentCover( project, now ) );
127         docModel.setToc( getDocumentTOC( decorationModel ) );
128 
129         return docModel;
130     }
131 
132     /**
133      * Extract a DocumentTOC from a DecorationModel.
134      *
135      * @param decorationModel a DecorationModel. May be null.
136      * @return a DocumentTOC, always non-null.
137      */
138     private static DocumentTOC getDocumentTOC( DecorationModel decorationModel )
139     {
140         final DocumentTOC toc = new DocumentTOC();
141 
142         if ( decorationModel != null && decorationModel.getMenus() != null )
143         {
144             for ( final Iterator it = decorationModel.getMenus().iterator(); it.hasNext(); )
145             {
146                 final Menu menu = (Menu) it.next();
147 
148                 for ( final Iterator it2 = menu.getItems().iterator(); it2.hasNext(); )
149                 {
150                     final MenuItem item = (MenuItem) it2.next();
151 
152                     final DocumentTOCItem documentTOCItem = new DocumentTOCItem();
153                     documentTOCItem.setName( item.getName() );
154                     documentTOCItem.setRef( item.getHref() );
155                     toc.addItem( documentTOCItem );
156                 }
157             }
158         }
159 
160         return toc;
161     }
162 
163     /**
164      * Extract meta information from a MavenProject.
165      *
166      * @param project a MavenProject. May be null.
167      * @param date the date to use in meta. May be null.
168      *
169      * @return a DocumentMeta object. Always non-null.
170      */
171     private static DocumentMeta getDocumentMeta( MavenProject project, Date date )
172     {
173         final DocumentMeta meta = new DocumentMeta();
174 
175         meta.setAuthors( getAuthors( project ) );
176         meta.setCreationDate( date );
177         meta.setCreator( System.getProperty( "user.name" ) );
178         meta.setDate( date );
179         meta.setDescription( project == null ? null : project.getDescription() );
180         //meta.setGenerator( generator );
181         meta.setInitialCreator( System.getProperty( "user.name" ) );
182         //meta.setLanguage( locale == null ? null : locale.getLanguage() );
183         //meta.setPageSize( pageSize );
184         meta.setSubject( getProjectName( project ) );
185         meta.setTitle( getProjectName( project ) );
186 
187         return meta;
188     }
189 
190     /**
191      * Extract information for a DocumentCover from a MavenProject.
192      *
193      * @param project a MavenProject. May be null.
194      * @param date the cover date. May be null.
195      *
196      * @return a DocumentCover object. Always non-null.
197      */
198     private static DocumentCover getDocumentCover( MavenProject project, Date date )
199     {
200         final DocumentCover cover = new DocumentCover();
201 
202         cover.setAuthors( getAuthors( project ) );
203         //cover.setCompanyLogo( companyLogo );
204         cover.setCompanyName( getProjectOrganizationName( project ) );
205         cover.setCoverDate( date );
206         cover.setCoverSubTitle( project == null ? null : "v. " + project.getVersion() );
207         cover.setCoverTitle( getProjectName( project ) );
208         //cover.setCoverType( type );
209         cover.setCoverVersion( project == null ? null : project.getVersion() );
210         //cover.setProjectLogo( projectLogo );
211         cover.setProjectName( getProjectName( project ) );
212 
213         return cover;
214     }
215 
216     /**
217      * Wrap the list of project {@link Developer} to a list of {@link DocumentAuthor}.
218      *
219      * @param project the MavenProject to extract the authors from.
220      * @return a list of DocumentAuthors from the project developers.
221      * Returns null if project is null or contains no developers.
222      */
223     private static List getAuthors( MavenProject project )
224     {
225         if ( project == null || project.getDevelopers() == null )
226         {
227             return null;
228         }
229 
230         final List ret = new ArrayList( 4 );
231 
232         for ( final Iterator it = project.getDevelopers().iterator(); it.hasNext(); )
233         {
234             final Developer developer = (Developer) it.next();
235 
236             final DocumentAuthor author = new DocumentAuthor();
237             author.setName( developer.getName() );
238             author.setEmail( developer.getEmail() );
239             author.setCompanyName( developer.getOrganization() );
240             StringBuilder roles = null;
241 
242             for ( final Iterator it2 = developer.getRoles().iterator(); it2.hasNext(); )
243             {
244                 final String role = (String) it2.next();
245 
246                 if ( roles == null )
247                 {
248                     roles = new StringBuilder( 32 );
249                 }
250 
251                 roles.append( role );
252 
253                 if ( it2.hasNext() )
254                 {
255                     roles.append( ',' ).append( ' ' );
256                 }
257             }
258             if ( roles != null )
259             {
260                 author.setPosition( roles.toString() );
261             }
262 
263             ret.add( author );
264         }
265 
266         return ret;
267     }
268 
269     /**
270      * @param project the MavenProject to extract the project organization name from.
271      * @return the project organization name if not empty, or the current System user name otherwise.
272      */
273     private static String getProjectOrganizationName( MavenProject project )
274     {
275         if ( project != null && project.getOrganization() != null
276                 && StringUtils.isNotEmpty( project.getOrganization().getName() ) )
277         {
278             return project.getOrganization().getName();
279         }
280 
281         return System.getProperty( "user.name" );
282     }
283 
284     /**
285      * Extract the name of the project.
286      *
287      * @param project the MavenProject to extract the project name from.
288      * @return the project name, or the project groupId and artifactId if
289      * the project name is empty, or null if project is null.
290      */
291     private static String getProjectName( MavenProject project )
292     {
293         if ( project == null )
294         {
295             return null;
296         }
297 
298         if ( StringUtils.isEmpty( project.getName() ) )
299         {
300             return project.getGroupId() + ":" + project.getArtifactId();
301         }
302 
303         return project.getName();
304     }
305 
306     /**
307      * Extract the encoding.
308      *
309      * @param project the MavenProject to extract the encoding name from.
310      * @return the project encoding if defined, or UTF-8 otherwise, or null if project is null.
311      */
312     private static String getProjectModelEncoding( MavenProject project )
313     {
314         if ( project == null )
315         {
316             return null;
317         }
318 
319         String encoding = project.getModel().getModelEncoding();
320         // Workaround for MNG-4289
321         XmlStreamReader reader = null;
322         try
323         {
324             reader = new XmlStreamReader( project.getFile() );
325             encoding = reader.getEncoding();
326         }
327         catch ( IOException e )
328         {
329             // nop
330         }
331         finally
332         {
333             IOUtil.close( reader );
334         }
335 
336         if ( StringUtils.isEmpty( encoding ) )
337         {
338             return "UTF-8";
339         }
340 
341         return encoding;
342     }
343 }