KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.regex.Matcher JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25
26 /**
27  * <p>
28  * A generic check for code problems, the user can search for any pattern.
29  * This is similar to a recursive grep, only that it's integrated in checkstyle.
30  * </p>
31  * <p>
32  * Rationale: This Check can be used to prototype checks and to find common
33  * bad pratice such as calling
34  * ex.printStacktrace(), System.out.println(), System.exit(), etc.
35  * </p>
36  * <p>
37  * An example of how to configure the check for calls to
38  * <code>System.out.println</code> is:
39  * </p>
40  * <pre>
41  * &lt;module name="GenericIllegalRegexp"&gt;
42  * &lt;property name="format" value="System\.out\.println"/&gt;
43  * &lt;/module&gt;
44  * </pre>
45  * @author lkuehne
46  * @author <a HREF="mailto:bschneider@vecna.com">Bill Schneider</a>
47  * @author Daniel Grenner
48  */

49 public class GenericIllegalRegexpCheck extends AbstractFormatCheck
50 {
51     /**
52      * Custom message for report if illegal regexp found
53      * ignored if empty.
54      */

55     private String JavaDoc mMessage = "";
56
57     /** Ignore comments in code? **/
58     private boolean mIgnoreComments;
59
60     /**
61      * Setter for message property.
62      * @param aMessage custom message which should be used
63      * to report about violations.
64      */

65
66     public void setMessage(String JavaDoc aMessage)
67     {
68         if (aMessage == null) {
69             aMessage = "";
70         }
71         mMessage = aMessage;
72     }
73
74     /**
75      * Getter for message property.
76      * @return custom message which should be used
77      * to report about violations.
78      */

79     public String JavaDoc getMessage()
80     {
81         return mMessage;
82     }
83
84     /**
85      * Set whether or not the match is case sensitive.
86      * @param aCaseInsensitive true if the match is case insensitive.
87      */

88     public void setIgnoreCase(boolean aCaseInsensitive)
89     {
90         if (aCaseInsensitive) {
91             setCompileFlags(Pattern.CASE_INSENSITIVE);
92         }
93     }
94
95     /**
96      * Sets if comments should be ignored.
97      * @param aIgnoreComments True if comments should be ignored.
98      */

99     public void setIgnoreComments(boolean aIgnoreComments)
100     {
101         mIgnoreComments = aIgnoreComments;
102     }
103
104     /**
105      * Instantiates an new GenericIllegalRegexpCheck.
106      */

107     public GenericIllegalRegexpCheck()
108     {
109         super("$^"); // the empty language
110
}
111
112     /** {@inheritDoc} */
113     public int[] getDefaultTokens()
114     {
115         return new int[0];
116     }
117
118     /** {@inheritDoc} */
119     public void beginTree(DetailAST aRootAST)
120     {
121         final String JavaDoc[] lines = getLines();
122
123         for (int i = 0; i < lines.length; i++) {
124
125             final String JavaDoc line = lines[i];
126             final boolean foundMatch;
127             if (mIgnoreComments) {
128                 foundMatch = findNonCommentMatch(line, i + 1, 0);
129             }
130             else {
131                 foundMatch = getRegexp().matcher(line).find();
132             }
133             if (foundMatch) {
134                 if ("".equals(mMessage)) {
135                     log(i + 1, "illegal.regexp", getFormat());
136                 }
137                 else {
138                     log(i + 1, mMessage);
139                 }
140             }
141         }
142     }
143
144     /**
145      * Finds matches that are not inside comments.
146      * @param aLine The text that should be matched.
147      * @param aLineNumber The current line number.
148      * @param aStartPosition The position to start searching from.
149      * @return true if a match is done where there is no comment.
150      */

151     private boolean findNonCommentMatch(
152             String JavaDoc aLine, int aLineNumber, int aStartPosition)
153     {
154         final Pattern JavaDoc pattern = getRegexp();
155         final Matcher JavaDoc matcher = pattern.matcher(aLine);
156         final boolean foundMatch = matcher.find(aStartPosition);
157         if (!foundMatch) {
158             return false;
159         }
160         // match is found, check for intersection with comment
161
final int startCol = matcher.start(0);
162         final int endCol = matcher.end(0);
163         // Note that Matcher.end(int) returns he offset AFTER the
164
// last matched character, but hasIntersectionWithComment()
165
// needs column number of the last character.
166
// So we need to use (endCol - 1) here.
167
final boolean intersectsWithComment = getFileContents()
168             .hasIntersectionWithComment(aLineNumber, startCol,
169                                         aLineNumber, endCol - 1);
170         if (intersectsWithComment) {
171             if (endCol < aLine.length()) {
172                 // check if the expression is on the rest of the line
173
return findNonCommentMatch(aLine, aLineNumber, endCol);
174             }
175             // end of line reached
176
return false;
177         }
178         // not intersecting with comment
179
return true;
180     }
181 }
182
Popular Tags