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