From d298d68e672ce7a153d547a7b8cf44c1bfbe5932 Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Sun, 11 Jan 2004 04:43:25 +0000 Subject: [PATCH] Added multiLines property for RegexpHeaderCheck. Added AbstractHeaderCheck - superclass for header checks. --- docs/config_header.html | 49 +++++--- docs/releasenotes.html | 12 ++ .../checks/AbstractHeaderCheck.java | 106 ++++++++++++++++++ .../tools/checkstyle/InputRegexpHeader1.java | 11 ++ .../tools/checkstyle/InputRegexpHeader2.java | 14 +++ .../tools/checkstyle/InputRegexpHeader3.java | 7 ++ .../tools/checkstyle/InputRegexpHeader4.java | 6 + .../checkstyle/InputRegexpSmallHeader.java | 6 + .../tools/checkstyle/regexp.header1 | 5 + .../tools/checkstyle/regexp.header2 | 7 ++ 10 files changed, 207 insertions(+), 16 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AbstractHeaderCheck.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader1.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader2.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader3.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader4.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpSmallHeader.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header1 create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header2 diff --git a/docs/config_header.html b/docs/config_header.html index edcdfa721..81413694c 100644 --- a/docs/config_header.html +++ b/docs/config_header.html @@ -112,18 +112,29 @@ line 5: //////////////////////////////////////////////////////////////////// For example, consider the following header file:

-line 1: /{71}
-line 2: // checkstyle:
-line 3: // Checks Java source code for adherence to a set of rules\.
-line 4: // Copyright \(C\) \d\d\d\d  Oliver Burn
-line 5: // Last modification by \$Author.*\$
-line 6: /{71}
+line  1: ^/{71}$
+line  2: ^// checkstyle:$
+line  3: ^// Checks Java source code for adherence to a set of rules\.$
+line  4: ^// Copyright \(C\) \d\d\d\d  Oliver Burn$
+line  5: ^// Last modification by \$Author.*\$$
+line  6: ^/{71}$
+line  7: 
+line  8: ^package
+line  9:
+line 10: ^import
+line 11:
+line 12: ^/\*\*
+line 13: ^ \*([^/]|$)
+line 14: ^ \*/
       

- Lines 1 and 6 demonstrate a more compact notation for 71 '/' characters. Line 4 - enforces that the copyright notice includes a four digit year. Line 5 is an - example how to enforce revision control keywords in a file header. + Lines 1 and 6 demonstrate a more compact notation for 71 '/' + characters. Line 4 enforces that the copyright notice includes a + four digit year. Line 5 is an example how to enforce revision + control keywords in a file header. Lines 12-14 is a template for + javadoc (line 13 is so complecated to remove conflict with + and of javadoc comment).

Properties

@@ -140,8 +151,8 @@ line 6: /{71} - - + + @@ -149,16 +160,22 @@ line 6: /{71}

Example

- To configure the check to use header file "java.header" - and ignore lines 2, 3, and - 4: + To configure the check to use header file "java.header" and 10 and 13 + muli-lines:

 <module name="RegexpHeader">
     <property name="headerFile" value="java.header"/>
-    <property name="ignoreLines" value="2, 3, 4"/>
+    <property name="multiLines" value="10, 13"/>
 </module>
       
+

+ Note: ignoreLines property has been + removed from this check to simplify it. To make some line + optional use "^.*$" regexp for this line. +

Package

com.puppycrawl.tools.checkstyle.checks @@ -177,4 +194,4 @@ Copyright © 2002-2003 Oliver Burn. All rights Reserved.

- \ No newline at end of file + diff --git a/docs/releasenotes.html b/docs/releasenotes.html index 9d92b5a37..ce3cefa5f 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -107,6 +107,9 @@ (module RequireThis, contributed by Stephen Bloch, requests 755550, 696295). +
  • Added multiLines property to regexpCheck + (request 597676)
  • +

    @@ -126,6 +129,15 @@ +

    + Removed features: +

    + +

    Release 3.3

    diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AbstractHeaderCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AbstractHeaderCheck.java new file mode 100644 index 000000000..dba3a005c --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AbstractHeaderCheck.java @@ -0,0 +1,106 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2003 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks; + +import java.io.FileReader; +import java.io.IOException; +import java.io.LineNumberReader; +import java.util.ArrayList; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; + +import org.apache.commons.beanutils.ConversionException; + +/** + * Abstract super class for header checks. + * Provides support for headerFile property. + * @author o_sukhosolsky + */ +public abstract class AbstractHeaderCheck extends Check +{ + /** empty array to avoid instantiations. */ + private static final int[] EMPTY_INT_ARRAY = new int[0]; + + /** the lines of the header file. */ + private String[] mHeaderLines; + + /** + * Return the header lines to check against. + * @return the header lines to check against. + */ + protected String[] getHeaderLines() + { + return mHeaderLines; + } + + /** + * Set the header file to check against. + * @param aFileName the file that contains the header to check against. + * @throws ConversionException if the file cannot be loaded + */ + public void setHeaderFile(String aFileName) + throws ConversionException + { + // Handle empty param + if ((aFileName == null) || (aFileName.trim().length() == 0)) { + return; + } + + // load the file + try { + final LineNumberReader lnr = + new LineNumberReader(new FileReader(aFileName)); + final ArrayList lines = new ArrayList(); + while (true) { + final String l = lnr.readLine(); + if (l == null) { + break; + } + lines.add(l); + } + mHeaderLines = (String[]) lines.toArray(new String[0]); + } + catch (IOException ex) { + throw new ConversionException( + "unable to load header file " + aFileName, ex); + } + + } + + /** + * Checks that required args were specified. + * @see com.puppycrawl.tools.checkstyle.api.AutomaticBean#finishLocalSetup + */ + protected final void finishLocalSetup() throws CheckstyleException + { + if (mHeaderLines == null) { + throw new CheckstyleException( + "property 'headerFile' is missing or invalid in module " + + getConfiguration().getName()); + } + } + + /** {@inheritDoc} */ + public final int[] getDefaultTokens() + { + return new int[0]; + } +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader1.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader1.java new file mode 100644 index 000000000..e5d47bc4b --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader1.java @@ -0,0 +1,11 @@ +package blah; + +import java.awt.*; + +/** + * Some doc. + */ + +public class InputRegexpHeader1 +{ +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader2.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader2.java new file mode 100644 index 000000000..39741071c --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader2.java @@ -0,0 +1,14 @@ +package blah; + +import java.awt.*; +import java.awt.event.*; +import java.io.*; + +/** + * + * blah blah + * @see foo + */ +public class InputRegexpHeader2 +{ +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader3.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader3.java new file mode 100644 index 000000000..6fc49c24a --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader3.java @@ -0,0 +1,7 @@ +package blah; + +import java.awt.*; + +public class InputRegexpHeader3 +{ +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader4.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader4.java new file mode 100644 index 000000000..d5d4fd2a7 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpHeader4.java @@ -0,0 +1,6 @@ +package blah; + +import java.awt.*; +import java.awt.*; +import java.awt.*; +import java.awt.*; diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpSmallHeader.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpSmallHeader.java new file mode 100644 index 000000000..6998cada8 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputRegexpSmallHeader.java @@ -0,0 +1,6 @@ +package blah; + + +/** + */ +public class InputRegexpSmallHeader {} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header1 b/src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header1 new file mode 100644 index 000000000..6a2ee4545 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header1 @@ -0,0 +1,5 @@ +^/*$ +// .* +^.*$ +^.*$ +^.*$ diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header2 b/src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header2 new file mode 100644 index 000000000..075455c46 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/regexp.header2 @@ -0,0 +1,7 @@ +^package +^$ +^import +^$ +^/\*\* +^ \*([^/]|$) +^ \*/

    null
    ignoreLinesline numbers to ignoremultiLinesline numbers to repeat (zero or more times) list of integers {}