View Javadoc

1   /*
2    *  jDTAUS Core Container Mojo
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.mojo;
22  
23  import java.io.File;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.LinkedList;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.codehaus.plexus.util.DirectoryScanner;
31  
32  /**
33   * Mojo to cleanup source files (e.g. remove trailing spaces).
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: CleanMojo.java 8743 2012-10-07 03:06:20Z schulte $
37   * @goal clean-sources
38   */
39  public class CleanMojo extends AbstractContainerMojo
40  {
41  
42      /** Creates a new {@code CleanMojo} instance. */
43      public CleanMojo()
44      {
45          super();
46      }
47  
48      /**
49       * Scans a project's source directory for files to clean.
50       *
51       * @return a {@code Collection} holding {@code File} instances for all
52       * files to clean found in the project's source directory.
53       */
54      protected final Collection getCleanSources()
55      {
56          final Collection files = new LinkedList();
57          final File sourceDirectory =
58              new File( this.getMavenProject().getBasedir(), "src" );
59  
60          if ( sourceDirectory.exists() && sourceDirectory.isDirectory() )
61          {
62              final DirectoryScanner scanner = new DirectoryScanner();
63              scanner.setBasedir( sourceDirectory );
64              scanner.setIncludes( DEFAULT_SOURCE_INCLUDES );
65              scanner.addDefaultExcludes();
66              scanner.scan();
67  
68              for ( final Iterator it =
69                  Arrays.asList( scanner.getIncludedFiles() ).iterator();
70                    it.hasNext(); )
71              {
72                  files.add( new File( sourceDirectory, (String) it.next() ) );
73              }
74          }
75  
76          return files;
77      }
78  
79      //--AbstractMojo------------------------------------------------------------
80  
81      public void execute() throws MojoExecutionException, MojoFailureException
82      {
83          final Collection sources = this.getCleanSources();
84          final SourceEditor editor = new RemoveTrailingSpacesEditor();
85  
86          for ( final Iterator it = sources.iterator(); it.hasNext();)
87          {
88              final File file = (File) it.next();
89              final String content = this.load( file );
90  
91              if ( this.getLog().isDebugEnabled() )
92              {
93                  this.getLog().debug( file.getAbsolutePath() );
94              }
95  
96              final String edited = this.edit( content, editor );
97              if ( !content.equals( edited ) )
98              {
99                  this.save( file, edited );
100             }
101         }
102     }
103 
104     //------------------------------------------------------------AbstractMojo--
105     //--CleanMojo---------------------------------------------------------------
106 
107     /** Removes trailing spaces. */
108     public static class RemoveTrailingSpacesEditor
109         implements AbstractContainerMojo.SourceEditor
110     {
111 
112         private boolean modified;
113 
114         /** Creates a new {@code RemoveTrailingSpacesEditor} instance. */
115         public RemoveTrailingSpacesEditor()
116         {
117             super();
118         }
119 
120         public String editLine( final String line ) throws MojoFailureException
121         {
122             String ret = null;
123 
124             if ( line != null )
125             {
126                 StringBuffer spaces = null;
127                 boolean sawSpace = false;
128                 final StringBuffer replacement =
129                     new StringBuffer( line.length() );
130 
131                 final char[] chars = line.toCharArray();
132 
133                 for ( int i = 0; i < chars.length; i++ )
134                 {
135                     if ( chars[i] == ' ' )
136                     {
137                         if ( spaces == null )
138                         {
139                             spaces = new StringBuffer();
140                         }
141 
142                         spaces.append( chars[i] );
143                         sawSpace = true;
144                     }
145                     else
146                     {
147                         if ( sawSpace )
148                         {
149                             replacement.append( spaces );
150                             sawSpace = false;
151                             spaces = null;
152                         }
153                         replacement.append( chars[i] );
154                     }
155                 }
156 
157                 ret = replacement.toString();
158                 this.modified = !ret.equals( line );
159             }
160 
161             return ret;
162         }
163 
164         public boolean isModified()
165         {
166             return this.modified;
167         }
168     }
169 
170     //---------------------------------------------------------------CleanMojo--
171 }