KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > ant > SubDirInfoTask


1 /**
2  * $RCSfile: SubDirInfoTask.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/03/08 02:09:06 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.ant;
13
14 import org.apache.tools.ant.Task;
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Project;
17
18 import java.io.File JavaDoc;
19
20 /**
21  * A simple ant task to return the sub directories of a given dir as a comma delimited string.
22  *
23  * This class does not need jdk 1.5 to compile.
24  */

25 public class SubDirInfoTask extends Task {
26
27     public static final String JavaDoc DEFAULT_DELIM = ",";
28
29     private File JavaDoc dir;
30     private String JavaDoc property;
31     private String JavaDoc delimiter;
32     private String JavaDoc ifexists;
33     private String JavaDoc except;
34
35     public SubDirInfoTask() {
36     }
37
38     public File JavaDoc getDir() {
39         return dir;
40     }
41
42     public void setDir(File JavaDoc dir) {
43         this.dir = dir;
44     }
45
46     public String JavaDoc getProperty() {
47         return property;
48     }
49
50     public void setProperty(String JavaDoc property) {
51         this.property = property;
52     }
53
54     public String JavaDoc getDelimiter() {
55         if (delimiter == null) {
56             return DEFAULT_DELIM;
57         }
58         return delimiter;
59     }
60
61     public void setDelimiter(String JavaDoc delimiter) {
62         this.delimiter = delimiter;
63     }
64
65     public String JavaDoc getIfexists() {
66         return ifexists;
67     }
68
69     public void setIfexists(String JavaDoc ifexists) {
70         this.ifexists = ifexists;
71     }
72
73     public String JavaDoc getExcept() {
74         return except;
75     }
76
77     public void setExcept(String JavaDoc except) {
78         this.except = except;
79     }
80
81     public void execute() throws BuildException {
82         // Get the siblings of the given directory, add sub directory names to the property
83
File JavaDoc[] subdirs = dir.listFiles();
84         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
85         String JavaDoc value = null;
86         String JavaDoc sep = "";
87         if (subdirs != null) {
88             for (int i=0; i<subdirs.length; i++) {
89                 File JavaDoc subdir = subdirs[i];
90                 boolean add = false;
91                 if (subdir.isDirectory()) {
92                     if (getIfexists() != null) {
93                         File JavaDoc file = new File JavaDoc(subdir, getIfexists());
94                         if (file.exists()) {
95                             add = true;
96                         }
97                     }
98                     else {
99                         add = true;
100                     }
101                 }
102                 if (add && !subdir.getName().equals(except)) {
103                     buf.append(sep).append(subdir.getName());
104                     sep = getDelimiter();
105                 }
106             }
107         }
108         if (buf.length() > 0) {
109             value = buf.toString();
110         }
111         if (value == null) {
112             log("No tokens found.", Project.MSG_DEBUG);
113         }
114         else {
115             log("Setting property '" + property + "' to " + value, Project.MSG_DEBUG);
116             if (buf.length() >= 0) {
117                 getProject().setProperty(property, value);
118             }
119         }
120     }
121 }
122
Popular Tags