Browse Source

added the onmissingfiltersfile attribute to filterset. Bugzilla report 19845.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@359262 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 19 years ago
parent
commit
3fb7be5f43
8 changed files with 275 additions and 88 deletions
  1. +2
    -0
      WHATSNEW
  2. +8
    -1
      docs/manual/CoreTypes/filterset.html
  3. +75
    -3
      src/etc/testcases/types/filterset.xml
  4. +1
    -0
      src/etc/testcases/types/filtersetd.txt
  5. +2
    -0
      src/etc/testcases/types/filtersfile1
  6. +1
    -0
      src/etc/testcases/types/filtersfile2
  7. +165
    -84
      src/main/org/apache/tools/ant/types/FilterSet.java
  8. +21
    -0
      src/testcases/org/apache/tools/ant/types/FilterSetTest.java

+ 2
- 0
WHATSNEW View File

@@ -337,6 +337,8 @@ Other changes:


* new resourcesmatch condition. * new resourcesmatch condition.


* added the onmissingfiltersfile attribute to filterset. Bugzilla report 19845.

Changes from Ant 1.6.4 to Ant 1.6.5 Changes from Ant 1.6.4 to Ant 1.6.5
=================================== ===================================




+ 8
- 1
docs/manual/CoreTypes/filterset.html View File

@@ -62,7 +62,7 @@ you should ensure that the set of files being filtered are all text files.
<tr> <tr>
<td vAlign=top>filtersfile</td> <td vAlign=top>filtersfile</td>
<td vAlign=top>Specify a single filtersfile.</td> <td vAlign=top>Specify a single filtersfile.</td>
<td vAlign=top><i>n/a</i></td>
<td vAlign=top><i>none</i></td>
<td vAlign=top align="center">No</td> <td vAlign=top align="center">No</td>
</tr> </tr>
<tr> <tr>
@@ -72,6 +72,13 @@ you should ensure that the set of files being filtered are all text files.
<td vAlign=top><i>true</i></td> <td vAlign=top><i>true</i></td>
<td vAlign=top align="center">No</td> <td vAlign=top align="center">No</td>
</tr> </tr>
<tr>
<td vAlign=top>onmissingfiltersfile</td>
<td vAlign=top>Indicate behavior when a nonexistent <i>filtersfile</i>
is specified. One of "fail", "warn", "ignore". <b>Since Ant 1.7</b></td>
<td vAlign=top>"fail"</td>
<td vAlign=top align="center">No</td>
</tr>
</table> </table>


<h2>Filter</h2> <h2>Filter</h2>


+ 75
- 3
src/etc/testcases/types/filterset.xml View File

@@ -48,10 +48,82 @@
</filterset> </filterset>
</target> </target>


<target name="testFiltersFileElement">
<copy file="filtersetd.txt" tofile="dest4.txt">
<filterset>
<filtersfile file="filtersfile1" />
</filterset>
</copy>
<fail>
<condition>
<not>
<resourcesmatch asText="true">
<file file="dest4.txt" />
<string value="FOO BAR @baz@ @blah@" />
</resourcesmatch>
</not>
</condition>
</fail>
</target>

<target name="testFiltersFileAttribute">
<copy file="filtersetd.txt" tofile="dest5.txt">
<filterset filtersfile="filtersfile1" />
</copy>
<fail>
<condition>
<not>
<resourcesmatch asText="true">
<file file="dest5.txt" />
<string value="FOO BAR @baz@ @blah@" />
</resourcesmatch>
</not>
</condition>
</fail>
</target>

<target name="testMultipleFiltersFiles">
<copy file="filtersetd.txt" tofile="dest6.txt">
<filterset filtersfile="filtersfile1">
<filtersfile file="filtersfile2" />
</filterset>
</copy>
<fail>
<condition>
<not>
<resourcesmatch asText="true">
<file file="dest6.txt" />
<string value="FOO BAR BAZ @blah@" />
</resourcesmatch>
</not>
</condition>
</fail>
</target>

<target name="testMissingFiltersFile">
<copy file="filtersetd.txt" tofile="dest7.txt">
<filterset filtersfile="nonexistentfiltersfile" />
</copy>
</target>

<target name="testAllowMissingFiltersFile">
<copy file="filtersetd.txt" tofile="dest8.txt">
<filterset filtersfile="nonexistentfiltersfile"
onmissingfiltersfile="ignore" />
</copy>
<fail>
<condition>
<not>
<filesmatch file1="filtersetd.txt" file2="dest8.txt" />
</not>
</condition>
</fail>
</target>

<target name="cleanup"> <target name="cleanup">
<delete file="dest1.txt" quiet="true" />
<delete file="dest2.txt" quiet="true" />
<delete file="dest3.txt" quiet="true" />
<delete quiet="true">
<fileset dir="." includes="dest?.txt" />
</delete>
</target> </target>


</project> </project>

+ 1
- 0
src/etc/testcases/types/filtersetd.txt View File

@@ -0,0 +1 @@
@foo@ @bar@ @baz@ @blah@

+ 2
- 0
src/etc/testcases/types/filtersfile1 View File

@@ -0,0 +1,2 @@
foo=FOO
bar=BAR

+ 1
- 0
src/etc/testcases/types/filtersfile2 View File

@@ -0,0 +1 @@
baz=BAZ

+ 165
- 84
src/main/org/apache/tools/ant/types/FilterSet.java View File

@@ -118,7 +118,44 @@ public class FilterSet extends DataType implements Cloneable {
* @param file the file from which filters will be read. * @param file the file from which filters will be read.
*/ */
public void setFile(File file) { public void setFile(File file) {
readFiltersFromFile(file);
filtersFiles.add(file);
}
}

/**
* EnumeratedAttribute to set behavior WRT missing filtersfiles:
* "fail" (default), "warn", "ignore".
* @since Ant 1.7
*/
public static class OnMissing extends EnumeratedAttribute {
private static final String[] VALUES
= new String[] {"fail", "warn", "ignore"};

public static final OnMissing FAIL = new OnMissing("fail");
public static final OnMissing WARN = new OnMissing("warn");
public static final OnMissing IGNORE = new OnMissing("ignore");

private static final int FAIL_INDEX = 0;
private static final int WARN_INDEX = 1;
private static final int IGNORE_INDEX = 2;

/**
* Default constructor.
*/
public OnMissing() {
}

/**
* Convenience constructor.
* @param value the value to set.
*/
public OnMissing(String value) {
setValue(value);
}

//inherit doc
public String[] getValues() {
return VALUES;
} }
} }


@@ -138,6 +175,9 @@ public class FilterSet extends DataType implements Cloneable {


private boolean recurse = true; private boolean recurse = true;
private Hashtable filterHash = null; private Hashtable filterHash = null;
private Vector filtersFiles = new Vector();
private OnMissing onMissingFiltersFile = OnMissing.FAIL;
private boolean readingFiles = false;


/** /**
* List of ordered filters and filter files. * List of ordered filters and filter files.
@@ -169,6 +209,15 @@ public class FilterSet extends DataType implements Cloneable {
if (isReference()) { if (isReference()) {
return getRef().getFilters(); return getRef().getFilters();
} }
//silly hack to avoid stack overflow...
if (!readingFiles) {
readingFiles = true;
for (int i = 0, sz = filtersFiles.size(); i < sz; i++) {
readFiltersFromFile((File) filtersFiles.get(i));
}
filtersFiles.clear();
readingFiles = false;
}
return filters; return filters;
} }


@@ -202,13 +251,12 @@ public class FilterSet extends DataType implements Cloneable {
* *
* @param filtersFile sets the filter file from which to read filters * @param filtersFile sets the filter file from which to read filters
* for this filter set. * for this filter set.
* @exception BuildException if there is a problem reading the filters.
*/ */
public void setFiltersfile(File filtersFile) throws BuildException { public void setFiltersfile(File filtersFile) throws BuildException {
if (isReference()) { if (isReference()) {
throw tooManyAttributes(); throw tooManyAttributes();
} }
readFiltersFromFile(filtersFile);
filtersFiles.add(filtersFile);
} }


/** /**
@@ -292,7 +340,7 @@ public class FilterSet extends DataType implements Cloneable {
throw tooManyAttributes(); throw tooManyAttributes();
} }
if (!filtersFile.exists()) { if (!filtersFile.exists()) {
throw new BuildException("Could not read filters from file "
handleMissingFile("Could not read filters from file "
+ filtersFile + " as it doesn't exist."); + filtersFile + " as it doesn't exist.");
} }
if (filtersFile.isFile()) { if (filtersFile.isFile()) {
@@ -312,13 +360,14 @@ public class FilterSet extends DataType implements Cloneable {
} }
} catch (Exception ex) { } catch (Exception ex) {
throw new BuildException("Could not read filters from file: " throw new BuildException("Could not read filters from file: "
+ filtersFile);
+ filtersFile);
} finally { } finally {
FileUtils.close(in); FileUtils.close(in);
} }
} else { } else {
throw new BuildException("Must specify a file not a directory in "
+ "the filtersfile attribute:" + filtersFile);
handleMissingFile(
"Must specify a file rather than a directory in "
+ "the filtersfile attribute:" + filtersFile);
} }
filterHash = null; filterHash = null;
} }
@@ -338,6 +387,104 @@ public class FilterSet extends DataType implements Cloneable {
return iReplaceTokens(line); return iReplaceTokens(line);
} }


/**
* Add a new filter.
*
* @param filter the filter to be added.
*/
public synchronized void addFilter(Filter filter) {
if (isReference()) {
throw noChildrenAllowed();
}
filters.addElement(filter);
filterHash = null;
}

/**
* Create a new FiltersFile.
*
* @return The filtersfile that was created.
*/
public FiltersFile createFiltersfile() {
if (isReference()) {
throw noChildrenAllowed();
}
return new FiltersFile();
}

/**
* Add a new filter made from the given token and value.
*
* @param token The token for the new filter.
* @param value The value for the new filter.
*/
public synchronized void addFilter(String token, String value) {
if (isReference()) {
throw noChildrenAllowed();
}
addFilter(new Filter(token, value));
}

/**
* Add a Filterset to this filter set.
*
* @param filterSet the filterset to be added to this filterset
*/
public synchronized void addConfiguredFilterSet(FilterSet filterSet) {
if (isReference()) {
throw noChildrenAllowed();
}
for (Enumeration e = filterSet.getFilters().elements(); e.hasMoreElements();) {
addFilter((Filter) e.nextElement());
}
}

/**
* Test to see if this filter set has filters.
*
* @return Return true if there are filters in this set.
*/
public synchronized boolean hasFilters() {
return getFilters().size() > 0;
}

/**
* Clone the filterset.
*
* @return a deep clone of this filterset.
*
* @throws BuildException if the clone cannot be performed.
*/
public synchronized Object clone() throws BuildException {
if (isReference()) {
return ((FilterSet) getRef()).clone();
}
try {
FilterSet fs = (FilterSet) super.clone();
fs.filters = (Vector) getFilters().clone();
fs.setProject(getProject());
return fs;
} catch (CloneNotSupportedException e) {
throw new BuildException(e);
}
}

/**
* Set the behavior WRT missing filtersfiles.
* @param onMissingFiltersFile the OnMissing describing the behavior.
*/
public void setOnMissingFiltersFile(OnMissing onMissingFiltersFile) {
this.onMissingFiltersFile = onMissingFiltersFile;
}

/**
* Get the onMissingFiltersFile setting.
* @return the OnMissing instance.
*/
public OnMissing getOnMissingFiltersFile() {
return onMissingFiltersFile;
}

/** /**
* Does replacement on the given string with token matching. * Does replacement on the given string with token matching.
* This uses the defined begintoken and endtoken values which default * This uses the defined begintoken and endtoken values which default
@@ -438,87 +585,21 @@ public class FilterSet extends DataType implements Cloneable {
return value; return value;
} }


/**
* Add a new filter.
*
* @param filter the filter to be added.
*/
public synchronized void addFilter(Filter filter) {
if (isReference()) {
throw noChildrenAllowed();
}
filters.addElement(filter);
filterHash = null;
}

/**
* Create a new FiltersFile.
*
* @return The filtersfile that was created.
*/
public FiltersFile createFiltersfile() {
if (isReference()) {
throw noChildrenAllowed();
}
return new FiltersFile();
}

/**
* Add a new filter made from the given token and value.
*
* @param token The token for the new filter.
* @param value The value for the new filter.
*/
public synchronized void addFilter(String token, String value) {
if (isReference()) {
throw noChildrenAllowed();
private void addFiltersFile(File f) {
if (!filtersFiles.contains(f)) {
filtersFiles.add(f);
} }
addFilter(new Filter(token, value));
} }


/**
* Add a Filterset to this filter set.
*
* @param filterSet the filterset to be added to this filterset
*/
public synchronized void addConfiguredFilterSet(FilterSet filterSet) {
if (isReference()) {
throw noChildrenAllowed();
}
for (Enumeration e = filterSet.getFilters().elements(); e.hasMoreElements();) {
addFilter((Filter) e.nextElement());
private void handleMissingFile(String message) {
switch (onMissingFiltersFile.getIndex()) {
case OnMissing.IGNORE_INDEX:
return;
case OnMissing.FAIL_INDEX:
throw new BuildException(message);
case OnMissing.WARN_INDEX:
log(message, Project.MSG_WARN);
} }
} }


/**
* Test to see if this filter set has filters.
*
* @return Return true if there are filters in this set.
*/
public synchronized boolean hasFilters() {
return getFilters().size() > 0;
}

/**
* Clone the filterset.
*
* @return a deep clone of this filterset.
*
* @throws BuildException if the clone cannot be performed.
*/
public synchronized Object clone() throws BuildException {
if (isReference()) {
return ((FilterSet) getRef()).clone();
} else {
try {
FilterSet fs = (FilterSet) super.clone();
fs.filters = (Vector) getFilters().clone();
fs.setProject(getProject());
return fs;
} catch (CloneNotSupportedException e) {
throw new BuildException(e);
}
}
}
} }


+ 21
- 0
src/testcases/org/apache/tools/ant/types/FilterSetTest.java View File

@@ -141,6 +141,27 @@ public class FilterSetTest extends BuildFileTest {
assertEquals("value1", filters.get("token1")); assertEquals("value1", filters.get("token1"));
} }


public void testFiltersFileElement() {
executeTarget("testFiltersFileElement");
}

public void testFiltersFileAttribute() {
executeTarget("testFiltersFileAttribute");
}

public void testMultipleFiltersFiles() {
executeTarget("testMultipleFiltersFiles");
}

public void testMissingFiltersFile() {
expectBuildException("testMissingFiltersFile",
"should fail due to missing filtersfile");
}

public void testAllowMissingFiltersFile() {
executeTarget("testAllowMissingFiltersFile");
}

private boolean compareFiles(String name1, String name2) { private boolean compareFiles(String name1, String name2) {
File file1 = new File(System.getProperty("root"), name1); File file1 = new File(System.getProperty("root"), name1);
File file2 = new File(System.getProperty("root"), name2); File file2 = new File(System.getProperty("root"), name2);


Loading…
Cancel
Save