KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > FileHeaderRule


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Pavel Vlasov
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: vlasov@pavelvlasov.com
22
23  */

24 package org.hammurapi.inspectors;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.apache.oro.text.GlobCompiler;
30 import org.apache.oro.text.regex.MalformedPatternException;
31 import org.apache.oro.text.regex.Pattern;
32 import org.apache.oro.text.regex.Perl5Matcher;
33 import org.hammurapi.InspectorBase;
34
35 import com.pavelvlasov.antlr.AST;
36 import com.pavelvlasov.config.ConfigurationException;
37 import com.pavelvlasov.config.Parameterizable;
38 import com.pavelvlasov.jsel.CompilationUnit;
39 import com.pavelvlasov.jsel.impl.JavaTokenTypes;
40 import com.pavelvlasov.jsel.impl.Token;
41
42
43 /**
44  * ER-100
45  * Copyrights information should be present in each file.
46  * @author Pavel Vlasov
47  * @version $Revision: 1.10 $
48  */

49 public class FileHeaderRule extends InspectorBase implements Parameterizable {
50     private Perl5Matcher matcher=new Perl5Matcher();
51     
52     /**
53      * Reviews the compilation unit if it's file violates agaianst the rule.
54      * @param element the compilation unit to be reviewed.
55      */

56     public void visit(CompilationUnit element) {
57         AST ast = element.getAst();
58         if (ast!=null) {
59             Token firstToken = (Token) ast.getFirstToken();
60             if (firstToken!=null) {
61                 while (firstToken.getPrevToken()!=null) {
62                     firstToken=(Token) firstToken.getPrevToken();
63                 }
64 // Anu 24-May-05: Modified code to check for copyright information
65
// before the class declaration with both multiline or singleline comments.
66
// while ((firstToken.getType()!=JavaTokenTypes.ML_COMMENT && firstToken.getType()!=JavaTokenTypes.SL_COMMENT) && firstToken.isWhiteSpace() && firstToken.getNextToken()!=null) {
67
// firstToken=(Token) firstToken.getNextToken();
68
// }
69

70                 while(firstToken.getType()!=JavaTokenTypes.LITERAL_class && firstToken.getNextToken()!=null)
71                 {
72                     if ((firstToken.getType()==JavaTokenTypes.ML_COMMENT||firstToken.getType()==JavaTokenTypes.SL_COMMENT) && firstToken.getText()!=null ) {
73                         Iterator JavaDoc it=patterns.iterator();
74                         while (it.hasNext()) {
75                             if (matcher.contains(firstToken.getText(), (Pattern) it.next())) {
76                                 return;
77                             }
78                         }
79                     }
80                     firstToken=(Token) firstToken.getNextToken();
81                 }
82             }
83     
84 // if ((firstToken.getType()==JavaTokenTypes.ML_COMMENT||firstToken.getType()==JavaTokenTypes.SL_COMMENT) && firstToken.getText()!=null && !classToken) {
85
// Iterator it=patterns.iterator();
86
// while (it.hasNext()) {
87
// if (matcher.contains(firstToken.getText(), (Pattern) it.next())) {
88
// return;
89
// }
90
// }
91
// }
92

93                 context.reportViolation(element);
94             }
95         
96     }
97
98     /**
99      * Stores the setting from the configuration for the mandatory
100      * copyright text.
101      */

102     private ArrayList JavaDoc copyrights=new ArrayList JavaDoc();
103     private ArrayList JavaDoc patterns=new ArrayList JavaDoc();
104     
105     /**
106      * Configures the rule. Reads in the values of the parameter copyright.
107      *
108      * @param name the name of the parameter being loaded from Hammurapi configuration
109      * @param parameter the value of the parameter being loaded from Hammurapi configuration
110      * @exception ConfigurationException in case of a not supported parameter
111      */

112     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
113         if ("copyright".equals(name)) {
114             if (!copyrights.contains(parameter.toString())) {
115                 copyrights.add(parameter.toString());
116                 GlobCompiler compiler=new GlobCompiler();
117                 try {
118                     patterns.add(compiler.compile(parameter.toString()));
119                 } catch (MalformedPatternException e) {
120                     throw new ConfigurationException("Malformed pattern: "+parameter, e);
121                 }
122             }
123             return true;
124         }
125         
126         throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName());
127     }
128
129     /**
130      * Gives back the preconfigured values.
131      */

132     public String JavaDoc getConfigInfo() {
133         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Configured Copyright text:\n");
134         Iterator JavaDoc it=copyrights.iterator();
135         while (it.hasNext()) {
136             ret.append(" " + it.next() + "\n");
137         }
138         return ret.toString();
139     }
140 }
141
Popular Tags