KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2005 Johannes Bellert
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: CraftOfObjects@gmail.com
22  */

23 package org.hammurapi.inspectors;
24
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.config.ConfigurationException;
36 import com.pavelvlasov.config.Parameterizable;
37 import com.pavelvlasov.jsel.impl.JavaTokenTypes;
38 import com.pavelvlasov.jsel.impl.Token;
39 import com.pavelvlasov.review.SourceMarker;
40
41
42 /**
43  * ER-209 as a variant of ER-100
44  * Copyrights information should be present in each file.
45  * @author Johannes Bellert
46  * @version $Revision: 1.3 $
47  */

48 public class VendorNameViolation extends InspectorBase implements Parameterizable {
49     private Perl5Matcher matcher=new Perl5Matcher();
50
51     /**
52      * Stores the setting from the configuration for the mandatory
53      * copyright text.
54      */

55     private ArrayList JavaDoc vendorNames=new ArrayList JavaDoc();
56     private ArrayList JavaDoc patterns=new ArrayList JavaDoc();
57     
58     
59     public void visit(Token token) {
60         if (token.getType()==JavaTokenTypes.SL_COMMENT || token.getType()==JavaTokenTypes.ML_COMMENT) {
61             context.info(token, token.getText() ) ; // + " ---> " + token.getOwner() + " " + token.getOwner().getLocation());
62
checkForViolations(token, token.getText() );
63         }
64     }
65
66     public void checkForViolations( SourceMarker scrMrk, String JavaDoc text){
67     Iterator JavaDoc it=patterns.iterator();
68     
69     while (it.hasNext()) {
70         Pattern pt = (Pattern) it.next();
71         if (matcher.contains(text, pt )) {
72             context.reportViolation(scrMrk, "Found Pattern " + pt.getPattern().toString() );
73         }
74     }
75     }
76     
77     
78     /**
79      * Configures the rule. Reads in the values of the parameter vendor-name.
80      *
81      * @param name the name of the parameter being loaded from Hammurapi configuration
82      * @param parameter the value of the parameter being loaded from Hammurapi configuration
83      * @exception ConfigurationException in case of a not supported parameter
84      */

85     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter)
86             throws ConfigurationException {
87         if ("vendor-name".equals(name)) {
88          // System.out.println("+ " + parameter.toString());
89

90             if (!vendorNames.contains(parameter.toString())) {
91                 vendorNames.add(parameter.toString());
92
93                 GlobCompiler compiler = new GlobCompiler();
94                 try {
95                     patterns.add(compiler.compile(parameter.toString(),
96                             GlobCompiler.CASE_INSENSITIVE_MASK));
97                 } catch (MalformedPatternException e) {
98                     throw new ConfigurationException("Malformed pattern: "
99                             + parameter, e);
100                 }
101             }
102             return true;
103         }
104
105         throw new ConfigurationException("Parameter '" + name
106                 + "' is not supported by " + getClass().getName());
107     }
108
109     /**
110      * Gives back the preconfigured values.
111      */

112     public String JavaDoc getConfigInfo() {
113         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Configured Vendor Names text:\n");
114         Iterator JavaDoc it=vendorNames.iterator();
115         
116         
117         while (it.hasNext()) {
118             ret.append(" " + it.next() + "\n");
119         }
120         return ret.toString();
121     }
122 }
123
124
Popular Tags