KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > ContainsSelector


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
19 package org.apache.tools.ant.types.selectors;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.types.Parameter;
29 import org.apache.tools.ant.types.Resource;
30 import org.apache.tools.ant.types.resources.FileResource;
31 import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
32 import org.apache.tools.ant.util.FileUtils;
33
34 /**
35  * Selector that filters files/resources based on whether they contain a
36  * particular string.
37  *
38  * @since 1.5
39  */

40 public class ContainsSelector extends BaseExtendSelector implements ResourceSelector {
41
42     private String JavaDoc contains = null;
43     private boolean casesensitive = true;
44     private boolean ignorewhitespace = false;
45     /** Key to used for parameterized custom selector */
46     public static final String JavaDoc EXPRESSION_KEY = "expression";
47     /** Used for parameterized custom selector */
48     public static final String JavaDoc CONTAINS_KEY = "text";
49     /** Used for parameterized custom selector */
50     public static final String JavaDoc CASE_KEY = "casesensitive";
51     /** Used for parameterized custom selector */
52     public static final String JavaDoc WHITESPACE_KEY = "ignorewhitespace";
53
54
55     /**
56      * Creates a new <code>ContainsSelector</code> instance.
57      *
58      */

59     public ContainsSelector() {
60     }
61
62     /**
63      * @return a string describing this object
64      */

65     public String JavaDoc toString() {
66         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{containsselector text: ");
67         buf.append('"').append(contains).append('"');
68         buf.append(" casesensitive: ");
69         buf.append(casesensitive ? "true" : "false");
70         buf.append(" ignorewhitespace: ");
71         buf.append(ignorewhitespace ? "true" : "false");
72         buf.append("}");
73         return buf.toString();
74     }
75
76     /**
77      * The string to search for within a file.
78      *
79      * @param contains the string that a file must contain to be selected.
80      */

81     public void setText(String JavaDoc contains) {
82         this.contains = contains;
83     }
84
85     /**
86      * Whether to ignore case in the string being searched.
87      *
88      * @param casesensitive whether to pay attention to case sensitivity
89      */

90     public void setCasesensitive(boolean casesensitive) {
91         this.casesensitive = casesensitive;
92     }
93
94     /**
95      * Whether to ignore whitespace in the string being searched.
96      *
97      * @param ignorewhitespace whether to ignore any whitespace
98      * (spaces, tabs, etc.) in the searchstring
99      */

100     public void setIgnorewhitespace(boolean ignorewhitespace) {
101         this.ignorewhitespace = ignorewhitespace;
102     }
103
104     /**
105      * When using this as a custom selector, this method will be called.
106      * It translates each parameter into the appropriate setXXX() call.
107      *
108      * @param parameters the complete set of parameters for this selector
109      */

110     public void setParameters(Parameter[] parameters) {
111         super.setParameters(parameters);
112         if (parameters != null) {
113             for (int i = 0; i < parameters.length; i++) {
114                 String JavaDoc paramname = parameters[i].getName();
115                 if (CONTAINS_KEY.equalsIgnoreCase(paramname)) {
116                     setText(parameters[i].getValue());
117                 } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
118                     setCasesensitive(Project.toBoolean(
119                             parameters[i].getValue()));
120                 } else if (WHITESPACE_KEY.equalsIgnoreCase(paramname)) {
121                     setIgnorewhitespace(Project.toBoolean(
122                             parameters[i].getValue()));
123                 } else {
124                     setError("Invalid parameter " + paramname);
125                 }
126             }
127         }
128     }
129
130     /**
131      * Checks to make sure all settings are kosher. In this case, it
132      * means that the pattern attribute has been set.
133      *
134      */

135     public void verifySettings() {
136         if (contains == null) {
137             setError("The text attribute is required");
138         }
139     }
140
141     /**
142      * The heart of the matter. This is where the selector gets to decide
143      * on the inclusion of a file in a particular fileset.
144      *
145      * @param basedir the base directory the scan is being done from
146      * @param filename is the name of the file to check
147      * @param file is a java.io.File object the selector can use
148      * @return whether the file should be selected or not
149      */

150     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
151         return isSelected(new FileResource(file));
152     }
153
154     /**
155      * The heart of the matter. This is where the selector gets to decide
156      * on the inclusion of a Resource.
157      *
158      * @param r the Resource to check.
159      * @return whether the Resource is selected.
160      */

161     public boolean isSelected(Resource r) {
162
163         // throw BuildException on error
164
validate();
165
166         if (r.isDirectory()) {
167             return true;
168         }
169
170         String JavaDoc userstr = contains;
171         if (!casesensitive) {
172             userstr = contains.toLowerCase();
173         }
174         if (ignorewhitespace) {
175             userstr = SelectorUtils.removeWhitespace(userstr);
176         }
177         BufferedReader JavaDoc in = null;
178         try {
179             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(r.getInputStream()));
180         } catch (Exception JavaDoc e) {
181             throw new BuildException("Could not get InputStream from "
182                     + r.toLongString(), e);
183         }
184         try {
185             String JavaDoc teststr = in.readLine();
186             while (teststr != null) {
187                 if (!casesensitive) {
188                     teststr = teststr.toLowerCase();
189                 }
190                 if (ignorewhitespace) {
191                     teststr = SelectorUtils.removeWhitespace(teststr);
192                 }
193                 if (teststr.indexOf(userstr) > -1) {
194                     return true;
195                 }
196                 teststr = in.readLine();
197             }
198             return false;
199         } catch (IOException JavaDoc ioe) {
200             throw new BuildException("Could not read " + r.toLongString());
201         } finally {
202             FileUtils.close(in);
203         }
204     }
205
206 }
207
208
Popular Tags