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.File;
|
---|
21 |
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.types.Parameter;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Selector that filters files based on the filename.
|
---|
27 | *
|
---|
28 | * @since 1.5
|
---|
29 | */
|
---|
30 | public class FilenameSelector extends BaseExtendSelector {
|
---|
31 |
|
---|
32 | private String pattern = null;
|
---|
33 | private boolean casesensitive = true;
|
---|
34 |
|
---|
35 | private boolean negated = false;
|
---|
36 | /** Used for parameterized custom selector */
|
---|
37 | public static final String NAME_KEY = "name";
|
---|
38 | /** Used for parameterized custom selector */
|
---|
39 | public static final String CASE_KEY = "casesensitive";
|
---|
40 | /** Used for parameterized custom selector */
|
---|
41 | public static final String NEGATE_KEY = "negate";
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates a new <code>FilenameSelector</code> instance.
|
---|
45 | *
|
---|
46 | */
|
---|
47 | public FilenameSelector() {
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @return a string describing this object
|
---|
52 | */
|
---|
53 | public String toString() {
|
---|
54 | StringBuffer buf = new StringBuffer("{filenameselector name: ");
|
---|
55 | buf.append(pattern);
|
---|
56 | buf.append(" negate: ");
|
---|
57 | if (negated) {
|
---|
58 | buf.append("true");
|
---|
59 | } else {
|
---|
60 | buf.append("false");
|
---|
61 | }
|
---|
62 | buf.append(" casesensitive: ");
|
---|
63 | if (casesensitive) {
|
---|
64 | buf.append("true");
|
---|
65 | } else {
|
---|
66 | buf.append("false");
|
---|
67 | }
|
---|
68 | buf.append("}");
|
---|
69 | return buf.toString();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The name of the file, or the pattern for the name, that
|
---|
74 | * should be used for selection.
|
---|
75 | *
|
---|
76 | * @param pattern the file pattern that any filename must match
|
---|
77 | * against in order to be selected.
|
---|
78 | */
|
---|
79 | public void setName(String pattern) {
|
---|
80 | pattern = pattern.replace('/', File.separatorChar).replace('\\',
|
---|
81 | File.separatorChar);
|
---|
82 | if (pattern.endsWith(File.separator)) {
|
---|
83 | pattern += "**";
|
---|
84 | }
|
---|
85 | this.pattern = pattern;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Whether to ignore case when checking filenames.
|
---|
90 | *
|
---|
91 | * @param casesensitive whether to pay attention to case sensitivity
|
---|
92 | */
|
---|
93 | public void setCasesensitive(boolean casesensitive) {
|
---|
94 | this.casesensitive = casesensitive;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * You can optionally reverse the selection of this selector,
|
---|
99 | * thereby emulating an <exclude> tag, by setting the attribute
|
---|
100 | * negate to true. This is identical to surrounding the selector
|
---|
101 | * with <not></not>.
|
---|
102 | *
|
---|
103 | * @param negated whether to negate this selection
|
---|
104 | */
|
---|
105 | public void setNegate(boolean negated) {
|
---|
106 | this.negated = negated;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * When using this as a custom selector, this method will be called.
|
---|
111 | * It translates each parameter into the appropriate setXXX() call.
|
---|
112 | *
|
---|
113 | * @param parameters the complete set of parameters for this selector
|
---|
114 | */
|
---|
115 | public void setParameters(Parameter[] parameters) {
|
---|
116 | super.setParameters(parameters);
|
---|
117 | if (parameters != null) {
|
---|
118 | for (int i = 0; i < parameters.length; i++) {
|
---|
119 | String paramname = parameters[i].getName();
|
---|
120 | if (NAME_KEY.equalsIgnoreCase(paramname)) {
|
---|
121 | setName(parameters[i].getValue());
|
---|
122 | } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
|
---|
123 | setCasesensitive(Project.toBoolean(
|
---|
124 | parameters[i].getValue()));
|
---|
125 | } else if (NEGATE_KEY.equalsIgnoreCase(paramname)) {
|
---|
126 | setNegate(Project.toBoolean(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 name attribute has been set.
|
---|
137 | *
|
---|
138 | */
|
---|
139 | public void verifySettings() {
|
---|
140 | if (pattern == null) {
|
---|
141 | setError("The name 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. Most of the work
|
---|
148 | * for this selector is offloaded into SelectorUtils, a static class
|
---|
149 | * that provides the same services for both FilenameSelector and
|
---|
150 | * DirectoryScanner.
|
---|
151 | *
|
---|
152 | * @param basedir the base directory the scan is being done from
|
---|
153 | * @param filename is the name of the file to check
|
---|
154 | * @param file is a java.io.File object the selector can use
|
---|
155 | * @return whether the file should be selected or not
|
---|
156 | */
|
---|
157 | public boolean isSelected(File basedir, String filename, File file) {
|
---|
158 | validate();
|
---|
159 |
|
---|
160 | return (SelectorUtils.matchPath(pattern, filename,
|
---|
161 | casesensitive) == !(negated));
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|