diff --git a/docs/anttask.html b/docs/anttask.html index df4e111b0..f4048accc 100644 --- a/docs/anttask.html +++ b/docs/anttask.html @@ -133,9 +133,14 @@ This task is included in the checkstyle distribution.

Specifies the file containing the header lines. Default is to not check. No + + headerLinesRegexp + Specifies whether to interpret each line in the headerFile as a regular expression. Default is "false". + No + headerIgnoreLine - Specifies the line in the header to ignore when comparing. Default it to not ignore any line. + Specifies the line in the header to ignore when comparing. Default is to not ignore any line. No diff --git a/docs/cmdline.html b/docs/cmdline.html index 70008b76a..864df6dd6 100644 --- a/docs/cmdline.html +++ b/docs/cmdline.html @@ -118,9 +118,13 @@ This command line tool is included in the checkstyle distribution.

checkstyle.header.file Specifies the file containing the header lines. Default is to not check. + + checkstyle.header.regexp + Specifies whether to interpret each line in the checkstyle.header.file as a regular expression. Default is "false". + checkstyle.header.ignoreline - Specifies the line in the header to ignore when comparing. Default it to not ignore any line. + Specifies the line in the header to ignore when comparing. Default is to not ignore any line. checkstyle.javadoc.scope diff --git a/docs/engine.html b/docs/engine.html index 639d065e6..9c8fa82a4 100644 --- a/docs/engine.html +++ b/docs/engine.html @@ -225,6 +225,21 @@ line 4: ///////////////////////////////////////////////////////////////////////

Since the year information will change over time, you can tell checkstyle to ignore line 3.

+
+
+

Tip

+

Checkstyle also supports interpreting each header line as a regular expression. For example, consider the following header when regular expression checking is turned on:

+ +
+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}
+
+ +

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.

+

Output Format

diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java index 7ec512d04..f573ddd60 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java @@ -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) { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index 2c04909a7..a48e76971 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -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 */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java index f83985047..aa1c02a4b 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java @@ -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 */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index ac1edabd7..9231949bb 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -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 '" +