001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.regexp;
021
022import com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
026
027/**
028 * Implementation of a check that looks for a single line in Java files.
029 * Supports ignoring comments for matches.
030 * @author Oliver Burn
031 */
032@StatelessCheck
033public class RegexpSinglelineJavaCheck extends AbstractCheck {
034
035    /** The format of the regular expression to match. */
036    private String format = "$.";
037    /** The message to report for a match. */
038    private String message;
039    /** The minimum number of matches required per file. */
040    private int minimum;
041    /** The maximum number of matches required per file. */
042    private int maximum;
043    /** Whether to ignore case when matching. */
044    private boolean ignoreCase;
045    /** Suppress comments. **/
046    private boolean ignoreComments;
047
048    @Override
049    public int[] getDefaultTokens() {
050        return getRequiredTokens();
051    }
052
053    @Override
054    public int[] getAcceptableTokens() {
055        return getRequiredTokens();
056    }
057
058    @Override
059    public int[] getRequiredTokens() {
060        return CommonUtils.EMPTY_INT_ARRAY;
061    }
062
063    @Override
064    public void beginTree(DetailAST rootAST) {
065        MatchSuppressor suppressor = null;
066        if (ignoreComments) {
067            suppressor = new CommentSuppressor(getFileContents());
068        }
069
070        final DetectorOptions options = DetectorOptions.newBuilder()
071            .reporter(this)
072            .compileFlags(0)
073            .suppressor(suppressor)
074            .format(format)
075            .message(message)
076            .minimum(minimum)
077            .maximum(maximum)
078            .ignoreCase(ignoreCase)
079            .build();
080        final SinglelineDetector detector = new SinglelineDetector(options);
081        detector.processLines(getFileContents().getText());
082    }
083
084    /**
085     * Set the format of the regular expression to match.
086     * @param format the format of the regular expression to match.
087     */
088    public void setFormat(String format) {
089        this.format = format;
090    }
091
092    /**
093     * Set the message to report for a match.
094     * @param message the message to report for a match.
095     */
096    public void setMessage(String message) {
097        this.message = message;
098    }
099
100    /**
101     * Set the minimum number of matches required per file.
102     * @param minimum the minimum number of matches required per file.
103     */
104    public void setMinimum(int minimum) {
105        this.minimum = minimum;
106    }
107
108    /**
109     * Set the maximum number of matches required per file.
110     * @param maximum the maximum number of matches required per file.
111     */
112    public void setMaximum(int maximum) {
113        this.maximum = maximum;
114    }
115
116    /**
117     * Set whether to ignore case when matching.
118     * @param ignoreCase whether to ignore case when matching.
119     */
120    public void setIgnoreCase(boolean ignoreCase) {
121        this.ignoreCase = ignoreCase;
122    }
123
124    /**
125     * Set whether to ignore comments when matching.
126     * @param ignore whether to ignore comments when matching.
127     */
128    public void setIgnoreComments(boolean ignore) {
129        ignoreComments = ignore;
130    }
131
132}