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 |
|
---|
18 | package org.apache.tools.ant.types.selectors;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 |
|
---|
22 | import org.apache.tools.ant.BuildException;
|
---|
23 | import org.apache.tools.ant.types.EnumeratedAttribute;
|
---|
24 | import org.apache.tools.ant.types.Mapper;
|
---|
25 | import org.apache.tools.ant.util.FileNameMapper;
|
---|
26 | import org.apache.tools.ant.util.IdentityMapper;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Selector that filters files based on whether they appear in another
|
---|
30 | * directory tree. It can contain a mapper element, so isn't available
|
---|
31 | * as an ExtendSelector (since those parameters can't hold other
|
---|
32 | * elements).
|
---|
33 | *
|
---|
34 | * @since 1.5
|
---|
35 | */
|
---|
36 | public class PresentSelector extends BaseSelector {
|
---|
37 |
|
---|
38 | private File targetdir = null;
|
---|
39 | private Mapper mapperElement = null;
|
---|
40 | private FileNameMapper map = null;
|
---|
41 | private boolean destmustexist = true;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates a new <code>PresentSelector</code> instance.
|
---|
45 | *
|
---|
46 | */
|
---|
47 | public PresentSelector() {
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @return a string describing this object
|
---|
52 | */
|
---|
53 | public String toString() {
|
---|
54 | StringBuffer buf = new StringBuffer("{presentselector targetdir: ");
|
---|
55 | if (targetdir == null) {
|
---|
56 | buf.append("NOT YET SET");
|
---|
57 | } else {
|
---|
58 | buf.append(targetdir.getName());
|
---|
59 | }
|
---|
60 | buf.append(" present: ");
|
---|
61 | if (destmustexist) {
|
---|
62 | buf.append("both");
|
---|
63 | } else {
|
---|
64 | buf.append("srconly");
|
---|
65 | }
|
---|
66 | if (map != null) {
|
---|
67 | buf.append(map.toString());
|
---|
68 | } else if (mapperElement != null) {
|
---|
69 | buf.append(mapperElement.toString());
|
---|
70 | }
|
---|
71 | buf.append("}");
|
---|
72 | return buf.toString();
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * The name of the file or directory which is checked for matching
|
---|
77 | * files.
|
---|
78 | *
|
---|
79 | * @param targetdir the directory to scan looking for matching files.
|
---|
80 | */
|
---|
81 | public void setTargetdir(File targetdir) {
|
---|
82 | this.targetdir = targetdir;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Defines the FileNameMapper to use (nested mapper element).
|
---|
87 | * @return a mapper to be configured
|
---|
88 | * @throws BuildException if more that one mapper defined
|
---|
89 | */
|
---|
90 | public Mapper createMapper() throws BuildException {
|
---|
91 | if (mapperElement != null) {
|
---|
92 | throw new BuildException("Cannot define more than one mapper");
|
---|
93 | }
|
---|
94 | mapperElement = new Mapper(getProject());
|
---|
95 | return mapperElement;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * This sets whether to select a file if its dest file is present.
|
---|
101 | * It could be a <code>negate</code> boolean, but by doing things
|
---|
102 | * this way, we get some documentation on how the system works.
|
---|
103 | * A user looking at the documentation should clearly understand
|
---|
104 | * that the ONLY files whose presence is being tested are those
|
---|
105 | * that already exist in the source directory, hence the lack of
|
---|
106 | * a <code>destonly</code> option.
|
---|
107 | *
|
---|
108 | * @param fp An attribute set to either <code>srconly</code or
|
---|
109 | * <code>both</code>.
|
---|
110 | */
|
---|
111 | public void setPresent(FilePresence fp) {
|
---|
112 | if (fp.getIndex() == 0) {
|
---|
113 | destmustexist = false;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Checks to make sure all settings are kosher. In this case, it
|
---|
119 | * means that the targetdir attribute has been set and we have a mapper.
|
---|
120 | */
|
---|
121 | public void verifySettings() {
|
---|
122 | if (targetdir == null) {
|
---|
123 | setError("The targetdir attribute is required.");
|
---|
124 | }
|
---|
125 | if (mapperElement == null) {
|
---|
126 | map = new IdentityMapper();
|
---|
127 | } else {
|
---|
128 | map = mapperElement.getImplementation();
|
---|
129 | }
|
---|
130 | if (map == null) {
|
---|
131 | setError("Could not set <mapper> element.");
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * The heart of the matter. This is where the selector gets to decide
|
---|
137 | * on the inclusion of a file in a particular fileset.
|
---|
138 | *
|
---|
139 | * @param basedir the base directory the scan is being done from
|
---|
140 | * @param filename is the name of the file to check
|
---|
141 | * @param file is a java.io.File object the selector can use
|
---|
142 | * @return whether the file should be selected or not
|
---|
143 | */
|
---|
144 | public boolean isSelected(File basedir, String filename, File file) {
|
---|
145 |
|
---|
146 | // throw BuildException on error
|
---|
147 | validate();
|
---|
148 |
|
---|
149 | // Determine file whose existence is to be checked
|
---|
150 | String[] destfiles = map.mapFileName(filename);
|
---|
151 | // If filename does not match the To attribute of the mapper
|
---|
152 | // then filter it out of the files we are considering
|
---|
153 | if (destfiles == null) {
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | // Sanity check
|
---|
157 | if (destfiles.length != 1 || destfiles[0] == null) {
|
---|
158 | throw new BuildException("Invalid destination file results for "
|
---|
159 | + targetdir + " with filename " + filename);
|
---|
160 | }
|
---|
161 | String destname = destfiles[0];
|
---|
162 | File destfile = new File(targetdir, destname);
|
---|
163 | return destfile.exists() == destmustexist;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Enumerated attribute with the values for indicating where a file's
|
---|
168 | * presence is allowed and required.
|
---|
169 | */
|
---|
170 | public static class FilePresence extends EnumeratedAttribute {
|
---|
171 | /**
|
---|
172 | * @return the values as an array of strings
|
---|
173 | */
|
---|
174 | public String[] getValues() {
|
---|
175 | return new String[]{"srconly", "both"};
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|