View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    * 
5    * $Id: CommentStyleListMojo.java 13519 2011-02-05 09:32:50Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/CommentStyleListMojo.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  
26  package org.codehaus.mojo.license;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.codehaus.mojo.license.header.transformer.FileHeaderTransformer;
31  
32  import java.util.*;
33  
34  /**
35   * Displays all the available comment style to box file headers.
36   *
37   * @author themit <chemit@codelutin.com>
38   * @requiresProject false
39   * @requiresDirectInvocation
40   * @goal comment-style-list
41   * @since 1.0
42   */
43  public class CommentStyleListMojo
44      extends AbstractLicenseMojo
45  {
46  
47      /**
48       * A flag to display also the content of each license.
49       *
50       * @parameter expression="${detail}"
51       * @since 1.0
52       */
53      private boolean detail;
54  
55      /**
56       * All available header transformers.
57       *
58       * @component role="org.codehaus.mojo.license.header.transformer.FileHeaderTransformer"
59       * @since 1.0
60       */
61      private Map<String, FileHeaderTransformer> transformers;
62  
63      @Override
64      protected void init()
65          throws Exception
66      {
67          //nothing to do
68      }
69  
70      @Override
71      public void doAction()
72          throws MojoExecutionException, MojoFailureException
73      {
74  
75          StringBuilder buffer = new StringBuilder();
76          if ( isVerbose() )
77          {
78              buffer.append( "\n\n-------------------------------------------------------------------------------\n" );
79              buffer.append( "                           license-maven-plugin\n" );
80              buffer.append( "-------------------------------------------------------------------------------\n\n" );
81          }
82          List<String> names = new ArrayList<String>( transformers.keySet() );
83          Collections.sort( names );
84  
85          int maxLength = 0;
86          int maxDLength = 0;
87          for ( String name : names )
88          {
89              if ( name.length() > maxLength )
90              {
91                  maxLength = name.length();
92              }
93              FileHeaderTransformer transformer = transformers.get( name );
94              if ( transformer.getDescription().length() > maxDLength )
95              {
96                  maxDLength = transformer.getDescription().length();
97              }
98          }
99  
100         String pattern = "  - %1$-" + maxLength + "s : %2$-" + maxDLength + "s, extensions : %3$s\n";
101 
102         buffer.append( "List of available comment styles:\n\n" );
103         for ( String transformerName : names )
104         {
105             FileHeaderTransformer transformer = transformers.get( transformerName );
106             buffer.append( String.format( pattern, transformerName, transformer.getDescription(),
107                                           Arrays.toString( transformer.getDefaultAcceptedExtensions() ) ) );
108             if ( detail )
109             {
110                 buffer.append( "\n   example : \n" );
111                 buffer.append( transformer.boxComment( "header", true ) );
112                 buffer.append( '\n' );
113             }
114         }
115 
116         getLog().info( buffer.toString() );
117     }
118 
119     public boolean isDetail()
120     {
121         return detail;
122     }
123 
124     public void setDetail( boolean detail )
125     {
126         this.detail = detail;
127     }
128 
129     public Map<String, FileHeaderTransformer> getTransformers()
130     {
131         return transformers;
132     }
133 
134     public void setTransformers( Map<String, FileHeaderTransformer> transformers )
135     {
136         this.transformers = transformers;
137     }
138 }