From 18a6ba8332a3c1564132e8b1880bec336ea5d15d Mon Sep 17 00:00:00 2001 From: Kevin Jackson Date: Sat, 12 May 2007 06:09:06 +0000 Subject: [PATCH] -new ResourceContains condition git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@537344 13f79535-47bb-0310-9956-ffa450edef68 --- .../taskdefs/condition/ResourceContains.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java b/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java new file mode 100644 index 000000000..5fb9b02d5 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java @@ -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 "); + } + + 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