KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
22
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.Parameter;
25
26 /**
27  * Selector that filters files based on the filename.
28  *
29  * @since 1.5
30  */

31 public class FilenameSelector extends BaseExtendSelector {
32
33     private String JavaDoc pattern = null;
34     private boolean casesensitive = true;
35
36     private boolean negated = false;
37     /** Used for parameterized custom selector */
38     public static final String JavaDoc NAME_KEY = "name";
39     /** Used for parameterized custom selector */
40     public static final String JavaDoc CASE_KEY = "casesensitive";
41     /** Used for parameterized custom selector */
42     public static final String JavaDoc NEGATE_KEY = "negate";
43
44     /**
45      * Creates a new <code>FilenameSelector</code> instance.
46      *
47      */

48     public FilenameSelector() {
49     }
50
51     /**
52      * @return a string describing this object
53      */

54     public String JavaDoc toString() {
55         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{filenameselector name: ");
56         buf.append(pattern);
57         buf.append(" negate: ");
58         if (negated) {
59             buf.append("true");
60         } else {
61             buf.append("false");
62         }
63         buf.append(" casesensitive: ");
64         if (casesensitive) {
65             buf.append("true");
66         } else {
67             buf.append("false");
68         }
69         buf.append("}");
70         return buf.toString();
71     }
72
73     /**
74      * The name of the file, or the pattern for the name, that
75      * should be used for selection.
76      *
77      * @param pattern the file pattern that any filename must match
78      * against in order to be selected.
79      */

80     public void setName(String JavaDoc pattern) {
81         pattern = pattern.replace('/', File.separatorChar).replace('\\',
82                 File.separatorChar);
83         if (pattern.endsWith(File.separator)) {
84             pattern += "**";
85         }
86         this.pattern = pattern;
87     }
88
89     /**
90      * Whether to ignore case when checking filenames.
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      * You can optionally reverse the selection of this selector,
100      * thereby emulating an &lt;exclude&gt; tag, by setting the attribute
101      * negate to true. This is identical to surrounding the selector
102      * with &lt;not&gt;&lt;/not&gt;.
103      *
104      * @param negated whether to negate this selection
105      */

106     public void setNegate(boolean negated) {
107         this.negated = negated;
108     }
109
110     /**
111      * When using this as a custom selector, this method will be called.
112      * It translates each parameter into the appropriate setXXX() call.
113      *
114      * @param parameters the complete set of parameters for this selector
115      */

116     public void setParameters(Parameter[] parameters) {
117         super.setParameters(parameters);
118         if (parameters != null) {
119             for (int i = 0; i < parameters.length; i++) {
120                 String JavaDoc paramname = parameters[i].getName();
121                 if (NAME_KEY.equalsIgnoreCase(paramname)) {
122                     setName(parameters[i].getValue());
123                 } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
124                     setCasesensitive(Project.toBoolean(
125                             parameters[i].getValue()));
126                 } else if (NEGATE_KEY.equalsIgnoreCase(paramname)) {
127                     setNegate(Project.toBoolean(parameters[i].getValue()));
128                 } else {
129                     setError("Invalid parameter " + paramname);
130                 }
131             }
132         }
133     }
134
135     /**
136      * Checks to make sure all settings are kosher. In this case, it
137      * means that the name attribute has been set.
138      *
139      */

140     public void verifySettings() {
141         if (pattern == null) {
142             setError("The name attribute is required");
143         }
144     }
145
146     /**
147      * The heart of the matter. This is where the selector gets to decide
148      * on the inclusion of a file in a particular fileset. Most of the work
149      * for this selector is offloaded into SelectorUtils, a static class
150      * that provides the same services for both FilenameSelector and
151      * DirectoryScanner.
152      *
153      * @param basedir the base directory the scan is being done from
154      * @param filename is the name of the file to check
155      * @param file is a java.io.File object the selector can use
156      * @return whether the file should be selected or not
157      */

158     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
159         validate();
160
161         return (SelectorUtils.matchPath(pattern, filename,
162                 casesensitive) == !(negated));
163     }
164
165 }
166
167
Popular Tags