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.pretty;
010:
011: import java.io.File;
012: import java.io.IOException;
013: import java.io.Writer;
014: import java.io.OutputStreamWriter;
015: import org.acm.seguin.awt.ExceptionPrinter;
016: import org.acm.seguin.awt.Question;
017: import org.acm.seguin.io.InplaceOutputStream;
018: import net.sourceforge.jrefactory.ast.SimpleNode;
019: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
020: import org.acm.seguin.util.FileSettings;
021: import net.sourceforge.jrefactory.factory.FileParserFactory;
022: import net.sourceforge.jrefactory.factory.ParserFactory;
023:
024: import java.util.*;
025:
026: /**
027: * Holds a refactoring. Default version just pretty prints the file.
028: *
029: * @author Chris Seguin
030: * @author <a href="JRefactory@ladyshot.demon.co.uk">Mike Atkinson</a>
031: * @created July 1, 1999
032: * @version $Id: PrettyPrintFile.java,v 1.12 2003/11/18 18:46:14 mikeatkinson Exp $
033: */
034: public class PrettyPrintFile {
035: private ParserFactory factory;
036: private boolean ask;
037:
038: /** Refactors java code. */
039: public PrettyPrintFile() {
040: ask = true;
041: }
042:
043: /**
044: * Sets whether we should ask the user
045: *
046: * @param way the way to set the variable
047: */
048: public void setAsk(boolean way) {
049: ask = way;
050: }
051:
052: /**
053: * Set the parser factory
054: *
055: * @param factory Description of Parameter
056: */
057: public void setParserFactory(ParserFactory factory) {
058: this .factory = factory;
059: }
060:
061: /**
062: * Returns true if this refactoring is applicable
063: *
064: * @param inputFile the input file
065: * @return true if this refactoring is applicable
066: */
067: public boolean isApplicable(File inputFile) {
068: if (!inputFile.canWrite()) {
069: return false;
070: }
071:
072: boolean result = true;
073:
074: if (ask) {
075: result = Question.isYes("Pretty Printer",
076: "Do you want to pretty print the file\n"
077: + inputFile.getPath() + "?");
078: }
079:
080: // Create a factory if necessary
081: if (result) {
082: setParserFactory(new FileParserFactory(inputFile));
083: }
084:
085: return result;
086: }
087:
088: /**
089: * Return the factory that gets the abstract syntax trees
090: *
091: * @return the parser factory
092: */
093: public ParserFactory getParserFactory() {
094: return factory;
095: }
096:
097: /**
098: * Apply the refactoring
099: *
100: * @param inputFile the input file
101: */
102: public void apply(File inputFile) {
103: SimpleNode root = factory.getAbstractSyntaxTree(true,
104: ExceptionPrinter.getInstance());
105:
106: if (root != null) {
107: apply(inputFile, root);
108: postApply(inputFile, root);
109: }
110: }
111:
112: /**
113: * Apply the refactoring
114: *
115: * @param inputFile the input file
116: * @param root Description of Parameter
117: */
118: public void apply(File inputFile, SimpleNode root) {
119: if (root != null) {
120: FileSettings pretty = FileSettings
121: .getRefactoryPrettySettings();
122:
123: pretty.setReloadNow(true);
124:
125: // Create the visitor
126: PrettyPrintVisitor printer = new PrettyPrintVisitor();
127:
128: // Create the appropriate print data
129: PrintData data = getPrintData(inputFile);
130:
131: if (root instanceof ASTCompilationUnit) {
132: printer.visit((ASTCompilationUnit) root, data);
133: } else {
134: printer.visit(root, data);
135: }
136:
137: data.close(); // Flush the output stream and close it
138: }
139: }
140:
141: /**
142: * Create the output stream
143: *
144: * @param file the name of the file
145: * @return the output stream
146: */
147: protected Writer getWriter(File file) {
148: Writer out = null;
149:
150: try {
151: out = new OutputStreamWriter(new InplaceOutputStream(file));
152: } catch (IOException ioe) {
153: out = new OutputStreamWriter(System.out);
154: }
155:
156: return out; // Return the output stream
157: }
158:
159: /**
160: * Return the appropriate print data
161: *
162: * @param input Description of Parameter
163: * @return the print data
164: */
165: protected PrintData getPrintData(File input) {
166: JavadocTags.get().reload();
167:
168: // Create the new stream
169: return new PrintData(getWriter(input));
170: }
171:
172: /**
173: * Apply the refactoring
174: *
175: * @param inputFile the input file
176: * @param root Description of Parameter
177: */
178: protected void postApply(File inputFile, SimpleNode root) {
179: }
180: }
|