001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2009-2017 CQSE GmbH                                        |
004|                                                                          |
005+-------------------------------------------------------------------------*/
006package eu.cqse.check.base;
007
008import java.util.List;
009
010import eu.cqse.check.framework.core.CheckException;
011import eu.cqse.check.framework.scanner.IToken;
012import eu.cqse.check.framework.util.tokens.TokenPattern;
013import eu.cqse.check.framework.util.tokens.TokenPatternMatch;
014
015/**
016 * Base class for tokens that create findings if a token pattern matches the
017 * processed tokens.
018 */
019public abstract class EntityTokenPatternCheckBase extends EntityTokenCheckBase {
020
021        /** {@inheritDoc} */
022        @Override
023        protected void processTokens(List<IToken> tokens) throws CheckException {
024                TokenPattern pattern = getFindingPattern();
025                for (TokenPatternMatch match : pattern.findAll(tokens)) {
026                        List<IToken> matchedTokens = match.groupTokens(0);
027                        String message = getFindingMessage(matchedTokens);
028                        createFinding(message, matchedTokens);
029                }
030        }
031
032        /**
033         * Returns the pattern that the processed tokens are matched against. The
034         * pattern must define the group index 0 with {@link TokenPattern#group(int)}.
035         * All tokens that are matched within this group are passed to
036         * {@link #getFindingMessage(List)} and a finding will be created for them.
037         */
038        protected abstract TokenPattern getFindingPattern();
039
040        /** Creates a finding messages for the given tokens. */
041        protected abstract String getFindingMessage(List<IToken> tokens);
042}