Source code: com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 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 org.apache.regexp.RE;
22
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
25 /**
26 * <p>
27 * A generic check for code problems, the user can search for any pattern.
28 * This is similar to a recursive grep, only that it's integrated in checkstyle.
29 * </p>
30 * <p>
31 * Rationale: This Check can be used to prototype checks and to find common
32 * bad pratice such as calling
33 * ex.printStacktrace(), System.out.println(), System.exit(), etc.
34 * </p>
35 * <p>
36 * An example of how to configure the check for calls to
37 * <code>System.out.println</code> is:
38 * </p>
39 * <pre>
40 * <module name="GenericIllegalRegexp">
41 * <property name="format" value="System\.out\.println"/>
42 * </module>
43 * </pre>
44 * @author lkuehne
45 * @author <a href="mailto:bschneider@vecna.com">Bill Schneider</a>
46 */
47 public class GenericIllegalRegexpCheck extends AbstractFormatCheck
48 {
49 /**
50 * Custom message for report if illegal regexp found
51 * ignored if empty.
52 */
53 private String mMessage = "";
54
55 /** case insensitive? **/
56 private boolean mIgnoreCase;
57
58 /**
59 * Setter for message property.
60 * @param aMessage custom message which should be used
61 * to report about violations.
62 */
63
64 public void setMessage(String aMessage)
65 {
66 if (aMessage == null) {
67 aMessage = "";
68 }
69 mMessage = aMessage;
70 }
71
72 /**
73 * Getter for message property.
74 * @return custom message which should be used
75 * to report about violations.
76 */
77 public String getMessage()
78 {
79 return mMessage;
80 }
81
82 /**
83 * Set whether or not the match is case sensitive.
84 * @param aCaseInsensitive true if the match is case insensitive.
85 */
86 public void setIgnoreCase(boolean aCaseInsensitive)
87 {
88 mIgnoreCase = aCaseInsensitive;
89 }
90
91 /**
92 * Instantiates an new GenericIllegalRegexpCheck.
93 */
94 public GenericIllegalRegexpCheck()
95 {
96 super("$^"); // the empty language
97 }
98
99 /** @see com.puppycrawl.tools.checkstyle.api.Check */
100 public int[] getDefaultTokens()
101 {
102 return new int[0];
103 }
104
105 /** @see com.puppycrawl.tools.checkstyle.api.Check */
106 public void beginTree(DetailAST aRootAST)
107 {
108 final String[] lines = getLines();
109 for (int i = 0; i < lines.length; i++) {
110
111 final String line = lines[i];
112 if (getRegexp().match(line)) {
113 if ("".equals(mMessage)) {
114 log(i + 1, "illegal.regexp", getFormat());
115 }
116 else {
117 log(i + 1, mMessage);
118 }
119 }
120 }
121 }
122
123 /** @return the regexp to match against */
124 public RE getRegexp()
125 {
126 final RE regexp = super.getRegexp();
127
128 // we should explicitly set match flags because
129 // we caching RE and another check (or instance
130 // of this check could change match flags.
131 if (mIgnoreCase) {
132 regexp.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
133 }
134 else {
135 regexp.setMatchFlags(RE.MATCH_NORMAL);
136 }
137 return regexp;
138 }
139 }