001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: */
054:
055: package org.apache.tools.ant.taskdefs.optional.javastyle;
056:
057: import java.io.File;
058: import java.util.Vector;
059: import org.acm.seguin.pretty.PrettyPrintFile;
060: import org.acm.seguin.tools.RefactoryInstaller;
061: import org.apache.tools.ant.BuildException;
062: import org.apache.tools.ant.DirectoryScanner;
063: import org.apache.tools.ant.Project;
064: import org.apache.tools.ant.Task;
065: import org.apache.tools.ant.types.FileSet;
066: import org.apache.tools.ant.taskdefs.MatchingTask;
067:
068: /**
069: * <p>
070: *
071: * Task for formatting Java source code. This task utilises the PrettyPrinter
072: * code in the JRefactory code produced by <a href="mailto:seguin@acm.org">Chris
073: * Sequin</a> . This library can be found at <a
074: * href="http://users.snip.net/~aseguin/chrissoft.html">
075: * http://users.snip.net/~aseguin/chrissoft.html</a> .</p> <p>
076: *
077: * The current implementation is <i>very</i> simple. I am implemented it as
078: * quickly as possible to make it available for others to be able to improve
079: * upon!</p> <p>
080: *
081: * The task takes only a "file" attribute or an embedded fileset. It contains no
082: * other attributes at present, as all formatting controls are read from the
083: * default pretty printer preferences file which is stored in a ".Refactory"
084: * directory in your home directory. If no such file exists the first time this
085: * task is run, a default file is generated automatically.</p> <p>
086: *
087: * Features that would be well worth implementing include: adding attributes to
088: * control all of the formatting features of the PrettyPrinter, and removing any
089: * reliance on an external preference file; allowing the formatted source files
090: * to be saved into a different location than the originals.</p>
091: *
092: * @author Stuart Roebuck <a href="mailto:stuart.roebuck@adolos.com">
093: * stuart.roebuck@adolos.com</a>
094: * @created January 22, 2001
095: */
096: public class JavaStyle extends MatchingTask {
097:
098: /**
099: * The individual file specified for styling.
100: */
101: protected File file = null;
102: /**
103: * A collection of filesets to be styled.
104: */
105: protected Vector filesets = new Vector();
106: private int verbosity = Project.MSG_VERBOSE;
107: private boolean quiet = false;
108:
109: /**
110: * Constructor for the JavaStyle object
111: */
112: public JavaStyle() {
113: super ();
114: }
115:
116: /**
117: * Set the name of a single file to be styled.
118: *
119: * @param file the file to be styled
120: */
121: public void setFile(File file) {
122: this .file = file;
123: }
124:
125: /**
126: * Used to force listing of all names of styled files.
127: *
128: * @param verbose "true" or "on"
129: */
130: public void setVerbose(boolean verbose) {
131: if (verbose) {
132: this .verbosity = Project.MSG_INFO;
133: } else {
134: this .verbosity = Project.MSG_VERBOSE;
135: }
136: }
137:
138: /**
139: * If the file does not exist, do not display a diagnostic message or modify
140: * the exit status to reflect an error.
141: *
142: * @param quiet "true" or "on"
143: */
144: public void setQuiet(boolean quiet) {
145: this .quiet = quiet;
146: }
147:
148: /**
149: * Adds a set of files (nested fileset attribute).
150: *
151: * @param set the fileset to add.
152: */
153: public void addFileset(FileSet set) {
154: filesets.addElement(set);
155: }
156:
157: /**
158: * Styles the file(s).
159: *
160: * @exception BuildException can be thrown if no files are specified in the
161: * task.
162: */
163: public void execute() throws BuildException {
164: if (file == null && filesets.size() == 0) {
165: throw new BuildException(
166: "At least one of the file or dir attributes, or a fileset element, must be set.");
167: }
168:
169: // style the single file
170: if (file != null) {
171: if (file.exists()) {
172: if (file.isDirectory()) {
173: log("Directory "
174: + file.getAbsolutePath()
175: + " is not a file, use the dir attribute instead.");
176: } else {
177: log("JavaStyling: " + file.getAbsolutePath());
178: styleFile(file);
179: }
180: } else {
181: log("Could not find file " + file.getAbsolutePath()
182: + " to style.");
183: }
184: }
185:
186: // style the files in the filesets
187: for (int i = 0; i < filesets.size(); i++) {
188: FileSet fs = (FileSet) filesets.elementAt(i);
189: DirectoryScanner ds = fs.getDirectoryScanner(project);
190: String[] files = ds.getIncludedFiles();
191: styleFiles(fs.getDir(project), files);
192: }
193:
194: }
195:
196: /**
197: * Initialise all the preference files for JRefactory. If there are no
198: * existing preference files then create some default ones.
199: *
200: * @throws BuildException if someting goes wrong with the build
201: */
202: public void init() throws BuildException {
203: // Make sure everything is installed properly
204: (new RefactoryInstaller(false)).run();
205: }
206:
207: /**
208: * Style a single file
209: *
210: * @param file The file to be styled.
211: */
212: protected void styleFile(File file) {
213: PrettyPrintFile ppf = new PrettyPrintFile();
214: ppf.setAsk(false);
215: if (ppf.isApplicable(file)) {
216: ppf.apply(file);
217: }
218: }
219:
220: /**
221: * Style a list of files in a given directory.
222: *
223: * @param dir the directory containing the files.
224: * @param files an array of filenames within the directory.
225: */
226: protected void styleFiles(File dir, String[] files) {
227: if (files.length > 0) {
228: log("JavaStyling " + files.length + " files from "
229: + dir.getAbsolutePath());
230: for (int j = 0; j < files.length; j++) {
231: File f = new File(dir, files[j]);
232: log("JavaStyling " + f.getAbsolutePath(), verbosity);
233: styleFile(f);
234: }
235: }
236: }
237:
238: }
|