001: /*
002: * Author: Chris Seguin
003: * Author: Mike Atkinson
004: *
005: * This software has been developed under the copyleft
006: * rules of the GNU General Public License. Please
007: * consult the GNU General Public License for more
008: * details about use and distribution of this software.
009: */
010: package org.acm.seguin.ide.jedit;
011:
012: import java.util.ArrayList;
013: import java.io.File;
014: import java.io.IOException;
015: import net.sourceforge.jrefactory.ast.SimpleNode;
016: import org.acm.seguin.pretty.PrettyPrintFile;
017: import org.acm.seguin.refactor.ComplexTransform;
018: import org.acm.seguin.refactor.TransformAST;
019: import org.acm.seguin.refactor.undo.UndoAction;
020: import org.acm.seguin.version.VersionControl;
021: import org.acm.seguin.version.VersionControlFactory;
022: import org.acm.seguin.awt.ExceptionPrinter;
023: import net.sourceforge.jrefactory.factory.FileParserFactory;
024: import net.sourceforge.jrefactory.factory.ParserFactory;
025:
026: /**
027: * Base class for a program that reads in an abstract syntax tree, transforms
028: * the code, and rewrites the file to disk.
029: *
030: *@author Chris Seguin
031: *@author <a href="mailto:JRefactoryPlugin@ladyshot.demon.co.uk">Mike
032: * Atkinson</a>
033: *@created 23 July 2003
034: *@version $Id: JEditComplexTransform.java,v 1.4 2003/10/30 15:24:22 mikeatkinson Exp $
035: *@since 0.0.1
036: */
037: public class JEditComplexTransform implements ComplexTransform {
038: // Instance Variables
039: private ArrayList transforms;
040: private UndoAction undo;
041:
042: /**
043: * Constructor for the JEditComplexTransform object
044: */
045: public JEditComplexTransform() {
046: transforms = new ArrayList();
047: }
048:
049: /**
050: * Sets the undoAction attribute of the JEditComplexTransform object
051: *
052: *@param init The new undoAction value
053: */
054: public void setUndoAction(UndoAction init) {
055: undo = init;
056: }
057:
058: /**
059: * Adds a syntax tree transformation
060: *
061: *@param value Description of Parameter
062: */
063: public void add(TransformAST value) {
064: //System.out.println("JEditComplexTransform.add(TransformAST)");
065: if (value != null) {
066: transforms.add(value);
067: }
068: }
069:
070: /**
071: * Clears all the transforms
072: */
073: public void clear() {
074: //System.out.println("JEditComplexTransform.clear()");
075: transforms.clear();
076: }
077:
078: /**
079: * Is it worth applying the transforms
080: *
081: *@return true if there is any
082: */
083: public boolean hasAnyChanges() {
084: //System.out.println("JEditComplexTransform.hasAnyChanges() => " + (transforms.size() > 0));
085: return (transforms.size() > 0);
086: }
087:
088: /**
089: * Given a file, applies a set of transformations to it
090: *
091: *@param inputFile Description of Parameter
092: *@param outputFile Description of Parameter
093: */
094: public void apply(File inputFile, File outputFile) {
095: //System.out.println("JEditComplexTransform.apply(" + inputFile + "," + outputFile + ")");
096: // Get the abstract syntax tree
097: ParserFactory factory = new FileParserFactory(inputFile);
098: SimpleNode root = factory.getAbstractSyntaxTree(false,
099: ExceptionPrinter.getInstance());
100:
101: // Apply each individual transformation
102: int last = transforms.size();
103: for (int ndx = 0; ndx < last; ndx++) {
104: TransformAST next = (TransformAST) transforms.get(ndx);
105: next.update(root);
106: }
107:
108: // Check it out if it is read only
109: if (!inputFile.canWrite()) {
110: checkOut(inputFile);
111: }
112:
113: // Print it
114: undo.add(inputFile, outputFile);
115: createParent(outputFile);
116: try {
117: (new PrettyPrintFile()).apply(outputFile, root);
118: } catch (Throwable thrown) {
119: ExceptionPrinter.print(thrown, false);
120: }
121: }
122:
123: /**
124: * Creates a new file
125: *
126: *@param file Description of Parameter
127: */
128: public void createFile(File file) {
129: //System.out.println("JEditComplexTransform.createFile(" + file + ")");
130: undo.add(null, file);
131: }
132:
133: /**
134: * Removes an old file
135: *
136: *@param file Description of Parameter
137: */
138: public void removeFile(File file) {
139: //System.out.println("JEditComplexTransform.removeFile(" + file + ")");
140: undo.add(file, null);
141: }
142:
143: /**
144: * Creates the parent directory if it does not exist
145: *
146: *@param file the file that is about to be created
147: */
148: private void createParent(File file) {
149: File parent = file.getParentFile();
150: if (!parent.exists()) {
151: parent.mkdirs();
152: }
153: }
154:
155: /**
156: * Checks out the specific file from the repository
157: *
158: *@param file the file to check out
159: */
160: private void checkOut(File file) {
161: VersionControl controller = VersionControlFactory.get();
162: String filename;
163:
164: try {
165: filename = file.getCanonicalPath();
166: } catch (IOException ioe) {
167: filename = file.getPath();
168: }
169:
170: controller.checkOut(filename);
171: }
172: }
|