KickJava   Java API By Example, From Geeks To Geeks.

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


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.BuildException;
24 import org.apache.tools.ant.types.EnumeratedAttribute;
25 import org.apache.tools.ant.types.Mapper;
26 import org.apache.tools.ant.util.FileNameMapper;
27 import org.apache.tools.ant.util.IdentityMapper;
28
29 /**
30  * Selector that filters files based on whether they appear in another
31  * directory tree. It can contain a mapper element, so isn't available
32  * as an ExtendSelector (since those parameters can't hold other
33  * elements).
34  *
35  * @since 1.5
36  */

37 public class PresentSelector extends BaseSelector {
38
39     private File JavaDoc targetdir = null;
40     private Mapper mapperElement = null;
41     private FileNameMapper map = null;
42     private boolean destmustexist = true;
43
44     /**
45      * Creates a new <code>PresentSelector</code> instance.
46      *
47      */

48     public PresentSelector() {
49     }
50
51     /**
52      * @return a string describing this object
53      */

54     public String JavaDoc toString() {
55         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{presentselector targetdir: ");
56         if (targetdir == null) {
57             buf.append("NOT YET SET");
58         } else {
59             buf.append(targetdir.getName());
60         }
61         buf.append(" present: ");
62         if (destmustexist) {
63             buf.append("both");
64         } else {
65             buf.append("srconly");
66         }
67         if (map != null) {
68             buf.append(map.toString());
69         } else if (mapperElement != null) {
70             buf.append(mapperElement.toString());
71         }
72         buf.append("}");
73         return buf.toString();
74     }
75
76     /**
77      * The name of the file or directory which is checked for matching
78      * files.
79      *
80      * @param targetdir the directory to scan looking for matching files.
81      */

82     public void setTargetdir(File JavaDoc targetdir) {
83         this.targetdir = targetdir;
84     }
85
86     /**
87      * Defines the FileNameMapper to use (nested mapper element).
88      * @return a mapper to be configured
89      * @throws BuildException if more that one mapper defined
90      */

91     public Mapper createMapper() throws BuildException {
92         if (mapperElement != null) {
93             throw new BuildException("Cannot define more than one mapper");
94         }
95         mapperElement = new Mapper(getProject());
96         return mapperElement;
97     }
98
99
100     /**
101      * This sets whether to select a file if its dest file is present.
102      * It could be a <code>negate</code> boolean, but by doing things
103      * this way, we get some documentation on how the system works.
104      * A user looking at the documentation should clearly understand
105      * that the ONLY files whose presence is being tested are those
106      * that already exist in the source directory, hence the lack of
107      * a <code>destonly</code> option.
108      *
109      * @param fp An attribute set to either <code>srconly</code or
110      * <code>both</code>.
111      */

112     public void setPresent(FilePresence fp) {
113         if (fp.getIndex() == 0) {
114             destmustexist = false;
115         }
116     }
117
118     /**
119      * Checks to make sure all settings are kosher. In this case, it
120      * means that the targetdir attribute has been set and we have a mapper.
121      */

122     public void verifySettings() {
123         if (targetdir == null) {
124             setError("The targetdir attribute is required.");
125         }
126         if (mapperElement == null) {
127             map = new IdentityMapper();
128         } else {
129             map = mapperElement.getImplementation();
130         }
131         if (map == null) {
132             setError("Could not set <mapper> element.");
133         }
134     }
135
136     /**
137      * The heart of the matter. This is where the selector gets to decide
138      * on the inclusion of a file in a particular fileset.
139      *
140      * @param basedir the base directory the scan is being done from
141      * @param filename is the name of the file to check
142      * @param file is a java.io.File object the selector can use
143      * @return whether the file should be selected or not
144      */

145     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
146
147         // throw BuildException on error
148
validate();
149
150         // Determine file whose existence is to be checked
151
String JavaDoc[] destfiles = map.mapFileName(filename);
152         // If filename does not match the To attribute of the mapper
153
// then filter it out of the files we are considering
154
if (destfiles == null) {
155             return false;
156         }
157         // Sanity check
158
if (destfiles.length != 1 || destfiles[0] == null) {
159             throw new BuildException("Invalid destination file results for "
160                     + targetdir + " with filename " + filename);
161         }
162         String JavaDoc destname = destfiles[0];
163         File JavaDoc destfile = new File JavaDoc(targetdir, destname);
164         return destfile.exists() == destmustexist;
165     }
166
167     /**
168      * Enumerated attribute with the values for indicating where a file's
169      * presence is allowed and required.
170      */

171     public static class FilePresence extends EnumeratedAttribute {
172         /**
173          * @return the values as an array of strings
174          */

175         public String JavaDoc[] getValues() {
176             return new String JavaDoc[]{"srconly", "both"};
177         }
178     }
179
180 }
181
182
Popular Tags