KickJava   Java API By Example, From Geeks To Geeks.

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


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.hammurapi.InspectorBase;
30
31 import com.pavelvlasov.config.ConfigurationException;
32 import com.pavelvlasov.config.Parameterizable;
33 import com.pavelvlasov.jsel.CompilationUnit;
34
35 /**
36  * Packages should begin with the root (project/organization) package.
37  * @author Pavel Vlasov
38  * @version $Revision: 1.4 $
39  */

40 public class RootPackageRule extends InspectorBase implements Parameterizable {
41     //Anu 20050525 : added Arraylist of storing multiple packagenames
42
private ArrayList JavaDoc rootPackage = new ArrayList JavaDoc();
43     
44     /** Reviews the node.
45      * @return Collection of violated ASTs
46      */

47     public void visit(CompilationUnit compilationUnit) {
48         String JavaDoc packageName=compilationUnit.getPackage().getName();
49 // Anu 20050525 : updated for matching the package name with the passed parameters
50
Iterator JavaDoc it = rootPackage.iterator();
51         while (it.hasNext())
52         {
53             String JavaDoc rootPkg = (String JavaDoc)it.next();
54             if (rootPkg!=null){
55                 if(packageName.equals(rootPkg) || packageName.startsWith(rootPkg+".")) {
56                     return;
57                 }
58             }
59             else
60             {
61                 return;
62             }
63         
64         }
65         context.reportViolation(compilationUnit, new Object JavaDoc[] {rootPackage});
66     }
67     
68     /** Configures rule
69      */

70     public boolean setParameter(String JavaDoc name, Object JavaDoc value) throws ConfigurationException {
71 // Anu 20050525 : updated for storing the package names passed as parameter
72
if ("root-package".equals(name)) {
73             if (!rootPackage.contains(value.toString())) {
74              rootPackage.add(value.toString());
75             }
76             return true;
77         } else {
78             throw new ConfigurationException("Parameter '"+name+"' is not supported");
79         }
80     }
81
82     /**
83      * Gives back the preconfigured values.
84      */

85     public String JavaDoc getConfigInfo() {
86         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Application root package:\n");
87         Iterator JavaDoc it=rootPackage.iterator();
88         while (it.hasNext()) {
89             ret.append(" " + it.next() + "\n");
90         }
91         return ret.toString();
92     }
93 }
94
Popular Tags