1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package com.puppycrawl.tools.checkstyle.checks.header; |
21 | |
|
22 | |
import java.util.Arrays; |
23 | |
|
24 | |
import java.io.File; |
25 | |
import java.util.List; |
26 | |
import java.util.regex.Pattern; |
27 | |
import java.util.regex.PatternSyntaxException; |
28 | |
|
29 | |
import org.apache.commons.beanutils.ConversionException; |
30 | |
|
31 | |
import com.google.common.collect.Lists; |
32 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 11 | public class RegexpHeaderCheck extends AbstractHeaderCheck |
43 | |
{ |
44 | |
|
45 | 1 | private static final int[] EMPTY_INT_ARRAY = new int[0]; |
46 | |
|
47 | |
|
48 | 11 | private final List<Pattern> mHeaderRegexps = Lists.newArrayList(); |
49 | |
|
50 | |
|
51 | 11 | private int[] mMultiLines = EMPTY_INT_ARRAY; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public void setMultiLines(int[] aList) |
58 | |
{ |
59 | 6 | if ((aList == null) || (aList.length == 0)) { |
60 | 0 | mMultiLines = EMPTY_INT_ARRAY; |
61 | 0 | return; |
62 | |
} |
63 | |
|
64 | 6 | mMultiLines = new int[aList.length]; |
65 | 6 | System.arraycopy(aList, 0, mMultiLines, 0, aList.length); |
66 | 6 | Arrays.sort(mMultiLines); |
67 | 6 | } |
68 | |
|
69 | |
@Override |
70 | |
protected void processFiltered(File aFile, List<String> aLines) |
71 | |
{ |
72 | 10 | final int headerSize = getHeaderLines().size(); |
73 | 10 | final int fileSize = aLines.size(); |
74 | |
|
75 | 10 | if (headerSize - mMultiLines.length > fileSize) { |
76 | 0 | log(1, "header.missing"); |
77 | |
} |
78 | |
else { |
79 | 10 | int headerLineNo = 0; |
80 | |
int i; |
81 | 63 | for (i = 0; (headerLineNo < headerSize) && (i < fileSize); i++) { |
82 | 56 | final String line = aLines.get(i); |
83 | 56 | boolean isMatch = isMatch(line, headerLineNo); |
84 | 68 | while (!isMatch && isMultiLine(headerLineNo)) { |
85 | 12 | headerLineNo++; |
86 | 12 | isMatch = (headerLineNo == headerSize) |
87 | |
|| isMatch(line, headerLineNo); |
88 | |
} |
89 | 56 | if (!isMatch) { |
90 | 3 | log(i + 1, "header.mismatch", getHeaderLines().get( |
91 | |
headerLineNo)); |
92 | 3 | break; |
93 | |
} |
94 | 53 | if (!isMultiLine(headerLineNo)) { |
95 | 38 | headerLineNo++; |
96 | |
} |
97 | |
} |
98 | 10 | if (i == fileSize) { |
99 | |
|
100 | |
|
101 | 3 | for (; headerLineNo < headerSize; headerLineNo++) { |
102 | 2 | if (!isMultiLine(headerLineNo)) { |
103 | 1 | log(1, "header.missing"); |
104 | 1 | break; |
105 | |
} |
106 | |
} |
107 | |
} |
108 | |
} |
109 | 10 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
private boolean isMatch(String aLine, int aHeaderLineNo) |
118 | |
{ |
119 | 66 | return mHeaderRegexps.get(aHeaderLineNo).matcher(aLine).find(); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private boolean isMultiLine(int aLineNo) |
127 | |
{ |
128 | 70 | return (Arrays.binarySearch(mMultiLines, aLineNo + 1) >= 0); |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
protected void postprocessHeaderLines() |
133 | |
{ |
134 | 11 | final List<String> headerLines = getHeaderLines(); |
135 | 11 | mHeaderRegexps.clear(); |
136 | 11 | for (String line : headerLines) { |
137 | |
try { |
138 | |
|
139 | 63 | mHeaderRegexps.add(Utils.getPattern(line)); |
140 | |
} |
141 | 1 | catch (final PatternSyntaxException ex) { |
142 | 1 | throw new ConversionException("line " |
143 | |
+ (mHeaderRegexps.size() + 1) |
144 | |
+ " in header specification" |
145 | |
+ " is not a regular expression"); |
146 | 62 | } |
147 | |
} |
148 | 10 | } |
149 | |
|
150 | |
} |