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.build.ant;
038:
039: import java.io.File;
040: import java.io.FileFilter;
041: import java.io.FileOutputStream;
042: import java.io.IOException;
043: import org.apache.tools.ant.BuildException;
044: import org.apache.tools.ant.Task;
045:
046: /**
047: *
048: * @author Dmitry Lipin
049: */
050: public class WriteFileList extends Task {
051: private String dir;
052: private String output;
053: private String mask;
054:
055: private void check(String s, String desc) throws BuildException {
056: if (s == null) {
057: throw new BuildException("Error! Parameter '" + desc
058: + "' can`t be null!!");
059: }
060: }
061:
062: private void write(StringBuilder sb, String s) {
063: sb.append(s);
064: sb.append(System.getProperty("line.separator"));
065:
066: }
067:
068: private void listFile(File parent, File f, StringBuilder sb)
069: throws IOException {
070: String path = f.getPath();
071: String parentPath = parent.getPath();
072: path = path.substring(parentPath.length());
073: path = path.replaceAll("\\\\", "/");
074: if (path.length() > 0) {
075: path = path.substring(1);
076: }
077:
078: if (f.isFile()) {
079: if (path.length() > 0 && path.matches(mask)) {
080: write(sb, path);
081: }
082: } else if (f.isDirectory()) {
083:
084: if (path.length() > 0) {
085: path = path + "/";
086: if (path.matches(mask)) {
087: write(sb, path);
088: }
089: }
090: File[] dirs = f.listFiles(new FileFilter() {
091: public boolean accept(File pathname) {
092: return (pathname.isDirectory());
093: }
094: });
095:
096: for (File file : dirs) {
097: listFile(parent, file, sb);
098: }
099:
100: File[] files = f.listFiles(new FileFilter() {
101: public boolean accept(File pathname) {
102: return (pathname.isFile() && !pathname
103: .isDirectory());
104: }
105: });
106: for (File file : files) {
107: listFile(parent, file, sb);
108: }
109: }
110: }
111:
112: public void execute() throws BuildException {
113: check(dir, "starting directory");
114: check(output, "output file");
115: check(mask, "file mask");
116:
117: File root = new File(dir);
118: if (!root.equals(root.getAbsoluteFile())) {
119: root = new File(getProject().getBaseDir(), dir);
120: }
121: File outFile = new File(output);
122: if (!outFile.equals(outFile.getAbsoluteFile())) {
123: outFile = new File(getProject().getBaseDir(), output);
124: }
125: FileOutputStream fos = null;
126:
127: log("Root directory : " + root);
128: log("Output file : " + outFile);
129: log("Mask : " + mask);
130:
131: try {
132: StringBuilder sb = new StringBuilder();
133: listFile(root, root, sb);
134: fos = new FileOutputStream(outFile);
135: fos.write(sb.toString().getBytes());
136: } catch (IOException ex) {
137: throw new BuildException(ex);
138: } finally {
139: if (fos != null) {
140: try {
141: fos.close();
142: } catch (IOException ex) {
143: throw new BuildException(ex);
144: }
145: }
146: }
147:
148: }
149:
150: public void setDir(final String dir) {
151: this .dir = dir;
152: }
153:
154: public void setOutput(final String output) {
155: this .output = output;
156: }
157:
158: public void setMask(final String mask) {
159: this.mask = mask;
160: }
161:
162: }
|