View Javadoc
1   package org.apache.maven.plugins.checkstyle.exec;
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  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
23  import com.puppycrawl.tools.checkstyle.api.AuditListener;
24  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
25  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
26  import com.puppycrawl.tools.checkstyle.api.Configuration;
27  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
28  
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import java.io.File;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * Listener in charge of receiving events from the Checker.
37   *
38   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   *
41   */
42  public class CheckstyleCheckerListener
43      extends AutomaticBean
44      implements AuditListener
45  {
46      private final List<File> sourceDirectories;
47  
48      private CheckstyleResults results;
49  
50      private String currentFile;
51  
52      private List<AuditEvent> events;
53  
54      private SeverityLevel severityLevel;
55  
56      private Configuration checkstyleConfiguration;
57  
58      /**
59       * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
60       */
61      public CheckstyleCheckerListener( File sourceDirectory )
62      {
63          this.sourceDirectories = new ArrayList<>();
64          this.sourceDirectories.add( sourceDirectory );
65      }
66      /**
67       * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
68       * @param configuration Checkstyle configuration
69       * @since 2.5
70       */
71      public CheckstyleCheckerListener( File sourceDirectory, Configuration configuration )
72      {
73          this.sourceDirectories = new ArrayList<>();
74          this.sourceDirectories.add( sourceDirectory );
75          this.checkstyleConfiguration = configuration;
76      }
77  
78      /**
79       * @param configuration Checkstyle configuration
80       * @since 2.5
81       */
82      public CheckstyleCheckerListener( Configuration configuration )
83      {
84          this.sourceDirectories = new ArrayList<>();
85          this.checkstyleConfiguration = configuration;
86      }
87  
88      /**
89       * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
90       */
91      public void addSourceDirectory( File sourceDirectory )
92      {
93          this.sourceDirectories.add( sourceDirectory );
94      }
95  
96      /**
97       * @param severityLevel The severity level of the events to listen to.
98       */
99      public void setSeverityLevelFilter( SeverityLevel severityLevel )
100     {
101         this.severityLevel = severityLevel;
102     }
103 
104     /**
105      * @return The severity level of the events to listen to.
106      */
107     public SeverityLevel getSeverityLevelFilter()
108     {
109         return severityLevel;
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public void auditStarted( AuditEvent event )
115     {
116         setResults( new CheckstyleResults() );
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public void auditFinished( AuditEvent event )
122     {
123         //do nothing
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public void fileStarted( AuditEvent event )
129     {
130         final String fileName = StringUtils.replace( event.getFileName(), "\\", "/" );
131 
132         for ( File sourceDirectory : sourceDirectories )
133         {
134             String sourceDirectoryPath = StringUtils.replace( sourceDirectory.getPath(), "\\", "/" );
135             
136             if ( fileName.startsWith( sourceDirectoryPath + "/" ) )
137             {
138                 currentFile = StringUtils.substring( fileName, sourceDirectoryPath.length() + 1 );
139 
140                 events = getResults().getFileViolations( currentFile );
141                 
142                 break;
143             }
144         }
145 
146         if ( events == null )
147         {
148             events = new ArrayList<>();
149         }
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public void fileFinished( AuditEvent event )
155     {
156         getResults().setFileViolations( currentFile, events );
157         currentFile = null;
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public void addError( AuditEvent event )
163     {
164         if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) )
165         {
166             return;
167         }
168 
169         if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) )
170         {
171             events.add( event );
172         }
173     }
174 
175     /** {@inheritDoc} */
176     @Override
177     public void addException( AuditEvent event, Throwable throwable )
178     {
179         //Do Nothing
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     protected void finishLocalSetup() throws CheckstyleException
185     {
186         //Do Nothing
187     }
188 
189     /**
190      * @return The results of Checkstyle invocation.
191      */
192     public CheckstyleResults getResults()
193     {
194         results.setConfiguration( checkstyleConfiguration );
195         return results;
196     }
197 
198     /**
199      * @param results The results of Checkstyle invocation.
200      */
201     public void setResults( CheckstyleResults results )
202     {
203         this.results = results;
204     }
205 
206     /**
207      * @since 2.5
208      * @return The configuration of Checkstyle to use.
209      */
210     public Configuration getCheckstyleConfiguration()
211     {
212         return checkstyleConfiguration;
213     }
214 
215     /**
216      * @param checkstyleConfiguration The configuration of Checkstyle to use.
217      * @since 2.5
218      */
219     public void setCheckstyleConfiguration( Configuration checkstyleConfiguration )
220     {
221         this.checkstyleConfiguration = checkstyleConfiguration;
222     }
223 
224 }
225