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