01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.taskdefs.condition;
19:
20: import org.apache.tools.ant.BuildException;
21: import org.apache.tools.ant.util.FileUtils;
22: import java.io.File;
23: import org.apache.tools.ant.types.selectors.FileSelector;
24: import org.apache.tools.ant.types.selectors.AbstractSelectorContainer;
25:
26: /**
27: * This is a condition that checks to see if a file passes an embedded selector.
28: */
29: public class IsFileSelected extends AbstractSelectorContainer implements
30: Condition {
31:
32: private static final FileUtils FILE_UTILS = FileUtils
33: .getFileUtils();
34:
35: private File file;
36: private File baseDir;
37:
38: /**
39: * The file to check.
40: * @param file the file to check if if passes the embedded selector.
41: */
42: public void setFile(File file) {
43: this .file = file;
44: }
45:
46: /**
47: * The base directory to use.
48: * @param baseDir the base directory to use, if null use the project's
49: * basedir.
50: */
51: public void setBaseDir(File baseDir) {
52: this .baseDir = baseDir;
53: }
54:
55: /**
56: * validate the parameters.
57: */
58: public void validate() {
59: if (selectorCount() != 1) {
60: throw new BuildException("Only one selector allowed");
61: }
62: super .validate();
63: }
64:
65: /**
66: * Evaluate the selector with the file.
67: * @return true if the file is selected by the embedded selector.
68: */
69: public boolean eval() {
70: if (file == null) {
71: throw new BuildException("file attribute not set");
72: }
73: validate();
74: File myBaseDir = baseDir;
75: if (myBaseDir == null) {
76: myBaseDir = getProject().getBaseDir();
77: }
78:
79: FileSelector f = getSelectors(getProject())[0];
80: return f.isSelected(myBaseDir, FILE_UTILS.removeLeadingPath(
81: myBaseDir, file), file);
82: }
83: }
|