KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.types.Parameter;
26
27 /**
28  * Selector that filters files based on the how deep in the directory
29  * tree they are.
30  *
31  * @since 1.5
32  */

33 public class DepthSelector extends BaseExtendSelector {
34
35     // CheckStyle:VisibilityModifier OFF - bc
36

37     /** min attribute */
38     public int min = -1;
39     /** max attribute */
40     public int max = -1;
41
42     // CheckStyle:VisibilityModifier ON
43

44     /** Used for parameterized custom selector */
45     public static final String JavaDoc MIN_KEY = "min";
46     /** Used for parameterized custom selector */
47     public static final String JavaDoc MAX_KEY = "max";
48
49     /**
50      * Creates a new <code>DepthSelector</code> instance.
51      *
52      */

53     public DepthSelector() {
54     }
55
56     /**
57      * @return a string describing this object
58      */

59     public String JavaDoc toString() {
60         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{depthselector min: ");
61         buf.append(min);
62         buf.append(" max: ");
63         buf.append(max);
64         buf.append("}");
65         return buf.toString();
66     }
67
68     /**
69      * The minimum depth below the basedir before a file is selected.
70      *
71      * @param min minimum directory levels below basedir to go
72      */

73     public void setMin(int min) {
74         this.min = min;
75     }
76
77     /**
78      * The minimum depth below the basedir before a file is selected.
79      *
80      * @param max maximum directory levels below basedir to go
81      */

82     public void setMax(int max) {
83         this.max = max;
84     }
85
86     /**
87      * When using this as a custom selector, this method will be called.
88      * It translates each parameter into the appropriate setXXX() call.
89      *
90      * @param parameters the complete set of parameters for this selector
91      */

92     public void setParameters(Parameter[] parameters) {
93         super.setParameters(parameters);
94         if (parameters != null) {
95             for (int i = 0; i < parameters.length; i++) {
96                 String JavaDoc paramname = parameters[i].getName();
97                 if (MIN_KEY.equalsIgnoreCase(paramname)) {
98                     try {
99                         setMin(Integer.parseInt(parameters[i].getValue()));
100                     } catch (NumberFormatException JavaDoc nfe1) {
101                         setError("Invalid minimum value "
102                                 + parameters[i].getValue());
103                     }
104                 } else if (MAX_KEY.equalsIgnoreCase(paramname)) {
105                     try {
106                         setMax(Integer.parseInt(parameters[i].getValue()));
107                     } catch (NumberFormatException JavaDoc nfe1) {
108                         setError("Invalid maximum value "
109                                 + parameters[i].getValue());
110                     }
111                 } else {
112                     setError("Invalid parameter " + paramname);
113                 }
114             }
115         }
116     }
117
118     /**
119      * Checks to make sure all settings are kosher. In this case, it
120      * means that the max depth is not lower than the min depth.
121      */

122     public void verifySettings() {
123         if (min < 0 && max < 0) {
124             setError("You must set at least one of the min or the "
125                     + "max levels.");
126         }
127         if (max < min && max > -1) {
128             setError("The maximum depth is lower than the minimum.");
129         }
130     }
131
132     /**
133      * The heart of the matter. This is where the selector gets to decide
134      * on the inclusion of a file in a particular fileset. Most of the work
135      * for this selector is offloaded into SelectorUtils, a static class
136      * that provides the same services for both FilenameSelector and
137      * DirectoryScanner.
138      *
139      * @param basedir the base directory the scan is being done from
140      * @param filename is the name of the file to check
141      * @param file is a java.io.File object the selector can use
142      * @return whether the file should be selected or not
143      */

144     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
145
146         // throw BuildException on error
147
validate();
148
149         int depth = -1;
150         // If you felt daring, you could cache the basedir absolute path
151
String JavaDoc absBase = basedir.getAbsolutePath();
152         String JavaDoc absFile = file.getAbsolutePath();
153         StringTokenizer JavaDoc tokBase = new StringTokenizer JavaDoc(absBase,
154                 File.separator);
155         StringTokenizer JavaDoc tokFile = new StringTokenizer JavaDoc(absFile,
156                 File.separator);
157         while (tokFile.hasMoreTokens()) {
158             String JavaDoc filetoken = tokFile.nextToken();
159             if (tokBase.hasMoreTokens()) {
160                 String JavaDoc basetoken = tokBase.nextToken();
161                 // Sanity check. Ditch it if you want faster performance
162
if (!basetoken.equals(filetoken)) {
163                     throw new BuildException("File " + filename
164                             + " does not appear within " + absBase
165                             + "directory");
166                 }
167             } else {
168                 depth += 1;
169                 if (max > -1 && depth > max) {
170                     return false;
171                 }
172             }
173         }
174         if (tokBase.hasMoreTokens()) {
175             throw new BuildException("File " + filename
176                 + " is outside of " + absBase + "directory tree");
177         }
178         if (min > -1 && depth < min) {
179             return false;
180         }
181         return true;
182     }
183
184 }
185
186
Popular Tags