KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.types.Mapper;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.util.IdentityMapper;
24 import org.apache.tools.ant.util.FileNameMapper;
25 import org.apache.tools.ant.util.FileUtils;
26
27 import java.io.File JavaDoc;
28
29 /**
30  * A mapping selector is an abstract class adding mapping support to the base
31  * selector
32  */

33 public abstract class MappingSelector extends BaseSelector {
34
35     /** Utilities used for file operations */
36     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
37
38     // CheckStyle:VisibilityModifier OFF - bc
39

40     protected File JavaDoc targetdir = null;
41     protected Mapper mapperElement = null;
42     protected FileNameMapper map = null;
43     protected int granularity = 0;
44
45     // CheckStyle:VisibilityModifier ON
46

47     /**
48      * Creates a new <code>MappingSelector</code> instance.
49      *
50      */

51     public MappingSelector() {
52         granularity = (int) FILE_UTILS.getFileTimestampGranularity();
53     }
54
55
56     /**
57      * The name of the file or directory which is checked for out-of-date
58      * files.
59      *
60      * @param targetdir the directory to scan looking for files.
61      */

62     public void setTargetdir(File JavaDoc targetdir) {
63         this.targetdir = targetdir;
64     }
65
66     /**
67      * Defines the FileNameMapper to use (nested mapper element).
68      * @return a mapper to be configured
69      * @throws BuildException if more that one mapper defined
70      */

71     public Mapper createMapper() throws BuildException {
72         if (mapperElement != null) {
73             throw new BuildException("Cannot define more than one mapper");
74         }
75         mapperElement = new Mapper(getProject());
76         return mapperElement;
77     }
78
79     /**
80      * Checks to make sure all settings are kosher. In this case, it
81      * means that the dest attribute has been set and we have a mapper.
82      */

83     public void verifySettings() {
84         if (targetdir == null) {
85             setError("The targetdir attribute is required.");
86         }
87         if (mapperElement == null) {
88             map = new IdentityMapper();
89         } else {
90             map = mapperElement.getImplementation();
91         }
92         if (map == null) {
93             setError("Could not set <mapper> element.");
94         }
95     }
96
97     /**
98      * The heart of the matter. This is where the selector gets to decide
99      * on the inclusion of a file in a particular fileset.
100      *
101      * @param basedir the base directory the scan is being done from
102      * @param filename is the name of the file to check
103      * @param file is a java.io.File object the selector can use
104      * @return whether the file should be selected or not
105      */

106     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
107
108         // throw BuildException on error
109
validate();
110
111         // Determine file whose out-of-dateness is to be checked
112
String JavaDoc[] destfiles = map.mapFileName(filename);
113         // If filename does not match the To attribute of the mapper
114
// then filter it out of the files we are considering
115
if (destfiles == null) {
116             return false;
117         }
118         // Sanity check
119
if (destfiles.length != 1 || destfiles[0] == null) {
120             throw new BuildException("Invalid destination file results for "
121                     + targetdir.getName() + " with filename " + filename);
122         }
123         String JavaDoc destname = destfiles[0];
124         File JavaDoc destfile = new File JavaDoc(targetdir, destname);
125
126         boolean selected = selectionTest(file, destfile);
127         return selected;
128     }
129
130     /**
131      * this test is our selection test that compared the file with the destfile
132      * @param srcfile file to test; may be null
133      * @param destfile destination file
134      * @return true if source file compares with destination file
135      */

136     protected abstract boolean selectionTest(File JavaDoc srcfile, File JavaDoc destfile);
137
138     /**
139      * Sets the number of milliseconds leeway we will give before we consider
140      * a file out of date. Defaults to 2000 on MS-DOS derivatives and 1000 on
141      * others.
142      * @param granularity the leeway in milliseconds
143      */

144     public void setGranularity(int granularity) {
145         this.granularity = granularity;
146     }
147
148 }
149
Popular Tags