001: /**
002: * $RCSfile$
003: * $Revision: 1106 $
004: * $Date: 2005-03-07 18:09:06 -0800 (Mon, 07 Mar 2005) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.ant;
011:
012: import org.apache.tools.ant.Task;
013: import org.apache.tools.ant.BuildException;
014: import org.apache.tools.ant.Project;
015:
016: import java.io.File;
017:
018: /**
019: * A simple ant task to return the sub directories of a given dir as a comma delimited string.
020: *
021: * This class does not need jdk 1.5 to compile.
022: */
023: public class SubDirInfoTask extends Task {
024:
025: public static final String DEFAULT_DELIM = ",";
026:
027: private File dir;
028: private String property;
029: private String delimiter;
030: private String ifexists;
031: private String except;
032:
033: public SubDirInfoTask() {
034: }
035:
036: public File getDir() {
037: return dir;
038: }
039:
040: public void setDir(File dir) {
041: this .dir = dir;
042: }
043:
044: public String getProperty() {
045: return property;
046: }
047:
048: public void setProperty(String property) {
049: this .property = property;
050: }
051:
052: public String getDelimiter() {
053: if (delimiter == null) {
054: return DEFAULT_DELIM;
055: }
056: return delimiter;
057: }
058:
059: public void setDelimiter(String delimiter) {
060: this .delimiter = delimiter;
061: }
062:
063: public String getIfexists() {
064: return ifexists;
065: }
066:
067: public void setIfexists(String ifexists) {
068: this .ifexists = ifexists;
069: }
070:
071: public String getExcept() {
072: return except;
073: }
074:
075: public void setExcept(String except) {
076: this .except = except;
077: }
078:
079: public void execute() throws BuildException {
080: // Get the siblings of the given directory, add sub directory names to the property
081: File[] subdirs = dir.listFiles();
082: StringBuffer buf = new StringBuffer();
083: String value = null;
084: String sep = "";
085: if (subdirs != null) {
086: for (int i = 0; i < subdirs.length; i++) {
087: File subdir = subdirs[i];
088: boolean add = false;
089: if (subdir.isDirectory()) {
090: if (getIfexists() != null) {
091: File file = new File(subdir, getIfexists());
092: if (file.exists()) {
093: add = true;
094: }
095: } else {
096: add = true;
097: }
098: }
099: if (add && !subdir.getName().equals(except)) {
100: buf.append(sep).append(subdir.getName());
101: sep = getDelimiter();
102: }
103: }
104: }
105: if (buf.length() > 0) {
106: value = buf.toString();
107: }
108: if (value == null) {
109: log("No tokens found.", Project.MSG_DEBUG);
110: } else {
111: log("Setting property '" + property + "' to " + value,
112: Project.MSG_DEBUG);
113: if (buf.length() >= 0) {
114: getProject().setProperty(property, value);
115: }
116: }
117: }
118: }
|