KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > selectors > Type


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 package org.apache.tools.ant.types.resources.selectors;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.types.Resource;
22 import org.apache.tools.ant.types.EnumeratedAttribute;
23
24 /**
25  * Type file/dir ResourceSelector.
26  * @since Ant 1.7
27  */

28 public class Type implements ResourceSelector {
29
30     private static final String JavaDoc FILE_ATTR = "file";
31     private static final String JavaDoc DIR_ATTR = "dir";
32
33     /** Static file type selector. */
34     public static final Type FILE = new Type(new FileDir(FILE_ATTR));
35
36     /** Static dir type selector. */
37     public static final Type DIR = new Type(new FileDir(DIR_ATTR));
38
39     /**
40      * Implements the type attribute.
41      */

42     public static class FileDir extends EnumeratedAttribute {
43         private static final String JavaDoc[] VALUES = new String JavaDoc[] {FILE_ATTR, DIR_ATTR};
44
45         /**
46          * Default constructor.
47          */

48         public FileDir() {
49         }
50
51         /**
52          * Convenience constructor.
53          * @param value the String EnumeratedAttribute value.
54          */

55         public FileDir(String JavaDoc value) {
56             setValue(value);
57         }
58
59         /**
60          * Return the possible values.
61          * @return a String array.
62          */

63         public String JavaDoc[] getValues() {
64             return VALUES;
65         }
66     }
67
68     private FileDir type = null;
69
70     /**
71      * Default constructor.
72      */

73     public Type() {
74     }
75
76     /**
77      * Convenience constructor.
78      * @param fd the FileDir type.
79      */

80     public Type(FileDir fd) {
81         setType(fd);
82     }
83
84     /**
85      * Set type; file|dir.
86      * @param fd a FileDir object.
87      */

88     public void setType(FileDir fd) {
89         type = fd;
90     }
91
92     /**
93      * Return true if this Resource is selected.
94      * @param r the Resource to check.
95      * @return whether the Resource was selected.
96      */

97     public boolean isSelected(Resource r) {
98         if (type == null) {
99             throw new BuildException("The type attribute is required.");
100         }
101         int i = type.getIndex();
102         return r.isDirectory() ? i == 1 : i == 0;
103     }
104
105 }
106
Popular Tags