~ubuntu-branches/ubuntu/maverick/ant/maverick

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-09-30 14:47:45 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080930144745-0x8uzivd9t15dua3
Tags: 1.7.1-0ubuntu1
* New upstream version (bug fix release).
  - mainly a bugfix release.
  - has extended support for Java6 features.
  - <script> now has support for JavaFX.
  - release notes: http://apache.linux-mirror.org/ant/README.html
* Remove debian/patches/05_ant-bug433444.patch. Obsoleted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 *
 
17
 */
 
18
package org.apache.tools.ant.taskdefs.condition;
 
19
 
 
20
import java.io.BufferedReader;
 
21
import java.io.File;
 
22
import java.io.IOException;
 
23
import java.io.InputStreamReader;
 
24
 
 
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;
 
31
 
 
32
/**
 
33
 * &lt;resourcecontains&gt;
 
34
 * Is a string contained in a resource (file currently)?
 
35
 * @since Ant 1.7.1
 
36
 */
 
37
public class ResourceContains implements Condition {
 
38
 
 
39
    private Project project;
 
40
    private String substring;
 
41
    private Resource resource;
 
42
    private String refid;
 
43
    private boolean casesensitive = true;
 
44
 
 
45
    /**
 
46
     * Set this condition's Project.
 
47
     * @param project Project
 
48
     */
 
49
    public void setProject(Project project) {
 
50
        this.project = project;
 
51
    }
 
52
 
 
53
    /**
 
54
     * Get this condition's Project.
 
55
     * @return Project
 
56
     */
 
57
    public Project getProject() {
 
58
        return project;
 
59
    }
 
60
 
 
61
    /**
 
62
     * Sets the resource to search
 
63
     * @param r the value to use.
 
64
     */
 
65
    public void setResource(String r) {
 
66
        this.resource = new FileResource(new File(r));
 
67
    }
 
68
 
 
69
    /**
 
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.
 
73
     */
 
74
    public void setRefid(String refid) {
 
75
        this.refid = refid;
 
76
    }
 
77
 
 
78
    private void resolveRefid() {
 
79
        try {
 
80
            if (getProject() == null) {
 
81
                throw new BuildException("Cannot retrieve refid; project unset");
 
82
            }
 
83
            Object o = getProject().getReference(refid);
 
84
            if (!(o instanceof Resource)) {
 
85
                if (o instanceof ResourceCollection) {
 
86
                    ResourceCollection rc = (ResourceCollection) o;
 
87
                    if (rc.size() == 1) {
 
88
                        o = rc.iterator().next();
 
89
                    }
 
90
                } else {
 
91
                    throw new BuildException(
 
92
                        "Illegal value at '" + refid + "': " + String.valueOf(o));
 
93
                }
 
94
            }
 
95
            this.resource = (Resource) o;
 
96
        } finally {
 
97
            refid = null;
 
98
        }
 
99
    }
 
100
 
 
101
    /**
 
102
     * Sets the substring to look for
 
103
     * @param substring the value to use.
 
104
     */
 
105
    public void setSubstring(String substring) {
 
106
        this.substring = substring;
 
107
    }
 
108
 
 
109
    /**
 
110
     * Sets case sensitivity attribute.
 
111
     * @param casesensitive the value to use.
 
112
     */
 
113
    public void setCasesensitive(boolean casesensitive) {
 
114
        this.casesensitive = casesensitive;
 
115
    }
 
116
 
 
117
    private void validate() {
 
118
        if (resource != null && refid != null) {
 
119
            throw new BuildException("Cannot set both resource and refid");
 
120
        }
 
121
        if (resource == null && refid != null) {
 
122
            resolveRefid();
 
123
        }
 
124
        if (resource == null || substring == null) {
 
125
            throw new BuildException("both resource and substring are required "
 
126
                                     + "in <resourcecontains>");
 
127
        }
 
128
    }
 
129
 
 
130
    /**
 
131
     * Evaluates the condition.
 
132
     * @return true if the substring is contained in the resource
 
133
     * @throws BuildException if there is a problem.
 
134
     */
 
135
    public synchronized boolean eval() throws BuildException {
 
136
        validate();
 
137
 
 
138
        if (substring.length() == 0) {
 
139
            if (getProject() != null) {
 
140
                getProject().log("Substring is empty; returning true",
 
141
                                 Project.MSG_VERBOSE);
 
142
            }
 
143
            return true;
 
144
        }
 
145
        if (resource.getSize() == 0) {
 
146
            return false;
 
147
        }
 
148
 
 
149
        BufferedReader reader = null;
 
150
        try {
 
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();
 
157
            }
 
158
            return contents.indexOf(sub) >= 0;
 
159
        } catch (IOException e) {
 
160
            throw new BuildException("There was a problem accessing resource : " + resource);
 
161
        } finally {
 
162
            FileUtils.close(reader);
 
163
        }
 
164
    }
 
165
}