KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > IsFileSelected


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.taskdefs.condition;
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.util.FileUtils;
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.types.selectors.FileSelector;
23 import org.apache.tools.ant.types.selectors.AbstractSelectorContainer;
24
25 /**
26  * This is a condition that checks to see if a file passes an embedded selector.
27  */

28 public class IsFileSelected extends AbstractSelectorContainer implements Condition {
29
30     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
31
32     private File JavaDoc file;
33     private File JavaDoc baseDir;
34
35     /**
36      * The file to check.
37      * @param file the file to check if if passes the embedded selector.
38      */

39     public void setFile(File JavaDoc file) {
40         this.file = file;
41     }
42
43     /**
44      * The base directory to use.
45      * @param baseDir the base directory to use, if null use the project's
46      * basedir.
47      */

48     public void setBaseDir(File JavaDoc baseDir) {
49         this.baseDir = baseDir;
50     }
51
52     /**
53      * validate the parameters.
54      */

55     public void validate() {
56         if (selectorCount() != 1) {
57             throw new BuildException("Only one selector allowed");
58         }
59         super.validate();
60     }
61
62     /**
63      * Evaluate the selector with the file.
64      * @return true if the file is selected by the embedded selector.
65      */

66     public boolean eval() {
67         if (file == null) {
68             throw new BuildException("file attribute not set");
69         }
70         validate();
71         File JavaDoc myBaseDir = baseDir;
72         if (myBaseDir == null) {
73             myBaseDir = getProject().getBaseDir();
74         }
75
76         FileSelector f = getSelectors(getProject())[0];
77         return f.isSelected(
78             myBaseDir, FILE_UTILS.removeLeadingPath(myBaseDir, file), file);
79     }
80 }
81
Popular Tags