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