001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.utils.comment.ant;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.util.LinkedList;
042: import java.util.List;
043: import org.apache.tools.ant.BuildException;
044: import org.apache.tools.ant.DirectoryScanner;
045: import org.apache.tools.ant.Task;
046: import org.apache.tools.ant.types.FileSet;
047: import org.netbeans.installer.infra.utils.comment.CommentCorrecter;
048: import org.netbeans.installer.infra.utils.comment.handlers.FileHandler;
049: import org.netbeans.installer.infra.utils.comment.utils.Utils;
050:
051: /**
052: * This is the apache ant interface of the initial comment correcting utility.
053: *
054: * <p>
055: * The ant task defined in this class supports two arguments: <code>text</code> -
056: * its value should be the path to the file which contains the desired intiial
057: * comment text; 'lineLength' - a positive integer defining the requried line length
058: * for the comment (optional).
059: *
060: * <p>
061: * The task supports the nested <code><fileset></code> elements which should be
062: * used to define the set of files for which the correction should take place.
063: *
064: * @author Kirill Sorokin
065: */
066: public final class CommentCorrecterTask extends Task {
067: /////////////////////////////////////////////////////////////////////////////////
068: // Instance
069: /**
070: * The file with the desired initial comment text.
071: */
072: private File text;
073:
074: /**
075: * The desired line length for the comment.
076: */
077: private int lineLength;
078:
079: /**
080: * List of <code>FileSet</code> which define the set of files for which the
081: * comment correction should take place.
082: */
083: private List<FileSet> filesets;
084:
085: // constructor //////////////////////////////////////////////////////////////////
086: /**
087: * The default and only constructor for the ant task. It only sets the defaults
088: * values for the class fields.
089: */
090: public CommentCorrecterTask() {
091: lineLength = CommentCorrecter.DEFAULT_LINE_LENGTH;
092: filesets = new LinkedList<FileSet>();
093: }
094:
095: // setters //////////////////////////////////////////////////////////////////////
096: /**
097: * Setter for the <code>text</code> property.
098: *
099: * @param path New value for the <code>text</code> property.
100: */
101: public void setText(String path) {
102: text = new File(path).getAbsoluteFile();
103: }
104:
105: /**
106: * Setter for the <code>lineLength</code> property.
107: *
108: * @param string New value for the <code>text</code> property as a string - it
109: * will be converted to an integer.
110: */
111: public void setLineLength(String string) {
112: try {
113: lineLength = Integer.parseInt(string);
114: } catch (NumberFormatException e) {
115: log(e.getMessage());
116: }
117: }
118:
119: /**
120: * Adds a new <code>FileSet</code> to the list. The list of file sets will be
121: * used to perform the comment correction.
122: *
123: * @param fileset The file set to add to the list.
124: */
125: public void addFileSet(FileSet fileset) {
126: filesets.add(fileset);
127: }
128:
129: /**
130: * Executes the ant task, performing initial comment correction on the set of
131: * files defined in <code>filesets</code>. Overrides the <code>execute()</code>
132: * method in the <code>Task</code> class.
133: *
134: * @throws org.apache.tools.ant.BuildException if parameters validation fails or
135: * an I/O error occurs
136: */
137: @Override
138: public void execute() throws BuildException {
139: if (text == null) {
140: throw new BuildException(
141: "The 'text' attribute is required."); // NOI18N
142: }
143:
144: try {
145: final String comment = Utils.readFile(text);
146:
147: for (FileSet fileset : filesets) {
148: final DirectoryScanner scanner = fileset
149: .getDirectoryScanner(getProject());
150:
151: for (String filename : scanner.getIncludedFiles()) {
152: final File file = new File(fileset
153: .getDir(getProject()), filename);
154:
155: log(file.getAbsolutePath());
156:
157: final FileHandler handler = CommentCorrecter
158: .getInstance().getHandler(file);
159: if (handler != null) {
160: handler.load(file);
161:
162: final String current = handler
163: .getCurrentComment();
164: if (current == null) {
165: log(" ...inserting " + // NOI18N
166: "the initial comment"); // NOI18N
167: handler.insertComment(comment, lineLength);
168: } else {
169: final String correct = handler
170: .getCorrectComment(comment,
171: lineLength);
172: if (current.equals(correct)) {
173: log(" ...skipping -- " + // NOI18N
174: "the comment is up to date"); // NOI18N
175: } else {
176: log(" ...updating " + // NOI18N
177: "the initial comment"); // NOI18N
178: handler.updateComment(comment,
179: lineLength);
180: }
181: }
182:
183: handler.save(file);
184: } else {
185: log(" ...skipping -- " + // NOI18N
186: "not recognized by any handler"); // NOI18N
187: }
188: }
189: }
190: } catch (IOException e) {
191: throw new BuildException(e);
192: }
193: }
194: }
|