source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.8 KB
Line 
1/*
2 * Copyright 2003-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.types.selectors;
19
20import java.io.BufferedReader;
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.IOException;
24import java.io.InputStreamReader;
25
26import org.apache.tools.ant.BuildException;
27import org.apache.tools.ant.types.Parameter;
28import org.apache.tools.ant.types.RegularExpression;
29import org.apache.tools.ant.util.regexp.Regexp;
30
31/**
32 * Selector that filters files based on a regular expression.
33 *
34 * @since Ant 1.6
35 */
36public class ContainsRegexpSelector extends BaseExtendSelector {
37
38 private String userProvidedExpression = null;
39 private RegularExpression myRegExp = null;
40 private Regexp myExpression = null;
41 /** Key to used for parameterized custom selector */
42 public static final String EXPRESSION_KEY = "expression";
43
44 /**
45 * Creates a new <code>ContainsRegexpSelector</code> instance.
46 */
47 public ContainsRegexpSelector() {
48 }
49
50 /**
51 * @return a string describing this object
52 */
53 public String toString() {
54 StringBuffer buf = new StringBuffer(
55 "{containsregexpselector expression: ");
56 buf.append(userProvidedExpression);
57 buf.append("}");
58 return buf.toString();
59 }
60
61 /**
62 * The regular expression used to search the file.
63 *
64 * @param theexpression this must match a line in the file to be selected.
65 */
66 public void setExpression(String theexpression) {
67 this.userProvidedExpression = theexpression;
68 }
69
70 /**
71 * When using this as a custom selector, this method will be called.
72 * It translates each parameter into the appropriate setXXX() call.
73 *
74 * @param parameters the complete set of parameters for this selector
75 */
76 public void setParameters(Parameter[] parameters) {
77 super.setParameters(parameters);
78 if (parameters != null) {
79 for (int i = 0; i < parameters.length; i++) {
80 String paramname = parameters[i].getName();
81 if (EXPRESSION_KEY.equalsIgnoreCase(paramname)) {
82 setExpression(parameters[i].getValue());
83 } else {
84 setError("Invalid parameter " + paramname);
85 }
86 }
87 }
88 }
89
90 /**
91 * Checks that an expression was specified.
92 *
93 */
94 public void verifySettings() {
95 if (userProvidedExpression == null) {
96 setError("The expression attribute is required");
97 }
98 }
99
100 /**
101 * Tests a regular expression against each line of text in the file.
102 *
103 * @param basedir the base directory the scan is being done from
104 * @param filename is the name of the file to check
105 * @param file is a java.io.File object the selector can use
106 * @return whether the file should be selected or not
107 */
108 public boolean isSelected(File basedir, String filename, File file) {
109 String teststr = null;
110 BufferedReader in = null;
111
112 // throw BuildException on error
113
114 validate();
115
116 if (file.isDirectory()) {
117 return true;
118 }
119
120 if (myRegExp == null) {
121 myRegExp = new RegularExpression();
122 myRegExp.setPattern(userProvidedExpression);
123 myExpression = myRegExp.getRegexp(getProject());
124 }
125
126 try {
127 in = new BufferedReader(new InputStreamReader(
128 new FileInputStream(file)));
129
130 teststr = in.readLine();
131
132 while (teststr != null) {
133
134 if (myExpression.matches(teststr)) {
135 return true;
136 }
137 teststr = in.readLine();
138 }
139
140 return false;
141 } catch (IOException ioe) {
142 throw new BuildException("Could not read file " + filename);
143 } finally {
144 if (in != null) {
145 try {
146 in.close();
147 } catch (Exception e) {
148 throw new BuildException("Could not close file "
149 + filename);
150 }
151 }
152 }
153 }
154}
155
Note: See TracBrowser for help on using the repository browser.