001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors.formatting;
024:
025: import com.pavelvlasov.jsel.impl.JavaTokenTypes;
026: import com.pavelvlasov.jsel.impl.Token;
027:
028: /**
029: * Implementation of FormattingChecker for formatting style with opening curly
030: * braces in a new line
031: *
032: * @author Jochen Skulj
033: * @version $Revision: 1.1 $
034: */
035: class FormattingCheckerNewLine extends FormattingCheckerSameLine {
036:
037: /**
038: * constructor
039: */
040: public FormattingCheckerNewLine() {
041: }
042:
043: /**
044: * checks formatting for tokens that are followed by expression in parenthesis
045: *
046: * @param aToken
047: * currentToken
048: * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
049: */
050: protected boolean checkNextLParen(Token aToken) {
051: // The token must be followed by a variously complex braced expression.
052: // The expression must start in the same line as the token
053: // This expression is followed by a LCURLY. The LCURLY must be in the
054: // next line after the last token of the expression and in the same column
055: // like the current token
056: Token expressionFirst = nextNonWhitespace(aToken);
057: boolean violation = false;
058: if (expressionFirst.getLine() != aToken.getLine()) {
059: violation = true;
060: }
061: Token expressionLast = skipExpressionTokens(expressionFirst);
062: Token lcurly = nextNonWhitespace(expressionLast);
063: if (lcurly.getType() == JavaTokenTypes.LCURLY) {
064: if ((expressionLast.getLine() + 1) != lcurly.getLine()
065: || lcurly.getColumn() != aToken.getColumn()) {
066: violation = true;
067: }
068: } else {
069: violation = true;
070: }
071: return violation;
072: }
073:
074: /**
075: * checks formatting for tokens that are follow opening curly brace (like do)
076: *
077: * @param aToken
078: * current Token
079: * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
080: */
081: protected boolean checkNextLCurly(Token aToken) {
082: // The token must be followed by a LCURLY in the same column and the next
083: // line
084: boolean violation = false;
085: Token lcurly = nextNonWhitespace(aToken);
086: if (lcurly.getType() == JavaTokenTypes.LCURLY) {
087: if (lcurly.getLine() != (aToken.getLine() + 1)
088: || lcurly.getColumn() != aToken.getColumn()) {
089: violation = true;
090: }
091: } else {
092: violation = true;
093: }
094: return violation;
095: }
096:
097: /**
098: * checks formatting for tokens that are successors of a closing curly brace
099: * 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: }
|