001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 the ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package eu.cqse.check.base; 018 019import java.util.List; 020 021import eu.cqse.check.framework.core.CheckException; 022import eu.cqse.check.framework.scanner.ETokenType; 023import eu.cqse.check.framework.scanner.IToken; 024import eu.cqse.check.framework.shallowparser.TokenStreamUtils; 025 026/** 027 * Base class for checks that create findings on occurrences of token sequences. 028 */ 029public abstract class EntityTokenSequenceFindingCheckBase extends EntityTokenCheckBase { 030 031 /** {@inheritDoc} */ 032 @Override 033 protected void processTokens(List<IToken> tokens) throws CheckException { 034 ETokenType[] findingSequence = getFindingSequence(); 035 List<Integer> indices = TokenStreamUtils.firstTokenOfTypeSequences(tokens, 0, findingSequence); 036 for (int index : indices) { 037 int matchEnd = index + findingSequence.length; 038 createFinding(getFindingMessage(tokens, index, matchEnd), tokens.get(index), tokens.get(matchEnd - 1)); 039 } 040 } 041 042 /** Returns the token type sequence for which findings should be created. */ 043 protected abstract ETokenType[] getFindingSequence(); 044 045 /** 046 * Returns a finding message for the given token type sequence. The sequence 047 * contains the tokens from matchStart inclusively to matchEnd exclusively. 048 */ 049 protected abstract String getFindingMessage(List<IToken> tokens, int matchStart, int matchEnd); 050}