Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
28   131   14   7
16   64   0.5   4
4     3.5  
1    
 
  RegexpHeaderChecker       Line # 33 97.9% 0.9791667
14 28 14 14 0.5
 
  ( 10 of 12)
 
1    ////////////////////////////////////////////////////////////////////////////////
2    // checkstyle: Checks Java source code for adherence to a set of rules.
3    // Copyright (C) 2001-2005 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   
20    package com.puppycrawl.tools.checkstyle.checks.header;
21   
22    import java.util.Arrays;
23    import java.util.regex.Pattern;
24   
25    /**
26    * Isolates the check funtionality in RegexpHeaderCheck in an external class.
27    * This makes it easier to use the funtionality both in a Java
28    * {@link com.puppycrawl.tools.checkstyle.api.Check} and in
29    * a {@link com.puppycrawl.tools.checkstyle.api.FileSetCheck}.
30    *
31    * @author lk
32    */
 
33    class RegexpHeaderChecker
34    {
35    /** the lines of the header file. */
36    private final String[] mHeaderLines;
37   
38    /** the compiled regular expressions */
39    private Pattern[] mHeaderRegexps;
40   
41    /** the header lines to repeat (0 or more) in the check, sorted. */
42    private int[] mMultiLines;
43   
44    /** A monitor for the violations that are detected. */
45    private final HeaderViolationMonitor mViolationObserver;
46   
47    /**
48    * Creates a new instance.
49    *
50    * @param aRegexpHeaderInfo check parameters
51    * @param aViolationObserver error reporting strategy object
52    */
 
53  12 toggle RegexpHeaderChecker(
54    RegexpHeaderInfo aRegexpHeaderInfo,
55    HeaderViolationMonitor aViolationObserver)
56    {
57  12 mHeaderLines = aRegexpHeaderInfo.getHeaderLines();
58  12 mHeaderRegexps = aRegexpHeaderInfo.geHeaderRegexps();
59  12 mMultiLines = aRegexpHeaderInfo.getMultLines();
60  12 mViolationObserver = aViolationObserver;
61    }
62   
63    /**
64    * Checks the lines of an individual file against the
65    * {@link #getHeaderLines() header lines}.
66    *
67    * @param aLines the lines of an individual file
68    */
 
69  21 toggle void checkLines(final String[] aLines)
70    {
71  21 final int headerSize = mHeaderLines.length;
72  21 final int fileSize = aLines.length;
73   
74  21 if (headerSize - mMultiLines.length > fileSize) {
75  1 mViolationObserver.reportHeaderMissing();
76    }
77    else {
78  20 int headerLineNo = 0;
79  20 int i;
80  103 for (i = 0; (headerLineNo < headerSize) && (i < fileSize); i++) {
81  87 final String line = aLines[i];
82  87 boolean isMatch = isMatch(line, headerLineNo);
83  110 while (!isMatch && isMultiLine(headerLineNo)) {
84  23 headerLineNo++;
85  23 isMatch = (headerLineNo == headerSize)
86    || isMatch(line, headerLineNo);
87    }
88  87 if (!isMatch) {
89  4 mViolationObserver.reportHeaderMismatch(
90    i + 1, mHeaderLines[headerLineNo]);
91  4 break; // stop checking
92    }
93  83 if (!isMultiLine(headerLineNo)) {
94  64 headerLineNo++;
95    }
96    }
97  20 if (i == fileSize) {
98    // if file finished, but we have at least one non-multi-line
99    // header isn't completed
100  2 for (; headerLineNo < headerSize; headerLineNo++) {
101  2 if (!isMultiLine(headerLineNo)) {
102  1 mViolationObserver.reportHeaderMissing();
103  1 break;
104    }
105    }
106    }
107    }
108    }
109   
110    /**
111    * Checks if a code line matches the required header line.
112    * @param aLine the code line
113    * @param aHeaderLineNo the header line number.
114    * @return true if and only if the line matches the required header line.
115    */
 
116  108 toggle private boolean isMatch(String aLine, int aHeaderLineNo)
117    {
118  108 return mHeaderRegexps[aHeaderLineNo].matcher(aLine).find();
119    }
120   
121    /**
122    * @param aLineNo a line number
123    * @return if <code>aLineNo</code> is one of the repeat header lines.
124    */
 
125  112 toggle private boolean isMultiLine(int aLineNo)
126    {
127  112 return (Arrays.binarySearch(mMultiLines, aLineNo + 1) >= 0);
128    }
129   
130   
131    }