ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 42603] New: - enhance <available> to search for files using wildecards
Date Wed, 06 Jun 2007 09:23:08 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=42603>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=42603

           Summary: enhance <available> to search for files using wildecards
           Product: Ant
           Version: 1.6.5
          Platform: Other
        OS/Version: Windows XP
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: Core
        AssignedTo: dev@ant.apache.org
        ReportedBy: christof.engel@web.de


As I use ant to syncronize my buildprocess over seperated computers, I need to
know, if some is still building or not. I do that over filesystem with files
like "abc.building" "def.building" etc.

Now, the main task should wait until none of the others is building any more. 
Until today (I modified my ant version today to achieve the goal) I have to
write it like this:
    <waitfor maxwait="60" maxwaitunit="minute" checkevery="5"
checkeveryunit="second" timeoutproperty="waitforOthersTimeout">
      <not>
        <or>
      <available file="${buildsrv_exchange}\list_abc.building"/>
      <available file="${buildsrv_exchange}\list_def.building"/>
      <available file="${buildsrv_exchange}\list_something.building"/>
      <available file="${buildsrv_exchange}\list_abc.shouldstart"/>
      <available file="${buildsrv_exchange}\list_def.shouldstart"/>
      <available file="${buildsrv_exchange}\list_something.shouldstart"/>
        </or>
      </not>
    </waitfor>

I now can write this like this:
 <available MatchFilename="list*.building" MatchFilepath="${buildsrv_exchange}"/>
 <available MatchFilename="list*.shouldstart"     
MatchFilepath="${buildsrv_exchange}"/>

I have not only 2 tasks to wait for, but many, so it is in deed a gread
enhancement if I can be able to use wildcards like '*" and '?'.

The code is not perfect, but a quick-and-dirty hack for <available>, the
"MatchFilepath" must be set in that case, you can do that better and use the
existend "filepath" instead of introducing a new "MatchFilepath". You can also
easyly add a "scanSubdir" feature.
But here it is (additional lines for Available.java (in v.1.6.5):
add:
    private String matchFilepath = null;
enhance:
    private boolean checkFile() {
        if (matchFilepath != null) {
            return checkMatchFilePath(file, false);
        }

add:
  /**
   * Check if a given filename exists and matches the pattern (no RegEx)
   */

  public void setMatchFilename(String filename) {
    this.filename = filename;
  }
  public void setMatchFilepath(String path) {
    this.matchFilepath = path;
    this.file = new File(path);
  }

	private boolean stringCompare(String strCompare, String strSource) {
		int i = 0;
		int l = strCompare.length();
		if (strSource.length() < l) {
			return false;
		}
		if (strCompare.equalsIgnoreCase(strSource)) {
			return true;
		}
		boolean bMatch = true;
		while (bMatch && (i < l)) {
			switch (strCompare.charAt(i)) {
			case '?':
				break; // ignore single char
			case '*':
				i = l; // ignore rest
				break;
			default:
				if (strCompare.charAt(i) != strSource.charAt(i)) {
					bMatch = false; // Stop here.
				}
				break;
			}
			i++;
		}
		return bMatch;
	}

	private boolean filenameMatchesFilter(String sFilename, String sFilter) {
		if (sFilter.length() == 0) 
		  return true;
		int i = sFilter.lastIndexOf(".");
		int j;
		// filter has file extension?
		if (i > 0) {
			j = sFilename.lastIndexOf(".");
			if (j > 0) {
				// check file extension seperatly
				if (stringCompare(sFilter.substring(i + 1), sFilename.substring(j + 1))) {
					// check filename (without ext)
					return (stringCompare(sFilter.substring(0, i), sFilename.substring(0, j)));
				}
			} else {
				// file has no extension, check filter extension is .*
				if (stringCompare(sFilter.substring(i + 1), "*")) {
					// check filename (without ext)
					return (stringCompare(sFilter.substring(0, i - 1), sFilename));
				}
			}
		} else {
			// filter without file extension
			return (stringCompare(sFilter, sFilename));
		}
		return false;
	}

	private boolean fileMatchFilter(String fi, String filename) {
		if (fi != null) {
			if ((fi == "*.*") || (fi == "*")) {
				// this normally means "all files"
				return true;
			}
			if (filenameMatchesFilter(filename.toUpperCase(), fi.toUpperCase())) {
				return true;
			}
		}
		return false;
	}

  private boolean checkMatchFilePath(File recurDir, boolean scanSubdirs) {
    if ((!recurDir.exists()) || (!recurDir.isDirectory())) {
      return false;
    }
    int i;
    File aDirectory = null;
    File[] aDirList = recurDir.listFiles();
    if (aDirList != null && aDirList.length > 0) {
      for (i = 0; i < aDirList.length; i++) {
        aDirectory = aDirList[i];
        if (aDirectory.canRead()) {
          if (aDirectory.isDirectory()) {
            if (scanSubdirs) {
              if (checkMatchFilePath(aDirectory, true)) {
                return true;
              }
            }
          } else {
            if (fileMatchFilter(this.filename, aDirectory.getName())) {
              return true;
            }
          }
        }
      }
    }
    return false;
  }

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message