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 com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.FileContents;
023import com.puppycrawl.tools.checkstyle.api.TextBlock;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * <p>
029 * A check for TODO comments.
030 * Actually it is a generic {@link java.util.regex.Pattern regular expression}
031 * matcher on Java comments.
032 * To check for other patterns in Java comments, set property format.
033 * </p>
034 * <p>
035 * An example of how to configure the check is:
036 * </p>
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>WARNING</code> is:
043 * </p>
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 * @version 1.0
051 */
052public class TodoCommentCheck
053    extends AbstractFormatCheck
054{
055    /**
056     * Creates a new <code>TodoCommentCheck</code> instance.
057     */
058    public TodoCommentCheck()
059    {
060        super("TODO:"); // the empty language
061    }
062
063    @Override
064    public int[] getDefaultTokens()
065    {
066        return new int[0];
067    }
068
069    @Override
070    public void beginTree(DetailAST aRootAST)
071    {
072        final FileContents contents = getFileContents();
073        checkCppComments(contents);
074        checkBadComments(contents);
075    }
076
077    /**
078     * Checks the C++ comments for todo expressions.
079     * @param aContents the <code>FileContents</code>
080     */
081    private void checkCppComments(FileContents aContents)
082    {
083        final Map<Integer, TextBlock> comments = aContents.getCppComments();
084        for (Map.Entry<Integer, TextBlock> entry : comments.entrySet()) {
085            final String cmt = entry.getValue().getText()[0];
086            if (getRegexp().matcher(cmt).find()) {
087                log(entry.getKey().intValue(), "todo.match", getFormat());
088            }
089        }
090    }
091
092    /**
093     * Checks the C-style comments for todo expressions.
094     * @param aContents the <code>FileContents</code>
095     */
096    private void checkBadComments(FileContents aContents)
097    {
098        final Map<Integer, List<TextBlock>> allComments = aContents
099                .getCComments();
100        for (Map.Entry<Integer, List<TextBlock>> entry : allComments.entrySet())
101        {
102            for (TextBlock line : entry.getValue()) {
103                final String[] cmt = line.getText();
104                for (int i = 0; i < cmt.length; i++) {
105                    if (getRegexp().matcher(cmt[i]).find()) {
106                        log(entry.getKey().intValue() + i, "todo.match",
107                                getFormat());
108                    }
109                }
110            }
111        }
112    }
113}