KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > RequiredRegexpCheck


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
package com.puppycrawl.tools.checkstyle.checks;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22
23 import java.util.regex.Pattern JavaDoc;
24
25 /**
26  * <p>
27  * A check that makes sure that a specified pattern exists in the code.
28  * </p>
29  * <p>
30  * An example of how to configure the check to make sure a copyright statement
31  * is included in the file (but without requirements on where in the file
32  * it should be):
33  * </p>
34  * <pre>
35  * &lt;module name="RequiredRegexp"&gt;
36  * &lt;property name="format" value="This code is copyrighted"/&gt;
37  * &lt;/module&gt;
38  * </pre>
39  * @author Daniel Grenner
40  */

41 public class RequiredRegexpCheck extends AbstractFormatCheck
42 {
43     /**
44      * Instantiates an new GenericIllegalRegexpCheck.
45      */

46     public RequiredRegexpCheck()
47     {
48         super("$^"); // the empty language
49
}
50
51     /** {@inheritDoc} */
52     public int[] getDefaultTokens()
53     {
54         return new int[0];
55     }
56
57     /** {@inheritDoc} */
58     public void beginTree(DetailAST aRootAST)
59     {
60         final Pattern JavaDoc pattern = getRegexp();
61         final String JavaDoc[] lines = getLines();
62         for (int i = 0; i < lines.length; i++) {
63
64             final String JavaDoc line = lines[i];
65             if (pattern.matcher(line).find()) {
66                 return;
67             }
68         }
69         log(0, "required.regexp", getFormat());
70     }
71 }
72
73
Popular Tags