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