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;
020
021import java.util.regex.Pattern;
022
023import com.puppycrawl.tools.checkstyle.api.Check;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * <p>
029 * A check for TODO comments. To check for other patterns in Java comments, set
030 * property format.
031 * </p>
032 * <p>
033 * An example of how to configure the check is:
034 * </p>
035 *
036 * <pre>
037 * &lt;module name="TodoComment"/&gt;
038 * </pre>
039 * <p>
040 * An example of how to configure the check for comments that contain
041 * <code>WARNING</code> is:
042 * </p>
043 *
044 * <pre>
045 * &lt;module name="TodoComment"&gt;
046 *    &lt;property name="format" value="WARNING"/&gt;
047 * &lt;/module&gt;
048 * </pre>
049 * @author Oliver Burn
050 * @author Baratali Izmailov
051 * @version 1.0
052 */
053public class TodoCommentCheck
054        extends Check
055{
056    /**
057     * Format of todo comment.
058     */
059    private String format = "TODO:";
060
061    /**
062     * Regular expression pattern compiled from format.
063     */
064    private Pattern regexp = Pattern.compile(format);
065
066    @Override
067    public boolean isCommentNodesRequired()
068    {
069        return true;
070    }
071
072    /**
073     * Setter for todo comment format.
074     * @param format format of todo comment.
075     */
076    public void setFormat(String format)
077    {
078        this.format = format;
079        regexp = Pattern.compile(format);
080    }
081
082    @Override
083    public int[] getDefaultTokens()
084    {
085        return new int[] {TokenTypes.COMMENT_CONTENT };
086    }
087
088    @Override
089    public void visitToken(DetailAST ast)
090    {
091        final String[] lines = ast.getText().split("\n");
092
093        for (int i = 0; i < lines.length; i++) {
094            if (regexp.matcher(lines[i]).find()) {
095                log(ast.getLineNo() + i, "todo.match", format);
096            }
097        }
098    }
099}