View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.license;
17  
18  import org.apache.maven.plugin.MojoExecutionException;
19  import org.apache.maven.plugin.MojoFailureException;
20  import org.codehaus.mojo.license.header.transformer.FileHeaderTransformer;
21  
22  import java.util.*;
23  
24  /**
25   * Displays all the available comment style to box file headers.
26   *
27   * @author themit <chemit@codelutin.com>
28   * @requiresProject false
29   * @requiresDirectInvocation
30   * @goal comment-style-list
31   * @since 1.0
32   */
33  public class CommentStyleListMojo
34      extends AbstractLicenseMojo
35  {
36  
37      /**
38       * A flag to display also the content of each license.
39       *
40       * @parameter expression="${detail}"
41       * @since 1.0
42       */
43      private boolean detail;
44  
45      /**
46       * All available header transformers.
47       *
48       * @component role="org.codehaus.mojo.license.header.transformer.FileHeaderTransformer"
49       * @since 1.0
50       */
51      private Map<String, FileHeaderTransformer> transformers;
52  
53      @Override
54      protected void init()
55          throws Exception
56      {
57          //nothing to do
58      }
59  
60      @Override
61      public void doAction()
62          throws MojoExecutionException, MojoFailureException
63      {
64  
65          StringBuilder buffer = new StringBuilder();
66          if ( isVerbose() )
67          {
68              buffer.append( "\n\n-------------------------------------------------------------------------------\n" );
69              buffer.append( "                           license-maven-plugin\n" );
70              buffer.append( "-------------------------------------------------------------------------------\n\n" );
71          }
72          List<String> names = new ArrayList<String>( transformers.keySet() );
73          Collections.sort( names );
74  
75          int maxLength = 0;
76          int maxDLength = 0;
77          for ( String name : names )
78          {
79              if ( name.length() > maxLength )
80              {
81                  maxLength = name.length();
82              }
83              FileHeaderTransformer transformer = transformers.get( name );
84              if ( transformer.getDescription().length() > maxDLength )
85              {
86                  maxDLength = transformer.getDescription().length();
87              }
88          }
89  
90          String pattern = "  - %1$-" + maxLength + "s : %2$-" + maxDLength + "s, extensions : %3$s\n";
91  
92          buffer.append( "List of available comment styles:\n\n" );
93          for ( String transformerName : names )
94          {
95              FileHeaderTransformer transformer = transformers.get( transformerName );
96              buffer.append( String.format( pattern, transformerName, transformer.getDescription(),
97                                            Arrays.toString( transformer.getDefaultAcceptedExtensions() ) ) );
98              if ( detail )
99              {
100                 buffer.append( "\n   example : \n" );
101                 buffer.append( transformer.boxComment( "header", true ) );
102                 buffer.append( '\n' );
103             }
104         }
105 
106         getLog().info( buffer.toString() );
107     }
108 
109     public boolean isDetail()
110     {
111         return detail;
112     }
113 
114     public void setDetail( boolean detail )
115     {
116         this.detail = detail;
117     }
118 
119     public Map<String, FileHeaderTransformer> getTransformers()
120     {
121         return transformers;
122     }
123 
124     public void setTransformers( Map<String, FileHeaderTransformer> transformers )
125     {
126         this.transformers = transformers;
127     }
128 }