[14982] | 1 | /*
|
---|
| 2 | * Copyright 2002-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 |
|
---|
| 18 | package org.apache.tools.ant.types.selectors;
|
---|
| 19 |
|
---|
| 20 | import java.io.BufferedReader;
|
---|
| 21 | import java.io.File;
|
---|
| 22 | import java.io.FileInputStream;
|
---|
| 23 | import java.io.IOException;
|
---|
| 24 | import java.io.InputStreamReader;
|
---|
| 25 |
|
---|
| 26 | import org.apache.tools.ant.BuildException;
|
---|
| 27 | import org.apache.tools.ant.Project;
|
---|
| 28 | import org.apache.tools.ant.types.Parameter;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Selector that filters files based on whether they contain a
|
---|
| 32 | * particular string.
|
---|
| 33 | *
|
---|
| 34 | * @since 1.5
|
---|
| 35 | */
|
---|
| 36 | public class ContainsSelector extends BaseExtendSelector {
|
---|
| 37 |
|
---|
| 38 | private String contains = null;
|
---|
| 39 | private boolean casesensitive = true;
|
---|
| 40 | private boolean ignorewhitespace = false;
|
---|
| 41 | /** Key to used for parameterized custom selector */
|
---|
| 42 | public static final String EXPRESSION_KEY = "expression";
|
---|
| 43 | /** Used for parameterized custom selector */
|
---|
| 44 | public static final String CONTAINS_KEY = "text";
|
---|
| 45 | /** Used for parameterized custom selector */
|
---|
| 46 | public static final String CASE_KEY = "casesensitive";
|
---|
| 47 | /** Used for parameterized custom selector */
|
---|
| 48 | public static final String WHITESPACE_KEY = "ignorewhitespace";
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Creates a new <code>ContainsSelector</code> instance.
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
| 55 | public ContainsSelector() {
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * @return a string describing this object
|
---|
| 60 | */
|
---|
| 61 | public String toString() {
|
---|
| 62 | StringBuffer buf = new StringBuffer("{containsselector text: ");
|
---|
| 63 | buf.append(contains);
|
---|
| 64 | buf.append(" casesensitive: ");
|
---|
| 65 | if (casesensitive) {
|
---|
| 66 | buf.append("true");
|
---|
| 67 | } else {
|
---|
| 68 | buf.append("false");
|
---|
| 69 | }
|
---|
| 70 | buf.append(" ignorewhitespace: ");
|
---|
| 71 | if (ignorewhitespace) {
|
---|
| 72 | buf.append("true");
|
---|
| 73 | } else {
|
---|
| 74 | buf.append("false");
|
---|
| 75 | }
|
---|
| 76 | buf.append("}");
|
---|
| 77 | return buf.toString();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * The string to search for within a file.
|
---|
| 82 | *
|
---|
| 83 | * @param contains the string that a file must contain to be selected.
|
---|
| 84 | */
|
---|
| 85 | public void setText(String contains) {
|
---|
| 86 | this.contains = contains;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Whether to ignore case in the string being searched.
|
---|
| 91 | *
|
---|
| 92 | * @param casesensitive whether to pay attention to case sensitivity
|
---|
| 93 | */
|
---|
| 94 | public void setCasesensitive(boolean casesensitive) {
|
---|
| 95 | this.casesensitive = casesensitive;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Whether to ignore whitespace in the string being searched.
|
---|
| 100 | *
|
---|
| 101 | * @param ignorewhitespace whether to ignore any whitespace
|
---|
| 102 | * (spaces, tabs, etc.) in the searchstring
|
---|
| 103 | */
|
---|
| 104 | public void setIgnorewhitespace(boolean ignorewhitespace) {
|
---|
| 105 | this.ignorewhitespace = ignorewhitespace;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * When using this as a custom selector, this method will be called.
|
---|
| 110 | * It translates each parameter into the appropriate setXXX() call.
|
---|
| 111 | *
|
---|
| 112 | * @param parameters the complete set of parameters for this selector
|
---|
| 113 | */
|
---|
| 114 | public void setParameters(Parameter[] parameters) {
|
---|
| 115 | super.setParameters(parameters);
|
---|
| 116 | if (parameters != null) {
|
---|
| 117 | for (int i = 0; i < parameters.length; i++) {
|
---|
| 118 | String paramname = parameters[i].getName();
|
---|
| 119 | if (CONTAINS_KEY.equalsIgnoreCase(paramname)) {
|
---|
| 120 | setText(parameters[i].getValue());
|
---|
| 121 | } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
|
---|
| 122 | setCasesensitive(Project.toBoolean(
|
---|
| 123 | parameters[i].getValue()));
|
---|
| 124 | } else if (WHITESPACE_KEY.equalsIgnoreCase(paramname)) {
|
---|
| 125 | setIgnorewhitespace(Project.toBoolean(
|
---|
| 126 | parameters[i].getValue()));
|
---|
| 127 | } else {
|
---|
| 128 | setError("Invalid parameter " + paramname);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Checks to make sure all settings are kosher. In this case, it
|
---|
| 136 | * means that the pattern attribute has been set.
|
---|
| 137 | *
|
---|
| 138 | */
|
---|
| 139 | public void verifySettings() {
|
---|
| 140 | if (contains == null) {
|
---|
| 141 | setError("The text attribute is required");
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * The heart of the matter. This is where the selector gets to decide
|
---|
| 147 | * on the inclusion of a file in a particular fileset.
|
---|
| 148 | *
|
---|
| 149 | * @param basedir the base directory the scan is being done from
|
---|
| 150 | * @param filename is the name of the file to check
|
---|
| 151 | * @param file is a java.io.File object the selector can use
|
---|
| 152 | * @return whether the file should be selected or not
|
---|
| 153 | */
|
---|
| 154 | public boolean isSelected(File basedir, String filename, File file) {
|
---|
| 155 |
|
---|
| 156 | // throw BuildException on error
|
---|
| 157 | validate();
|
---|
| 158 |
|
---|
| 159 | if (file.isDirectory()) {
|
---|
| 160 | return true;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | String userstr = contains;
|
---|
| 164 | if (!casesensitive) {
|
---|
| 165 | userstr = contains.toLowerCase();
|
---|
| 166 | }
|
---|
| 167 | if (ignorewhitespace) {
|
---|
| 168 | userstr = SelectorUtils.removeWhitespace(userstr);
|
---|
| 169 | }
|
---|
| 170 | BufferedReader in = null;
|
---|
| 171 | try {
|
---|
| 172 | in = new BufferedReader(new InputStreamReader(
|
---|
| 173 | new FileInputStream(file)));
|
---|
| 174 | String teststr = in.readLine();
|
---|
| 175 | while (teststr != null) {
|
---|
| 176 | if (!casesensitive) {
|
---|
| 177 | teststr = teststr.toLowerCase();
|
---|
| 178 | }
|
---|
| 179 | if (ignorewhitespace) {
|
---|
| 180 | teststr = SelectorUtils.removeWhitespace(teststr);
|
---|
| 181 | }
|
---|
| 182 | if (teststr.indexOf(userstr) > -1) {
|
---|
| 183 | return true;
|
---|
| 184 | }
|
---|
| 185 | teststr = in.readLine();
|
---|
| 186 | }
|
---|
| 187 | return false;
|
---|
| 188 | } catch (IOException ioe) {
|
---|
| 189 | throw new BuildException("Could not read file " + filename);
|
---|
| 190 | } finally {
|
---|
| 191 | if (in != null) {
|
---|
| 192 | try {
|
---|
| 193 | in.close();
|
---|
| 194 | } catch (Exception e) {
|
---|
| 195 | throw new BuildException("Could not close file "
|
---|
| 196 | + filename);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 |
|
---|