You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ke...@apache.org on 2007/05/12 08:09:07 UTC

svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Author: kevj
Date: Fri May 11 23:09:06 2007
New Revision: 537344

URL: http://svn.apache.org/viewvc?view=rev&rev=537344
Log:
-new ResourceContains condition

Added:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Added: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java?view=auto&rev=537344
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java (added)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java Fri May 11 23:09:06 2007
@@ -0,0 +1,99 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.tools.ant.taskdefs.condition;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.util.FileUtils;
+
+/**
+ * <resourcecontains>
+ * Is a string contained in a resource (file currently)?
+ * @since Ant 1.7.1
+ */
+public class ResourceContains implements Condition {
+
+    private String substring;
+    private Resource resource;
+    private boolean casesensitive = true;
+    
+    /**
+     * Sets the resource to search
+     * @param r
+     */
+    public void setResource(String r) {
+        this.resource = new FileResource(new File(r));
+    }
+
+    /**
+     * Sets the substring to look for
+     * @param substring
+     */
+    public void setSubstring(String substring) {
+        this.substring = substring;
+    }
+
+    /**
+     * Sets case sensitivity
+     * @param casesensitive
+     */
+    public void setCasesensitive(boolean casesensitive) {
+        this.casesensitive = casesensitive;
+    }
+    
+    /**
+     * Evaluates
+     * Returns true if the substring is contained in the resource
+     */
+    public boolean eval() throws BuildException {
+        if (resource == null || substring == null) {
+            throw new BuildException("both resource and substring are required "
+                                     + "in <resourcecontains>");
+        }
+        
+        if (resource.getSize() == 0) {
+            return false;
+        }
+        
+        BufferedReader reader = null;
+        try {
+            reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
+            String contents = FileUtils.readFully(reader);
+            if(casesensitive) {
+                if(contents.indexOf(substring) > -1) {
+                    return true;
+                }
+            } else {
+                if(contents.toLowerCase().indexOf(substring) > -1) {
+                    return true;
+                }
+            }
+            return false;
+        } catch (IOException e) {
+            throw new BuildException("There was a problem accessing resource : "+resource);
+        } finally {
+            FileUtils.close(reader);
+        }
+    }
+}
\ No newline at end of file



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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Peter Reilly <pe...@gmail.com>.
I would perfer to wait until after ant 1.7.1 before getting antlib.xml
loaded for ant.

Peter
On 5/22/07, Kevin Jackson <fo...@gmail.com> wrote:
> Hi Matt,
>
> > > > we already had this covered, e.g.:
> > > >
> > > > <resourcecount count="1">
> > > >   <restrict>
> > > >     <file file="${file}" />
> > > >     <contains text="text" casesensitive="false"
> > > >
> > > >
> > >
> > xmlns="antlib:org.apache.tools.ant.types.resources.selectors"
> > > > />
> > > >   </restrict>
> > > > </resourcecount>
> > > >
> > > > Note that this approach supports any resource type
> > > > right off the bat.  Actually with the suggested
> > > "add"
> > > > idiom, <resourcecontains> should be rewritten as a
> > > > macrodef.  :|
>
> I think that using a macrodef makes more sense than using Java in this case
>
> > Back to this... do we plan to replace
> > au:assertResourceContains with some usage of the
> > <contains> selector as I demonstrated, then remove the
> > ResourceContains java condition?
>
> Putting the macrodef in the antunit antlib.xml is trivial, but how do
> we make resourcecontains available beyond the scope of antunit?
> Should we make resourcecontains a general condition (as it is right
> now)?
>
> me:
> +1 use a macrodef (less code to maintain)
> +1 make it available in ant core, not just in the antunit antlib
>
> Kev
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Kevin Jackson <fo...@gmail.com>.
Hi Matt,

> > > we already had this covered, e.g.:
> > >
> > > <resourcecount count="1">
> > >   <restrict>
> > >     <file file="${file}" />
> > >     <contains text="text" casesensitive="false"
> > >
> > >
> >
> xmlns="antlib:org.apache.tools.ant.types.resources.selectors"
> > > />
> > >   </restrict>
> > > </resourcecount>
> > >
> > > Note that this approach supports any resource type
> > > right off the bat.  Actually with the suggested
> > "add"
> > > idiom, <resourcecontains> should be rewritten as a
> > > macrodef.  :|

I think that using a macrodef makes more sense than using Java in this case

> Back to this... do we plan to replace
> au:assertResourceContains with some usage of the
> <contains> selector as I demonstrated, then remove the
> ResourceContains java condition?

Putting the macrodef in the antunit antlib.xml is trivial, but how do
we make resourcecontains available beyond the scope of antunit?
Should we make resourcecontains a general condition (as it is right
now)?

me:
+1 use a macrodef (less code to maintain)
+1 make it available in ant core, not just in the antunit antlib

Kev

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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Matt Benson <gu...@yahoo.com>.
--- Kevin Jackson <fo...@gmail.com> wrote:

> > Hi guys.  Sorry I've been having trouble keeping
> up
> > lately.  Saw this stuff but missed the intervening
> > discussion.  Started playing today with adding
> refid
> > support, etc., but after awhile it occurred to me
> that
> > we already had this covered, e.g.:
> >
> > <resourcecount count="1">
> >   <restrict>
> >     <file file="${file}" />
> >     <contains text="text" casesensitive="false"
> >
> >
>
xmlns="antlib:org.apache.tools.ant.types.resources.selectors"
> > />
> >   </restrict>
> > </resourcecount>
> >
> > Note that this approach supports any resource type
> > right off the bat.  Actually with the suggested
> "add"
> > idiom, <resourcecontains> should be rewritten as a
> > macrodef.  :|
> 
> Well that's definitely simpler than having a custom
> task in Java! I
> think that's probably the way to go as we already
> have the basic
> building blocks available.

Back to this... do we plan to replace
au:assertResourceContains with some usage of the
<contains> selector as I demonstrated, then remove the
ResourceContains java condition?

-Matt

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



      ____________________________________________________________________________________
Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/ 

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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Kevin Jackson <fo...@gmail.com>.
> Hi guys.  Sorry I've been having trouble keeping up
> lately.  Saw this stuff but missed the intervening
> discussion.  Started playing today with adding refid
> support, etc., but after awhile it occurred to me that
> we already had this covered, e.g.:
>
> <resourcecount count="1">
>   <restrict>
>     <file file="${file}" />
>     <contains text="text" casesensitive="false"
>
> xmlns="antlib:org.apache.tools.ant.types.resources.selectors"
> />
>   </restrict>
> </resourcecount>
>
> Note that this approach supports any resource type
> right off the bat.  Actually with the suggested "add"
> idiom, <resourcecontains> should be rewritten as a
> macrodef.  :|

Well that's definitely simpler than having a custom task in Java! I
think that's probably the way to go as we already have the basic
building blocks available.

Kev

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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Matt Benson <gu...@yahoo.com>.
--- Kevin Jackson <fo...@gmail.com> wrote:

> Hi Steve,
> > Not sure about this. Why take a string? Its not
> being resolved relative
> > to the project base, and is doing work that ant
> should do.
> >
> 
> This is why I wanted someone to check over my
> resource stuff as I
> wasn't sure it was correct.
> 
> > If we want to look in a resource for a string
> >   -the test should take any resource defined
> inline :
> >   add(Resource r)
> > or by a refid
> 
> So if I change from setResource to add(Resource r)
> it would be more in
> keeping with the spirit of resources?
> 
> Ok I'll look into making the changes and ensuring
> that the tests pass etc

Hi guys.  Sorry I've been having trouble keeping up
lately.  Saw this stuff but missed the intervening
discussion.  Started playing today with adding refid
support, etc., but after awhile it occurred to me that
we already had this covered, e.g.:

<resourcecount count="1">
  <restrict>
    <file file="${file}" />
    <contains text="text" casesensitive="false"
     
xmlns="antlib:org.apache.tools.ant.types.resources.selectors"
/>
  </restrict>
</resourcecount>

Note that this approach supports any resource type
right off the bat.  Actually with the suggested "add"
idiom, <resourcecontains> should be rewritten as a
macrodef.  :|

If we decide to keep <resourcecontains> anyway, we
could plan something like this:

accept any nested resourcecollection but assert in
validation that it must have a single element (this
will include any Resource)

whenever we implement String resource decoding we can
add setResource(Resource) for IH to set for us.

-Matt

> 
> Thanks for the feedback
> Kev
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> dev-unsubscribe@ant.apache.org
> For additional commands, e-mail:
> dev-help@ant.apache.org
> 
> 



 
____________________________________________________________________________________
Sucker-punch spam with award-winning protection. 
Try the free Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/features_spam.html

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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Kevin Jackson <fo...@gmail.com>.
Hi Steve,
> Not sure about this. Why take a string? Its not being resolved relative
> to the project base, and is doing work that ant should do.
>

This is why I wanted someone to check over my resource stuff as I
wasn't sure it was correct.

> If we want to look in a resource for a string
>   -the test should take any resource defined inline :
>   add(Resource r)
> or by a refid

So if I change from setResource to add(Resource r) it would be more in
keeping with the spirit of resources?

Ok I'll look into making the changes and ensuring that the tests pass etc

Thanks for the feedback
Kev

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


Re: svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

Posted by Steve Loughran <st...@apache.org>.
kevj@apache.org wrote:
> Author: kevj
> Date: Fri May 11 23:09:06 2007
> New Revision: 537344
> 
> URL: http://svn.apache.org/viewvc?view=rev&rev=537344
> Log:
> -new ResourceContains condition
> 
> Added:
>     ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java
> 
> Added: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java
> URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java?view=auto&rev=537344
> ==============================================================================
> --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java (added)
> +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java Fri May 11 23:09:06 2007
> @@ -0,0 +1,99 @@
> +/*
> + *  Licensed to the Apache Software Foundation (ASF) under one or more
> + *  contributor license agreements.  See the NOTICE file distributed with
> + *  this work for additional information regarding copyright ownership.
> + *  The ASF licenses this file to You under the Apache License, Version 2.0
> + *  (the "License"); you may not use this file except in compliance with
> + *  the License.  You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing, software
> + *  distributed under the License is distributed on an "AS IS" BASIS,
> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + *  See the License for the specific language governing permissions and
> + *  limitations under the License.
> + *
> + */
> +package org.apache.tools.ant.taskdefs.condition;
> +
> +import java.io.BufferedReader;
> +import java.io.File;
> +import java.io.IOException;
> +import java.io.InputStreamReader;
> +
> +import org.apache.tools.ant.BuildException;
> +import org.apache.tools.ant.types.Resource;
> +import org.apache.tools.ant.types.resources.FileResource;
> +import org.apache.tools.ant.util.FileUtils;
> +
> +/**
> + * &lt;resourcecontains&gt;
> + * Is a string contained in a resource (file currently)?
> + * @since Ant 1.7.1
> + */
> +public class ResourceContains implements Condition {
> +
> +    private String substring;
> +    private Resource resource;
> +    private boolean casesensitive = true;
> +    
> +    /**
> +     * Sets the resource to search
> +     * @param r
> +     */
> +    public void setResource(String r) {
> +        this.resource = new FileResource(new File(r));
> +    }
> +

Not sure about this. Why take a string? Its not being resolved relative 
to the project base, and is doing work that ant should do.

If we want to look in a resource for a string
  -the test should take any resource defined inline :
  add(Resource r)
or by a refid


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