KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > filters > SuppressElement


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.filters;
20
21 import java.util.regex.Pattern JavaDoc;
22 import java.util.regex.PatternSyntaxException JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
25 import com.puppycrawl.tools.checkstyle.api.Filter;
26 import com.puppycrawl.tools.checkstyle.api.Utils;
27
28 /**
29  * This filter processes {@link com.puppycrawl.tools.checkstyle.api.AuditEvent}
30  * objects based on the criteria of file, check, module id, line, and
31  * column. It rejects an AuditEvent if the following match:
32  * <ul>
33  * <li>the event's file name; and
34  * <li>the check name or the module identifier; and
35  * <li>(optionally) the event's line is in the filter's line CSV; and
36  * <li>(optionally) the check's columns is in the filter's column CSV.
37  * </ul>
38  *
39  * @author Rick Giles
40  */

41 public class SuppressElement
42     implements Filter
43 {
44     /** hash function multiplicand */
45     private static final int HASH_MULT = 29;
46
47     /** the regexp to match file names against */
48     private final Pattern JavaDoc mFileRegexp;
49
50     /** the pattern for file names*/
51     private final String JavaDoc mFilePattern;
52
53     /** the regexp to match check names against */
54     private Pattern JavaDoc mCheckRegexp;
55
56     /** the pattern for check class names*/
57     private String JavaDoc mCheckPattern;
58
59     /** module id filter. */
60     private String JavaDoc mModuleId;
61
62     /** line number filter */
63     private CSVFilter mLineFilter;
64
65     /** CSV for line number filter */
66     private String JavaDoc mLinesCSV;
67
68     /** column number filter */
69     private CSVFilter mColumnFilter;
70
71     /** CSV for column number filter */
72     private String JavaDoc mColumnsCSV;
73
74     /**
75      * Constructs a <code>SuppressElement</code> for a
76      * file name pattern. Must either call {@link #setColumns(String)} or
77      * {@link #setModuleId(String)} before using this object.
78      * @param aFiles regular expression for names of filtered files.
79      * @throws PatternSyntaxException if there is an error.
80      */

81     public SuppressElement(String JavaDoc aFiles)
82         throws PatternSyntaxException JavaDoc
83     {
84         mFilePattern = aFiles;
85         mFileRegexp = Utils.getPattern(aFiles);
86     }
87
88     /**
89      * Set the check class pattern.
90      * @param aChecks regular expression for filtered check classes.
91      */

92     public void setChecks(final String JavaDoc aChecks)
93     {
94         mCheckPattern = aChecks;
95         mCheckRegexp = Utils.getPattern(aChecks);
96     }
97
98     /**
99      * Set the module id for filtering. Cannot be null.
100      * @param aModuleId the id
101      */

102     public void setModuleId(final String JavaDoc aModuleId)
103     {
104         mModuleId = aModuleId;
105     }
106     /**
107      * Sets the CSV values and ranges for line number filtering.
108      * E.g. "1,7-15,18".
109      * @param aLines CSV values and ranges for line number filtering.
110      */

111     public void setLines(String JavaDoc aLines)
112     {
113         mLinesCSV = aLines;
114         if (aLines != null) {
115             mLineFilter = new CSVFilter(aLines);
116         }
117         else {
118             mLineFilter = null;
119         }
120     }
121
122     /**
123      * Sets the CSV values and ranges for column number filtering.
124      * E.g. "1,7-15,18".
125      * @param aColumns CSV values and ranges for column number filtering.
126      */

127     public void setColumns(String JavaDoc aColumns)
128     {
129         mColumnsCSV = aColumns;
130         if (aColumns != null) {
131             mColumnFilter = new CSVFilter(aColumns);
132         }
133         else {
134             mColumnFilter = null;
135         }
136     }
137
138     /** {@inheritDoc} */
139     public boolean accept(AuditEvent aEvent)
140     {
141         // file and check match?
142
if ((aEvent.getFileName() == null)
143                 || !mFileRegexp.matcher(aEvent.getFileName()).find()
144                 || (aEvent.getLocalizedMessage() == null)
145                 || ((mModuleId != null) && !mModuleId.equals(aEvent
146                         .getModuleId()))
147                 || ((mCheckRegexp != null) && !mCheckRegexp.matcher(
148                         aEvent.getSourceName()).find()))
149         {
150             return true;
151         }
152
153         // reject if no line/column matching
154
if ((mLineFilter == null) && (mColumnFilter == null)) {
155             return false;
156         }
157
158         // reject if line matches a line CSV value.
159
if (mLineFilter != null) {
160             final Integer JavaDoc line = new Integer JavaDoc(aEvent.getLine());
161             if (mLineFilter.accept(line)) {
162                 return false;
163             }
164         }
165
166         // reject if column matches a column CSV value.
167
if (mColumnFilter != null) {
168             final Integer JavaDoc column = new Integer JavaDoc(aEvent.getColumn());
169             if (mColumnFilter.accept(column)) {
170                 return false;
171             }
172         }
173         return true;
174     }
175
176     /** {@inheritDoc} */
177     public String JavaDoc toString()
178     {
179         return "SupressElement[files=" + mFilePattern + ",checks="
180             + mCheckPattern + ",lines=" + mLinesCSV + ",columns="
181             + mColumnsCSV + "]";
182     }
183
184     /** {@inheritDoc} */
185     public int hashCode()
186     {
187         int result = HASH_MULT * mFilePattern.hashCode();
188         if (mCheckPattern != null) {
189             result = HASH_MULT * result + mCheckPattern.hashCode();
190         }
191         if (mModuleId != null) {
192             result = HASH_MULT * result + mModuleId.hashCode();
193         }
194         if (mLinesCSV != null) {
195             result = HASH_MULT * result + mLinesCSV.hashCode();
196         }
197         if (mColumnsCSV != null) {
198             result = HASH_MULT * result + mColumnsCSV.hashCode();
199         }
200         return result;
201     }
202
203     /** {@inheritDoc} */
204     public boolean equals(Object JavaDoc aObject)
205     {
206         if (aObject instanceof SuppressElement) {
207             final SuppressElement other = (SuppressElement) aObject;
208
209             // same file pattern?
210
if (!this.mFilePattern.equals(other.mFilePattern)) {
211                 return false;
212             }
213
214             // same check pattern?
215
if (mCheckPattern != null) {
216                 if (!mCheckPattern.equals(other.mCheckPattern)) {
217                     return false;
218                 }
219             }
220             else if (other.mCheckPattern != null) {
221                 return false;
222             }
223
224             // same module id?
225
if (mModuleId != null) {
226                 if (!mModuleId.equals(other.mModuleId)) {
227                     return false;
228                 }
229             }
230             else if (other.mModuleId != null) {
231                 return false;
232             }
233
234             // same line number filter?
235
if (mLineFilter != null) {
236                 if (!mLineFilter.equals(other.mLineFilter)) {
237                     return false;
238                 }
239             }
240             else if (other.mLineFilter != null) {
241                 return false;
242             }
243
244             // same column number filter?
245
if (mColumnFilter != null) {
246                 if (!mColumnFilter.equals(other.mColumnFilter)) {
247                     return false;
248                 }
249             }
250             else if (other.mColumnFilter != null) {
251                 return false;
252             }
253
254             // everything is the same
255
return true;
256         }
257         return false;
258     }
259 }
260
Popular Tags