View Javadoc

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