1 | /*
|
---|
2 | * Copyright 2004-2005 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 | package org.apache.tools.ant.taskdefs.condition;
|
---|
18 | import org.apache.tools.ant.BuildException;
|
---|
19 | import org.apache.tools.ant.util.FileUtils;
|
---|
20 | import java.io.File;
|
---|
21 | import org.apache.tools.ant.types.selectors.FileSelector;
|
---|
22 | import org.apache.tools.ant.types.selectors.AbstractSelectorContainer;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This is a condition that checks to see if a file passes an embedded selector.
|
---|
26 | */
|
---|
27 | public class IsFileSelected extends AbstractSelectorContainer implements Condition {
|
---|
28 |
|
---|
29 | private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
|
---|
30 |
|
---|
31 | private File file;
|
---|
32 | private File baseDir;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * The file to check.
|
---|
36 | * @param file the file to check if if passes the embedded selector.
|
---|
37 | */
|
---|
38 | public void setFile(File file) {
|
---|
39 | this.file = file;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * The base directory to use.
|
---|
44 | * @param baseDir the base directory to use, if null use the project's
|
---|
45 | * basedir.
|
---|
46 | */
|
---|
47 | public void setBaseDir(File baseDir) {
|
---|
48 | this.baseDir = baseDir;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * validate the parameters.
|
---|
53 | */
|
---|
54 | public void validate() {
|
---|
55 | if (selectorCount() != 1) {
|
---|
56 | throw new BuildException("Only one selector allowed");
|
---|
57 | }
|
---|
58 | super.validate();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Evaluate the selector with the file.
|
---|
63 | * @return true if the file is selected by the embedded selector.
|
---|
64 | */
|
---|
65 | public boolean eval() {
|
---|
66 | if (file == null) {
|
---|
67 | throw new BuildException("file attribute not set");
|
---|
68 | }
|
---|
69 | validate();
|
---|
70 | File myBaseDir = baseDir;
|
---|
71 | if (myBaseDir == null) {
|
---|
72 | myBaseDir = getProject().getBaseDir();
|
---|
73 | }
|
---|
74 |
|
---|
75 | FileSelector f = getSelectors(getProject())[0];
|
---|
76 | return f.isSelected(
|
---|
77 | myBaseDir, FILE_UTILS.removeLeadingPath(myBaseDir, file), file);
|
---|
78 | }
|
---|
79 | }
|
---|