source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/TypeSelector.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: 4.0 KB
Line 
1/*
2 * Copyright 2003-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;
21
22import org.apache.tools.ant.types.EnumeratedAttribute;
23import org.apache.tools.ant.types.Parameter;
24
25/**
26 * Selector that selects a certain kind of file: directory or regular.
27 *
28 * @since 1.6
29 */
30public class TypeSelector extends BaseExtendSelector {
31
32 private String type = null;
33
34 /** Key to used for parameterized custom selector */
35 public static final String TYPE_KEY = "type";
36
37 /**
38 * Creates a new <code>TypeSelector</code> instance.
39 *
40 */
41 public TypeSelector() {
42 }
43
44 /**
45 * @return a string describing this object
46 */
47 public String toString() {
48 StringBuffer buf = new StringBuffer("{typeselector type: ");
49 buf.append(type);
50 buf.append("}");
51 return buf.toString();
52 }
53
54 /**
55 * Set the type of file to require.
56 * @param fileTypes the type of file - file or dir
57 */
58 public void setType(FileType fileTypes) {
59 this.type = fileTypes.getValue();
60 }
61
62 /**
63 * When using this as a custom selector, this method will be called.
64 * It translates each parameter into the appropriate setXXX() call.
65 *
66 * @param parameters the complete set of parameters for this selector
67 */
68 public void setParameters(Parameter[] parameters) {
69 super.setParameters(parameters);
70 if (parameters != null) {
71 for (int i = 0; i < parameters.length; i++) {
72 String paramname = parameters[i].getName();
73 if (TYPE_KEY.equalsIgnoreCase(paramname)) {
74 FileType type = new FileType();
75 type.setValue(parameters[i].getValue());
76 setType(type);
77 } else {
78 setError("Invalid parameter " + paramname);
79 }
80 }
81 }
82 }
83
84 /**
85 * Checks to make sure all settings are kosher. In this case, it
86 * means that the pattern attribute has been set.
87 *
88 */
89 public void verifySettings() {
90 if (type == null) {
91 setError("The type attribute is required");
92 }
93 }
94
95 /**
96 * The heart of the matter. This is where the selector gets to decide
97 * on the inclusion of a file in a particular fileset.
98 *
99 * @param basedir the base directory the scan is being done from
100 * @param filename is the name of the file to check
101 * @param file is a java.io.File object the selector can use
102 * @return whether the file should be selected or not
103 */
104 public boolean isSelected(File basedir, String filename, File file) {
105
106 // throw BuildException on error
107 validate();
108
109 if (file.isDirectory()) {
110 return type.equals(FileType.DIR);
111 } else {
112 return type.equals(FileType.FILE);
113 }
114 }
115
116 /**
117 * Enumerated attribute with the values for types of file
118 */
119 public static class FileType extends EnumeratedAttribute {
120 /** the string value for file */
121 public static final String FILE = "file";
122 /** the string value for dir */
123 public static final String DIR = "dir";
124
125 /**
126 * @return the values as an array of strings
127 */
128 public String[] getValues() {
129 return new String[]{FILE, DIR};
130 }
131 }
132
133
134}
Note: See TracBrowser for help on using the repository browser.