Coverage Report - org.apache.maven.plugins.pdf.DocumentModelBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentModelBuilder
96 %
88/91
89 %
43/48
3,583
 
 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.codehaus.plexus.util.xml.XmlStreamReader;
 44  
 
 45  
 /**
 46  
  * Construct a DocumentModel from a MavenProject and related information.
 47  
  *
 48  
  * @author ltheussl
 49  
  * @version $Id: DocumentModelBuilder.java 803108 2009-08-11 13:14:33Z vsiveton $
 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  2
         this( project, null );
 67  2
     }
 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  4
     {
 77  4
         this.project = project;
 78  4
         this.decorationModel = decorationModel;
 79  4
     }
 80  
 
 81  
     /**
 82  
      * Get a DocumentModel.
 83  
      *
 84  
      * @return a DocumentModel. Always non-null.
 85  
      */
 86  
     public DocumentModel getDocumentModel()
 87  
     {
 88  4
         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  0
         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  4
         final Date now = ( date == null ? new Date() : date );
 119  
 
 120  4
         final DocumentModel docModel = new DocumentModel();
 121  
 
 122  4
         docModel.setModelEncoding( getProjectModelEncoding( project ) );
 123  4
         docModel.setOutputName( project == null || project.getArtifactId() == null
 124  
                 ? "unnamed" : project.getArtifactId() );
 125  4
         docModel.setMeta( getDocumentMeta( project, now ) );
 126  4
         docModel.setCover( getDocumentCover( project, now ) );
 127  4
         docModel.setToc( getDocumentTOC( decorationModel ) );
 128  
 
 129  4
         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  4
         final DocumentTOC toc = new DocumentTOC();
 141  
 
 142  4
         if ( decorationModel != null && decorationModel.getMenus() != null )
 143  
         {
 144  2
             for ( final Iterator it = decorationModel.getMenus().iterator(); it.hasNext(); )
 145  
             {
 146  2
                 final Menu menu = (Menu) it.next();
 147  
 
 148  2
                 for ( final Iterator it2 = menu.getItems().iterator(); it2.hasNext(); )
 149  
                 {
 150  6
                     final MenuItem item = (MenuItem) it2.next();
 151  
 
 152  6
                     final DocumentTOCItem documentTOCItem = new DocumentTOCItem();
 153  6
                     documentTOCItem.setName( item.getName() );
 154  6
                     documentTOCItem.setRef( item.getHref() );
 155  6
                     toc.addItem( documentTOCItem );
 156  6
                 }
 157  2
             }
 158  
         }
 159  
 
 160  4
         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  4
         final DocumentMeta meta = new DocumentMeta();
 174  
 
 175  4
         meta.setAuthors( getAuthors( project ) );
 176  4
         meta.setCreationDate( date );
 177  4
         meta.setCreator( System.getProperty( "user.name" ) );
 178  4
         meta.setDate( date );
 179  4
         meta.setDescription( project == null ? null : project.getDescription() );
 180  
         //meta.setGenerator( generator );
 181  4
         meta.setInitialCreator( System.getProperty( "user.name" ) );
 182  
         //meta.setLanguage( locale == null ? null : locale.getLanguage() );
 183  
         //meta.setPageSize( pageSize );
 184  4
         meta.setSubject( getProjectName( project ) );
 185  4
         meta.setTitle( getProjectName( project ) );
 186  
 
 187  4
         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  4
         final DocumentCover cover = new DocumentCover();
 201  
 
 202  4
         cover.setAuthors( getAuthors( project ) );
 203  
         //cover.setCompanyLogo( companyLogo );
 204  4
         cover.setCompanyName( getProjectOrganizationName( project ) );
 205  4
         cover.setCoverDate( date );
 206  4
         cover.setCoverSubTitle( project == null ? null : "v. " + project.getVersion() );
 207  4
         cover.setCoverTitle( getProjectName( project ) );
 208  
         //cover.setCoverType( type );
 209  4
         cover.setCoverVersion( project == null ? null : project.getVersion() );
 210  
         //cover.setProjectLogo( projectLogo );
 211  4
         cover.setProjectName( getProjectName( project ) );
 212  
 
 213  4
         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  8
         if ( project == null || project.getDevelopers() == null )
 226  
         {
 227  2
             return null;
 228  
         }
 229  
 
 230  6
         final List ret = new ArrayList();
 231  
 
 232  6
         for ( final Iterator it = project.getDevelopers().iterator(); it.hasNext(); )
 233  
         {
 234  8
             final Developer developer = (Developer) it.next();
 235  
 
 236  8
             final DocumentAuthor author = new DocumentAuthor();
 237  8
             author.setName( developer.getName() );
 238  8
             author.setEmail( developer.getEmail() );
 239  8
             author.setCompanyName( developer.getOrganization() );
 240  8
             StringBuffer roles = null;
 241  
 
 242  8
             for ( final Iterator it2 = developer.getRoles().iterator(); it2.hasNext(); )
 243  
             {
 244  8
                 final String role = (String) it2.next();
 245  
 
 246  8
                 if ( roles == null )
 247  
                 {
 248  4
                     roles = new StringBuffer();
 249  
                 }
 250  
 
 251  8
                 roles.append( role );
 252  
 
 253  8
                 if ( it2.hasNext() )
 254  
                 {
 255  4
                     roles.append( ',' ).append( ' ' );
 256  
                 }
 257  8
             }
 258  8
             if ( roles != null )
 259  
             {
 260  4
                 author.setPosition( roles.toString() );
 261  
             }
 262  
 
 263  8
             ret.add( author );
 264  8
         }
 265  
 
 266  6
         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  4
         if ( project != null && project.getOrganization() != null
 276  
                 && StringUtils.isNotEmpty( project.getOrganization().getName() ) )
 277  
         {
 278  2
             return project.getOrganization().getName();
 279  
         }
 280  
 
 281  2
         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  16
         if ( project == null )
 294  
         {
 295  4
             return null;
 296  
         }
 297  
 
 298  12
         if ( StringUtils.isEmpty( project.getName() ) )
 299  
         {
 300  4
             return project.getGroupId() + ":" + project.getArtifactId();
 301  
         }
 302  
 
 303  8
         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  4
         if ( project == null )
 315  
         {
 316  1
             return null;
 317  
         }
 318  
 
 319  3
         String encoding = project.getModel().getModelEncoding();
 320  
         // Workaround for MNG-4289
 321  3
         XmlStreamReader reader = null;
 322  
         try
 323  
         {
 324  3
             reader = ReaderFactory.newXmlReader( project.getFile() );
 325  3
             encoding = reader.getEncoding();
 326  
         }
 327  0
         catch ( IOException e )
 328  
         {
 329  
             // nop
 330  
         }
 331  
         finally
 332  
         {
 333  3
             IOUtil.close( reader );
 334  3
         }
 335  
 
 336  3
         if ( StringUtils.isEmpty( encoding ) )
 337  
         {
 338  0
             return "UTF-8";
 339  
         }
 340  
 
 341  3
         return encoding;
 342  
     }
 343  
 }