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.checks.regexp;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import java.util.Arrays;
024
025/**
026 * Implementation of a check that looks for a single line in Java files.
027 * Supports ignoring comments for matches.
028 * @author Oliver Burn
029 */
030public class RegexpSinglelineJavaCheck extends Check
031{
032    /** The detection options to use. */
033    private DetectorOptions options = new DetectorOptions(0, this);
034    /** The detector to use. */
035    private SinglelineDetector detector;
036    /** The suppressor to use. */
037    private final CommentSuppressor suppressor = new CommentSuppressor();
038
039    @Override
040    public int[] getDefaultTokens()
041    {
042        return new int[0];
043    }
044
045    @Override
046    public void init()
047    {
048        super.init();
049        detector = new SinglelineDetector(options);
050    }
051
052    @Override
053    public void beginTree(DetailAST rootAST)
054    {
055        suppressor.setCurrentContents(getFileContents());
056        detector.processLines(Arrays.asList(getLines()));
057    }
058
059    /**
060     * Set the format of the regular expression to match.
061     * @param format the format of the regular expression to match.
062     */
063    public void setFormat(String format)
064    {
065        options.setFormat(format);
066    }
067
068    /**
069     * Set the message to report for a match.
070     * @param message the message to report for a match.
071     */
072    public void setMessage(String message)
073    {
074        options.setMessage(message);
075    }
076
077    /**
078     * Set the minimum number of matches required per file.
079     * @param minimum the minimum number of matches required per file.
080     */
081    public void setMinimum(int minimum)
082    {
083        options.setMinimum(minimum);
084    }
085
086    /**
087     * Set the maximum number of matches required per file.
088     * @param maximum the maximum number of matches required per file.
089     */
090    public void setMaximum(int maximum)
091    {
092        options.setMaximum(maximum);
093    }
094
095    /**
096     * Set whether to ignore case when matching.
097     * @param ignore whether to ignore case when matching.
098     */
099    public void setIgnoreCase(boolean ignore)
100    {
101        options.setIgnoreCase(ignore);
102    }
103
104    /**
105     * Set whether to ignore comments when matching.
106     * @param ignore whether to ignore comments when matching.
107     */
108    public void setIgnoreComments(boolean ignore)
109    {
110        if (ignore) {
111            options.setSuppressor(suppressor);
112        }
113        else {
114            options.setSuppressor(NeverSuppress.INSTANCE);
115        }
116    }
117}