01: /*
02: * Hammurapi
03: * Automated Java code review system.
04: * Copyright (C) 2004 Pavel Vlasov
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (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;
27: import java.util.Iterator;
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
41: Parameterizable {
42: //Anu 20050525 : added Arraylist of storing multiple packagenames
43: private ArrayList rootPackage = new ArrayList();
44:
45: /** Reviews the node.
46: * @return Collection of violated ASTs
47: */
48: public void visit(CompilationUnit compilationUnit) {
49: String packageName = compilationUnit.getPackage().getName();
50: // Anu 20050525 : updated for matching the package name with the passed parameters
51: Iterator it = rootPackage.iterator();
52: while (it.hasNext()) {
53: String rootPkg = (String) it.next();
54: if (rootPkg != null) {
55: if (packageName.equals(rootPkg)
56: || packageName.startsWith(rootPkg + ".")) {
57: return;
58: }
59: } else {
60: return;
61: }
62:
63: }
64: context.reportViolation(compilationUnit,
65: new Object[] { rootPackage });
66: }
67:
68: /** Configures rule
69: */
70: public boolean setParameter(String name, Object value)
71: throws ConfigurationException {
72: // Anu 20050525 : updated for storing the package names passed as parameter
73: if ("root-package".equals(name)) {
74: if (!rootPackage.contains(value.toString())) {
75: rootPackage.add(value.toString());
76: }
77: return true;
78: } else {
79: throw new ConfigurationException("Parameter '" + name
80: + "' is not supported");
81: }
82: }
83:
84: /**
85: * Gives back the preconfigured values.
86: */
87: public String getConfigInfo() {
88: StringBuffer ret = new StringBuffer(
89: "Application root package:\n");
90: Iterator it = rootPackage.iterator();
91: while (it.hasNext()) {
92: ret.append(" " + it.next() + "\n");
93: }
94: return ret.toString();
95: }
96: }
|