KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.hammurapi.InspectorBase;
31
32 import com.pavelvlasov.config.ConfigurationException;
33 import com.pavelvlasov.config.Parameterizable;
34 import com.pavelvlasov.jsel.CompilationUnit;
35 import com.pavelvlasov.jsel.Identifier;
36 import com.pavelvlasov.review.SourceMarker;
37
38 /**
39  * Packages should be imported in alphabetical order
40  * @author Pavel Vlasov
41  * @version $Revision: 1.6 $
42  */

43 public class AlphabeticalImportRule extends InspectorBase implements Parameterizable {
44   
45   
46     private String JavaDoc [] importOrder ;
47     /**
48    * Reviews the node.
49    *
50    * @param compilationUnit the element under review
51    */

52     public void visit(CompilationUnit compilationUnit) {
53         Comparator JavaDoc importComparator = new Comparator JavaDoc() {
54
55             public int compare(Object JavaDoc importDef1, Object JavaDoc importDef2) {
56                 
57                 //20052406 Anu start : Modified to compare the import string with
58
//configuartion parameter
59
int index1 = getIndex((String JavaDoc) importDef1);
60                 int index2 = getIndex((String JavaDoc) importDef2);
61                 if(index1 != -1 && index2 != -1 ){
62                     if(index1<index2){
63                         return -1;
64                     }
65                     else if(index1>index2) {
66                         return 1;
67                     }
68                 }
69                 else if(index1 == -1 && index2 != -1)
70                 {
71                     return 1;
72                 }
73                 else if(index1 != -1 && index2 == -1)
74                 {
75                     return -1;
76                 }
77         
78                 
79                     
80 // if (((String) importDef1).startsWith("java.") && ((String) importDef2).startsWith("com.")) {
81
// return -1;
82
// } else if (((String) importDef1).startsWith("com.") && ((String) importDef2).startsWith("java.")) {
83
// return 1;
84

85                 
86                 return ((Comparable JavaDoc) importDef1).compareTo(importDef2);
87             }
88
89             private int getIndex(String JavaDoc importDef) {
90                 
91                 int retVal = -1;
92                 int orderLength = importOrder.length -1;
93                 for(int i=0;i<=orderLength;i++)
94                 {
95                     if (importDef.startsWith(importOrder[i]))
96                     {
97                         retVal = i;
98                         break;
99                     }
100                 }
101                             
102                 return retVal;
103             }
104             
105         };
106     
107         //20052406 Anu end
108
Iterator JavaDoc it=compilationUnit.getImports().iterator();
109         String JavaDoc prevImportValue=null;
110         while (it.hasNext()) {
111             Identifier ii=(Identifier) it.next();
112             if (prevImportValue!=null && importComparator.compare(ii.getValue(), prevImportValue)<0) {
113                 context.reportViolation((SourceMarker) ii);
114             }
115             prevImportValue=ii.getValue();
116         }
117     }
118     
119 // 20050524 Anu Start : updated for storing the import order passed as parameter
120
/** Configures rule
121      */

122     public boolean setParameter(String JavaDoc name, Object JavaDoc value) throws ConfigurationException {
123         
124         String JavaDoc impOrder = "";
125         if ("import-order".equals(name)) {
126             impOrder = value.toString();
127             StringTokenizer JavaDoc st=new StringTokenizer JavaDoc(impOrder, ",");
128             importOrder = new String JavaDoc[st.countTokens()];
129             for (int i=0; st.hasMoreTokens(); i++) {
130                 importOrder[i]=st.nextToken();
131             }
132             return true;
133         } else {
134             throw new ConfigurationException("Parameter '"+name+"' is not supported");
135         }
136     }
137
138     /**
139      * Gives back the preconfigured values.
140      */

141     public String JavaDoc getConfigInfo() {
142         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Import Order:\n");
143         int orderSize = importOrder.length -1;
144         for(int i=0;i<=orderSize;i++) {
145             ret.append(" " + importOrder[i] + "\n");
146         }
147         return ret.toString();
148     }
149     //20050524 Anu End
150
}
151
Popular Tags