001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.refactor.type;
010:
011: import java.io.File;
012: import java.io.IOException;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import net.sourceforge.jrefactory.ast.ASTName;
016: import net.sourceforge.jrefactory.ast.SimpleNode;
017: import net.sourceforge.jrefactory.query.PackageNameGetter;
018: import org.acm.seguin.refactor.Refactoring;
019: import org.acm.seguin.refactor.RefactoringException;
020: import org.acm.seguin.summary.*;
021: import org.acm.seguin.summary.query.TopLevelDirectory;
022: import org.acm.seguin.refactor.RefactoringException;
023:
024: /**
025: * Main program for repackaging. This object simply stores the main program
026: * and interprets the command line arguments for repackaging one or more
027: * files.
028: *
029: *@author Chris Seguin
030: *@created June 2, 1999
031: */
032: public class MoveClass extends Refactoring {
033: // Instance Variables
034: /**
035: * The directory
036: */
037: protected String initDir;
038: /**
039: * The list of filenames
040: */
041: protected LinkedList fileList;
042: private String oldPackage;
043: private File base;
044: private String srcPackage;
045: private String destPackage;
046:
047: /**
048: * Constructor for repackage
049: */
050: protected MoveClass() {
051: destPackage = null;
052: initDir = System.getProperty("user.dir");
053: fileList = new LinkedList();
054: }
055:
056: /**
057: * Set the destination package
058: *
059: *@param dest the package name
060: */
061: public void setDestinationPackage(String dest) {
062: destPackage = dest;
063: }
064:
065: /**
066: * Set the directory
067: *
068: *@param dir the initial directory
069: */
070: public void setDirectory(String dir) {
071: initDir = dir;
072: }
073:
074: /**
075: * Gets the Description attribute of the MoveClass object
076: *
077: *@return The Description value
078: */
079: public String getDescription() {
080: return "Repackaging classes from " + srcPackage + " to "
081: + destPackage;
082: }
083:
084: /**
085: * Gets the id for this refactoring to track which refactorings are used.
086: *
087: *@return the id
088: */
089: public int getID() {
090: return REPACKAGE;
091: }
092:
093: /**
094: * Add a file to the list. The file name includes only the name, and not the
095: * entire path.
096: *
097: *@param filename the file to add
098: */
099: public void add(String filename) {
100: fileList.add(filename);
101: }
102:
103: /**
104: * Main processing method for the MoveClass object
105: *
106: *@exception RefactoringException Description of Exception
107: */
108: protected void preconditions() throws RefactoringException {
109: if ((destPackage == null) || (fileList.size() == 0)) {
110: if (destPackage == null)
111: System.out.println("destPackage can not be null");
112: if (fileList.size() == 0)
113: System.out.println("No files provided");
114: return;
115: }
116: //Checks if the class extension is valid
117: validateClassName(fileList);
118:
119: File startDir = new File(initDir);
120: String firstFilename = (String) fileList.get(0);
121: ASTName srcPackageName = PackageNameGetter.query(startDir,
122: firstFilename);
123: srcPackage = "";
124: if (srcPackageName != null) {
125: srcPackage = srcPackageName.getName();
126: }
127:
128: base = TopLevelDirectory.query(startDir, firstFilename);
129:
130: String topLevelDir = base.getPath();
131: try {
132: topLevelDir = base.getCanonicalPath();
133: } catch (IOException ioe) {
134: }
135: (new SummaryTraversal(topLevelDir)).run();
136: }
137:
138: /**
139: * Performs the transformation of the class
140: */
141: protected void transform() {
142: MoveClassVisitor mcv = new MoveClassVisitor(srcPackage,
143: destPackage, base, getComplexTransform());
144: Iterator iter = fileList.iterator();
145: while (iter.hasNext()) {
146: // Get the next file
147: String nextFile = (String) iter.next();
148:
149: int start = Math.max(0, nextFile.indexOf(File.separator));
150: int end = nextFile.indexOf(".java");
151:
152: String nextClass = "";
153: if (end > 0) {
154: nextClass = nextFile.substring(start, end);
155: } else {
156: nextClass = nextFile.substring(start);
157: }
158:
159: mcv.add(nextClass);
160: }
161:
162: mcv.visit(null);
163: }
164:
165: protected void validateClassName(LinkedList inputFileList)
166: throws RefactoringException {
167: Iterator iter = inputFileList.iterator();
168: while (iter.hasNext()) {
169: // Get the next file
170: String nextFile = (String) iter.next();
171: if (!nextFile.endsWith(".java"))
172: throw new RefactoringException(nextFile
173: + " should ba a java file");
174: }
175: }
176:
177: }
|