source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/DepthSelector.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: 6.0 KB
Line 
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
18package org.apache.tools.ant.types.selectors;
19
20import java.io.File;
21import java.util.StringTokenizer;
22
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.types.Parameter;
25
26/**
27 * Selector that filters files based on the how deep in the directory
28 * tree they are.
29 *
30 * @since 1.5
31 */
32public class DepthSelector extends BaseExtendSelector {
33
34 public int min = -1;
35 public int max = -1;
36 /** Used for parameterized custom selector */
37 public static final String MIN_KEY = "min";
38 /** Used for parameterized custom selector */
39 public static final String MAX_KEY = "max";
40
41 /**
42 * Creates a new <code>DepthSelector</code> instance.
43 *
44 */
45 public DepthSelector() {
46 }
47
48 /**
49 * @return a string describing this object
50 */
51 public String toString() {
52 StringBuffer buf = new StringBuffer("{depthselector min: ");
53 buf.append(min);
54 buf.append(" max: ");
55 buf.append(max);
56 buf.append("}");
57 return buf.toString();
58 }
59
60 /**
61 * The minimum depth below the basedir before a file is selected.
62 *
63 * @param min minimum directory levels below basedir to go
64 */
65 public void setMin(int min) {
66 this.min = min;
67 }
68
69 /**
70 * The minimum depth below the basedir before a file is selected.
71 *
72 * @param max maximum directory levels below basedir to go
73 */
74 public void setMax(int max) {
75 this.max = max;
76 }
77
78 /**
79 * When using this as a custom selector, this method will be called.
80 * It translates each parameter into the appropriate setXXX() call.
81 *
82 * @param parameters the complete set of parameters for this selector
83 */
84 public void setParameters(Parameter[] parameters) {
85 super.setParameters(parameters);
86 if (parameters != null) {
87 for (int i = 0; i < parameters.length; i++) {
88 String paramname = parameters[i].getName();
89 if (MIN_KEY.equalsIgnoreCase(paramname)) {
90 try {
91 setMin(Integer.parseInt(parameters[i].getValue()));
92 } catch (NumberFormatException nfe1) {
93 setError("Invalid minimum value "
94 + parameters[i].getValue());
95 }
96 } else if (MAX_KEY.equalsIgnoreCase(paramname)) {
97 try {
98 setMax(Integer.parseInt(parameters[i].getValue()));
99 } catch (NumberFormatException nfe1) {
100 setError("Invalid maximum value "
101 + parameters[i].getValue());
102 }
103 } else {
104 setError("Invalid parameter " + paramname);
105 }
106 }
107 }
108 }
109
110 /**
111 * Checks to make sure all settings are kosher. In this case, it
112 * means that the max depth is not lower than the min depth.
113 */
114 public void verifySettings() {
115 if (min < 0 && max < 0) {
116 setError("You must set at least one of the min or the "
117 + "max levels.");
118 }
119 if (max < min && max > -1) {
120 setError("The maximum depth is lower than the minimum.");
121 }
122 }
123
124 /**
125 * The heart of the matter. This is where the selector gets to decide
126 * on the inclusion of a file in a particular fileset. Most of the work
127 * for this selector is offloaded into SelectorUtils, a static class
128 * that provides the same services for both FilenameSelector and
129 * DirectoryScanner.
130 *
131 * @param basedir the base directory the scan is being done from
132 * @param filename is the name of the file to check
133 * @param file is a java.io.File object the selector can use
134 * @return whether the file should be selected or not
135 */
136 public boolean isSelected(File basedir, String filename, File file) {
137
138 // throw BuildException on error
139 validate();
140
141 int depth = -1;
142 // If you felt daring, you could cache the basedir absolute path
143 String absBase = basedir.getAbsolutePath();
144 String absFile = file.getAbsolutePath();
145 StringTokenizer tokBase = new StringTokenizer(absBase,
146 File.separator);
147 StringTokenizer tokFile = new StringTokenizer(absFile,
148 File.separator);
149 while (tokFile.hasMoreTokens()) {
150 String filetoken = tokFile.nextToken();
151 if (tokBase.hasMoreTokens()) {
152 String basetoken = tokBase.nextToken();
153 // Sanity check. Ditch it if you want faster performance
154 if (!basetoken.equals(filetoken)) {
155 throw new BuildException("File " + filename
156 + " does not appear within " + absBase
157 + "directory");
158 }
159 } else {
160 depth += 1;
161 if (max > -1 && depth > max) {
162 return false;
163 }
164 }
165 }
166 if (tokBase.hasMoreTokens()) {
167 throw new BuildException("File " + filename
168 + " is outside of " + absBase + "directory tree");
169 }
170 if (min > -1 && depth < min) {
171 return false;
172 }
173 return true;
174 }
175
176}
177
Note: See TracBrowser for help on using the repository browser.