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