jhm 2005/07/19 23:45:30 Modified: src/main/org/apache/tools/ant/types/selectors/modifiedselector ModifiedSelector.java src/etc/testcases/types/resources/selectors build.xml docs/manual/CoreTypes selectors.html resources.html src/etc/testcases/types selectors.xml src/resources/org/apache/tools/ant/types/resources/selectors antlib.xml src/testcases/org/apache/tools/ant/types/selectors ModifiedSelectorTest.java Log: Greetings from Hackathon: Introduce ResourceSelector in <modified> Revision Changes Path 1.17 +83 -3 ant/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java Index: ModifiedSelector.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- ModifiedSelector.java 12 Jun 2005 17:55:14 -0000 1.16 +++ ModifiedSelector.java 20 Jul 2005 06:45:30 -0000 1.17 @@ -33,7 +33,11 @@ import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileResource; +import org.apache.tools.ant.types.resources.selectors.ResourceSelector; import org.apache.tools.ant.types.selectors.BaseExtendSelector; +import org.apache.tools.ant.util.FileUtils; /** @@ -228,10 +232,11 @@ * a nested <i><param name="algorithm.provider" value="MyProvider"/></i>. * * - * @version 2004-07-12 + * @version 2005-07-19 * @since Ant 1.6 */ -public class ModifiedSelector extends BaseExtendSelector implements BuildListener { +public class ModifiedSelector extends BaseExtendSelector + implements BuildListener, ResourceSelector { // ----- attributes ----- @@ -261,6 +266,12 @@ /** Are directories selected? */ private boolean selectDirectories = true; + /** + * Should Resources whithout an InputStream, and + * therefore without checking, be selected? + */ + private boolean selectResourcesWithoutInputStream = true; + /** Delay the writing of the cache file */ private boolean delayUpdate = true; @@ -507,13 +518,72 @@ /** + * Implementation of ResourceSelector.isSelected(). + * + * @param resource The resource to check + * @return whether the resource is selected + * @see org.apache.tools.ant.types.resources.selectors.ResourceSelector#isSelected(org.apache.tools.ant.types.Resource) + */ + public boolean isSelected(Resource resource) { + if (resource.isFilesystemOnly()) { + // We have a 'resourced' file, so reconvert it and use + // the 'old' implementation. + FileResource fileResource = (FileResource) resource; + File file = fileResource.getFile(); + String filename = fileResource.getName(); + File basedir = fileResource.getBaseDir(); + return isSelected(basedir, filename, file); + } else { + try { + // How to handle non-file-Resources? I copy temporarily the + // resource to a file and use the file-implementation. + FileUtils fu = FileUtils.getFileUtils(); + File tmpFile = fu.createTempFile("modified-", ".tmp", null); + Resource tmpResource = new FileResource(tmpFile); + fu.copyResource(resource, tmpResource); + boolean isSelected = isSelected(tmpFile.getParentFile(), + tmpFile.getName(), + resource.toLongString()); + tmpFile.delete(); + return isSelected; + } catch (UnsupportedOperationException uoe) { + log("The resource '" + + resource.getName() + + "' does not provide an InputStream, so it is not checked. " + + "Akkording to 'selres' attribute value it is " + + ((selectResourcesWithoutInputStream) ? "" : " not") + + "selected.", Project.MSG_INFO); + return selectResourcesWithoutInputStream; + } catch (Exception e) { + throw new BuildException(e); + } + } + } + + + /** * Implementation of BaseExtendSelector.isSelected(). + * * @param basedir as described in BaseExtendSelector * @param filename as described in BaseExtendSelector * @param file as described in BaseExtendSelector * @return as described in BaseExtendSelector */ public boolean isSelected(File basedir, String filename, File file) { + return isSelected(basedir, filename, file.getAbsolutePath()); + } + + + /** + * The business logic of this selector for use as ResourceSelector of + * FileSelector. + * + * @param basedir as described in BaseExtendSelector + * @param filename as described in BaseExtendSelector + * @param cacheKey the name for the key for storing the hashvalue + * @return + */ + private boolean isSelected(File basedir, String filename, String cacheKey) { validate(); File f = new File(basedir, filename); @@ -524,7 +594,7 @@ // Get the values and do the comparison String cachedValue = String.valueOf(cache.get(f.getAbsolutePath())); - String newValue = algorithm.getValue(f); + String newValue = algorithm.getValue(f); boolean rv = (comparator.compare(cachedValue, newValue) != 0); @@ -601,6 +671,15 @@ /** + * Support for <i>selres</i> attribute. + * @param newValue the new value + */ + public void setSelres(boolean newValue) { + this.selectResourcesWithoutInputStream = newValue; + } + + + /** * Getter for the modified count * @return modified count */ @@ -964,4 +1043,5 @@ return new String[] {"equal", "rule" }; } } + } 1.3 +8 -0 ant/src/etc/testcases/types/resources/selectors/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/types/resources/selectors/build.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- build.xml 31 May 2005 19:18:55 -0000 1.2 +++ build.xml 20 Jul 2005 06:45:30 -0000 1.3 @@ -407,4 +407,12 @@ <target name="all" depends="name,testexists,instanceof,testtype,testdate,testsize,logical" /> + + + <!-- + The tests for oata.types.selectors.ModifiedSelectorTest as + ResourceSelector are in its test-buildfile src\etc\testcases\types\selectors.xml. + --> + + </project> 1.35 +27 -14 ant/docs/manual/CoreTypes/selectors.html Index: selectors.html =================================================================== RCS file: /home/cvs/ant/docs/manual/CoreTypes/selectors.html,v retrieving revision 1.34 retrieving revision 1.35 diff -u -r1.34 -r1.35 --- selectors.html 29 Apr 2005 18:58:12 -0000 1.34 +++ selectors.html 20 Jul 2005 06:45:30 -0000 1.35 @@ -659,6 +659,12 @@ The comparison, computing of the hashvalue and the store is done by implementation of special interfaces. Therefore they may provide additional parameters.</p> + <p>The <code><modified></code> selector can be used as ResourceSelector. + In that case it maps simple file resources to files and does its job. If the + resource is from another type, the <code><modified></code> selector tries + to (<b>attention!</b>) copy the content into a local file for computing the + hashvalue.</p> + <table border="1" cellpadding="2" cellspacing="0"> <tr> <td valign="top"><b>Attribute</b></td> @@ -730,6 +736,13 @@ <td valign="top" align="center"> No, defaults to <i>true</i> </td> </tr> <tr> + <td valign="top"> selres </td> + <td valign="top"> Should Resources whithout an InputStream, and + therefore without checking, be selected? (boolean) </td> + <td valign="top" align="center"> No, defaults to <i>true</i>. Only relevant + when used as ResourceSelector. </td> + </tr> + <tr> <td valign="top"> delayupdate </td> <td valign="top"> If set to <i>true</i>, the storage of the cache will be delayed until the next finished BuildEvent; task finished, target finished or build finished, @@ -932,7 +945,7 @@ <h4>Script Selector</h4> <p> - The <code><scriptselector></code> element enables you + The <code><scriptselector></code> element enables you to write a complex selection algorithm in any <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a> supported language.</p> @@ -958,22 +971,22 @@ <td valign="top">filename of the script</td> <td valign="top" align="center">no</td> </tr> - </table> + </table> <p> If no <code>src</code> attribute is supplied, the script must be nested - inside the selector declaration. + inside the selector declaration. </p> <p>The embedded script is invoked for every test, with - the bean <code>self</code> + the bean <code>self</code> is bound to the selector. It has an attribute <code>selected</code> must can be set using <code>setSelected(boolean)</code> to select that file. - + <p> - + The following beans are configured for every script, alongside the classic set of project, properties, and targets. - + <table border="1" cellpadding="2" cellspacing="0"> <tr> <td valign="top"><b>Bean</b></td> @@ -1000,13 +1013,13 @@ <td valign="top">Fileset base directory</td> <td valign="top" >java.io.File</td> </tr> - - </table> + + </table> <p> The <code>self</code> bean maps to the selector, which has the following - attributes. Only the <code>selected</code> flag is writeable, the rest + attributes. Only the <code>selected</code> flag is writeable, the rest are read only via their getter methods. - + <table border="1" cellpadding="2" cellspacing="0"> <tr> <td valign="top"><b>Attribute</b></td> @@ -1034,7 +1047,7 @@ <td valign="top" >java.io.File</td> </tr> </table> - + <p> Example </p> @@ -1053,7 +1066,7 @@ </scriptselector> </pre> Select files whose filename length is even. - + <a name="selectcontainers"></a> <h3>Selector Containers</h3> @@ -1450,4 +1463,4 @@ </body> -</html> +</html> \ No newline at end of file 1.3 +4 -2 ant/docs/manual/CoreTypes/resources.html Index: resources.html =================================================================== RCS file: /home/cvs/ant/docs/manual/CoreTypes/resources.html,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- resources.html 31 May 2005 19:18:55 -0000 1.2 +++ resources.html 20 Jul 2005 06:45:30 -0000 1.3 @@ -154,7 +154,7 @@ </tr> </table> <p>The classpath along which to search for a Java resource - can also be specified by means of one or more nested + can also be specified by means of one or more nested <code><a href="../using.html#path">classpath</a></code> elements. </p> @@ -347,6 +347,8 @@ - select resources selected by no nested resource selectors.</li> <li><a href="#rsel.majority">majority</a> - select resources selected by a majority of nested resource selectors.</li> + <li><a href="selectors.html#modified">modified</a> - select resources which + content has changed.</li> </ul> <h4><a name="rsel.name">name</a></h4> @@ -597,4 +599,4 @@ Reserved.</p> </body> -</html> +</html> \ No newline at end of file 1.10 +88 -0 ant/src/etc/testcases/types/selectors.xml Index: selectors.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/types/selectors.xml,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- selectors.xml 12 Jul 2004 15:04:13 -0000 1.9 +++ selectors.xml 20 Jul 2005 06:45:30 -0000 1.10 @@ -261,4 +261,92 @@ <property name="fs.full.value" refid="fs.full"/> </target> + + <target name="modifiedselectortest-ResourceSimple"> + <fail message="Didnt get the required numbers of Resources."> + <condition> + <not> + <resourcecount when="equal" count="3"> + <restrict> + <resources> + <file file="foo" /> + <resource name="foo" /> + <file file="foo" basedir="${basedir}" /> + </resources> + <modified selres="true" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/> + </restrict> + </resourcecount> + </not> + </condition> + </fail> + </target> + + <target name="modifiedselectortest-ResourceSelresTrue"> + <fail message="Got the Resource, but should."> + <condition> + <not> + <resourcecount when="equal" count="1"> + <restrict> + <resources> + <resource name="notExisting" /> + </resources> + <modified selres="true" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/> + </restrict> + </resourcecount> + </not> + </condition> + </fail> + </target> + + <target name="modifiedselectortest-ResourceSelresFalse"> + <fail message="Got the Resource, but should not."> + <condition> + <not> + <resourcecount when="equal" count="0"> + <restrict> + <resources> + <resource name="notExisting" /> + </resources> + <modified selres="false" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/> + </restrict> + </resourcecount> + </not> + </condition> + </fail> + </target> + + <target name="modifiedselectortest-scenario-resourceSimple" depends="modifiedselectortest-scenario-prepare"> + <macrodef name="check"> + <attribute name="count"/> + <attribute name="message"/> + <sequential> + <fail message="@{message}"> + <condition> + <not> + <resourcecount when="equal" count="@{count}"> + <restrict> + <resources> + <fileset dir="${test.dir}/src"/> + </resources> + <modified selres="false" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/> + </restrict> + </resourcecount> + </not> + </condition> + </fail> + </sequential> + </macrodef> + <!-- select first time and create cachefile --> + <check count="14" message="Initial set of files not ok."/> + + <!-- check second time: nothing should be selected --> + <check count="0" message="Selected files but shouldnt."/> + + <!-- 'modify' the source files --> + <antcall target="modifiedselectortest-scenario-makeDirty"/> + + <!-- copy third time: only the files with new CONTENT should be copied --> + <check count="2" message="Didnt select the 2 modified files."/> + </target> + </project> \ No newline at end of file 1.2 +2 -0 ant/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml Index: antlib.xml =================================================================== RCS file: /home/cvs/ant/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- antlib.xml 23 May 2005 19:51:56 -0000 1.1 +++ antlib.xml 20 Jul 2005 06:45:30 -0000 1.2 @@ -21,4 +21,6 @@ classname="org.apache.tools.ant.types.resources.selectors.Size" /> <typedef name="date" classname="org.apache.tools.ant.types.resources.selectors.Date" /> + <typedef name="modified" + classname="org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector" /> </antlib> 1.13 +1 -1 ant/src/testcases/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java Index: ModifiedSelectorTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- ModifiedSelectorTest.java 6 Jan 2005 12:05:09 -0000 1.12 +++ ModifiedSelectorTest.java 20 Jul 2005 06:45:30 -0000 1.13 @@ -43,7 +43,7 @@ /** * Unit tests for ModifiedSelector. * - * @version 2004-07-12 + * @version 2005-07-19 * @since Ant 1.6 */ public class ModifiedSelectorTest extends BaseSelectorTest {
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]