Browse Source

Added an option to the ContainsSelector to ignore whitespace.

Submitted by Elizabeth Cooper (with some modifications)


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273967 13f79535-47bb-0310-9956-ffa450edef68
master
Bruce Atherton 22 years ago
parent
commit
63ec7ba4ca
4 changed files with 92 additions and 26 deletions
  1. +34
    -26
      docs/manual/CoreTypes/selectors.html
  2. +27
    -0
      src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java
  3. +19
    -0
      src/main/org/apache/tools/ant/types/selectors/SelectorUtils.java
  4. +12
    -0
      src/testcases/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java

+ 34
- 26
docs/manual/CoreTypes/selectors.html View File

@@ -14,18 +14,18 @@


<h3>How to use a Selector</h3> <h3>How to use a Selector</h3>


<p>A selector is an element of FileSet, and appears within it. It can
<p>A selector is an element of FileSet, and appears within it. It can
also be defined outside of any target by using the &lt;selector&gt; tag also be defined outside of any target by using the &lt;selector&gt; tag
and then using it as a reference. and then using it as a reference.
</p> </p>


<p>Different selectors have different attributes. Some selectors can <p>Different selectors have different attributes. Some selectors can
contain other selectors, and these are called
<a href="#selectcontainers"><code>Selector Containers</code></a>.
contain other selectors, and these are called
<a href="#selectcontainers"><code>Selector Containers</code></a>.
There is also a category of selectors that allow There is also a category of selectors that allow
user-defined extensions, called user-defined extensions, called
<a href="#customselect"><code>Custom Selectors</code></a>. <a href="#customselect"><code>Custom Selectors</code></a>.
The ones built in to Ant are called
The ones built in to Ant are called
<a href="#coreselect"><code>Core Selectors</code></a>. <a href="#coreselect"><code>Core Selectors</code></a>.
</p> </p>


@@ -88,6 +88,14 @@
</td> </td>
<td valign="top" align="center">No</td> <td valign="top" align="center">No</td>
</tr> </tr>
<tr>
<td valign="top">ignorewhitespace</td>
<td valign="top">Whether to eliminate whitespace before checking
for the string in the <code>text</code> attribute. Default is
false.
</td>
<td valign="top" align="center">No</td>
</tr>
</table> </table>


<p>Here is an example of how to use the Contains Selector:</p> <p>Here is an example of how to use the Contains Selector:</p>
@@ -164,11 +172,11 @@
<p>The <code>&lt;depend&gt;</code> tag selects files <p>The <code>&lt;depend&gt;</code> tag selects files
whose last modified date is later than another, equivalent file in whose last modified date is later than another, equivalent file in
another location.</p> another location.</p>
<p>The <code>&lt;depend&gt;</code> tag supports the use of a <p>The <code>&lt;depend&gt;</code> tag supports the use of a
contained <a href="mapper.html"><code>&lt;mapper&gt;</code></a> element
to define the location of the file to be compared against. If no
<code>&lt;mapper&gt;</code> element is specified, the
contained <a href="mapper.html"><code>&lt;mapper&gt;</code></a> element
to define the location of the file to be compared against. If no
<code>&lt;mapper&gt;</code> element is specified, the
<code>identity</code> type mapper is used.</p> <code>identity</code> type mapper is used.</p>


<table border="1" cellpadding="2" cellspacing="0"> <table border="1" cellpadding="2" cellspacing="0">
@@ -204,7 +212,7 @@
&lt;/fileset&gt; &lt;/fileset&gt;
</pre></blockquote> </pre></blockquote>


<p>Selects all the Java source files which were modified in the
<p>Selects all the Java source files which were modified in the
1.5 release. 1.5 release.
</p> </p>


@@ -385,11 +393,11 @@


<p>The <code>&lt;present&gt;</code> tag selects files <p>The <code>&lt;present&gt;</code> tag selects files
that have an equivalent file in another directory tree.</p> that have an equivalent file in another directory tree.</p>
<p>The <code>&lt;present&gt;</code> tag supports the use of a <p>The <code>&lt;present&gt;</code> tag supports the use of a
contained <a href="mapper.html"><code>&lt;mapper&gt;</code></a> element
to define the location of the file to be tested against. If no
<code>&lt;mapper&gt;</code> element is specified, the
contained <a href="mapper.html"><code>&lt;mapper&gt;</code></a> element
to define the location of the file to be tested against. If no
<code>&lt;mapper&gt;</code> element is specified, the
<code>identity</code> type mapper is used.</p> <code>identity</code> type mapper is used.</p>


<table border="1" cellpadding="2" cellspacing="0"> <table border="1" cellpadding="2" cellspacing="0">
@@ -408,18 +416,18 @@
</tr> </tr>
<tr> <tr>
<td valign="top">present</td> <td valign="top">present</td>
<td valign="top">Whether we are requiring that a file is present in
<td valign="top">Whether we are requiring that a file is present in
the src directory tree only, or in both the src and the target the src directory tree only, or in both the src and the target
directory tree. Valid values are: directory tree. Valid values are:
<ul> <ul>
<li>srconly - select files only if they are in the src
<li>srconly - select files only if they are in the src
directory tree but not in the target directory tree directory tree but not in the target directory tree
<li>both - select files only if they are present both in the <li>both - select files only if they are present both in the
src and target directory trees src and target directory trees
</ul> </ul>
Default is both. Setting this attribute to &quot;srconly&quot; Default is both. Setting this attribute to &quot;srconly&quot;
is equivalent to wrapping the selector in the &lt;not&gt;
selector container.
is equivalent to wrapping the selector in the &lt;not&gt;
selector container.
</td> </td>
<td valign="top" align="center">No</td> <td valign="top" align="center">No</td>
</tr> </tr>
@@ -433,7 +441,7 @@
&lt;/fileset&gt; &lt;/fileset&gt;
</pre></blockquote> </pre></blockquote>


<p>Selects all the Java source files which are new in the
<p>Selects all the Java source files which are new in the
1.5 release. 1.5 release.
</p> </p>


@@ -560,7 +568,7 @@
soon as it finds a selector that does not select the file, soon as it finds a selector that does not select the file,
so it is not guaranteed to check every selector. so it is not guaranteed to check every selector.
</p> </p>
<p>Here is an example of how to use the And Selector:</p> <p>Here is an example of how to use the And Selector:</p>


<blockquote><pre> <blockquote><pre>
@@ -614,8 +622,8 @@
&lt;/fileset&gt; &lt;/fileset&gt;
</pre></blockquote> </pre></blockquote>


<p>Selects all the HTML files which contain at least two of the three
phrases "project", "taskdef", and "IntrospectionHelper" (this last phrase must
<p>Selects all the HTML files which contain at least two of the three
phrases "project", "taskdef", and "IntrospectionHelper" (this last phrase must
match case exactly). match case exactly).
</p> </p>


@@ -628,7 +636,7 @@
soon as it finds a selector that selects the file, soon as it finds a selector that selects the file,
so it is not guaranteed to check every selector. so it is not guaranteed to check every selector.
</p> </p>
<p>Here is an example of how to use the None Selector:</p> <p>Here is an example of how to use the None Selector:</p>


<blockquote><pre> <blockquote><pre>
@@ -642,7 +650,7 @@
&lt;/fileset&gt; &lt;/fileset&gt;
</pre></blockquote> </pre></blockquote>


<p>Selects only Java files which do not have equivalent java or
<p>Selects only Java files which do not have equivalent java or
class files in the dest directory. class files in the dest directory.
</p> </p>


@@ -653,7 +661,7 @@
<p>The <code>&lt;not&gt;</code> tag reverses the meaning of the <p>The <code>&lt;not&gt;</code> tag reverses the meaning of the
single selector it contains. single selector it contains.
</p> </p>
<p>Here is an example of how to use the Not Selector:</p> <p>Here is an example of how to use the Not Selector:</p>


<blockquote><pre> <blockquote><pre>
@@ -677,7 +685,7 @@
soon as it finds a selector that selects the file, soon as it finds a selector that selects the file,
so it is not guaranteed to check every selector. so it is not guaranteed to check every selector.
</p> </p>
<p>Here is an example of how to use the Or Selector:</p> <p>Here is an example of how to use the Or Selector:</p>


<blockquote><pre> <blockquote><pre>
@@ -691,7 +699,7 @@
&lt;/fileset&gt; &lt;/fileset&gt;
</pre></blockquote> </pre></blockquote>


<p>Selects all the files in the top directory along with all the
<p>Selects all the files in the top directory along with all the
image files below it. image files below it.
</p> </p>




+ 27
- 0
src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java View File

@@ -74,8 +74,10 @@ public class ContainsSelector extends BaseExtendSelector {


private String contains = null; private String contains = null;
private boolean casesensitive = true; private boolean casesensitive = true;
private boolean ignorewhitespace = false;
public final static String CONTAINS_KEY = "text"; public final static String CONTAINS_KEY = "text";
public final static String CASE_KEY = "casesensitive"; public final static String CASE_KEY = "casesensitive";
public final static String WHITESPACE_KEY = "ignorewhitespace";




public ContainsSelector() { public ContainsSelector() {
@@ -90,6 +92,12 @@ public class ContainsSelector extends BaseExtendSelector {
} else { } else {
buf.append("false"); buf.append("false");
} }
buf.append(" ignorewhitespace: ");
if (ignorewhitespace) {
buf.append("true");
} else {
buf.append("false");
}
buf.append("}"); buf.append("}");
return buf.toString(); return buf.toString();
} }
@@ -112,6 +120,15 @@ public class ContainsSelector extends BaseExtendSelector {
this.casesensitive = casesensitive; this.casesensitive = casesensitive;
} }


/**
* Whether to ignore whitespace in the string being searched.
*
* @param whitespace whether to ignore any whitespace (spaces, tabs, etc.) in the searchstring
*/
public void setIgnorewhitespace(boolean ignorewhitespace) {
this.ignorewhitespace = ignorewhitespace;
}

/** /**
* When using this as a custom selector, this method will be called. * When using this as a custom selector, this method will be called.
* It translates each parameter into the appropriate setXXX() call. * It translates each parameter into the appropriate setXXX() call.
@@ -130,6 +147,10 @@ public class ContainsSelector extends BaseExtendSelector {
setCasesensitive(Project.toBoolean( setCasesensitive(Project.toBoolean(
parameters[i].getValue())); parameters[i].getValue()));
} }
else if (WHITESPACE_KEY.equalsIgnoreCase(paramname)) {
setIgnorewhitespace(Project.toBoolean(
parameters[i].getValue()));
}
else { else {
setError("Invalid parameter " + paramname); setError("Invalid parameter " + paramname);
} }
@@ -170,6 +191,9 @@ public class ContainsSelector extends BaseExtendSelector {
if (!casesensitive) { if (!casesensitive) {
userstr = contains.toLowerCase(); userstr = contains.toLowerCase();
} }
if (ignorewhitespace) {
userstr = SelectorUtils.removeWhitespace(userstr);
}
BufferedReader in = null; BufferedReader in = null;
try { try {
in = new BufferedReader(new InputStreamReader( in = new BufferedReader(new InputStreamReader(
@@ -179,6 +203,9 @@ public class ContainsSelector extends BaseExtendSelector {
if (!casesensitive) { if (!casesensitive) {
teststr = teststr.toLowerCase(); teststr = teststr.toLowerCase();
} }
if (ignorewhitespace) {
teststr = SelectorUtils.removeWhitespace(teststr);
}
if (teststr.indexOf(userstr) > -1) { if (teststr.indexOf(userstr) > -1) {
return true; return true;
} }


+ 19
- 0
src/main/org/apache/tools/ant/types/selectors/SelectorUtils.java View File

@@ -555,5 +555,24 @@ strLoop:
return false; return false;
} }


/**
* "Flattens" a string by removing all whitespace (space, tab, linefeed,
* carriage return, and formfeed). This uses StringTokenizer and the
* default set of tokens as documented in the single arguement constructor.
*
* @param input a String to remove all whitespace.
* @return a String that has had all whitespace removed.
*/
public static String removeWhitespace(String input) {
StringBuffer result = new StringBuffer();
if (input != null) {
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()){
result.append(st.nextToken());
}
}
return result.toString();
}

} }



+ 12
- 0
src/testcases/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java View File

@@ -143,6 +143,18 @@ public class ContainsSelectorTest extends BaseSelectorTest {
results = selectionString(s); results = selectionString(s);
assertEquals("TFFFTFFFFFFT", results); assertEquals("TFFFTFFFFFFT", results);


s = (ContainsSelector)getInstance();
s.setText("ApacheAnt");
s.setIgnorewhitespace(true);
results = selectionString(s);
assertEquals("TFFFTFFFFFFT", results);

s = (ContainsSelector)getInstance();
s.setText("A p a c h e A n t");
s.setIgnorewhitespace(true);
results = selectionString(s);
assertEquals("TFFFTFFFFFFT", results);

} }
finally { finally {
cleanupBed(); cleanupBed();


Loading…
Cancel
Save