source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/MappingSelector.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.7 KB
Line 
1/*
2 * Copyright 2000-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
18package org.apache.tools.ant.types.selectors;
19
20import org.apache.tools.ant.types.Mapper;
21import org.apache.tools.ant.BuildException;
22import org.apache.tools.ant.util.IdentityMapper;
23import org.apache.tools.ant.util.FileNameMapper;
24import org.apache.tools.ant.util.FileUtils;
25
26import java.io.File;
27
28/**
29 * A mapping selector is an abstract class adding mapping support to the base
30 * selector
31 */
32public abstract class MappingSelector extends BaseSelector {
33 protected File targetdir = null;
34 protected Mapper mapperElement = null;
35 protected FileNameMapper map = null;
36 protected int granularity = 0;
37
38 /**
39 * Creates a new <code>MappingSelector</code> instance.
40 *
41 */
42 public MappingSelector() {
43 granularity = (int) FileUtils.newFileUtils().getFileTimestampGranularity();
44 }
45
46
47 /**
48 * The name of the file or directory which is checked for out-of-date
49 * files.
50 *
51 * @param targetdir the directory to scan looking for files.
52 */
53 public void setTargetdir(File targetdir) {
54 this.targetdir = targetdir;
55 }
56
57 /**
58 * Defines the FileNameMapper to use (nested mapper element).
59 * @return a mapper to be configured
60 * @throws BuildException if more that one mapper defined
61 */
62 public Mapper createMapper() throws BuildException {
63 if (mapperElement != null) {
64 throw new BuildException("Cannot define more than one mapper");
65 }
66 mapperElement = new Mapper(getProject());
67 return mapperElement;
68 }
69
70 /**
71 * Checks to make sure all settings are kosher. In this case, it
72 * means that the dest attribute has been set and we have a mapper.
73 */
74 public void verifySettings() {
75 if (targetdir == null) {
76 setError("The targetdir attribute is required.");
77 }
78 if (mapperElement == null) {
79 map = new IdentityMapper();
80 } else {
81 map = mapperElement.getImplementation();
82 }
83 if (map == null) {
84 setError("Could not set <mapper> element.");
85 }
86 }
87
88 /**
89 * The heart of the matter. This is where the selector gets to decide
90 * on the inclusion of a file in a particular fileset.
91 *
92 * @param basedir the base directory the scan is being done from
93 * @param filename is the name of the file to check
94 * @param file is a java.io.File object the selector can use
95 * @return whether the file should be selected or not
96 */
97 public boolean isSelected(File basedir, String filename, File file) {
98
99 // throw BuildException on error
100 validate();
101
102 // Determine file whose out-of-dateness is to be checked
103 String[] destfiles = map.mapFileName(filename);
104 // If filename does not match the To attribute of the mapper
105 // then filter it out of the files we are considering
106 if (destfiles == null) {
107 return false;
108 }
109 // Sanity check
110 if (destfiles.length != 1 || destfiles[0] == null) {
111 throw new BuildException("Invalid destination file results for "
112 + targetdir.getName() + " with filename " + filename);
113 }
114 String destname = destfiles[0];
115 File destfile = new File(targetdir, destname);
116
117 boolean selected = selectionTest(file, destfile);
118 return selected;
119 }
120
121 /**
122 * this test is our selection test that compared the file with the destfile
123 * @param srcfile file to test; may be null
124 * @param destfile destination file
125 * @return true if source file compares with destination file
126 */
127 protected abstract boolean selectionTest(File srcfile, File destfile);
128
129 /**
130 * Sets the number of milliseconds leeway we will give before we consider
131 * a file out of date. Defaults to 2000 on MS-DOS derivatives as the FAT
132 * file system.
133 * @param granularity the leeway in milliseconds
134 */
135 public void setGranularity(int granularity) {
136 this.granularity = granularity;
137 }
138
139}
Note: See TracBrowser for help on using the repository browser.