KickJava   Java API By Example, From Geeks To Geeks.

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


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.types.EnumeratedAttribute;
24 import org.apache.tools.ant.types.Parameter;
25
26 /**
27  * Selector that selects a certain kind of file: directory or regular.
28  *
29  * @since 1.6
30  */

31 public class TypeSelector extends BaseExtendSelector {
32
33     private String JavaDoc type = null;
34
35     /** Key to used for parameterized custom selector */
36     public static final String JavaDoc TYPE_KEY = "type";
37
38     /**
39      * Creates a new <code>TypeSelector</code> instance.
40      *
41      */

42     public TypeSelector() {
43     }
44
45     /**
46      * @return a string describing this object
47      */

48     public String JavaDoc toString() {
49         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{typeselector type: ");
50         buf.append(type);
51         buf.append("}");
52         return buf.toString();
53     }
54
55     /**
56      * Set the type of file to require.
57      * @param fileTypes the type of file - file or dir
58      */

59     public void setType(FileType fileTypes) {
60         this.type = fileTypes.getValue();
61     }
62
63     /**
64      * When using this as a custom selector, this method will be called.
65      * It translates each parameter into the appropriate setXXX() call.
66      *
67      * @param parameters the complete set of parameters for this selector
68      */

69     public void setParameters(Parameter[] parameters) {
70         super.setParameters(parameters);
71         if (parameters != null) {
72             for (int i = 0; i < parameters.length; i++) {
73                 String JavaDoc paramname = parameters[i].getName();
74                 if (TYPE_KEY.equalsIgnoreCase(paramname)) {
75                     FileType t = new FileType();
76                     t.setValue(parameters[i].getValue());
77                     setType(t);
78                 } else {
79                     setError("Invalid parameter " + paramname);
80                 }
81             }
82         }
83     }
84
85     /**
86      * Checks to make sure all settings are kosher. In this case, it
87      * means that the pattern attribute has been set.
88      *
89      */

90     public void verifySettings() {
91         if (type == null) {
92             setError("The type attribute is required");
93         }
94     }
95
96     /**
97      * The heart of the matter. This is where the selector gets to decide
98      * on the inclusion of a file in a particular fileset.
99      *
100      * @param basedir the base directory the scan is being done from
101      * @param filename is the name of the file to check
102      * @param file is a java.io.File object the selector can use
103      * @return whether the file should be selected or not
104      */

105     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
106
107         // throw BuildException on error
108
validate();
109
110         if (file.isDirectory()) {
111             return type.equals(FileType.DIR);
112         } else {
113             return type.equals(FileType.FILE);
114         }
115     }
116
117     /**
118      * Enumerated attribute with the values for types of file
119      */

120     public static class FileType extends EnumeratedAttribute {
121         /** the string value for file */
122         public static final String JavaDoc FILE = "file";
123         /** the string value for dir */
124         public static final String JavaDoc DIR = "dir";
125
126         /**
127          * @return the values as an array of strings
128          */

129         public String JavaDoc[] getValues() {
130             return new String JavaDoc[]{FILE, DIR};
131         }
132     }
133
134
135 }
136
Popular Tags