KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > whitespace > NoWhitespaceBeforeCheck


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

20 package com.puppycrawl.tools.checkstyle.checks.whitespace;
21
22 import com.puppycrawl.tools.checkstyle.api.Check;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25
26 /**
27  * <p>
28  * Checks that there is no whitespace before a token.
29  * More specifically, it checks that it is not preceded with whitespace,
30  * or (if linebreaks are allowed) all characters on the line before are
31  * whitespace. To allow linebreaks before a token, set property
32  * allowLineBreaks to true.
33  * </p>
34  * <p> By default the check will check the following operators:
35  * {@link TokenTypes#SEMI SEMI},
36  * {@link TokenTypes#POST_DEC POST_DEC},
37  * {@link TokenTypes#POST_INC POST_INC}.
38  * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration
39  * of this check.
40  * </p>
41  *
42  * <p>
43  * An example of how to configure the check is:
44  * </p>
45  * <pre>
46  * &lt;module name="NoWhitespaceBefore"/&gt;
47  * </pre>
48  * <p> An example of how to configure the check to allow linebreaks before
49  * a {@link TokenTypes#DOT DOT} token is:
50  * </p>
51  * <pre>
52  * &lt;module name="NoWhitespaceBefore"&gt;
53  * &lt;property name="tokens" value="DOT"/&gt;
54  * &lt;property name="allowLineBreaks" value="true"/&gt;
55  * &lt;/module&gt;
56  * </pre>
57  * @author Rick Giles
58  * @author lkuehne
59  * @version 1.0
60  */

61 public class NoWhitespaceBeforeCheck
62     extends Check
63 {
64     /** Whether whitespace is allowed if the AST is at a linebreak */
65     private boolean mAllowLineBreaks;
66
67     /** {@inheritDoc} */
68     public int[] getDefaultTokens()
69     {
70         return new int[] {
71             TokenTypes.SEMI,
72             TokenTypes.POST_INC,
73             TokenTypes.POST_DEC,
74         };
75     }
76
77     /** {@inheritDoc} */
78     public int[] getAcceptableTokens()
79     {
80         return new int[] {
81             TokenTypes.SEMI,
82             TokenTypes.POST_INC,
83             TokenTypes.POST_DEC,
84             TokenTypes.DOT,
85         };
86     }
87
88     /** {@inheritDoc} */
89     public void visitToken(DetailAST aAST)
90     {
91         final String JavaDoc[] lines = getLines();
92         final String JavaDoc line = lines[aAST.getLineNo() - 1];
93         final int before = aAST.getColumnNo() - 1;
94
95         if ((before < 0) || Character.isWhitespace(line.charAt(before))) {
96
97             // empty FOR initializer?
98
if (aAST.getType() == TokenTypes.SEMI) {
99                 final DetailAST sibling = aAST.getPreviousSibling();
100                 if ((sibling != null)
101                         && (sibling.getType() == TokenTypes.FOR_INIT)
102                         && (sibling.getChildCount() == 0))
103                 {
104                     return;
105                 }
106             }
107
108             boolean flag = !mAllowLineBreaks;
109             // verify all characters before '.' are whitespace
110
for (int i = 0; !flag && (i < before); i++) {
111                 if (!Character.isWhitespace(line.charAt(i))) {
112                     flag = true;
113                 }
114             }
115             if (flag) {
116                 log(aAST.getLineNo(), before, "ws.preceded", aAST.getText());
117             }
118         }
119     }
120
121     /**
122      * Control whether whitespace is flagged at linebreaks.
123      * @param aAllowLineBreaks whether whitespace should be
124      * flagged at linebreaks.
125      */

126     public void setAllowLineBreaks(boolean aAllowLineBreaks)
127     {
128         mAllowLineBreaks = aAllowLineBreaks;
129     }
130 }
131
Popular Tags