KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > formatting > FormattingCheckerNewLine


1 /*
2   * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors.formatting;
24
25 import com.pavelvlasov.jsel.impl.JavaTokenTypes;
26 import com.pavelvlasov.jsel.impl.Token;
27
28 /**
29  * Implementation of FormattingChecker for formatting style with opening curly
30  * braces in a new line
31  *
32  * @author Jochen Skulj
33  * @version $Revision: 1.1 $
34  */

35 class FormattingCheckerNewLine extends FormattingCheckerSameLine {
36
37   /**
38    * constructor
39    */

40   public FormattingCheckerNewLine() {
41   }
42
43   /**
44    * checks formatting for tokens that are followed by expression in parenthesis
45    *
46    * @param aToken
47    * currentToken
48    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
49    */

50   protected boolean checkNextLParen(Token aToken) {
51     // The token must be followed by a variously complex braced expression.
52
// The expression must start in the same line as the token
53
// This expression is followed by a LCURLY. The LCURLY must be in the
54
// next line after the last token of the expression and in the same column
55
// like the current token
56
Token expressionFirst = nextNonWhitespace(aToken);
57     boolean violation = false;
58     if (expressionFirst.getLine() != aToken.getLine()) {
59       violation = true;
60     }
61     Token expressionLast = skipExpressionTokens(expressionFirst);
62     Token lcurly = nextNonWhitespace(expressionLast);
63     if (lcurly.getType() == JavaTokenTypes.LCURLY) {
64       if ((expressionLast.getLine() + 1) != lcurly.getLine()
65           || lcurly.getColumn() != aToken.getColumn()) {
66         violation = true;
67       }
68     } else {
69       violation = true;
70     }
71     return violation;
72   }
73
74   /**
75    * checks formatting for tokens that are follow opening curly brace (like do)
76    *
77    * @param aToken
78    * current Token
79    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
80    */

81   protected boolean checkNextLCurly(Token aToken) {
82     // The token must be followed by a LCURLY in the same column and the next
83
// line
84
boolean violation = false;
85     Token lcurly = nextNonWhitespace(aToken);
86     if (lcurly.getType() == JavaTokenTypes.LCURLY) {
87       if (lcurly.getLine() != (aToken.getLine() + 1)
88           || lcurly.getColumn() != aToken.getColumn()) {
89         violation = true;
90       }
91     } else {
92       violation = true;
93     }
94     return violation;
95   }
96
97   /**
98    * checks formatting for tokens that are successors of a closing curly brace
99    * like else, catch or finally
100    *
101    * @param aToken
102    * current Token
103    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
104    */

105   protected boolean checkPrevRCurly(Token aToken) {
106     // the token must have e previous RCURLY in the same column and the previous
107
// line
108
Token rcurly = previousNonWhitespace(aToken);
109     boolean violation = false;
110     if (rcurly.getType() == JavaTokenTypes.RCURLY) {
111       if (rcurly.getLine() != (aToken.getLine() - 1)
112           || rcurly.getColumn() != aToken.getColumn()) {
113         violation = true;
114       }
115     } else {
116       violation = true;
117     }
118     return violation;
119   }
120
121 }
Popular Tags