Coverage Report - com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector
 
Classes in this File Line Coverage Branch Coverage Complexity
MultilineDetector
89%
26/29
58%
7/12
2.4
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 package com.puppycrawl.tools.checkstyle.checks.regexp;
 20  
 
 21  
 import java.util.regex.Matcher;
 22  
 import com.puppycrawl.tools.checkstyle.api.FileText;
 23  
 import com.puppycrawl.tools.checkstyle.api.LineColumn;
 24  
 
 25  
 /**
 26  
  * A detector that matches across multiple lines.
 27  
  * @author oliver
 28  
  */
 29  
 class MultilineDetector
 30  
 {
 31  
     /** The detection options to use. */
 32  
     private final DetectorOptions mOptions;
 33  
     /** Tracks the number of matches. */
 34  
     private int mCurrentMatches;
 35  
     /** The mMatcher */
 36  
     private Matcher mMatcher;
 37  
     /** The file text content */
 38  
     private FileText mText;
 39  
 
 40  
     /**
 41  
      * Creates an instance.
 42  
      * @param aOptions the options to use.
 43  
      */
 44  
     public MultilineDetector(DetectorOptions aOptions)
 45  5
     {
 46  5
         mOptions = aOptions;
 47  5
     }
 48  
 
 49  
     /**
 50  
      * Processes an entire text file looking for matches.
 51  
      * @param aText the text to process
 52  
      */
 53  
     public void processLines(FileText aText)
 54  
     {
 55  5
         mText = aText;
 56  5
         resetState();
 57  5
         mMatcher = mOptions.getPattern().matcher(mText.getFullText());
 58  5
         findMatch();
 59  5
         finish();
 60  5
     }
 61  
 
 62  
     /** recursive method that finds the matches. */
 63  
     private void findMatch()
 64  
     {
 65  11
         final boolean foundMatch = mMatcher.find();
 66  11
         if (!foundMatch) {
 67  5
             return;
 68  
         }
 69  
 
 70  6
         final LineColumn start = mText.lineColumn(mMatcher.start());
 71  6
         final LineColumn end = mText.lineColumn(mMatcher.end());
 72  
 
 73  6
         if (!mOptions.getSuppressor().shouldSuppress(start.getLine(),
 74  
                 start.getColumn(), end.getLine(), end.getColumn()))
 75  
         {
 76  6
             mCurrentMatches++;
 77  6
             if (mCurrentMatches > mOptions.getMaximum()) {
 78  6
                 if ("".equals(mOptions.getMessage())) {
 79  5
                     mOptions.getReporter().log(start.getLine(),
 80  
                             "regexp.exceeded", mMatcher.pattern().toString());
 81  
                 }
 82  
                 else {
 83  1
                     mOptions.getReporter()
 84  
                             .log(start.getLine(), mOptions.getMessage());
 85  
                 }
 86  
             }
 87  
         }
 88  6
         findMatch();
 89  6
     }
 90  
     /** Perform processing at the end of a set of lines. */
 91  
     private void finish()
 92  
     {
 93  5
         if (mCurrentMatches < mOptions.getMinimum()) {
 94  0
             if ("".equals(mOptions.getMessage())) {
 95  0
                 mOptions.getReporter().log(0, "regexp.minimum",
 96  
                         mOptions.getMinimum(), mOptions.getFormat());
 97  
             }
 98  
             else {
 99  0
                 mOptions.getReporter().log(0, mOptions.getMessage());
 100  
             }
 101  
         }
 102  5
     }
 103  
 
 104  
     /**
 105  
      * Reset the state of the detector.
 106  
      */
 107  
     private void resetState()
 108  
     {
 109  5
         mCurrentMatches = 0;
 110  5
     }
 111  
 }