KickJava   Java API By Example, From Geeks To Geeks.

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


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.types.Parameter;
28 import org.apache.tools.ant.types.RegularExpression;
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.regexp.Regexp;
33
34 /**
35  * Selector that filters files based on a regular expression.
36  *
37  * @since Ant 1.6
38  */

39 public class ContainsRegexpSelector extends BaseExtendSelector
40         implements ResourceSelector {
41
42     private String JavaDoc userProvidedExpression = null;
43     private RegularExpression myRegExp = null;
44     private Regexp myExpression = null;
45     /** Key to used for parameterized custom selector */
46     public static final String JavaDoc EXPRESSION_KEY = "expression";
47
48     /**
49      * Creates a new <code>ContainsRegexpSelector</code> instance.
50      */

51     public ContainsRegexpSelector() {
52     }
53
54     /**
55      * @return a string describing this object
56      */

57     public String JavaDoc toString() {
58         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(
59                 "{containsregexpselector expression: ");
60         buf.append(userProvidedExpression);
61         buf.append("}");
62         return buf.toString();
63     }
64
65     /**
66      * The regular expression used to search the file.
67      *
68      * @param theexpression this must match a line in the file to be selected.
69      */

70     public void setExpression(String JavaDoc theexpression) {
71         this.userProvidedExpression = theexpression;
72     }
73
74     /**
75      * When using this as a custom selector, this method will be called.
76      * It translates each parameter into the appropriate setXXX() call.
77      *
78      * @param parameters the complete set of parameters for this selector
79      */

80     public void setParameters(Parameter[] parameters) {
81         super.setParameters(parameters);
82         if (parameters != null) {
83             for (int i = 0; i < parameters.length; i++) {
84                 String JavaDoc paramname = parameters[i].getName();
85                 if (EXPRESSION_KEY.equalsIgnoreCase(paramname)) {
86                     setExpression(parameters[i].getValue());
87                 } else {
88                     setError("Invalid parameter " + paramname);
89                 }
90             }
91         }
92     }
93
94     /**
95      * Checks that an expression was specified.
96      *
97      */

98     public void verifySettings() {
99         if (userProvidedExpression == null) {
100             setError("The expression attribute is required");
101         }
102     }
103
104     /**
105      * Tests a regular expression against each line of text in the file.
106      *
107      * @param basedir the base directory the scan is being done from
108      * @param filename is the name of the file to check
109      * @param file is a java.io.File object the selector can use
110      * @return whether the file should be selected or not
111      */

112     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
113         return isSelected(new FileResource(file));
114     }
115
116     /**
117      * Tests a regular expression against each line of text in a Resource.
118      *
119      * @param r the Resource to check.
120      * @return whether the Resource is selected or not
121      */

122     public boolean isSelected(Resource r) {
123         String JavaDoc teststr = null;
124         BufferedReader JavaDoc in = null;
125
126         // throw BuildException on error
127

128         validate();
129
130         if (r.isDirectory()) {
131             return true;
132         }
133
134         if (myRegExp == null) {
135             myRegExp = new RegularExpression();
136             myRegExp.setPattern(userProvidedExpression);
137             myExpression = myRegExp.getRegexp(getProject());
138         }
139
140         try {
141             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(r.getInputStream()));
142         } catch (Exception JavaDoc e) {
143             throw new BuildException("Could not get InputStream from "
144                     + r.toLongString(), e);
145         }
146         try {
147             teststr = in.readLine();
148
149             while (teststr != null) {
150
151                 if (myExpression.matches(teststr)) {
152                     return true;
153                 }
154                 teststr = in.readLine();
155             }
156
157             return false;
158         } catch (IOException JavaDoc ioe) {
159             throw new BuildException("Could not read " + r.toLongString());
160         } finally {
161             if (in != null) {
162                 try {
163                     in.close();
164                 } catch (Exception JavaDoc e) {
165                     throw new BuildException("Could not close "
166                                              + r.toLongString());
167                 }
168             }
169         }
170     }
171 }
172
173
Popular Tags