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 mOptions = new DetectorOptions(0, this);
034    /** The detector to use. */
035    private SinglelineDetector mDetector;
036    /** The suppressor to use. */
037    private final CommentSuppressor mSuppressor = 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        mDetector = new SinglelineDetector(mOptions);
050    }
051
052    @Override
053    public void beginTree(DetailAST aRootAST)
054    {
055        mSuppressor.setCurrentContents(getFileContents());
056        mDetector.processLines(Arrays.asList(getLines()));
057    }
058
059    /**
060     * Set the format of the regular expression to match.
061     * @param aFormat the format of the regular expression to match.
062     */
063    public void setFormat(String aFormat)
064    {
065        mOptions.setFormat(aFormat);
066    }
067
068    /**
069     * Set the message to report for a match.
070     * @param aMessage the message to report for a match.
071     */
072    public void setMessage(String aMessage)
073    {
074        mOptions.setMessage(aMessage);
075    }
076
077    /**
078     * Set the minimum number of matches required per file.
079     * @param aMinimum the minimum number of matches required per file.
080     */
081    public void setMinimum(int aMinimum)
082    {
083        mOptions.setMinimum(aMinimum);
084    }
085
086    /**
087     * Set the maximum number of matches required per file.
088     * @param aMaximum the maximum number of matches required per file.
089     */
090    public void setMaximum(int aMaximum)
091    {
092        mOptions.setMaximum(aMaximum);
093    }
094
095    /**
096     * Set whether to ignore case when matching.
097     * @param aIgnore whether to ignore case when matching.
098     */
099    public void setIgnoreCase(boolean aIgnore)
100    {
101        mOptions.setIgnoreCase(aIgnore);
102    }
103
104    /**
105     * Set whether to ignore comments when matching.
106     * @param aIgnore whether to ignore comments when matching.
107     */
108    public void setIgnoreComments(boolean aIgnore)
109    {
110        if (aIgnore) {
111            mOptions.setSuppressor(mSuppressor);
112        }
113        else {
114            mOptions.setSuppressor(NeverSuppress.INSTANCE);
115        }
116    }
117}