2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.tools.ant.taskdefs.condition;
20
import java.io.BufferedReader;
22
import java.io.IOException;
23
import java.io.InputStreamReader;
25
import org.apache.tools.ant.BuildException;
26
import org.apache.tools.ant.Project;
27
import org.apache.tools.ant.types.Resource;
28
import org.apache.tools.ant.types.ResourceCollection;
29
import org.apache.tools.ant.types.resources.FileResource;
30
import org.apache.tools.ant.util.FileUtils;
33
* <resourcecontains>
34
* Is a string contained in a resource (file currently)?
37
public class ResourceContains implements Condition {
39
private Project project;
40
private String substring;
41
private Resource resource;
43
private boolean casesensitive = true;
46
* Set this condition's Project.
47
* @param project Project
49
public void setProject(Project project) {
50
this.project = project;
54
* Get this condition's Project.
57
public Project getProject() {
62
* Sets the resource to search
63
* @param r the value to use.
65
public void setResource(String r) {
66
this.resource = new FileResource(new File(r));
70
* Sets the refid to search; should indicate a resource directly
71
* or by way of a single-element ResourceCollection.
72
* @param refid the value to use.
74
public void setRefid(String refid) {
78
private void resolveRefid() {
80
if (getProject() == null) {
81
throw new BuildException("Cannot retrieve refid; project unset");
83
Object o = getProject().getReference(refid);
84
if (!(o instanceof Resource)) {
85
if (o instanceof ResourceCollection) {
86
ResourceCollection rc = (ResourceCollection) o;
88
o = rc.iterator().next();
91
throw new BuildException(
92
"Illegal value at '" + refid + "': " + String.valueOf(o));
95
this.resource = (Resource) o;
102
* Sets the substring to look for
103
* @param substring the value to use.
105
public void setSubstring(String substring) {
106
this.substring = substring;
110
* Sets case sensitivity attribute.
111
* @param casesensitive the value to use.
113
public void setCasesensitive(boolean casesensitive) {
114
this.casesensitive = casesensitive;
117
private void validate() {
118
if (resource != null && refid != null) {
119
throw new BuildException("Cannot set both resource and refid");
121
if (resource == null && refid != null) {
124
if (resource == null || substring == null) {
125
throw new BuildException("both resource and substring are required "
126
+ "in <resourcecontains>");
131
* Evaluates the condition.
132
* @return true if the substring is contained in the resource
133
* @throws BuildException if there is a problem.
135
public synchronized boolean eval() throws BuildException {
138
if (substring.length() == 0) {
139
if (getProject() != null) {
140
getProject().log("Substring is empty; returning true",
141
Project.MSG_VERBOSE);
145
if (resource.getSize() == 0) {
149
BufferedReader reader = null;
151
reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
152
String contents = FileUtils.safeReadFully(reader);
153
String sub = substring;
154
if (!casesensitive) {
155
contents = contents.toLowerCase();
156
sub = sub.toLowerCase();
158
return contents.indexOf(sub) >= 0;
159
} catch (IOException e) {
160
throw new BuildException("There was a problem accessing resource : " + resource);
162
FileUtils.close(reader);