ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@apache.org
Subject svn commit: r537344 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java
Date Sat, 12 May 2007 06:09:07 GMT
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


Mime
View raw message