001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Pavel Vlasov
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: vlasov@pavelvlasov.com
022:
023: */
024: package org.hammurapi.inspectors;
025:
026: import java.util.Comparator;
027: import java.util.Iterator;
028: import java.util.StringTokenizer;
029:
030: import org.hammurapi.InspectorBase;
031:
032: import com.pavelvlasov.config.ConfigurationException;
033: import com.pavelvlasov.config.Parameterizable;
034: import com.pavelvlasov.jsel.CompilationUnit;
035: import com.pavelvlasov.jsel.Identifier;
036: import com.pavelvlasov.review.SourceMarker;
037:
038: /**
039: * Packages should be imported in alphabetical order
040: * @author Pavel Vlasov
041: * @version $Revision: 1.6 $
042: */
043: public class AlphabeticalImportRule extends InspectorBase implements
044: Parameterizable {
045:
046: private String[] importOrder;
047:
048: /**
049: * Reviews the node.
050: *
051: * @param compilationUnit the element under review
052: */
053: public void visit(CompilationUnit compilationUnit) {
054: Comparator importComparator = new Comparator() {
055:
056: public int compare(Object importDef1, Object importDef2) {
057:
058: //20052406 Anu start : Modified to compare the import string with
059: //configuartion parameter
060: int index1 = getIndex((String) importDef1);
061: int index2 = getIndex((String) importDef2);
062: if (index1 != -1 && index2 != -1) {
063: if (index1 < index2) {
064: return -1;
065: } else if (index1 > index2) {
066: return 1;
067: }
068: } else if (index1 == -1 && index2 != -1) {
069: return 1;
070: } else if (index1 != -1 && index2 == -1) {
071: return -1;
072: }
073:
074: // if (((String) importDef1).startsWith("java.") && ((String) importDef2).startsWith("com.")) {
075: // return -1;
076: // } else if (((String) importDef1).startsWith("com.") && ((String) importDef2).startsWith("java.")) {
077: // return 1;
078:
079: return ((Comparable) importDef1).compareTo(importDef2);
080: }
081:
082: private int getIndex(String importDef) {
083:
084: int retVal = -1;
085: int orderLength = importOrder.length - 1;
086: for (int i = 0; i <= orderLength; i++) {
087: if (importDef.startsWith(importOrder[i])) {
088: retVal = i;
089: break;
090: }
091: }
092:
093: return retVal;
094: }
095:
096: };
097:
098: //20052406 Anu end
099: Iterator it = compilationUnit.getImports().iterator();
100: String prevImportValue = null;
101: while (it.hasNext()) {
102: Identifier ii = (Identifier) it.next();
103: if (prevImportValue != null
104: && importComparator.compare(ii.getValue(),
105: prevImportValue) < 0) {
106: context.reportViolation((SourceMarker) ii);
107: }
108: prevImportValue = ii.getValue();
109: }
110: }
111:
112: // 20050524 Anu Start : updated for storing the import order passed as parameter
113: /** Configures rule
114: */
115: public boolean setParameter(String name, Object value)
116: throws ConfigurationException {
117:
118: String impOrder = "";
119: if ("import-order".equals(name)) {
120: impOrder = value.toString();
121: StringTokenizer st = new StringTokenizer(impOrder, ",");
122: importOrder = new String[st.countTokens()];
123: for (int i = 0; st.hasMoreTokens(); i++) {
124: importOrder[i] = st.nextToken();
125: }
126: return true;
127: } else {
128: throw new ConfigurationException("Parameter '" + name
129: + "' is not supported");
130: }
131: }
132:
133: /**
134: * Gives back the preconfigured values.
135: */
136: public String getConfigInfo() {
137: StringBuffer ret = new StringBuffer("Import Order:\n");
138: int orderSize = importOrder.length - 1;
139: for (int i = 0; i <= orderSize; i++) {
140: ret.append(" " + importOrder[i] + "\n");
141: }
142: return ret.toString();
143: }
144: //20050524 Anu End
145: }
|