001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.filters;
020
021import com.puppycrawl.tools.checkstyle.api.AuditEvent;
022import com.puppycrawl.tools.checkstyle.api.Filter;
023import com.puppycrawl.tools.checkstyle.api.Utils;
024import java.util.regex.Pattern;
025import java.util.regex.PatternSyntaxException;
026
027/**
028 * This filter processes {@link com.puppycrawl.tools.checkstyle.api.AuditEvent}
029 * objects based on the criteria of file, check, module id, line, and
030 * column. It rejects an AuditEvent if the following match:
031 * <ul>
032 *   <li>the event's file name; and</li>
033 *   <li>the check name or the module identifier; and</li>
034 *   <li>(optionally) the event's line is in the filter's line CSV; and</li>
035 *   <li>(optionally) the check's columns is in the filter's column CSV.</li>
036 * </ul>
037 *
038 * @author Rick Giles
039 */
040public class SuppressElement
041    implements Filter
042{
043    /** hash function multiplicand */
044    private static final int HASH_MULT = 29;
045
046    /** the regexp to match file names against */
047    private final Pattern fileRegexp;
048
049    /** the pattern for file names*/
050    private final String filePattern;
051
052    /** the regexp to match check names against */
053    private Pattern checkRegexp;
054
055    /** the pattern for check class names*/
056    private String checkPattern;
057
058    /** module id filter. */
059    private String moduleId;
060
061    /** line number filter */
062    private CSVFilter lineFilter;
063
064    /** CSV for line number filter */
065    private String linesCSV;
066
067    /** column number filter */
068    private CSVFilter columnFilter;
069
070    /** CSV for column number filter */
071    private String columnsCSV;
072
073    /**
074     * Constructs a <code>SuppressElement</code> for a
075     * file name pattern. Must either call {@link #setColumns(String)} or
076     * {@link #setModuleId(String)} before using this object.
077     * @param files regular expression for names of filtered files.
078     * @throws PatternSyntaxException if there is an error.
079     */
080    public SuppressElement(String files)
081        throws PatternSyntaxException
082    {
083        filePattern = files;
084        fileRegexp = Utils.getPattern(files);
085    }
086
087    /**
088     * Set the check class pattern.
089     * @param checks regular expression for filtered check classes.
090     */
091    public void setChecks(final String checks)
092    {
093        checkPattern = checks;
094        checkRegexp = Utils.getPattern(checks);
095    }
096
097    /**
098     * Set the module id for filtering. Cannot be null.
099     * @param moduleId the id
100     */
101    public void setModuleId(final String moduleId)
102    {
103        this.moduleId = moduleId;
104    }
105    /**
106     * Sets the CSV values and ranges for line number filtering.
107     * E.g. "1,7-15,18".
108     * @param lines CSV values and ranges for line number filtering.
109     */
110    public void setLines(String lines)
111    {
112        linesCSV = lines;
113        if (lines != null) {
114            lineFilter = new CSVFilter(lines);
115        }
116        else {
117            lineFilter = null;
118        }
119    }
120
121    /**
122     * Sets the CSV values and ranges for column number filtering.
123     *  E.g. "1,7-15,18".
124     * @param columns CSV values and ranges for column number filtering.
125     */
126    public void setColumns(String columns)
127    {
128        columnsCSV = columns;
129        if (columns != null) {
130            columnFilter = new CSVFilter(columns);
131        }
132        else {
133            columnFilter = null;
134        }
135    }
136
137    /** {@inheritDoc} */
138    @Override
139    public boolean accept(AuditEvent event)
140    {
141        // file and check match?
142        if ((event.getFileName() == null)
143                || !fileRegexp.matcher(event.getFileName()).find()
144                || (event.getLocalizedMessage() == null)
145                || ((moduleId != null) && !moduleId.equals(event
146                        .getModuleId()))
147                || ((checkRegexp != null) && !checkRegexp.matcher(
148                        event.getSourceName()).find()))
149        {
150            return true;
151        }
152
153        // reject if no line/column matching
154        if ((lineFilter == null) && (columnFilter == null)) {
155            return false;
156        }
157
158        if (lineFilter != null && lineFilter.accept(event.getLine())) {
159            return false;
160        }
161
162        if (columnFilter != null && columnFilter.accept(event.getColumn())) {
163            return false;
164        }
165        return true;
166    }
167
168    @Override
169    public String toString()
170    {
171        return "SuppressElement[files=" + filePattern + ",checks="
172            + checkPattern + ",lines=" + linesCSV + ",columns="
173            + columnsCSV + "]";
174    }
175
176    @Override
177    public int hashCode()
178    {
179        int result = HASH_MULT * filePattern.hashCode();
180        if (checkPattern != null) {
181            result = HASH_MULT * result + checkPattern.hashCode();
182        }
183        if (moduleId != null) {
184            result = HASH_MULT * result + moduleId.hashCode();
185        }
186        if (linesCSV != null) {
187            result = HASH_MULT * result + linesCSV.hashCode();
188        }
189        if (columnsCSV != null) {
190            result = HASH_MULT * result + columnsCSV.hashCode();
191        }
192        return result;
193    }
194
195    @Override
196    public boolean equals(Object object)
197    {
198        if (object instanceof SuppressElement) {
199            final SuppressElement other = (SuppressElement) object;
200
201            // same file pattern?
202            if (!this.filePattern.equals(other.filePattern)) {
203                return false;
204            }
205
206            // same check pattern?
207            if (checkPattern != null) {
208                if (!checkPattern.equals(other.checkPattern)) {
209                    return false;
210                }
211            }
212            else if (other.checkPattern != null) {
213                return false;
214            }
215
216            // same module id?
217            if (moduleId != null) {
218                if (!moduleId.equals(other.moduleId)) {
219                    return false;
220                }
221            }
222            else if (other.moduleId != null) {
223                return false;
224            }
225
226            // same line number filter?
227            if (lineFilter != null) {
228                if (!lineFilter.equals(other.lineFilter)) {
229                    return false;
230                }
231            }
232            else if (other.lineFilter != null) {
233                return false;
234            }
235
236            // same column number filter?
237            if (columnFilter != null) {
238                if (!columnFilter.equals(other.columnFilter)) {
239                    return false;
240                }
241            }
242            else if (other.columnFilter != null) {
243                return false;
244            }
245
246            // everything is the same
247            return true;
248        }
249        return false;
250    }
251}