new feature 'headerfile regular expression support, rfe #499259'

This commit is contained in:
Lars Kühne 2002-02-07 20:34:11 +00:00
parent fdd9ae8297
commit 6fc674d4bf
7 changed files with 69 additions and 7 deletions

View File

@ -133,9 +133,14 @@ This task is included in the checkstyle distribution.</p>
<td valign="top">Specifies the file containing the header lines. Default is to not check.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">headerLinesRegexp</td>
<td valign="top">Specifies whether to interpret each line in the headerFile as a regular expression. Default is <span class="default">&quot;false&quot;</span>.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">headerIgnoreLine</td>
<td valign="top">Specifies the line in the header to ignore when comparing. Default it to not ignore any line.</td>
<td valign="top">Specifies the line in the header to ignore when comparing. Default is to not ignore any line.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>

View File

@ -118,9 +118,13 @@ This command line tool is included in the checkstyle distribution.</p>
<td valign="top">checkstyle.header.file</td>
<td valign="top">Specifies the file containing the header lines. Default is to not check.</td>
</tr>
<tr>
<td valign="top">checkstyle.header.regexp</td>
<td valign="top">Specifies whether to interpret each line in the checkstyle.header.file as a regular expression. Default is <span class="default">&quot;false&quot;</span>.</td>
</tr>
<tr>
<td valign="top">checkstyle.header.ignoreline</td>
<td valign="top">Specifies the line in the header to ignore when comparing. Default it to not ignore any line.</td>
<td valign="top">Specifies the line in the header to ignore when comparing. Default is to not ignore any line.</td>
</tr>
<tr>
<td valign="top">checkstyle.javadoc.scope</td>

View File

@ -225,6 +225,21 @@ line 4: ///////////////////////////////////////////////////////////////////////
<p>Since the year information will change over time, you can tell checkstyle to ignore line 3.</p>
</div>
<br>
<div class="tip">
<h4 class="tip">Tip</h4>
<p>Checkstyle also supports interpreting each header line as a regular expression. For example, consider the following header when regular expression checking is turned on:</p>
<pre>
line 1: /{71}
line 2: // checkstyle: Checks Java source code for adherence to a set of rules\.
line 3: // Copyright \(C\) \d\d\d\d Oliver Burn
line 4: // Last modification by \$Author.*\$
line 5: /{71}
</pre>
<p>Lines 1 and 5 demonstrate a more compact notation for 71 '/' characters. Line 3 enforces that the copyright notice includes a four digit year. Line 4 is an example how to enforce revision control keywords in a file header.</p>
</div>
<!-- formatters -->
<h2>Output Format</h2>

View File

@ -246,6 +246,12 @@ public class CheckStyleTask
}
}
/** @param aIsRegexp whether to interpret header lines as regexp */
public void setHeaderLinesRegexp(boolean aIsRegexp)
{
mConfig.setHeaderLinesRegexp(aIsRegexp);
}
/** @param aFail whether to fail if a violation is found **/
public void setFailOnViolation(boolean aFail)
{

View File

@ -143,6 +143,8 @@ public class Configuration
/** the header lines to check for **/
private String[] mHeaderLines = {};
/** interpret the header lines as RE */
private boolean mHeaderLinesRegexp = false;
/** line number to ignore in header **/
private int mHeaderIgnoreLineNo = -1;
@ -217,6 +219,8 @@ public class Configuration
setHeaderIgnoreLineNo(
getIntProperty(aProps, aLog, HEADER_IGNORE_LINE_PROP,
mHeaderIgnoreLineNo));
setHeaderLinesRegexp(getBooleanProperty(
aProps, HEADER_LINES_REGEXP_PROP, mHeaderLinesRegexp));
final String fname = aProps.getProperty(HEADER_FILE_PROP);
if (fname != null) {
@ -446,6 +450,13 @@ public class Configuration
return mHeaderLines;
}
/** @return if lines in header file are regular expressions */
public boolean getHeaderLinesRegexp()
{
return mHeaderLinesRegexp;
}
/** @return line number to ignore in header **/
public int getHeaderIgnoreLineNo()
{
@ -689,6 +700,14 @@ public class Configuration
mHeaderLines = (String[]) lines.toArray(new String[0]);
}
/**
* @param aHeaderLinesRegexp lines in header file are regular expressions
*/
public void setHeaderLinesRegexp(boolean aHeaderLinesRegexp)
{
mHeaderLinesRegexp = aHeaderLinesRegexp;
}
/**
* @param aHeaderIgnoreLineNo line number to ignore in header
*/

View File

@ -60,6 +60,8 @@ interface Defn
String HEADER_FILE_PROP = "checkstyle.header.file";
/** property name for line in header file to ignore **/
String HEADER_IGNORE_LINE_PROP = "checkstyle.header.ignoreline";
/** property name for header file line interpretation as regexps */
String HEADER_LINES_REGEXP_PROP = "checkstyle.header.regexp";
/** property name for visibility scope where Javadoc is checked **/
String JAVADOC_CHECKSCOPE_PROP = "checkstyle.javadoc.scope";
/** property name for requiring package documentation */

View File

@ -1003,13 +1003,24 @@ class Verifier
/** checks that a file contains a valid header **/
private void checkHeader()
{
if (mConfig.getHeaderLines().length > mLines.length) {
if (mConfig.getHeaderLines().length > mLines.length)
{
log(1, "Missing a header - not enough lines in file.");
}
else {
for (int i = 0; i < mConfig.getHeaderLines().length; i++) {
if ((i != (mConfig.getHeaderIgnoreLineNo() - 1)) &&
!mConfig.getHeaderLines()[i].equals(mLines[i]))
else
{
for (int i = 0; i < mConfig.getHeaderLines().length; i++)
{
String headerLine = mConfig.getHeaderLines()[i];
// TODO: RE creation should be cached to avoid
// re-compilation when multiple files are checked
boolean match =
mConfig.getHeaderLinesRegexp() ?
createRE(headerLine).match(mLines[i]) :
headerLine.equals(mLines[i]);
if ((i != (mConfig.getHeaderIgnoreLineNo() - 1)) && !match)
{
log(i + 1,
"Line does not match expected header line of '" +