001package eu.cqse.check.base;
002
003import eu.cqse.check.framework.core.CheckException;
004import eu.cqse.check.framework.core.CheckImplementationBase;
005import eu.cqse.check.framework.core.phase.ECodeViewOption;
006import eu.cqse.check.framework.scanner.ETokenType.ETokenClass;
007import eu.cqse.check.framework.scanner.IToken;
008import eu.cqse.check.framework.util.tokens.TokenUtils;
009
010/**
011 * Base class for checks which inspects all comments in a resource.
012 */
013public abstract class CommentCheckBase extends CheckImplementationBase {
014
015        /** {@inheritDoc} */
016        @Override
017        public void execute() throws CheckException {
018                for (int i = 0; i < context.getTokens(ECodeViewOption.FILTERED_PREPROCESSED).size(); i++) {
019                        IToken token = context.getTokens(ECodeViewOption.FILTERED_PREPROCESSED).get(i);
020
021                        if (token.getType().getTokenClass() == ETokenClass.COMMENT) {
022                                int commentEndLine = 0;
023
024                                if (i == context.getTokens(ECodeViewOption.FILTERED_PREPROCESSED).size() - 1) {
025                                        commentEndLine = TokenUtils.calculateEndLineByCountingLines(token);
026                                } else {
027                                        // Estimate the end line for better performance whenever
028                                        // possible.
029                                        commentEndLine = TokenUtils.estimateEndLineByLookahead(token,
030                                                        context.getTokens(ECodeViewOption.FILTERED_PREPROCESSED).get(i + 1));
031                                }
032
033                                processComment(token, token.getLineNumber() + 1, commentEndLine + 1);
034                        }
035                }
036        }
037
038        /**
039         * When implemented in a subclass, processes the specified comment token, which
040         * runs from the provided start line to the provided end line.
041         *
042         * @param token
043         *            the comment token to process
044         * @param startLine
045         *            the 1-based start line of the comment token in the analyzed
046         *            resource.
047         * @param endLine
048         *            the 1-based end line of the comment token in the analyzed
049         *            resource.
050         */
051        protected abstract void processComment(IToken token, int startLine, int endLine) throws CheckException;
052}