001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Pavel Vlasov
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: vlasov@pavelvlasov.com
022:
023: */
024: package org.hammurapi.inspectors;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028:
029: import org.apache.oro.text.GlobCompiler;
030: import org.apache.oro.text.regex.MalformedPatternException;
031: import org.apache.oro.text.regex.Pattern;
032: import org.apache.oro.text.regex.Perl5Matcher;
033: import org.hammurapi.InspectorBase;
034:
035: import com.pavelvlasov.antlr.AST;
036: import com.pavelvlasov.config.ConfigurationException;
037: import com.pavelvlasov.config.Parameterizable;
038: import com.pavelvlasov.jsel.CompilationUnit;
039: import com.pavelvlasov.jsel.impl.JavaTokenTypes;
040: import com.pavelvlasov.jsel.impl.Token;
041:
042: /**
043: * ER-100
044: * Copyrights information should be present in each file.
045: * @author Pavel Vlasov
046: * @version $Revision: 1.10 $
047: */
048: public class FileHeaderRule extends InspectorBase implements
049: Parameterizable {
050: private Perl5Matcher matcher = new Perl5Matcher();
051:
052: /**
053: * Reviews the compilation unit if it's file violates agaianst the rule.
054: * @param element the compilation unit to be reviewed.
055: */
056: public void visit(CompilationUnit element) {
057: AST ast = element.getAst();
058: if (ast != null) {
059: Token firstToken = (Token) ast.getFirstToken();
060: if (firstToken != null) {
061: while (firstToken.getPrevToken() != null) {
062: firstToken = (Token) firstToken.getPrevToken();
063: }
064: // Anu 24-May-05: Modified code to check for copyright information
065: // before the class declaration with both multiline or singleline comments.
066: // while ((firstToken.getType()!=JavaTokenTypes.ML_COMMENT && firstToken.getType()!=JavaTokenTypes.SL_COMMENT) && firstToken.isWhiteSpace() && firstToken.getNextToken()!=null) {
067: // firstToken=(Token) firstToken.getNextToken();
068: // }
069:
070: while (firstToken.getType() != JavaTokenTypes.LITERAL_class
071: && firstToken.getNextToken() != null) {
072: if ((firstToken.getType() == JavaTokenTypes.ML_COMMENT || firstToken
073: .getType() == JavaTokenTypes.SL_COMMENT)
074: && firstToken.getText() != null) {
075: Iterator it = patterns.iterator();
076: while (it.hasNext()) {
077: if (matcher.contains(firstToken.getText(),
078: (Pattern) it.next())) {
079: return;
080: }
081: }
082: }
083: firstToken = (Token) firstToken.getNextToken();
084: }
085: }
086:
087: // if ((firstToken.getType()==JavaTokenTypes.ML_COMMENT||firstToken.getType()==JavaTokenTypes.SL_COMMENT) && firstToken.getText()!=null && !classToken) {
088: // Iterator it=patterns.iterator();
089: // while (it.hasNext()) {
090: // if (matcher.contains(firstToken.getText(), (Pattern) it.next())) {
091: // return;
092: // }
093: // }
094: // }
095:
096: context.reportViolation(element);
097: }
098:
099: }
100:
101: /**
102: * Stores the setting from the configuration for the mandatory
103: * copyright text.
104: */
105: private ArrayList copyrights = new ArrayList();
106: private ArrayList patterns = new ArrayList();
107:
108: /**
109: * Configures the rule. Reads in the values of the parameter copyright.
110: *
111: * @param name the name of the parameter being loaded from Hammurapi configuration
112: * @param parameter the value of the parameter being loaded from Hammurapi configuration
113: * @exception ConfigurationException in case of a not supported parameter
114: */
115: public boolean setParameter(String name, Object parameter)
116: throws ConfigurationException {
117: if ("copyright".equals(name)) {
118: if (!copyrights.contains(parameter.toString())) {
119: copyrights.add(parameter.toString());
120: GlobCompiler compiler = new GlobCompiler();
121: try {
122: patterns
123: .add(compiler.compile(parameter.toString()));
124: } catch (MalformedPatternException e) {
125: throw new ConfigurationException(
126: "Malformed pattern: " + parameter, e);
127: }
128: }
129: return true;
130: }
131:
132: throw new ConfigurationException("Parameter '" + name
133: + "' is not supported by " + getClass().getName());
134: }
135:
136: /**
137: * Gives back the preconfigured values.
138: */
139: public String getConfigInfo() {
140: StringBuffer ret = new StringBuffer(
141: "Configured Copyright text:\n");
142: Iterator it = copyrights.iterator();
143: while (it.hasNext()) {
144: ret.append(" " + it.next() + "\n");
145: }
146: return ret.toString();
147: }
148: }
|