View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.checkstyle.exec;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
26  import com.puppycrawl.tools.checkstyle.api.AuditListener;
27  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
28  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
29  import com.puppycrawl.tools.checkstyle.api.Configuration;
30  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
31  import org.apache.commons.lang3.StringUtils;
32  
33  /**
34   * Listener in charge of receiving events from the Checker.
35   *
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
38   *
39   */
40  public class CheckstyleCheckerListener extends AutomaticBean implements AuditListener {
41      private final List<File> sourceDirectories;
42  
43      private CheckstyleResults results;
44  
45      private String currentFile;
46  
47      private List<AuditEvent> events;
48  
49      private SeverityLevel severityLevel;
50  
51      private Configuration checkstyleConfiguration;
52  
53      /**
54       * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
55       */
56      public CheckstyleCheckerListener(File sourceDirectory) {
57          this.sourceDirectories = new ArrayList<>();
58          this.sourceDirectories.add(sourceDirectory);
59      }
60      /**
61       * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
62       * @param configuration Checkstyle configuration
63       * @since 2.5
64       */
65      public CheckstyleCheckerListener(File sourceDirectory, Configuration configuration) {
66          this.sourceDirectories = new ArrayList<>();
67          this.sourceDirectories.add(sourceDirectory);
68          this.checkstyleConfiguration = configuration;
69      }
70  
71      /**
72       * @param configuration Checkstyle configuration
73       * @since 2.5
74       */
75      public CheckstyleCheckerListener(Configuration configuration) {
76          this.sourceDirectories = new ArrayList<>();
77          this.checkstyleConfiguration = configuration;
78      }
79  
80      /**
81       * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
82       */
83      public void addSourceDirectory(File sourceDirectory) {
84          this.sourceDirectories.add(sourceDirectory);
85      }
86  
87      /**
88       * @param severityLevel The severity level of the events to listen to.
89       */
90      public void setSeverityLevelFilter(SeverityLevel severityLevel) {
91          this.severityLevel = severityLevel;
92      }
93  
94      /**
95       * @return The severity level of the events to listen to.
96       */
97      public SeverityLevel getSeverityLevelFilter() {
98          return severityLevel;
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public void auditStarted(AuditEvent event) {
104         setResults(new CheckstyleResults());
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public void auditFinished(AuditEvent event) {
110         // do nothing
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public void fileStarted(AuditEvent event) {
116         final String fileName = StringUtils.replace(event.getFileName(), "\\", "/");
117 
118         for (File sourceDirectory : sourceDirectories) {
119             String sourceDirectoryPath = StringUtils.replace(sourceDirectory.getPath(), "\\", "/");
120 
121             if (fileName.startsWith(sourceDirectoryPath + "/")) {
122                 currentFile = StringUtils.substring(fileName, sourceDirectoryPath.length() + 1);
123 
124                 events = getResults().getFileViolations(currentFile);
125 
126                 break;
127             }
128         }
129 
130         if (events == null) {
131             events = new ArrayList<>();
132         }
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public void fileFinished(AuditEvent event) {
138         getResults().setFileViolations(currentFile, events);
139         currentFile = null;
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public void addError(AuditEvent event) {
145         if (SeverityLevel.IGNORE.equals(event.getSeverityLevel())) {
146             return;
147         }
148 
149         if (severityLevel == null || severityLevel.equals(event.getSeverityLevel())) {
150             events.add(event);
151         }
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     public void addException(AuditEvent event, Throwable throwable) {
157         // Do Nothing
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     protected void finishLocalSetup() throws CheckstyleException {
163         // Do Nothing
164     }
165 
166     /**
167      * @return The results of Checkstyle invocation.
168      */
169     public CheckstyleResults getResults() {
170         results.setConfiguration(checkstyleConfiguration);
171         return results;
172     }
173 
174     /**
175      * @param results The results of Checkstyle invocation.
176      */
177     public void setResults(CheckstyleResults results) {
178         this.results = results;
179     }
180 
181     /**
182      * @since 2.5
183      * @return The configuration of Checkstyle to use.
184      */
185     public Configuration getCheckstyleConfiguration() {
186         return checkstyleConfiguration;
187     }
188 
189     /**
190      * @param checkstyleConfiguration The configuration of Checkstyle to use.
191      * @since 2.5
192      */
193     public void setCheckstyleConfiguration(Configuration checkstyleConfiguration) {
194         this.checkstyleConfiguration = checkstyleConfiguration;
195     }
196 }