001: /*
002: * Copyright 1999
003: *
004: * Chris Seguin
005: * All rights reserved
006: */
007: package org.acm.seguin.tools.builder;
008:
009: import java.io.BufferedReader;
010: import java.io.FileReader;
011: import java.io.IOException;
012: import java.util.StringTokenizer;
013: import org.acm.seguin.refactor.RefactoringException;
014: import org.acm.seguin.refactor.RefactoringFactory;
015: import org.acm.seguin.refactor.type.MoveClass;
016: import org.acm.seguin.tools.RefactoryInstaller;
017: import org.acm.seguin.util.FileSettings;
018:
019: /**
020: * Main program for repackaging. This object simply stores the main program and
021: * interprets the command line arguments for repackaging one or more files. It
022: * has two options --package or nopackage. package builds a specified package
023: * hierarchy and nopackage removes the package hierarchy and updates the java
024: * file(s) accordingly This program uses relative pathname to create a new
025: * package. So it may give unexpectd results if run on a file whose package
026: * name is not in accordance with its package hierarchy
027: *
028: *@author Chris Seguin
029: *@created June 2, 1999
030: *@Modified by Praveen on Feb 2002 Incorporated better messages
031: */
032: public class Repackage {
033: private boolean setPackage = false;
034: private boolean atLeastOneClass = false;
035: // Instance Variables
036: private MoveClass moveClass;
037:
038: /**
039: * Initialize the variables with command line arguments
040: *
041: *@param args the command line arguments
042: *@return true if we should continue processing
043: */
044: public boolean init(String[] args) {
045: int nCurrentArg = 0;
046:
047: while (nCurrentArg < args.length) {
048: if (args[nCurrentArg].equalsIgnoreCase("-dir")) {
049: moveClass.setDirectory(args[nCurrentArg + 1]);
050: nCurrentArg += 2;
051: } else if (args[nCurrentArg].equalsIgnoreCase("-package")) {
052: moveClass.setDestinationPackage(args[nCurrentArg + 1]);
053: nCurrentArg += 2;
054: setPackage = true;
055: } else if (args[nCurrentArg].equalsIgnoreCase("-nopackage")) {
056: moveClass.setDestinationPackage("");
057: nCurrentArg++;
058: setPackage = true;
059: } else if (args[nCurrentArg].equalsIgnoreCase("-file")) {
060: String filename = args[nCurrentArg + 1];
061: load(filename);
062: nCurrentArg += 2;
063: atLeastOneClass = true;
064: } else if ((args[nCurrentArg].equalsIgnoreCase("-help"))
065: || (args[nCurrentArg].equalsIgnoreCase("--help"))) {
066: printHelpMessage();
067: nCurrentArg++;
068: return false;
069: } else {
070: moveClass.add(args[nCurrentArg]);
071: nCurrentArg++;
072: atLeastOneClass = true;
073: }
074: }
075:
076: return atLeastOneClass && setPackage;
077: }
078:
079: /**
080: * Actual work of the main program occurs here
081: *
082: *@param args the command line arguments
083: *@exception RefactoringException Description of Exception
084: */
085: public void run(String[] args) throws RefactoringException {
086: moveClass = RefactoringFactory.get().moveClass();
087: if (args.length == 0) {
088: printHelpMessage();
089: }
090: try {
091: if (init(args)) {
092: moveClass.run();
093: }
094: //catches if the number of arguments are not as expected e.g. no dir for -dir
095: } catch (Exception aoe) {
096: printHelpMessage();
097: }
098: }
099:
100: /**
101: * Print the help message
102: */
103: protected void printHelpMessage() {
104: System.out.println("Syntax: java Repackage \\ ");
105: System.out.println(" [-dir <dir>] [-help | --help] ");
106: System.out
107: .println(" [-package <packagename> | -nopackage] (<file.java>)*");
108: System.out.println("");
109: System.out.println(" where:");
110: System.out
111: .println(" <dir> is the name of the directory containing the files");
112: System.out
113: .println(" <package> is the name of the new package");
114: System.out
115: .println(" <nopackage> is the way of flatening package hieracrhy");
116: System.out
117: .println(" <file.java> is the name of the java file(s) separated");
118: System.out
119: .println(" by whitespace to be moved");
120: System.out.println(" The syntax is case insensitive. ");
121: }
122:
123: /**
124: * Loads a file listing the names of java files to be moved
125: *
126: *@param filename the name of the file
127: */
128: private void load(String filename) {
129: try {
130: BufferedReader input = new BufferedReader(new FileReader(
131: filename));
132: String line = input.readLine();
133: if (line == null) {
134: System.out.println("Should have atleast one file name");
135: printHelpMessage();
136: }
137: while (line != null) {
138: StringTokenizer tok = new StringTokenizer(line);
139: while (tok.hasMoreTokens()) {
140: String next = tok.nextToken();
141: System.out.println("Adding: " + next);
142: moveClass.add(next);
143: }
144: line = input.readLine();
145: }
146: input.close();
147: } catch (IOException ioe) {
148: ioe.printStackTrace();
149: }
150: }
151:
152: /**
153: * Main program
154: *
155: *@param args the command line arguments
156: */
157: public static void main(String[] args) {
158: try {
159: for (int ndx = 0; ndx < args.length; ndx++) {
160: if (args[ndx].equals("-config")) {
161: String dir = args[ndx + 1];
162: ndx++;
163: FileSettings.setSettingsRoot(dir);
164: }
165: }
166:
167: // Make sure everything is installed properly
168: (new RefactoryInstaller(true)).run();
169:
170: (new Repackage()).run(args);
171: } catch (Throwable thrown) {
172: thrown.printStackTrace(System.out);
173: }
174:
175: System.exit(0);
176: }
177: }
|